forked from 1111joe1111/ida_ea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ea_utils.py
223 lines (159 loc) · 6.12 KB
/
ea_utils.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
from idaapi import *
from idc import *
from idautils import *
if IDA_SDK_VERSION < 690:
try:
from PySide import QtGui, QtCore
except:
print "IDA EA Error: Couldn't Find PySide Bindings, Trying PyQt"
from PyQt4 import QtGui, QtCore
else:
print "Warning: IDA EA untested with IDA >=6.9"
from PyQt5 import QtGui, QtCore
from string import printable
from json import load, dump
from api_funcs import get_rg
from ea_UI import Warning_UI
from os.path import isfile
from os import remove
from time import time, sleep
from threading import Thread
def read(file, mode="r"):
with open(file, mode) as f:
return f.read()
def write(string, file, type="w"):
with open(file, type) as f:
f.write(string)
def cPrint(color, msg):
return ("<span class='%s'>" % (color)) + msg + "</span>"
def parse_mem(mem):
return ("<img src='" + root_dir + "arrow.png'>").join(mem)
def get_bits():
global file_name
global _32_bit
new_name = get_root_filename()
if new_name != file_name:
file_name = new_name
# avoid IDA bug
if get_inf_structure().is_32bit() and get_inf_structure().is_64bit():
_32_bit = (next((False for i in ("r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15")
if get_rg(i) != 0xffffffffffffffff), True) and
next((False for i in ("rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp", "rip")
if get_rg(i) > 0xffffffff), True))
else:
_32_bit = get_inf_structure().is_32bit()
return _32_bit
def get_mem_recursive(mem, matches, prev_mem=False, get_perm=True, int_size=4):
global iterations
mem_str = hex(mem)[2:].strip("L").zfill(int_size * 2)
if get_perm:
try:
perm = bin(GetSegmentAttr(mem, SEGATTR_PERM))[2:].zfill(3)
if "1" in perm:
perm = '(' + "".join(sym if int(v) else "-" for v, sym in zip(perm, ("r", "w", "x"))) + ')'
else:
perm = ""
except:
perm = ""
else:
perm = ""
offset = None
if codeSegment and codeStart < mem < codeEnd:
offset = GetFuncOffset(mem)
if offset:
text = cPrint(b_red, "0x" + mem_str) + cPrint(b_red, " <" + offset + ">")
code = True
if not offset:
if perm or not get_perm:
text = cPrint(b_lightblue, "0x" + mem_str)
elif next((False for i in mem_str if i != "0"), True):
text = cPrint(b_yellow, "0x" + mem_str) # + "(NULL)"
else:
text = "0x" + mem_str
if next((False for i in reversed(mem_str.decode("HEX")) if i not in printable), True) and prev_mem:
r_mem = dbg_read_memory(prev_mem, 50)
if r_mem:
text += '(' + cPrint(b_green, '"' + r_mem.split("\x00")[0].replace("\n", "") + '"') + ')'
code = False
matches.append(text)
if not code and iterations < max_iterations:
iterations += 1
next_mem = dbg_read_memory(mem, int_size)
if next_mem:
get_mem_recursive(int("".join(reversed(next_mem)).encode("HEX"), 16), matches, mem, int_size=int_size)
iterations = 0
def ea_warning(text, additional_buttons=[], title="EA Warning"):
global warning
global form
global buttons
warning = QtGui.QFrame()
form = Warning_UI()
form.setupUi(warning)
form.label.setText(text)
form.pushButton.clicked.connect(warning.close)
for button, handler in additional_buttons:
setattr(form, button, QtGui.QPushButton(warning))
getattr(form, button).clicked.connect(handler)
getattr(form, button).setText(QtGui.QApplication.translate("Dialog", button, None, QtGui.QApplication.UnicodeUTF8))
form.horizontalLayout.addWidget(getattr(form, button))
warning.setWindowFlags(warning.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
warning.setWindowTitle(QtGui.QApplication.translate("Dialog", title, None, QtGui.QApplication.UnicodeUTF8))
warning.show()
def save_config():
with open(root_dir + "config.json", "w") as w:
dump(config, w)
def load_config():
global config
init_config = {
"libc_offsets": [0, 0, 0, 0],
"trace_dir": 0,
"stack_display_length": 25,
"apply_skin_on_startup": True,
"current_skin": ["1c1c2a", "ffffff", "818181", "00d5ff", "ffffff", "202030", "ffffff", "00e6ff", "ffffff"],
"skins": [["Neon Dark", "212121", "ffffff", "414141", "00fff7", "ffffff", "282828", "ffffff", "00ffea", "ffffff"],
["Neon Blue", "1c1c2a", "ffffff", "818181", "00d5ff", "ffffff", "202030", "ffffff", "00e6ff", "ffffff"]]
}
if not isfile(root_dir + "config.json"):
config = init_config
else:
try:
with open(root_dir + "config.json", "r") as f:
config = load(f)
except:
print 'IDA EA Error: Config File ("config.json") contained invalid JSON. Reinitializing config...'
remove(root_dir + "config.json")
load_config()
return
for i,v in init_config.items():
if i not in config:
config[i] = v
save_config()
def a_sync(func, ThreadClass=QtCore.QThread):
# if reference to QThread is not global,
# python's garbage collection cleans up QThread whilst running causing a crash in IDA
# to prevent this we have an array of global thread references which expands as nessecary
thread_idx = next((i for i, v in enumerate(threads) if not v.isRunning()), False)
thread = ThreadClass()
if thread_idx is not False:
threads[thread_idx] = thread
else:
threads.append(thread)
thread.run = func
thread.start()
max_iterations = 10
iterations = 0
codeSegment = get_segm_by_name(".text")
if codeSegment:
codeStart = codeSegment.startEA
codeEnd = codeSegment.endEA
b_red = 'red'
b_green = 'green'
b_yellow = 'yellow'
b_lightblue = 'blue'
file_name = None
_32_bit = None
root_dir = __file__[:max(__file__.rfind("/"), __file__.rfind("\\"), 0)] + "/"
warning = None
config = None
load_config()
threads = []