-
Notifications
You must be signed in to change notification settings - Fork 71
/
ea_heap.py
341 lines (244 loc) · 9.57 KB
/
ea_heap.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from api_funcs import *
from ea_UI import Heap_UI, Set_Offset_UI
from ea_utils import QtCore, QtWidgets, config, ea_warning, get_bits, read, root_dir, save_config
from idaapi import *
from idautils import *
from idc import *
from re import findall
class Hook(DBG_Hooks):
def __init__(self):
DBG_Hooks.__init__(self)
def dbg_bpt(self, tid, bptea):
if bptea == malloc_addr:
addr = get_rg("RAX") - int_size*2
c = chunk(*(to_list(dbg_read_memory(addr, 6 * int_size)) + [addr]))
addr = hex(addr).replace("L", "")
if not form.listWidget_4.findItems(addr, QtCore.Qt.MatchFlag.MatchExactly):
form.listWidget_4.addItem(addr)
chunkmap_2[addr] = c
update_chunk(c)
get_malloc_state()
return 0
class field():
def __init__(self,val,size):
self.val = val
self.size = size
class malloc_state():
def __init__(self, address):
self.mutex = field(0, 0.5 if int_size == 8 else 1)
self.flags = field(0, 0.5 if int_size == 8 else 1)
self.fastbinsY = field([1],10)
self.top = field(0,1)
self.last_remainder = field(0,1)
self.bins = field([1], 254)
self.binmap = field([0.5], 4)
self.next = field(0,1)
self.next_free = field(0,1)
self.attached_threads = field(0,1)
self.system_mem = field(0,1)
self.max_system_mem = field(0,1)
# Not a member of glibc malloc_state
self.address = address
class chunk():
def __init__(self, prev_size, size, fd, bk, fd_nextsize, bk_nextsize, address):
self.prev_size = prev_size
self.size = size & 0xfffffffc
self.fd = fd
self.bk = bk
self.fd_nextsize =fd_nextsize
self.bk_nextsize = bk_nextsize
self.prev_in_use = size & 0x1
self.is_mmapped = 1 if size & 0x2 else 0
# Not a member of glibc chunk
self.data = ""
self.address = address
def __str__(self):
a = ("prev_size" ,"size" ,"fd", "bk", "fd_nextsize", "bk_nextsize")
return ( "Chunk @ " + hex(getattr(self, "address")) + " = {\n" +
"".join( " " + i + " = " + hex(getattr(self, i)) + "\n" for i in a) + "\n" +
" prev_in_use = " + ("True" if self.prev_in_use else "False") + "\n"
" is_mmapped = " + ("True" if self.is_mmapped else "False") + "\n"
+ "}")
def to_hex(x):
return hex(x).replace("L","")
def to_list(x, chunk_size = 8):
return [ to_int(x[i:i+chunk_size]) for i in range(0, len(x), chunk_size) ]
def to_int(x):
a = "".join(reversed(x)).encode("HEX")
if len(a) % 2:
a = "0" + a
return int(a, 16)
def update_chunk(c):
next_chunk = dbg_read_memory(c.address, 6 * int_size)
if next_chunk:
c = chunk(*(to_list(next_chunk) + [c.address]))
c.data = dbg_read_memory(c.address + 2 * int_size, min(c.size, 0x500)).encode("HEX")
if c.data:
c.data = " ".join(c.data[i:i + 2] for i in range(0, len(c.data), 2))
else:
c.data = ""
return c
def fill_field(malloc_state, field, mem, current, list=False):
field_size = getattr(malloc_state,field).size
if list:
var_size = getattr(malloc_state,field).val.pop()
setattr(malloc_state, field, to_list(mem[current:int(current + int_size * var_size * field_size)], int(int_size*var_size)))
else:
var_size = 1
setattr(malloc_state, field, to_int(mem[current:int(current + int_size * field_size)]))
current += int(int_size * field_size * var_size)
return current
def get_malloc_state():
main_arena = malloc_state(main_arena_addr)
print main_arena
mem = dbg_read_memory(main_arena.address, 2200)
current = 0
fields = ["mutex", "flags", "fastbinsY", "top", "last_remainder", "bins",
"binmap", "next", "next_free", "attached_threads", "system_mem", "max_system_mem"]
for field in fields:
current = fill_field(main_arena, field, mem, current,
True if isinstance(getattr(main_arena, field).val, list) else False)
main_arena.fastbinsY = [[item] for item in main_arena.fastbinsY]
main_arena.bins = [[item] for item in main_arena.bins]
form.listWidget.clear()
form.listWidget_3.clear()
for n, bin in enumerate(main_arena.fastbinsY):
if bin and bin[0]:
get_chunks(bin, main_arena.address)
if bin:
name = "Fastbin %s" % hex(n)
form.listWidget.addItem(name)
binmap[name] = bin
elif bin:
bin.pop()
for n, bin in enumerate(main_arena.bins):
if bin and not (main_arena.address < bin[0] < main_arena.address + 2200):
get_chunks(bin, main_arena.address)
if bin:
name = "Bin %s" % hex(n)
form.listWidget_3.addItem(name)
binmap[name] = bin
elif bin:
bin.pop()
def get_chunks(bin, state_addr):
next_chunk = True
chunks = []
addr = bin.pop()
while next_chunk:
next_chunk = dbg_read_memory(addr, 6 * int_size)
if next_chunk:
c = chunk(*(to_list(next_chunk) + [addr]))
bin.append(c)
chunks.append(c.fd)
c.data = dbg_read_memory(addr + 2 * int_size, min(c.size, 0x500)).encode("HEX")
c.data = " ".join(c.data[i:i+2] for i in range(0, len(c.data), 2))
if state_addr < c.fd < state_addr + 2200:
break
addr = c.fd
else:
break
def get_main_arena():
global base_addr
for addr in Segments():
if findall("libc_.*\.so",SegName(addr)):
seg = getseg(addr)
if seg.perm | SEGPERM_EXEC == seg.perm:
return addr
def select_bin(item):
global chunkmap
form.listWidget_2.clear()
for chunk in binmap[item.text()]:
form.listWidget_2.addItem(hex(chunk.address))
chunkmap[hex(chunk.address).replace("L", "")] = chunk
def select_chunk(item, chunkmap):
chunk = chunkmap[item.text()] = update_chunk(chunkmap[item.text()])
form.textEdit.clear()
form.textEdit.insertHtml("<style>p{font-family:Hack;font-size:14px}</style>" + "<p>" + chunk.data + "</p>")
string = (chunk_template) % (
to_hex(chunk.address),
to_hex(chunk.prev_size),
to_hex(chunk.size),
to_hex(chunk.fd),
to_hex(chunk.bk),
to_hex(chunk.fd_nextsize),
to_hex(chunk.bk_nextsize),
"True" if chunk.prev_in_use else "False",
"True" if chunk.is_mmapped else "False"
)
form.textEdit_2.clear()
form.textEdit_2.insertHtml(string)
def set_config(restart):
global b
b = QtWidgets.QWidget()
form = Set_Offset_UI()
form.setupUi(b)
b.show()
form.pushButton.clicked.connect(lambda: get_text(form, restart))
def get_text(form, restart):
global malloc_offset
global main_arena_offset
offsets = [form.lineEdit.text(), form.lineEdit_2.text(), form.lineEdit_3.text(), form.lineEdit_4.text()]
for x in range(4):
if offsets[x][:2] == "0x":
offsets[x] = int(offsets[x][2:], 16)
else:
offsets[x] = int(offsets[x])
config["libc_offsets"] = offsets
main_arena_offset, malloc_offset = offsets[:2] if int_size == 8 else offsets[2:]
b.close()
save_config()
if restart:
ea_heap()
def ea_heap():
global form
global a
global item_no
global hook
global main_arena_addr
global malloc_addr
if "ELF" not in get_file_type_name():
ea_warning("Executable must be ELF fomat (glibc)")
else:
if main_arena_offset == 0 and malloc_offset == 0:
set_config(True)
else:
if not is_debugger_on():
ea_warning("Application must be running")
else:
base_addr = get_main_arena()
if not base_addr:
ea_warning("Could not find C Library in Segments")
else:
malloc_addr = find_ins("ret", base_addr + malloc_offset)
main_arena_addr = base_addr + main_arena_offset
a = QtWidgets.QWidget()
form = Heap_UI()
form.setupUi(a)
form.textEdit.setReadOnly(True)
form.textEdit_2.setReadOnly(True)
a.show()
hook = Hook()
hook.hook()
a.closeEvent = lambda x: hook.unhook()
form.listWidget.itemClicked.connect(select_bin)
form.listWidget_3.itemClicked.connect(select_bin)
form.listWidget_2.itemClicked.connect(lambda x: select_chunk(x, chunkmap))
form.listWidget_4.itemClicked.connect(lambda x: select_chunk(x, chunkmap_2))
form.pushButton_2.clicked.connect(lambda :set_config(False))
form.pushButton.clicked.connect(get_malloc_state)
form.checkBox.stateChanged.connect(lambda x: (
add_bp(malloc_addr, 10), hook.hook()) if x else (add_bp(malloc_addr, 2), hook.unhook()))
get_malloc_state()
chunk_template = read(root_dir + "chunk_template.html")
int_size = 4 if get_bits() else 8
main_arena_offset, malloc_offset = config["libc_offsets"][:2] if int_size == 4 else config["libc_offsets"][2:]
chunkmap = {}
chunkmap_2 = {}
binmap = {}
form = None
a = None
b = None
hook = None
base_addr = None
main_arena_addr = None
malloc_addr = None