This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
flare_emu_ida.py
214 lines (163 loc) · 5.98 KB
/
flare_emu_ida.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
import re
import idaapi
import idautils
import idc
import flare_emu
class IdaProAnalysisHelper(flare_emu.AnalysisHelper):
def __init__(self, eh):
super(IdaProAnalysisHelper, self).__init__()
self.eh = eh
info = idaapi.get_inf_structure()
if info.procname == "metapc":
self.arch = "X86"
else:
self.arch = info.procname
if info.is_64bit():
self.bitness = 64
elif info.is_32bit():
self.bitness = 32
else:
self.bitness = None
if info.filetype == 11:
self.filetype = "PE"
elif info.filetype == 25:
self.filetype = "MACHO"
elif info.filetype == 18:
self.filetype = "ELF"
else:
self.filetype = "UNKNOWN"
def getFuncStart(self, addr):
ret = idc.get_func_attr(addr, idc.FUNCATTR_START)
if ret == idc.BADADDR:
return None
return ret
def getFuncEnd(self, addr):
ret = idc.get_func_attr(addr, idc.FUNCATTR_END)
if ret == idc.BADADDR:
return None
return ret
def getFuncName(self, addr, normalized=True):
if normalized:
return self.normalizeFuncName(idc.get_func_name(addr))
else:
return idc.get_func_name(addr)
def getMnem(self, addr):
return idc.print_insn_mnem(addr)
def _getBlockByAddr(self, addr, flowchart):
for bb in flowchart:
if (addr >= bb.start_ea and addr < bb.end_ea) or addr == bb.start_ea:
return bb
return None
# gets address of last instruction in the basic block containing addr
def getBlockEndInsnAddr(self, addr, flowchart):
bb = self._getBlockByAddr(addr, flowchart)
return idc.prev_head(bb.end_ea, idc.get_inf_attr(idc.INF_MIN_EA))
def getMinimumAddr(self):
return idc.get_inf_attr(idc.INF_MIN_EA)
def getMaximumAddr(self):
return idc.get_inf_attr(idc.INF_MAX_EA)
def getBytes(self, addr, size):
return idc.get_bytes(addr, size, False)
def getCString(self, addr):
buf = ""
while self.getBytes(addr, 1) != "\x00" and self.getBytes(addr, 1) is not None:
buf += self.getBytes(addr, 1)
addr += 1
return buf
def getOperand(self, addr, opndNum):
return idc.print_operand(addr, opndNum)
def getWordValue(self, addr):
return idc.get_wide_word(addr)
def getDwordValue(self, addr):
return idc.get_wide_dword(addr)
def getQWordValue(self, addr):
return idc.get_qword(addr)
def isThumbMode(self, addr):
return idc.get_sreg(addr, "T") == 1
def getSegmentName(self, addr):
return idc.get_segm_name(addr)
def getSegmentStart(self, addr):
return idc.get_segm_start(addr)
def getSegmentEnd(self, addr):
return idc.get_segm_end(addr)
def getSegmentDefinedSize(self, addr):
size = 0
segEnd = self.getSegmentEnd(addr)
addr = self.getSegmentStart(addr)
while idc.has_value(idc.get_full_flags(addr)):
if addr >= segEnd:
break
size += 1
addr += 1
return size
def getSegments(self):
return idautils.Segments()
def getSegmentSize(self, addr):
return self.getSegmentEnd(addr) - self.getSegmentStart(addr)
def getSectionName(self, addr):
return self.getSegmentName(addr)
def getSectionStart(self, addr):
return self.getSegmentStart(addr)
def getSectionEnd(self, addr):
return self.getSegmentEnd(addr)
def getSectionSize(self, addr):
return self.getSegmentSize(addr)
def getSections(self):
return self.getSegments()
# gets disassembled instruction with names and comments as a string
def getDisasmLine(self, addr):
return idc.generate_disasm_line(addr, 0)
def getName(self, addr):
return idc.get_name(addr, idc.ida_name.GN_VISIBLE)
def getNameAddr(self, name):
name = idc.get_name_ea_simple(name)
if name == "":
name = idc.get_name_ea_simple(self.normalizeFuncName(name))
return name
def getOpndType(self, addr, opndNum):
return idc.get_operand_type(addr, opndNum)
def getOpndValue(self, addr, opndNum):
return idc.get_operand_value(addr, opndNum)
def makeInsn(self, addr):
if idc.create_insn(addr) == 0:
idc.del_items(addr, idc.DELIT_EXPAND)
idc.create_insn(addr)
idc.auto_wait()
def createFunction(self, addr):
pass
def getFlowChart(self, addr):
function = idaapi.get_func(addr)
return list(idaapi.FlowChart(function))
def getSpDelta(self, addr):
f = idaapi.get_func(addr)
return idaapi.get_sp_delta(f, addr)
def getXrefsTo(self, addr):
return list(map(lambda x: x.frm, list(idautils.XrefsTo(addr))))
def getArch(self):
return self.arch
def getBitness(self):
return self.bitness
def getFileType(self):
return self.filetype
def getInsnSize(self, addr):
return idc.get_item_size(addr)
def isTerminatingBB(self, bb):
if (bb.type == idaapi.fcb_ret or bb.type == idaapi.fcb_noret or
(bb.type == idaapi.fcb_indjump and len(list(bb.succs())) == 0)):
return True
for b in bb.succs():
if b.type == idaapi.fcb_extern:
return True
return False
def skipJumpTable(self, addr):
while idc.print_insn_mnem(addr) == "":
addr = idc.next_head(addr, idc.get_inf_attr(idc.INF_MAX_EA))
return addr
def setName(self, addr, name, size=0):
idc.set_name(addr, name, idc.SN_NOCHECK)
def setComment(self, addr, comment, repeatable=False):
idc.set_cmt(addr, comment, repeatable)
def normalizeFuncName(self, funcName):
# remove appended _n from IDA Pro names
funcName = re.sub(r"_[\d]+$", "", funcName)
return funcName