-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSigGen.py
199 lines (177 loc) · 5.83 KB
/
SigGen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Name: SigGen.py
# Version: 1.0.3
# Author: RenardDev (zeze839@gmail.com)
# IDA imports
import ida_idaapi
import ida_ua
import ida_bytes
import ida_pro
import ida_auto
import ida_ida
import ida_search
import idc
def IsNotValidAddress(address):
if (address == 0) | (address == ida_idaapi.BADADDR):
return True
return False
def DecodeInstruction(address):
if IsNotValidAddress(address):
return None
insn = ida_ua.insn_t()
if ida_ua.decode_insn(insn, address):
return insn
return None
def BytesToString(bytes):
return ' '.join(map(lambda x: f'{x:02X}', bytes))
def IsOperandIdpSpec(op):
return (op.type == ida_ua.o_idpspec0) | (op.type == ida_ua.o_idpspec1) | (op.type == ida_ua.o_idpspec2) | (op.type == ida_ua.o_idpspec3) | (op.type == ida_ua.o_idpspec4) | (op.type == ida_ua.o_idpspec5)
def GetInstructionSignature(address, show_mask):
insn = DecodeInstruction(address)
if insn:
insn_bytes = ida_bytes.get_bytes(address, insn.size)
if insn.ops[0].type == ida_ua.o_void:
return (insn, BytesToString(insn_bytes))
else:
insn_signature = BytesToString(insn_bytes[:insn.ops[0].offb])
op_pairs = [(i, insn.ops[i], insn.ops[i + 1]) for i in range(len(insn.ops) - 1)]
for i, op, next_op in op_pairs:
if (op.type == ida_ua.o_void) | ((i > 0) & (op.offb == 0)):
break
if next_op.offb != 0:
op_size = next_op.offb - op.offb
else:
op_size = insn.size - op.offb
if IsOperandIdpSpec(op):
insn_signature += BytesToString(insn_bytes[op.offb:op.offb + op_size])
elif op.type == ida_ua.o_reg:
insn_signature += BytesToString(insn_bytes[op.offb:op.offb + op_size])
elif op.type == ida_ua.o_phrase:
insn_signature += BytesToString(insn_bytes[op.offb:op.offb + op_size])
else:
if show_mask:
insn_signature += ' ?' * op_size
return (insn, insn_signature)
return (None, None)
def GetInstructionSignatureBytes(address):
insn = DecodeInstruction(address)
if insn:
insn_bytes = ida_bytes.get_bytes(address, insn.size)
insn_signature_bytes = bytearray()
if insn.ops[0].type == ida_ua.o_void:
insn_signature_bytes.extend(insn_bytes)
return (insn, insn_signature_bytes)
else:
insn_signature_bytes.extend(insn_bytes[:insn.ops[0].offb])
op_pairs = [(i, insn.ops[i], insn.ops[i + 1]) for i in range(len(insn.ops) - 1)]
for i, op, next_op in op_pairs:
if (op.type == ida_ua.o_void) | ((i > 0) & (op.offb == 0)):
break
if next_op.offb != 0:
op_size = next_op.offb - op.offb
else:
op_size = insn.size - op.offb
if IsOperandIdpSpec(op):
insn_signature_bytes.extend(insn_bytes[op.offb:op.offb + op_size])
elif op.type == ida_ua.o_reg:
insn_signature_bytes.extend(insn_bytes[op.offb:op.offb + op_size])
elif op.type == ida_ua.o_phrase:
insn_signature_bytes.extend(insn_bytes[op.offb:op.offb + op_size])
return (insn, insn_signature_bytes)
return (None, None)
def MakeSignature(address, insn_count):
signature = ''
i = 0
offset = 0
while i < insn_count:
insn, insn_signature = GetInstructionSignature(address + offset, i + 1 < insn_count)
if insn_signature:
signature += insn_signature
if i + 1 < insn_count:
signature += ' '
offset += insn.size
i += 1
else:
return None
return signature
def Adler32(bytes):
MOD_ADLER = 65521
a = 1
b = 0
for byte in bytes:
a = (a + byte) % MOD_ADLER
b = (b + a) % MOD_ADLER
return (b << 16) | a
def MakeFunctionHash(start_address, end_address):
signature_bytes = bytearray()
offset = 0
while start_address + offset < end_address:
if offset >= 0xFFFF:
break
insn, insn_signature_bytes = GetInstructionSignatureBytes(start_address + offset)
if insn_signature_bytes:
signature_bytes.extend(insn_signature_bytes)
offset += insn.size
else:
signature_bytes.extend(ida_bytes.get_bytes(start_address + offset, 1))
offset += 1
return ((Adler32(signature_bytes) & ~0xFFFF) | offset, signature_bytes)
class SigGen(ida_idaapi.plugin_t):
flags = ida_idaapi.PLUGIN_MOD
wanted_name = 'SigGen'
wanted_hotkey = 'Ctrl+Shift+M'
comment = 'SigGen - Signature Generator.\n'
help = ''
def init(self):
if ida_pro.IDA_SDK_VERSION < 770:
idc.msg('[SigGen] Error: Optimal IDA version for SigGen is 7.7.\n')
return ida_idaapi.PLUGIN_SKIP
return ida_idaapi.PLUGIN_KEEP
def term(self):
pass
def run(self, arg):
if ida_auto.auto_is_ok() != True:
idc.msg('[SigGen] Error: The analysis is not finished!\n')
return
min_address = ida_ida.inf_get_min_ea()
max_address = ida_ida.inf_get_max_ea()
current_address = idc.here()
if ida_bytes.is_code(ida_bytes.get_flags(current_address)) != True:
idc.msg('[SigGen] Error: The cursor is not on the code!\n')
return
count = 1
while count < 64:
signature = MakeSignature(current_address, count)
if signature:
if IsNotValidAddress(ida_search.find_binary(min_address, current_address - 1, signature, 0, ida_search.SEARCH_DOWN)) & IsNotValidAddress(ida_search.find_binary(current_address + 1, max_address, signature, 0, ida_search.SEARCH_DOWN)):
func_start = idc.get_func_attr(current_address, idc.FUNCATTR_START)
func_end = idc.get_func_attr(current_address, idc.FUNCATTR_END)
if current_address == func_start:
func_hash, signature_bytes = MakeFunctionHash(func_start, func_end)
if func_hash:
idc.msg(f'Signature (Hash: 0x{func_hash:08X}): {signature}\n')
else:
break
else:
idc.msg(f'Signature: {signature}\n')
return
else:
count += 1
continue
else:
break
idc.msg('Failed to generate signature.\n')
_SigGen = None
bPluginMode = False
def PLUGIN_ENTRY():
global _SigGen
global bPluginMode
if _SigGen == None:
_SigGen = SigGen()
bPluginMode = True
return _SigGen
if __name__ == '__main__':
if bPluginMode != True:
if ida_pro.IDA_SDK_VERSION < 770:
idc.msg('[SigGen] Error: Optimal IDA version for SigGen is 7.7.\n')
else:
SigGen().run(0)