-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathidaware.py
223 lines (171 loc) · 5.47 KB
/
idaware.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/python3
from functools import wraps
import inspect
import json
import idaapi
import idautils
import ida_funcs
import idc
from capstone.x86 import *
from capstone import *
DEBUG = True
def symbolAddr(name): return [tup for tup in idautils.Names() if tup[1] == name][0][0]
import_idaHook = """#autogenerated hook code
def idaHook(addr, enabled=True, halting=False):
def init(func):
@wraps(func)
def wrapper():
if not enabled: return False
if halting == None:
return func()
func()
return halting
return wrapper
return init
"""
def idaHook(addr, enabled=True, halting=False):
def init(func):
idaapi.add_bpt(addr, enabled, idaapi.BPT_SOFT)
@wraps(func)
def wrapper():
# let the hook determine
if halting == None: return func()
func()
return halting
bpt = idaapi.bpt_t()
idaapi.get_bpt(addr, bpt)
bpt.elang = 'Python'
cond = import_idaHook + inspect.getsource(wrapper) + "\n" + wrapper.__name__ + "()"
bpt.condition = cond
idaapi.update_bpt(bpt)
return wrapper
return init
def fmtHex(hexArr, fmt='{hexByte} '):
retStr = ''
for byte in hexArr:
byteStr = hex(byte)[2:].rjust(2, "0")
retStr += fmt.format(hexByte = byteStr)
return retStr
jmps = [X86_INS_JMP,
X86_INS_JAE,
X86_INS_JA,
X86_INS_JBE,
X86_INS_JB,
X86_INS_JECXZ,
X86_INS_JE,
X86_INS_JGE,
X86_INS_JG,
X86_INS_JLE,
X86_INS_JL,
X86_INS_JNE,
X86_INS_JNO,
X86_INS_JNP,
X86_INS_JNS,
X86_INS_JO,
X86_INS_JP,
X86_INS_JRCXZ,
X86_INS_JS,
X86_INS_CALL]
def makeSig(name, siglen):
code = idaapi.get_bytes(symbolAddr(name), siglen)
md = Cs(CS_ARCH_X86, CS_MODE_64)
md.detail = True
mask = ''
trackedLen = 0
for i in md.disasm(code, 0):
# if i.id == X86_INS_CMP: return i
trackedMask = ''
if DEBUG:
trackedBytes = b''
# print("insn:", i.mnemonic + " " + i.op_str)
if DEBUG: prefixBytes = b''
for prefixByte in i.prefix:
if prefixByte:
# sig += prefixByte.to_bytes(1, sys.byteorder)
trackedMask += 'x'
mask += 'x'
if DEBUG:
prefixBytes += prefixByte.to_bytes(1, sys.byteorder)
if prefixBytes and DEBUG:
# print("PREFIX:", fmtHex(prefixBytes))
trackedBytes += prefixBytes
if i.rex:
# sig += i.rex.to_bytes(1, sys.byteorder) # byteorder is important here
trackedMask += 'x'
mask += 'x'
if DEBUG:
# print("REX:", hex(i.rex))
trackedBytes += i.rex.to_bytes(1, sys.byteorder)
if DEBUG: opcodeBytes = b''
for opcodeByte in i.opcode:
if opcodeByte:
# sig += opcodeByte.to_bytes(1, sys.byteorder)
trackedMask += 'x'
mask += 'x'
if DEBUG:
opcodeBytes += opcodeByte.to_bytes(1, sys.byteorder)
if opcodeBytes and DEBUG:
# print("opcode:", fmtHex(opcodeBytes))
trackedBytes += opcodeBytes
if i.modrm:
# sig += i.modrm.to_bytes(1, sys.byteorder)
trackedMask += 'x'
mask += 'x'
if DEBUG:
# print("MODRM:", hex(i.modrm))
trackedBytes += i.modrm.to_bytes(1, sys.byteorder)
if i.sib:
# sig += i.sib.to_bytes(1, sys.byteorder)
trackedMask += 'x'
mask += 'x'
if DEBUG:
# print("SIB:", hex(i.sib))
trackedBytes += i.sib.to_bytes(1, sys.byteorder)
if i.disp_size:
for x in range(i.disp_size):
# sig += b'\x00'
mask += '?'
trackedMask += '?'
if DEBUG:
trackedBytes += b'\x00'
jmp = False
if i.id in jmps:
for x in range(i.imm_size):
# sig += b
mask += '?'
trackedMask += '?'
if DEBUG:
trackedBytes += b'\x00'
else:
trackedBytes += i.imm_offset.to_bytes(i.imm_size, sys.byteorder)
mask += 'x' * i.imm_size
trackedMask += 'x' * i.imm_size
# sig += i.imm_offset.to_bytes(i.imm_size, sys.byteorder)
# these two should be the same
if DEBUG:
print("insn mask:", trackedMask)
print("new bytes:", fmtHex(trackedBytes))
print("old bytes:", fmtHex(bytes(i.bytes)))
code = code[0:len(mask)]
return {name: {"sig": fmtHex(code)[0:-1], "mask": mask}}
@unwrapName := lambda f: (lambda sigmask: f(sigmask, list(sigmask)[0]))
def printSig(sigmask, name):
outStr = ''
sig = sigmask[name]["sig"]
mask = sigmask[name]["mask"]
sigBytes = sig.split(' ')
for x in range(len(sigBytes)):
if(mask[x] == 'x'):
outStr += sigBytes[x] + ' '
else:
outStr += '?? '
print(outStr)
print("DONE")
def getFuncDetails(name):
addr = symbolAddr(name)
funcSize = ida_funcs.get_func(addr).size()
return json.dumps({
"name":name,
"addr":hex(addr),
"bytes": fmtHex(idaapi.get_bytes(addr, funcSize), fmt='\\x{hexbyte}')[0:-1]
}, indent=2)