-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAngryIDA.py
667 lines (622 loc) · 18.5 KB
/
AngryIDA.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
'''
angr plug-in for IDA Pro.
Integrates point and click use of the angr binary analysis framework
inside of IDA Pro. The plug-in adds a sub menu, called AngryIDA, to
IDA View-A's pop-up context menu. Inside the IDA View-A window
right-click and expand the AngryIDA menu item to use.
'''
from __future__ import print_function
import angr
import claripy
import idaapi #pylint: disable=import-error
from idaapi import Form #pylint: disable=import-error
FIND_ADDRS = []
AVOID_ADDRS = []
EXP_OPTS = {
"load":{
"auto_load_libs":False
},
"state":{
"discard_lazy_solves":True
},
"path_group":{
"immutable":False
},
"time_limit":{
"minutes":10
},
"stdin":{
"length":-1,
"ascii":False,
"null":False,
"white_space":False,
"newline":True
},
"args":{
"length":-1
}
}
#----------------------------------------------------------------------------------
# Possible solution to limiting angr exploring the binary. Ideas:
# - Limit time
# - Limit RAM
# - Limit CPU
#
# import sys
# import thread
# import threading
# from time import sleep
#
# def quit_thread(fn_name):
# print('{0} took too long'.format(fn_name), file=sys.stderr)
# thread.interrupt_main()
#
# def time_limit(minutes):
# def decorate(fn):
# def internal(*args, **kwargs):
# timer = threading.Timer(minutes, quit_thread, args=[fn.__name__])
# timer.start()
# try:
# result = fn(*args, **kwargs)
# finally:
# timer.cancel()
# return result
# return internal
# return decorate
#----------------------------------------------------------------------------------
class TestEmbeddedChooserClass(Choose2):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
def __init__(self, title, nb=5, flags=0):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
Choose2.__init__(
self,
title,
[["Address", 10], ["Name", 30]],
embedded=True,
width=30,
height=20,
flags=flags
)
self.n = 0
self.items = [self.make_item()]*(nb+1)
self.icon = 5
self.selcount = 0
def make_item(self):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
r = [str(self.n), "func_%04d" % self.n]
self.n += 1
return r
def OnClose(self):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
pass
def OnGetLine(self, n):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
print("getline %d" % n)
return self.items[n]
def OnGetSize(self):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
n = len(self.items)
print("getsize -> %d" % n)
return n
class ExpFormOpts(Form):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
def __init__(self):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
self.invert = False
self.EChooser = TestEmbeddedChooserClass("E1", flags=Choose2.CH_MULTI)
Form.__init__(self, r"""STARTITEM {id:rDiscardLazySolves}
Options
<Discard LAZY_SOLVES:{rDiscardLazySolves}>
<Immutable:{rImmutable}>
<Auto Load Libs:{rAutoLoadLibs}>{cGroup1}>
<Time Limit:{iTimeLimit}>
""", {
'cGroup1': Form.ChkGroupControl(("rDiscardLazySolves", "rImmutable", "rAutoLoadLibs")),
'iTimeLimit':Form.NumericInput(),
})
class ExpFormStdin(Form):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
def __init__(self):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
self.invert = False
self.EChooser = TestEmbeddedChooserClass("E1", flags=Choose2.CH_MULTI)
Form.__init__(self, r"""STARTITEM
Symbolic stdin
<##Enter length of stdin:{iStdinLen}>
<Ending newline:{rNewline}>
<Allow Null:{rNull}>
<White Space:{rWhite}>
<Force ASCII:{rASCII}>{cGroup2}>
""", {
'iStdinLen':Form.NumericInput(),
'cGroup2': Form.ChkGroupControl(("rNewline", "rNull", "rWhite", "rASCII"))
})
class ExpFormArgs(Form):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
def __init__(self):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
self.invert = False
self.EChooser = TestEmbeddedChooserClass("E1", flags=Choose2.CH_MULTI)
Form.__init__(self, r"""STARTITEM
Arguments
<##Enter length of argument:{iArgLen}>
""", {
'iArgLen':Form.NumericInput()
})
class ActionHandler(idaapi.action_handler_t):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
def __init__(self, action):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
idaapi.action_handler_t.__init__(self)
self.action = action
def activate(self, ctx):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
if self.action == "Finds:Set":
find_set()
elif self.action == "Finds:Remove":
find_remove()
elif self.action == "Finds:Print":
find_view()
elif self.action == "Avoids:Set":
avoid_set()
elif self.action == "Avoids:Remove":
avoid_remove()
elif self.action == "Avoids:Print":
avoid_view()
elif self.action == "Explore:Run":
explore_run()
elif self.action == "Explore:Options":
explore_options()
elif self.action == "Explore:Stdin":
explore_stdin()
elif self.action == "Explore:Arguments":
explore_arguments()
elif self.action == "Refresh:Refresh":
refresh()
elif self.action == "Quit:Quit":
my_quit()
def update(self, ctx):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
return idaapi.AST_ENABLE_ALWAYS
class Hooks(idaapi.UI_Hooks):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
@staticmethod
def finish_populating_tform_popup(form, popup):
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
"""
if idaapi.get_tform_title(form) == "IDA View-A":
idaapi.attach_action_to_popup(form, popup, "Finds:Set", "AngryIDA/Finds/")
idaapi.attach_action_to_popup(form, popup, "Finds:Remove", "AngryIDA/Finds/")
idaapi.attach_action_to_popup(form, popup, "Finds:Print", "AngryIDA/Finds/")
idaapi.attach_action_to_popup(form, popup, "Avoids:Set", "AngryIDA/Avoids/")
idaapi.attach_action_to_popup(form, popup, "Avoids:Remove", "AngryIDA/Avoids/")
idaapi.attach_action_to_popup(form, popup, "Avoids:Print", "AngryIDA/Avoids/")
idaapi.attach_action_to_popup(form, popup, "Explore:Run", "AngryIDA/Explore/")
idaapi.attach_action_to_popup(form, popup, "Explore:Options", "AngryIDA/Explore/")
idaapi.attach_action_to_popup(form, popup, "Explore:Stdin", "AngryIDA/Explore/")
idaapi.attach_action_to_popup(form, popup, "Explore:Arguments", "AngryIDA/Explore/")
idaapi.attach_action_to_popup(form, popup, "Refresh:Refresh", "AngryIDA/")
idaapi.attach_action_to_popup(form, popup, "Quit:Quit", "AngryIDA/")
def set_line_color(color, addr=here(), item=CIC_ITEM): #pylint: disable=undefined-variable
"""
Arguments:
Mandatory:
- ( Name: color,
Position: 0,
Type: int,
Value: hex/int color value )
Optional:
- ( Name: addr,
Position: 1,
Type: int,
Default: here(),
Value: hex/int memory address )
- ( Name: item,
Position: 2,
Type: IDA Item,
Default: CIC_ITEM,
Value: IDA Item )
Return: None
Description:
- Change instruction line color in IDA with specified color and address.
Disabled pylint errors due to IDA function calls.
TODO:
- Nothing currently.
"""
SetColor(addr, item, color) #pylint: disable=undefined-variable
def find_set():
"""
Arguments: None
Return Value: None
Description:
- Function is called by the Finds:Set action and adds the address which is
currently selected in IDA View-A to the list of addresses angr will find
while exploring.
TODO:
- Nothing.
"""
addr = idaapi.get_screen_ea()
if addr in AVOID_ADDRS:
AVOID_ADDRS.remove(addr)
print("AngryIDA: Removed avoid address [%s]" % hex(addr))
FIND_ADDRS.append(addr)
set_line_color(0x208020, addr)
print("AngryIDA: Added find address [%s]" % hex(addr))
def find_remove():
"""
Arguments: None
Return Value: None
Description:
- Function is called by the Finds:Remove action and removes the address which
is currently selected in IDA View-A from the list of addresses angr will find
while exploring.
TODO:
- Nothing.
"""
addr = idaapi.get_screen_ea()
if addr in FIND_ADDRS:
FIND_ADDRS.remove(addr)
set_line_color(0xffffff, addr)
print("AngryIDA: Removed find address [%s]" % hex(addr))
def find_view():
"""
Arguments: None
Return Value: None
Description:
- Function is called by the Finds:Print action and displays
all the address in the global FIND_ADDRS list. This is
displayed in the IDA Pro Output Window.
TODO:
- Nothing
"""
print("AngryIDA:\n\tFind Addresses")
for addr in FIND_ADDRS:
print("\t\t%s" % hex(addr))
def avoid_set():
"""
Arguments:
Return Value:
Description:
- Function is called by the Avoids:Set action and adds the address which is
currently selected in IDA View-A to the list of addresses angr will avoid
while exploring.
TODO:
- Nothing.
"""
addr = idaapi.get_screen_ea()
if addr in FIND_ADDRS:
FIND_ADDRS.remove(addr)
print("AngryIDA: Removed find address [%s]" % hex(addr))
AVOID_ADDRS.append(addr)
set_line_color(0x2020c0, addr)
print("AngryIDA: Added avoid address [%s]" % hex(addr))
def avoid_remove():
"""
Arguments: None
Return Value: None
Description:
- Function is called by the Avoids:Remove action and removes the address
which is currently selected in IDA View-A from the list of addresses angr will
avoid while exploring.
TODO:
- Nothing.
"""
addr = idaapi.get_screen_ea()
if addr in AVOID_ADDRS:
AVOID_ADDRS.remove(addr)
set_line_color(0xffffff, addr)
print("AngryIDA: Removed avoid address [%s]" % hex(addr))
def avoid_view():
"""
Arguments: None
Return Value: None
Description:
- Function is called by the Avoids:Print action and displays
all the address in the global AVOID_ADDRS list. This is
displayed in the IDA Pro Output Window.
TODO:
- Nothing
"""
print("\tAvoid Addresses")
for addr in AVOID_ADDRS:
print("\t\t%s" % hex(addr))
#@time_limit(EXP_OPTS["time_limit"]["minutes"])
def explore_run():
"""
Arguments:
Return Value:
Description:
-
TODO:
- Doc String
- Handle Multiple Symbolic Arguments
- Handle Multiple Symbolic stdin
- Force ASCII printable for stdin
- Handle Symbolic Files
- Handle Symbolic Memory
-
"""
print(EXP_OPTS["time_limit"]["minutes"])
binary_file = idaapi.get_input_file_path()
proj = angr.Project(binary_file, load_options=EXP_OPTS["load"])
arg_len = EXP_OPTS["args"]["length"]
if arg_len > 0:
argv1 = claripy.BVS("argv1", arg_len * 8)
initial_state = proj.factory.entry_state(args=[binary_file, argv1])
else:
initial_state = proj.factory.entry_state()
if EXP_OPTS["state"]["discard_lazy_solves"]:
initial_state.options.discard("LAZY_SOLVES")
stdin_len = EXP_OPTS["stdin"]["length"]
if stdin_len > 0:
for _ in range(0, stdin_len-1):
k = initial_state.posix.files[0].read_from(1)
if not EXP_OPTS["stdin"]["null"]:
initial_state.se.add(k != 0)
if not EXP_OPTS["stdin"]["white_space"]:
initial_state.se.add(k != 10)
#if EXP_OPTS["stdin"]["ascii"]:
# NEED TO FIX THIS
#initial_state.se.add(33 <= k)
#initial_state.se.add(k <= 126)
k = initial_state.posix.files[0].read_from(1)
if EXP_OPTS["stdin"]["newline"]:
initial_state.se.add(k == 10)
initial_state.posix.files[0].seek(0)
initial_state.posix.files[0].length = stdin_len
immutable = EXP_OPTS["path_group"]["immutable"]
sim_manager = proj.factory.simulation_manager(initial_state, immutable=immutable)
sim_manager.explore(find=FIND_ADDRS, avoid=AVOID_ADDRS)
try:
found = sim_manager.found[0].state
found.posix.files[0].seek(0)
print("Found: "+ found.se.eval(found.posix.files[0].read_from(stdin_len), cast_to=str))
except IndexError:
print("No stdin found.")
try:
found = sim_manager.found[0]
print(found.state.se.eval(argv1, cast_to=str))
except IndexError:
print("No arguments found.")
def explore_options():
"""
Arguments: None
Return Value: None
Description:
-
TODO:
- Doc String.
"""
EXP_FORM_OPTS.Execute()
EXP_OPTS["state"]["discard_lazy_solves"] = EXP_FORM_OPTS.rDiscardLazySolves.checked
EXP_OPTS["load"]["auto_load_libs"] = EXP_FORM_OPTS.rAutoLoadLibs.checked
EXP_OPTS["path_group"]["immutable"] = EXP_FORM_OPTS.rImmutable.checked
EXP_OPTS["time_limit"]["minutes"] = EXP_FORM_OPTS.iTimeLimit.value
def explore_stdin():
"""
Arguments: None
Return Value: None
Description:
-
TODO:
- Doc String.
"""
EXP_FORM_STDIN.Execute()
EXP_OPTS["stdin"]["newline"] = EXP_FORM_STDIN.rNewline.checked
EXP_OPTS["stdin"]["null"] = EXP_FORM_STDIN.rNull.checked
EXP_OPTS["stdin"]["ascii"] = EXP_FORM_STDIN.rASCII.checked
EXP_OPTS["stdin"]["white_space"] = EXP_FORM_STDIN.rWhite.checked
EXP_OPTS["stdin"]["length"] = EXP_FORM_STDIN.iStdinLen.value
def explore_arguments():
"""
Arguments: None
Return Value: None
Description:
-
TODO:
- Doc String.
"""
EXP_FORM_ARGS.Execute()
EXP_OPTS["args"]["length"] = EXP_FORM_ARGS.iArgLen.value
def refresh():
"""
Arguments: None
Return Value: None
Description:
- Function is called by the Refresh:Refresh action and removes
all the address in the global AVOID_ADDRS list and FIND_ADDRS list. Also
removes all colored addresses in IDA View-A. Success/Fail is displayed
in the IDA Pro Output Window.
TODO:
- Nothing.
"""
for addr in FIND_ADDRS:
set_line_color(0xffffff, addr)
del FIND_ADDRS[:]
for addr in AVOID_ADDRS:
set_line_color(0xffffff, addr)
del AVOID_ADDRS[:]
if len(FIND_ADDRS) != 0 and len(AVOID_ADDRS) != 0:
print("AngryIDA: Refresh Failed")
else:
print("AngryIDA: Refresh completed.")
def my_quit():
"""
Arguments: None
Return Value: None
Description:
-
TODO:
- Stop script
- Clean state
- Remove context menu
- Undo any hot-keys (No hot-keys used currently)
- Reset changes to IDA views (Color/highlighting)
- Description
"""
return None
#------------------------------MAIN------------------------------------
EXP_FORM_OPTS = ExpFormOpts()
EXP_FORM_STDIN = ExpFormStdin()
EXP_FORM_ARGS = ExpFormArgs()
# Compile (in order to populate the controls)
EXP_FORM_OPTS.Compile()
EXP_FORM_STDIN.Compile()
EXP_FORM_ARGS.Compile()
# Set some defaults
EXP_FORM_OPTS.rDiscardLazySolves.checked = True
EXP_FORM_OPTS.iTimeLimit.value = 10
EXP_FORM_STDIN.rNewline.checked = True
# Create actions from context menu
ACTION_FS = idaapi.action_desc_t('Finds:Set', 'Set', ActionHandler("Finds:Set"))
ACTION_FR = idaapi.action_desc_t('Finds:Remove', 'Remove', ActionHandler("Finds:Remove"))
ACTION_FP = idaapi.action_desc_t('Finds:Print', 'Print', ActionHandler("Finds:Print"))
ACTION_AS = idaapi.action_desc_t('Avoids:Set', 'Set', ActionHandler("Avoids:Set"))
ACTION_AR = idaapi.action_desc_t('Avoids:Remove', 'Remove', ActionHandler("Avoids:Remove"))
ACTION_AP = idaapi.action_desc_t('Avoids:Print', 'Print', ActionHandler("Avoids:Print"))
ACTION_ER = idaapi.action_desc_t('Explore:Run', 'Run', ActionHandler("Explore:Run"))
ACTION_EO = idaapi.action_desc_t('Explore:Options', 'Options', ActionHandler("Explore:Options"))
ACTION_ES = idaapi.action_desc_t('Explore:Stdin', 'Stdin', ActionHandler("Explore:Stdin"))
ACTION_EA = idaapi.action_desc_t('Explore:Argument', 'Argument', ActionHandler("Explore:Argument"))
ACTION_RR = idaapi.action_desc_t('Refresh:Refresh', 'Refresh', ActionHandler("Refresh:Refresh"))
ACTION_QQ = idaapi.action_desc_t('Quit:Quit', 'Quit', ActionHandler("Quit:Quit"))
# Register Actions
idaapi.register_action(ACTION_FS)
idaapi.register_action(ACTION_FR)
idaapi.register_action(ACTION_FP)
idaapi.register_action(ACTION_AS)
idaapi.register_action(ACTION_AR)
idaapi.register_action(ACTION_AP)
idaapi.register_action(ACTION_ER)
idaapi.register_action(ACTION_EO)
idaapi.register_action(ACTION_ES)
idaapi.register_action(ACTION_EA)
idaapi.register_action(ACTION_RR)
idaapi.register_action(ACTION_QQ)
HOOKS = Hooks()
HOOKS.hook()