-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanti-fool-radare2.py
260 lines (227 loc) · 8.16 KB
/
anti-fool-radare2.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
# ----------------------------------------------------------------------------
# anti-fool-radare2.py
#
# Written by Manuel Gebele (m.gebele‹ατ›tuta.io)
# This code is licensed under the MIT License (MIT).
#
# An attempt to improve radare2's function detection such as tail-calls and
# address-taken functions.
# ----------------------------------------------------------------------------
import sys
import collections
import argparse
import r2pipe
r2p = None
functions = []
disasm_cache = {}
""" A scaled-down version of r2pipe's JSON encoded function info.
"""
Function = collections.namedtuple('Function', ('name', 'address'))
""" A scaled-down version of r2pipe's JSON encoded section info.
"""
Section = collections.namedtuple('Section', 'name')
""" A scaled-down version of r2pipe's JSON encoded reference info.
"""
Reference = collections.namedtuple('Reference', ('type', 'name', 'address'))
""" A scaled-down version of r2pipe's JSON encoded instruction info.
"""
Instruction = collections.namedtuple('Instruction', ('address', 'disasm'))
def get_functions():
""" Returns a list of available functions.
"""
return r2p.cmdj('aflj')
def get_function_disasm(function):
""" Returns a dict of the function's (recursive) disassembly.
"""
if function.address not in disasm_cache:
cmdj = 'pdfj @ ' + function.address
disasm = r2p.cmdj(cmdj)
if not disasm:
disasm = { "ops": [ { } ] }
[op.setdefault("type", "n/a") for op in disasm['ops']]
disasm_cache[function.address] = disasm
return disasm_cache[function.address]
def get_section(offset):
""" Returns the section to which the offset belongs to.
"""
cmdj = 'iSj. @ ' + str(offset)
section = r2p.cmdj(cmdj)
name = section['name'] if section else 'n/a'
return Section(name)
def get_noreturns():
""" Returns a list of well-known noreturns.
"""
return r2p.cmd('tn')
def get_function_references(function):
""" Returns a list of available data/code references.
"""
cmdj = 'axffj @ ' + function.name
references = r2p.cmdj(cmdj)
return references if references else []
def excluded_section_names():
""" Returns a list of excluded section names.
"""
return ['.plt', '.plt.got', 'n/a']
def interested_in_section(section):
""" Returns whether this section is of interest.
"""
return section.name not in excluded_section_names()
def build_function_info():
""" Build a scaled-down version of r2pipe's function list.
"""
for f in get_functions():
section = get_section(f['offset'])
if not interested_in_section(section):
continue
functions.append(Function(f['name'],
hex(f['offset'])))
def is_valid_noreturn(noreturn):
""" Determines whether noreturn is a valid noreturn target.
"""
noreturns = get_noreturns().split()
noreturns = ['sym.imp.' + nr for nr in noreturns]
return noreturn in noreturns
def search_tail_call(function):
""" Searches for tail calls.
"""
disasm = get_function_disasm(function)
op = disasm['ops'][-1]
if op['type'] != 'jmp':
return None
return Instruction(hex(op['offset']), op['disasm'])
def search_instructions(insn_type, function):
""" Searches for the given instruction type (e.g. call, ujmp, ...).
"""
disasm = get_function_disasm(function)
for op in disasm['ops']:
if op['type'] != insn_type:
continue
yield Instruction(hex(op['offset']), op['disasm'])
def search_references(ref_type, function):
""" Searches for the given reference type (DATA, CODE, or CALL).
"""
references = get_function_references(function)
for r in references:
if r['type'] != ref_type:
continue
yield Reference(r['type'], r['name'], hex(r['at']))
def search_address_taken_functions(function):
""" Searches for functions that have their address taken from
memory/register.
"""
function_names = [f['name'] for f in get_functions()]
references = search_references('DATA', function)
for r in references:
if r.name not in function_names:
continue
yield r
def search_noreturn_fake(function):
""" Searches for functions that are potential noreturn fakes.
"""
disasm = get_function_disasm(function)
op = disasm['ops'][-1]
if (op['type'] != 'call'):
return None
noreturn = op['disasm'].split()[-1]
if is_valid_noreturn(noreturn):
return None
return Instruction(hex(op['offset']), op['disasm'])
def print_tail_calls():
""" Prints all tail calls in the binary.
"""
for f in functions:
tail_call = search_tail_call(f)
if tail_call:
print('Found tail-call in %s at %s (%s)'
% (f.name,
tail_call.address,
tail_call.disasm))
def print_indirect_calls():
""" Prints all indirect calls in the binary.
"""
for f in functions:
indirect_calls = search_instructions('ucall', f)
for indirect_call in indirect_calls:
print('Found indirect call in %s at %s (%s)'
% (f.name,
indirect_call.address,
indirect_call.disasm))
def print_indirect_jumps():
""" Prints all indirect jumps in the binary.
"""
for f in functions:
indirect_jumps = search_instructions('ujmp', f)
for indirect_jump in indirect_jumps:
print('Found indirect jump in %s at %s (%s)'
% (f.name,
indirect_jump.address,
indirect_jump.disasm))
def print_address_taken_functions():
""" Prints all address-taken functions in the binary.
"""
for f in functions:
references = search_address_taken_functions(f)
for r in references:
print('Found address-taken function in %s at %s (%s)'
% (f.name,
r.address,
r.name))
def print_noreturn_fakes():
""" Prints all potential noreturn fakes in the binary.
"""
for f in functions:
noreturn_fake = search_noreturn_fake(f)
if noreturn_fake:
print('Found potential noreturn fake in %s at %s (%s)'
% (f.name,
noreturn_fake.address,
noreturn_fake.disasm))
# __main__
SCRIPT_DESCRIPTION = 'Exercise 3: Improving radare2\'s function detection'
SCRIPT_VERSION = "0.1"
argparser = argparse.ArgumentParser(
usage='usage: %(prog)s [options] <binary>',
description=SCRIPT_DESCRIPTION, prog=sys.argv[0])
argparser.add_argument('binary',
nargs='?', default=None, help='ELF binary to analyse')
argparser.add_argument('-t', '--tail-calls',
action='store_true', dest='search_tail_calls',
help='Search for tail-calls')
argparser.add_argument('-c', '--indirect-calls',
action='store_true', dest='search_indirect_calls',
help='Search for indirect calls')
argparser.add_argument('-j', '--indirect-jumps',
action='store_true', dest='search_indirect_jumps',
help='Search for indirect jumps')
argparser.add_argument('-f', '--address-taken',
action='store_true', dest='search_address_taken_functions',
help='Search for address taken functions')
argparser.add_argument('-n', '--noreturn-fakes',
action='store_true', dest='search_noreturn_fakes',
help='Search for potential noreturn fakes')
argparser.add_argument('-a', '--all',
action='store_true', dest='search_all',
help='Equivalent to: -t -c -j -f -n')
argparser.add_argument('-v', '--version',
action='version', version=SCRIPT_VERSION)
args = argparser.parse_args()
if not args.binary:
argparser.print_help()
sys.exit(0)
r2p = r2pipe.open(args.binary, flags=['-2'])
r2p.cmd('aaa')
build_function_info()
search_all = False
if args.search_all or len(sys.argv) == 2:
search_all = True
if args.search_tail_calls or search_all:
print_tail_calls()
if args.search_indirect_calls or search_all:
print_indirect_calls()
if args.search_indirect_jumps or search_all:
print_indirect_jumps()
if args.search_address_taken_functions or search_all:
print_address_taken_functions()
if args.search_noreturn_fakes or search_all:
print_noreturn_fakes()
r2p.quit()