-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_tracer.py
148 lines (128 loc) · 4.25 KB
/
heap_tracer.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
import pin
in_use={}
free_list={}
size=0
f=open("output","w")
def check_write(addr,n_bytes):
if free_list.has_key(addr):
print "\n[+][+]Writing data to freed memory at {}".format(hex(addr))
if in_use.has_key(addr) and in_use[addr] < n_bytes:
print "\n[+][+]Overflow while writing to {}".format(hex(addr))
for key in in_use.keys():
size=in_use[key]
if addr > key and addr <= key + size:
if n_bytes > (key+size-addr):
print "\n[+][+]Overflow while writing to chunk {} at offset {}".format(hex(key),addr-key)
return 1
return 0
def fgets(everything):
target = everything['arg_0']
n_bytes = everything['arg_1']
f.write("fgets({},{},stdin)\n".format(hex(target),n_bytes))
check_write(target,n_bytes)
def read_call(everything):
target = everything['arg_1']
n_bytes = everything['arg_2']
f.write("read(0,{},{})\n".format(hex(target),n_bytes))
check_write(target,n_bytes)
def strncpy(everything):
target = everything['arg_0']
n_bytes = everything['arg_2']
f.write("strncpy({},{},{})".format(hex(target),hex(everything['arg_1']),n_bytes))
check_write(target,n_bytes)
def memcpy(everything):
target = everything['arg_0']
n_bytes = everything['arg_1']
f.write("memcpy({},{},{})".format(hex(target),hex(everything['arg_1']),n_bytes))
check_write(target,n_bytes)
def calloc_before(everything):
global size
size = pin.get_pointer(everything['reg_gdi'])
def calloc_after(everything):
global size
addr = pin.get_pointer(everything['reg_gax'])
f.write("calloc({}) returns {}\n".format(size,hex(addr)))
if in_use.has_key(addr):
print "\n[+][+]Chunk {} allocated more than once".format(hex(addr))
if free_list.has_key(addr):
del free_list[addr]
in_use[addr]=size
def malloc_before(everything):
global size
size=pin.get_pointer(everything['reg_gdi'])
def malloc_after(everything):
global size
addr=pin.get_pointer(everything['reg_gax'])
f.write("malloc({}) returns {}\n".format(size,hex(addr)))
if in_use.has_key(addr):
print "\n[+][+]Chunk {} allocated more than once".format(hex(addr))
if free_list.has_key(addr):
del free_list[addr]
in_use[addr]=size
def free(everything):
rdi=pin.get_pointer(everything['reg_gdi'])
f.write("free({})\n".format(hex(rdi)))
if free_list.has_key(rdi):
print "\n[+][+]Chunk {} freed more than once".format(hex(rdi))
if in_use.has_key(rdi):
size=in_use[rdi]
del in_use[rdi]
free_list[rdi]=size
def img_handler(img):
rtn=pin.RTN_FindByName(img,'malloc')
if pin.RTN_Valid(rtn):
pin.RTN_Open(rtn)
pin.RTN_InsertCall(pin.IPOINT_BEFORE,'malloc',rtn,1,malloc_before)
pin.RTN_InsertCall(pin.IPOINT_AFTER,'malloc',rtn,1,malloc_after)
pin.RTN_Close(rtn)
rtn = pin.RTN_FindByName(img,'calloc')
if pin.RTN_Valid(rtn):
pin.RTN_Open(rtn)
pin.RTN_InsertCall(pin.IPOINT_BEFORE,'calloc',rtn,1,calloc_before)
pin.RTN_InsertCall(pin.IPOINT_AFTER,'calloc',rtn,1,calloc_after)
pin.RTN_Close(rtn)
rtn = pin.RTN_FindByName(img, "free")
if pin.RTN_Valid(rtn):
pin.RTN_Open(rtn)
pin.RTN_InsertCall(pin.IPOINT_BEFORE, "free", rtn, 1, free)
pin.RTN_Close(rtn)
rtn = pin.RTN_FindByName(img,'fgets')
if pin.RTN_Valid(rtn):
pin.RTN_Open(rtn)
pin.RTN_InsertCall(pin.IPOINT_BEFORE,'fgets',rtn,1,fgets)
pin.RTN_Close(rtn)
rtn = pin.RTN_FindByName(img,'read')
if pin.RTN_Valid(rtn):
pin.RTN_Open(rtn)
pin.RTN_InsertCall(pin.IPOINT_BEFORE,'read',rtn,1,read_call)
pin.RTN_Close(rtn)
rtn = pin.RTN_FindByName(img, 'strncpy')
if pin.RTN_Valid(rtn):
pin.RTN_Open(rtn)
pin.RTN_InsertCall(pin.IPOINT_BEFORE,'strncpy',rtn,2,strncpy)
pin.RTN_Close(rtn)
rtn = pin.RTN_FindByName(img, 'memcpy')
if pin.RTN_Valid(rtn):
pin.RTN_Open(rtn)
pin.RTN_InsertCall(pin.IPOINT_BEFORE,'memcpy',rtn,1,memcpy)
pin.RTN_Close(rtn)
def exiting():
f.close()
print "In use chunks"
if len(in_use.keys()) < 1:
print "\n[+]Empty list"
else:
for keys in in_use.keys():
print "{} : {}".format(hex(keys),in_use[keys])
print "Freed chunks"
if len(free_list.keys()) < 1:
print "\n[+]Empty list"
else:
for keys in free_list.keys():
print "{} : {}".format(hex(keys),free_list[keys])
if __name__ =="__main__":
try:
pin.IMG_AddInstrumentFunction(img_handler)
pin.AddFiniFunction(exiting)
except KeyboardInterrupt:
exiting()