forked from jtpereyda/boofuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathida_fuzz_library_extender.py
170 lines (124 loc) · 4.41 KB
/
ida_fuzz_library_extender.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
#!c:\python\python.exe
#
# Aaron Portnoy
# TippingPoint Security Research Team
# (C) 2007
#
from __future__ import print_function
from io import open
from past.builtins import map, xrange
def get_string(ea):
str_type = GetStringType(ea)
if str_type == 0:
string_buf = ""
while Byte(ea) != 0x00:
string_buf += "%c" % Byte(ea)
ea += 1
return string_buf
elif str_type == 3:
string_buf = ""
while Word(ea) != 0x0000:
string_buf += "%c%c" % (Byte(ea), Byte(ea + 1))
ea += 2
return string_buf
else:
pass
def get_arguments(ea):
xref_ea = ea
args = 0
found = None
if GetMnem(xref_ea) != "call":
return False
cur_ea = PrevHead(ea, xref_ea - 32)
while (cur_ea < xref_ea - 32) or (args <= 6):
cur_mnem = GetMnem(cur_ea)
if cur_mnem == "push":
args += 1
op_type = GetOpType(cur_ea, 0)
if Comment(cur_ea):
pass
# print(" %s = %s," % (Comment(cur_ea), GetOpnd(cur_ea, 0)))
else:
if op_type == 1:
pass
# print(" %s" % GetOpnd(cur_ea, 0))
elif op_type == 5:
found = get_string(GetOperandValue(cur_ea, 0))
elif cur_mnem == "call" or "j" in cur_mnem:
break
cur_ea = PrevHead(cur_ea, xref_ea - 32)
if found:
return found
def find_ints(start_address):
constants = []
# loop heads
for head in Heads(start_address, SegEnd(start_address)):
# if it's code, check for cmp instruction
if isCode(GetFlags(head)):
mnem = GetMnem(head)
op1 = int(GetOperandValue(head, 1))
# if it's a cmp and it's immediate value is unique, add it to the list
if "cmp" in mnem and op1 not in constants:
constants.append(op1)
print("Found %d constant values used in compares." % len(constants))
print("-----------------------------------------------------")
for i in xrange(0, len(constants), 20):
print(constants[i : i + 20])
return constants
def find_strings(start_address):
strings = []
# do import checking
import_ea = start_address
while import_ea < SegEnd(start_address):
import_name = Name(import_ea)
if len(import_name) > 1 and "cmp" in import_name:
xref_start = import_ea
xref_cur = DfirstB(xref_start)
while xref_cur != BADADDR:
# print("Found call to ", import_name)
string_arg = get_arguments(xref_cur)
if string_arg and string_arg not in strings:
strings.append(string_arg)
xref_cur = DnextB(xref_start, xref_cur)
import_ea += 4
# now do FLIRT checking
for function_ea in Functions(SegByName(".text"), SegEnd(start_address)):
flags = GetFunctionFlags(function_ea)
if flags & FUNC_LIB:
lib_name = GetFunctionName(function_ea)
if len(lib_name) > 1 and "cmp" in lib_name:
# found one, now find xrefs to it and grab arguments
xref_start = function_ea
xref_cur = RfirstB(xref_start)
while xref_cur != BADADDR:
string_arg = get_arguments(xref_cur)
if string_arg and string_arg not in strings:
strings.append(string_arg)
xref_cur = RnextB(xref_start, xref_cur)
print("Found %d string values used in compares." % len(strings))
print("-----------------------------------------------------")
for i in xrange(0, len(strings), 5):
print(strings[i : i + 5])
return strings
# get file names to save to
constants_file = AskFile(1, ".fuzz_ints", "Enter filename for saving the discovered integers: ")
strings_file = AskFile(1, ".fuzz_strings", "Enter filename for saving the discovered strings: ")
# get integers
start_address = SegByName(".text")
constants = find_ints(start_address)
constants = map(lambda x: "0x%x" % x, constants)
print()
# get strings
start_address = SegByName(".idata")
strings = find_strings(start_address)
# write integers
fh = open(constants_file, "w+")
for c in constants:
fh.write(c + "\n")
fh.close()
# write strings
fh = open(strings_file, "w+")
for s in strings:
fh.write(s + "\n")
fh.close()
print("Done.")