-
Notifications
You must be signed in to change notification settings - Fork 47
/
ScratchABit.py
executable file
·1111 lines (988 loc) · 39.3 KB
/
ScratchABit.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# ScratchABit - interactive disassembler
#
# Copyright (c) 2015-2018 Paul Sokolovsky
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
if sys.version_info < (3, 0):
sys.stderr.write("""\
ERROR: ScratchABit requires Python 3.3 or higher. You are running it with
following Python version:
---
%s
---
Try running it as 'python3 %s' and/or install suitable version.
""" % (sys.version, sys.argv[0]))
sys.exit(1)
import os
import os.path
import time
import re
import string
import binascii
import logging as log
import argparse
from scratchabit import engine
import idaapi
from picotui.widgets import *
from picotui import editorext as editor
from picotui.screen import Screen
from picotui.editorext import Viewer
from picotui.menu import *
from picotui.dialogs import *
from scratchabit import utils
from scratchabit import help
from scratchabit import saveload
from scratchabit import actions
from scratchabit import uiprefs
HEIGHT = 21
MENU_PREFS = 2000
MENU_PLUGIN = 2001
MENU_ADD_TO_FUNC = 2002
MENU_WRITE_ALL_HTML = 2003
class AppClass:
def set_show_bytes(self, show_bytes):
self.show_bytes = show_bytes
sz = 8 + 1
if APP.show_bytes:
sz += show_bytes * 2 + 1
engine.DisasmObj.LEADER_SIZE = sz
APP = AppClass()
def disasm_one(p):
insnsz = p.ana()
p.out()
print("%08x %s" % (p.cmd.ea, p.cmd.disasm))
p.cmd.ea += p.cmd.size
p.cmd.size = 0
class DisasmViewer(editor.EditorExt):
def __init__(self, *args):
super().__init__(*args)
self.model = None
self.addr_stack = []
self.search_str = ""
self.def_color = C_PAIR(C_CYAN, C_BLUE)
def set_model(self, model):
self.model = model
self.set_lines(model.lines())
# Invalidate top_line. Assuming goto_*() will be called
# after set_model().
self.top_line = sys.maxsize
def show_line(self, l, i):
show_bytes = APP.show_bytes
res = l
if not isinstance(l, str):
res = "%08x " % l.ea
if show_bytes > 0:
bin = ""
if not l.virtual:
b = self.model.AS.get_bytes(l.ea, l.size)
bin = str(binascii.hexlify(b[:show_bytes]), "ascii")
if l.size > show_bytes:
bin += "+"
res += idaapi.fillstr(bin, show_bytes * 2 + 1)
res += l.indent + l.render()
COLOR_MAP = {
engine.Label: C_PAIR(C_GREEN, C_BLUE),
engine.AreaWrapper: C_PAIR(C_YELLOW, C_BLUE),
engine.FunctionWrapper: C_PAIR(C_B_YELLOW, C_BLUE),
engine.Xref: C_PAIR(C_MAGENTA, C_BLUE),
engine.Unknown: C_PAIR(C_WHITE, C_BLUE),
engine.Data: C_PAIR(C_MAGENTA, C_BLUE),
engine.String: C_PAIR(C_B_MAGENTA, C_BLUE),
engine.Fill: C_PAIR(C_B_BLUE, C_BLUE),
}
c = COLOR_MAP.get(type(l), self.def_color)
self.attr_color(c)
super().show_line(res, i)
self.attr_reset()
def handle_input(self, key):
try:
return super().handle_input(key)
except Exception as ex:
self.show_exception(ex)
return None
def goto_addr(self, to_addr, col=None, from_addr=None):
if to_addr is None:
self.show_status("No address-like value to go to")
return
subno = -1
if isinstance(to_addr, tuple):
to_addr, subno = to_addr
adj_addr = self.model.AS.adjust_addr_reverse(to_addr)
if adj_addr is None:
self.show_status("Unknown address: 0x%x" % to_addr)
return
to_addr = adj_addr
# If we can position cursor within current screen, do that,
# to avoid jumpy UI
no = self.model.addr2line_no(to_addr, subno)
if no is not None:
if self.line_visible(no):
self.goto_line(no, col=col)
if from_addr is not None:
self.addr_stack.append(from_addr)
return
# Otherwise, re-render model around needed address, and redraw screen
t = time.time()
model = engine.render_partial_around(to_addr, 0, HEIGHT * 2)
self.show_status("Rendering time: %fs" % (time.time() - t))
if not model:
self.show_status("Unknown address: 0x%x" % to_addr)
return
self.set_model(model)
no = self.model.addr2line_no(to_addr, subno)
if no is not None:
if from_addr is not None:
self.addr_stack.append(from_addr)
if not self.goto_line(no, col=col):
# Need to redraw always, because we changed underlying model
self.redraw()
else:
self.show_status("Unknown address: %x" % to_addr)
def update_model(self, stay_on_real=False):
"""Re-render model and update screen in such way that cursor stayed
on the same line (as far as possible).
stay_on_real == False - try to stay on same relative line no. for
the current address.
stay_on_real == True - try to stay on the line which contains real
bytes for the current address (use this if you know that cursor
stayed on such line before the update).
"""
addr, subno = self.cur_addr_subno()
t = time.time()
model = engine.render_partial_around(addr, subno, HEIGHT * 2)
self.show_status("Rendering time: %fs" % (time.time() - t))
self.set_model(model)
if stay_on_real:
self.cur_line = model.target_addr_lineno_real
else:
self.cur_line = model.target_addr_lineno
self.top_line = self.cur_line - self.row
#log.debug("update_model: addr=%x, row=%d, cur_line=%d, top_line=%d" % (addr, self.row, self.cur_line, self.top_line))
self.redraw()
def handle_cursor_keys(self, key):
cl = self.cur_line
if super().handle_cursor_keys(key):
if self.cur_line == cl:
return True
#log.debug("handle_cursor_keys: cur: %d, total: %d", self.cur_line, self.total_lines)
if self.cur_line <= HEIGHT or self.total_lines - self.cur_line <= HEIGHT:
log.debug("handle_cursor_keys: triggering update")
self.update_model()
return True
else:
return False
def cur_addr(self):
line = self.get_cur_line()
return line.ea
# Address of the next line. It may be the same address as the
# current line, as several lines may "belong" to the same address,
# (virtual lines like headers, etc.)
def next_line_addr_subno(self):
try:
l = self.content[self.cur_line + 1]
return (l.ea, l.subno)
except:
return None
# Return next address following the current line. May need to skip
# few virtual lines.
def next_addr(self):
addr = self.cur_addr()
n = self.cur_line + 1
try:
while self.content[n].ea == addr:
n += 1
return self.content[n].ea
except:
return None
def cur_addr_subno(self):
line = self.get_cur_line()
return (line.ea, line.subno)
def cur_operand_no(self, line):
col = self.col - engine.DisasmObj.LEADER_SIZE - len(line.indent)
#self.show_status("Enter pressed: %s, %s" % (col, line))
for i, pos in enumerate(line.arg_pos):
if pos[0] <= col <= pos[1]:
return i
return -1
def analyze_status(self, cnt):
self.show_status("Analyzing (%d insts so far)" % cnt)
def expect_flags(self, fl, allowed_flags):
if fl not in allowed_flags:
self.show_status("Undefine first (u key)")
return False
return True
def show_exception(self, e):
log.exception("Exception processing user command")
L = 3
T = 2
W = 74
H = 20
self.dialog_box(L, T, W, H)
v = Viewer(L + 1, T + 1, W - 2, H - 2)
import traceback
v.set_lines([
"Exception occurred processing the command. Press Esc to continue.",
"Recommended action is saving database, quitting and comparing",
"database files with backup copies for possibility of data loss",
"or corruption. The exception was also logged to scratchabit.log.",
"Please report way to reproduce it to",
"https://github.com/pfalcon/ScratchABit/issues",
"",
] + traceback.format_exc().splitlines())
v.loop()
self.redraw()
def resolve_expr(self, expr):
if expr:
if expr[0].isdigit():
return int(expr, 0)
else:
words = expr.split("+", 1)
addend = 0
if len(words) > 1:
try:
addend = int(words[1], 0)
except:
pass
to_addr = self.model.AS.resolve_label(words[0])
if to_addr is None:
return
return to_addr + addend
def require_non_func(self, fl):
if fl & ~(self.model.AS.FUNC | self.model.AS.ALT_CODE) != self.model.AS.CODE:
self.show_status("Code required")
return False
if fl & self.model.AS.FUNC:
self.show_status("Already a function")
return False
return True
#
# UI action handlers
#
def action_goto(self):
d = Dialog(4, 4, title="Go to")
d.add(1, 1, WLabel("Label/addr:"))
entry = WAutoComplete(20, "", self.model.AS.get_label_list())
entry.popup_h = 12
entry.finish_dialog = ACTION_OK
d.add(13, 1, entry)
d.add(1, 2, WLabel("Press Down to auto-complete"))
res = d.loop()
self.redraw()
if res == ACTION_OK:
value = entry.get()
if '0' <= value[0] <= '9':
addr = int(value, 0)
else:
addr = self.model.AS.resolve_label(value)
self.goto_addr(addr, from_addr=self.cur_addr())
def action_make_ascii(self):
addr = self.cur_addr()
fl = self.model.AS.get_flags(addr)
if not self.expect_flags(fl, (self.model.AS.DATA, self.model.AS.UNK)):
return
sz = 0
label = "s_"
while True:
b = self.model.AS.get_byte(addr)
fl = self.model.AS.get_flags(addr)
if not (0x20 <= b <= 0x7e or b in (0x0a, 0x0d)):
if b == 0:
sz += 1
break
if fl not in (self.model.AS.UNK, self.model.AS.DATA, self.model.AS.DATA_CONT):
break
c = chr(b)
if c < '0' or c in string.punctuation:
c = '_'
label += c
addr += 1
sz += 1
if sz > 0:
self.model.AS.set_flags(self.cur_addr(), sz, self.model.AS.STR, self.model.AS.DATA_CONT)
self.model.AS.make_unique_label(self.cur_addr(), label)
self.update_model()
def handle_edit_key(self, key):
if key in ACTION_MAP:
return ACTION_MAP[key](self)
line = self.get_cur_line()
if key == editor.KEY_ENTER:
line = self.get_cur_line()
log.info("Enter pressed: %s" % line)
op_no = self.cur_operand_no(line)
self.show_status("Enter pressed: %s, %s" % (self.col, op_no))
to_addr = None
# No longer try to jump only to addresses in args, parse
# textual representation below
if False and isinstance(line, engine.DisasmObj):
if op_no >= 0:
o = line[op_no]
to_addr = o.get_addr()
if to_addr is None:
o = line.get_operand_addr()
if o:
to_addr = o.get_addr()
if to_addr is None:
pos = self.col - line.LEADER_SIZE - len(line.indent)
word = utils.get_word_at_pos(line.cache, pos)
self.show_status("Enter pressed: %s, %s, %s" % (self.col, op_no, word))
to_addr = self.resolve_expr(word)
if to_addr is None:
self.show_status("Unknown address: %s" % word)
return
self.goto_addr(to_addr, from_addr=self.cur_addr_subno())
elif key == editor.KEY_ESC:
if self.addr_stack:
self.show_status("Returning")
self.goto_addr(self.addr_stack.pop())
elif key == b"q":
res = ACTION_OK
if self.model.AS.changed:
res = DConfirmation("There're unsaved changes. Quit?").result()
if res == ACTION_OK:
return editor.KEY_QUIT
self.redraw()
elif key == b"\x1b[5;5~": # Ctrl+PgUp
self.goto_addr(self.model.AS.min_addr(), from_addr=line.ea)
elif key == b"\x1b[6;5~": # Ctrl+PgDn
self.goto_addr(self.model.AS.max_addr(), from_addr=line.ea)
elif key == b"c":
addr = self.cur_addr()
self.show_status("Analyzing at %x" % addr)
engine.add_entrypoint(addr, False)
engine.analyze(self.analyze_status)
self.update_model()
elif key == b"C":
addr = self.cur_addr()
self.show_status("Analyzing at %x" % addr)
self.model.AS.make_alt_code(addr)
engine.add_entrypoint(addr, False)
engine.analyze(self.analyze_status)
self.update_model()
elif key == b"F":
addr = self.cur_addr()
fl = self.model.AS.get_flags(addr, 0xff)
if not self.require_non_func(fl):
return
self.show_status("Retracing as a function...")
self.model.AS.make_label("fun_", addr)
engine.add_entrypoint(addr, True)
engine.analyze(self.analyze_status)
self.update_model()
self.show_status("Retraced as a function")
elif key == MENU_ADD_TO_FUNC:
addr = self.cur_addr()
if actions.add_code_to_func(APP, addr):
self.update_model()
elif key == b"d":
addr = self.cur_addr()
fl = self.model.AS.get_flags(addr)
if not self.expect_flags(fl, (self.model.AS.DATA, self.model.AS.UNK)):
return
if fl == self.model.AS.UNK:
self.model.AS.set_flags(addr, 1, self.model.AS.DATA, self.model.AS.DATA_CONT)
else:
sz = self.model.AS.get_unit_size(addr)
self.model.undefine_unit(addr)
sz *= 2
if sz > 4: sz = 1
self.model.AS.set_flags(addr, sz, self.model.AS.DATA, self.model.AS.DATA_CONT)
self.update_model()
elif key == b"f":
addr = self.cur_addr()
fl = self.model.AS.get_flags(addr)
if not self.expect_flags(fl, (self.model.AS.UNK,)):
return
off, area = self.model.AS.addr2area(self.cur_addr())
# Don't cross area boundaries with filler
remaining = area[engine.END] - addr + 1
sz = 0
while remaining:
try:
fl = self.model.AS.get_flags(addr)
except engine.InvalidAddrException:
break
if fl != self.model.AS.UNK:
break
b = self.model.AS.get_byte(addr)
if b not in (0, 0xff):
self.show_status("Filler must consist of 0x00 or 0xff")
return
sz += 1
addr += 1
remaining -= 1
if sz > 0:
self.model.AS.make_filler(self.cur_addr(), sz)
self.update_model()
elif key == b"u":
addr = self.cur_addr()
self.model.undefine_unit(addr)
self.update_model()
elif key == b"h":
op_no = self.cur_operand_no(self.get_cur_line())
if op_no >= 0:
addr = self.cur_addr()
subtype = self.model.AS.get_arg_prop(addr, op_no, "subtype")
if subtype != engine.IMM_ADDR:
next_subtype = {
engine.IMM_UHEX: engine.IMM_UDEC,
engine.IMM_UDEC: engine.IMM_UHEX,
}
self.model.AS.set_arg_prop(addr, op_no, "subtype", next_subtype[subtype])
self.redraw()
self.show_status("Changed arg #%d to %s" % (op_no, next_subtype[subtype]))
elif key == b"o":
addr = self.cur_addr()
line = self.get_cur_line()
o = line.get_operand_addr()
if not o:
self.show_status("Cannot convert operand to offset")
return
if o.type != idaapi.o_imm or not self.model.AS.is_valid_addr(o.get_addr()):
self.show_status("Cannot convert operand to offset: #%s: %s" % (o.n, o.type))
return
if self.model.AS.get_arg_prop(addr, o.n, "subtype") == engine.IMM_ADDR:
self.model.AS.unmake_arg_offset(addr, o.n, o.get_addr())
else:
self.model.AS.make_arg_offset(addr, o.n, o.get_addr())
self.update_model(True)
elif key == b";":
addr = self.cur_addr()
comment = self.model.AS.get_comment(addr) or ""
res = DMultiEntry(60, 5, comment.split("\n"), title="Comment:").result()
if res != ACTION_CANCEL:
res = "\n".join(res).rstrip("\n")
self.model.AS.set_comment(addr, res)
self.update_model()
else:
self.redraw()
elif key == b"n":
addr = self.cur_addr()
label = self.model.AS.get_label(addr)
def_label = self.model.AS.get_default_label(addr)
s = label or def_label
while True:
res = DTextEntry(30, s, title="New label:").result()
if not res:
break
if res == def_label:
res = addr
else:
if self.model.AS.label_exists(res):
s = res
self.show_status("Duplicate label")
continue
self.model.AS.set_label(addr, res)
if not label:
# If it's new label, we need to add it to model
self.update_model()
return
break
self.redraw()
elif key == editor.KEY_F1:
help.help(self)
self.redraw()
elif key == b"S":
self.show_status("Saving...")
timer = time.time()
saveload.save_state(project_dir)
timer = time.time() - timer
log.info("Saved database in %fs", timer)
self.model.AS.changed = False
self.show_status("Saved in %fs" % timer)
elif key == b"\x11": # ^Q
class IssueList(WListBox):
def render_line(self, l):
return "%08x %s" % l
d = Dialog(4, 4, title="Problems list")
lw = IssueList(40, 16, self.model.AS.get_issues())
lw.finish_dialog = ACTION_OK
d.add(1, 1, lw)
res = d.loop()
self.redraw()
if res == ACTION_OK:
val = lw.get_cur_line()
if val:
self.goto_addr(val[0], from_addr=self.cur_addr())
elif key == b"i":
off, area = self.model.AS.addr2area(self.cur_addr())
props = area[engine.PROPS]
percent = 100 * off / (area[engine.END] - area[engine.START] + 1)
func = self.model.AS.lookup_func(self.cur_addr())
func = self.model.AS.get_label(func.start) if func else None
status = "Area: 0x%x %s (%s): %.1f%%, func: %s" % (
area[engine.START], props.get("name", "noname"), props["access"], percent, func
)
subarea = self.model.AS.lookup_subarea(self.cur_addr())
if subarea:
status += ", subarea: " + subarea[2]
self.show_status(status)
elif key == b"I":
from scratchabit import memmap
addr = memmap.show(self.model.AS, self.cur_addr())
if addr is not None:
self.goto_addr(addr, from_addr=self.cur_addr())
self.redraw()
elif key == b"W":
out_fname = "out.lst"
actions.write_disasm_all(APP, out_fname, feedback_obj=self)
self.show_status("Disassembly listing written: " + out_fname)
elif key == MENU_WRITE_ALL_HTML:
out_fname = "out.html"
with open(out_fname, "w") as f:
f.write("<pre>\n")
m = actions.HTMLSaveModel(f, self)
m.aspace = self.model.AS
engine.render_partial(m, 0, 0, 10000000)
f.write("</pre>\n")
self.show_status("Disassembly HTML listing written: " + out_fname)
elif key == b"\x17": # Ctrl+W
outfile = actions.write_func_by_addr(APP, self.cur_addr(), feedback_obj=self)
if outfile:
self.show_status("Wrote file: %s" % outfile)
elif key == b"\x15": # Ctrl+U
# Next undefined
addr = self.cur_addr()
flags = self.model.AS.get_flags(addr)
if flags == self.model.AS.UNK:
# If already on undefined, skip the current stride of them,
# as they indeed go in batches.
while True:
flags = self.model.AS.get_flags(addr)
if flags != self.model.AS.UNK:
break
addr = self.model.AS.next_addr(addr)
if addr is None:
break
if addr is not None:
while True:
flags = self.model.AS.get_flags(addr)
if flags == self.model.AS.UNK:
self.goto_addr(addr, from_addr=self.cur_addr())
break
addr = self.model.AS.next_addr(addr)
if addr is None:
break
if addr is None:
self.show_status("There're no further undefined strides")
elif key == b"\x06": # Ctrl+F
# Next non-function
addr = self.cur_addr()
flags = self.model.AS.get_flags(addr, ~ADDRESS_SPACE.ALT_CODE)
if flags == self.model.AS.CODE:
# If already on non-func code, skip the current stride of it,
# as it indeed go in batches.
while True:
flags = self.model.AS.get_flags(addr, ~ADDRESS_SPACE.ALT_CODE)
self.show_status("fl=%x" % flags)
if flags not in (self.model.AS.CODE, self.model.AS.CODE_CONT):
break
addr = self.model.AS.next_addr(addr)
if addr is None:
break
if addr is not None:
while True:
flags = self.model.AS.get_flags(addr, ~ADDRESS_SPACE.ALT_CODE)
if flags == self.model.AS.CODE:
self.goto_addr(addr, from_addr=self.cur_addr())
break
addr = self.model.AS.next_addr(addr)
if addr is None:
break
if addr is None:
self.show_status("There're no further non-function code strides")
elif key in (b"/", b"?"): # "/" and Shift+"/"
class FoundException(Exception): pass
class TextSearchModel(engine.Model):
def __init__(self, substr, ctrl, this_addr, this_subno):
super().__init__()
self.search = substr
self.ctrl = ctrl
self.this_addr = this_addr
self.this_subno = this_subno
self.cnt = 0
def add_object(self, addr, line):
super().add_object(addr, line)
# Skip virtual lines before the line from which we started
if addr == self.this_addr and line.subno < self.this_subno:
return
txt = line.render()
idx = txt.find(self.search)
if idx != -1:
raise FoundException((addr, line.subno), idx + line.LEADER_SIZE + len(line.indent))
if self.cnt % 256 == 0:
self.ctrl.show_status("Searching: 0x%x" % addr)
self.cnt += 1
# Don't accumulate lines
self._lines = []
self._addr2line = {}
if key == b"/":
d = Dialog(4, 4, title="Text Search")
d.add(1, 1, WLabel("Search for:"))
entry = WTextEntry(20, self.search_str)
entry.finish_dialog = ACTION_OK
d.add(13, 1, entry)
res = d.loop()
self.redraw()
self.search_str = entry.get_text()
if res != ACTION_OK or not self.search_str:
return
addr, subno = self.cur_addr_subno()
else:
addr, subno = self.next_line_addr_subno()
try:
engine.render_from(TextSearchModel(self.search_str, self, addr, subno), addr, 10000000)
except FoundException as res:
self.goto_addr(res.args[0], col=res.args[1], from_addr=self.cur_addr())
else:
self.show_status("Not found: " + self.search_str)
elif key == MENU_PREFS:
uiprefs.handle(APP)
elif key == MENU_PLUGIN:
res = DTextEntry(30, "", title="Plugin module name:").result()
self.redraw()
if res:
self.show_status("Running '%s' plugin..." % res)
call_script(res)
self.update_model()
self.show_status("Plugin '%s' ran successfully" % res)
else:
self.show_status("Unbound key: " + repr(key))
ACTION_MAP = {
b"g": DisasmViewer.action_goto,
b"a": DisasmViewer.action_make_ascii,
}
CPU_PLUGIN = None
ENTRYPOINTS = []
APP.show_bytes = 4
def filter_config_line(l):
l = re.sub(r"#.*$", "", l)
l = l.strip()
return l
def load_symbols(fname):
with open(fname) as f:
for l in f:
l = filter_config_line(l)
if not l:
continue
m = re.search(r"\b([A-Za-z_$.][A-Za-z0-9_$.]*)\s*=\s*((0x)?[0-9A-Fa-f]+)", l)
if m:
#print(m.groups())
ENTRYPOINTS.append((m.group(1), int(m.group(2), 0)))
else:
print("Warning: cannot parse entrypoint info from: %r" % l)
# Allow undescores to separate digit groups
def str2int(s):
return int(s.replace("_", ""), 0)
def parse_range(arg):
# name start(len)
# name start-end
if "(" in arg:
m = re.match(r"(.+?)\s*\(\s*(.+?)\s*\)", arg)
start = str2int(m.group(1))
end = start + str2int(m.group(2)) - 1
else:
m = re.match(r"(.+)\s*-\s*(.+)", arg)
start = str2int(m.group(1))
end = str2int(m.group(2))
return start, end
def parse_entrypoints(f):
for l in f:
l = filter_config_line(l)
if not l:
continue
if l[0] == "[":
return l
m = re.match(r'load "(.+?)"', l)
if m:
load_symbols(m.group(1))
else:
label, addr = [v.strip() for v in l.split("=")]
ENTRYPOINTS.append((label, str2int(addr)))
return ""
def parse_subareas(f):
subareas = []
for l in f:
l = filter_config_line(l)
if not l:
continue
if l[0] == "[":
return l
args = l.split()
assert len(args) == 2
start, end = parse_range(args[1])
engine.ADDRESS_SPACE.add_subarea(start, end, args[0])
engine.ADDRESS_SPACE.finish_subareas()
return ""
def load_target_file(loader, fname):
print("Loading %s..." % fname)
timer = time.time()
entry = loader.load(engine.ADDRESS_SPACE, fname)
log.info("Loaded %s in %fs, entrypoint: %s", fname, time.time() - timer, hex(entry) if entry is not None else None)
if entry is not None:
ENTRYPOINTS.append(("_ENTRY_", entry))
def parse_disasm_def(fname):
global CPU_PLUGIN
with open(fname) as f:
for l in f:
l = filter_config_line(l)
if not l:
continue
#print(l)
while True:
if not l:
#return
break
if l[0] == "[":
section = l[1:-1]
print("Processing section: %s" % section)
if section == "entrypoints":
l = parse_entrypoints(f)
elif section == "subareas":
l = parse_subareas(f)
else:
assert 0, "Unknown section: " + section
else:
break
if not l:
break
if l.startswith("load"):
args = l.split()
if args[2][0] in string.digits:
addr = str2int(args[2])
print("Loading %s @0x%x" % (args[1], addr))
engine.ADDRESS_SPACE.load_content(open(args[1], "rb"), addr)
else:
print("Loading %s (%s plugin)" % (args[1], args[2]))
loader = __import__(args[2])
load_target_file(loader, args[1])
elif l.startswith("cpu "):
args = l.split()
CPU_PLUGIN = __import__(args[1])
if hasattr(CPU_PLUGIN, "arch_id"):
engine.set_arch_id(CPU_PLUGIN.arch_id)
print("Loading CPU plugin %s" % (args[1]))
elif l.startswith("show bytes "):
args = l.split()
APP.show_bytes = int(args[2])
elif l.startswith("area "):
args = l.split()
assert len(args) == 4
start, end = parse_range(args[2])
a = engine.ADDRESS_SPACE.add_area(start, end, {"name": args[1], "access": args[3].upper()})
print("Adding area: %s" % engine.str_area(a))
else:
assert 0, "Unknown directive: " + l
class MainScreen:
def __init__(self):
self.screen_size = Screen.screen_size()
self.e = DisasmViewer(1, 2, self.screen_size[0] - 2, self.screen_size[1] - 4)
menu_file = WMenuBox([
("Save (Shift+s)", b"S"),
("Write disasm (Shift+w)", b"W"),
("Write disasm in HTML", MENU_WRITE_ALL_HTML),
("Write function (Ctrl+w)", b"\x17"),
("Quit (q)", b"q")
])
menu_goto = WMenuBox([
("Follow (Enter)", KEY_ENTER), ("Return (Esc)", KEY_ESC),
("Goto... (g)", b"g"), ("Search disasm... (/)", b"/"),
("Search next (Shift+/)", b"?"), ("Next undefined (Ctrl+u)", b"\x15"),
("Next non-function code (Ctrl+f)", b"\x06"),
])
menu_edit = WMenuBox([
("Undefined (u)", b"u"),
("Code (c)", b"c"),
("Alt code (Shift+c)", b"C"),
("Data (d)", b"d"),
("ASCII String (a)", b"a"), ("Filler (f)", b"f"), ("Make label (n)", b"n"),
("Mark function start (F)", b"F"), ("Add code to function", MENU_ADD_TO_FUNC),
("Number/Address (o)", b"o"), ("Hex/dec (h)", b"h"),
])
menu_analysis = WMenuBox([
("Info (whereami) (i)", b"i"), ("Memory map (Shift+i)", b"I"),
("Issue list (Ctrl+q)", b"\x11"),
("Run plugin...", MENU_PLUGIN),
("Preferences...", MENU_PREFS),
])
menu_help = WMenuBox([
("Help (F1)", KEY_F1), ("About...", "about"),
])
self.menu_bar = WMenuBar([
("File", menu_file), ("Goto", menu_goto), ("Edit", menu_edit),
("Analysis", menu_analysis), ("Help", menu_help)
])
self.menu_bar.permanent = True
def redraw(self, allow_cursor=True):
self.menu_bar.redraw()
self.e.attr_color(C_B_WHITE, C_BLUE)
self.e.draw_box(0, 1, self.screen_size[0], self.screen_size[1] - 2)
self.e.attr_reset()
self.e.redraw()
if allow_cursor:
self.e.cursor(True)
def loop(self):
while 1:
key = self.e.get_input()
if isinstance(key, list):
x, y = key
if self.menu_bar.inside(x, y):
self.menu_bar.focus = True
if self.menu_bar.focus:
res = self.menu_bar.handle_input(key)
if res == ACTION_CANCEL:
self.menu_bar.focus = False
elif res is not None and res is not True:
res = self.e.handle_input(res)
if res is not None and res is not True:
return res
else:
if key == KEY_F9:
self.menu_bar.focus = True
self.menu_bar.redraw()
continue
res = self.e.handle_input(key)
if res is not None and res is not True:
return res
def call_script(script, progress_func=None):
mod = __import__(script)
main_f = getattr(mod, "main", None)
if main_f:
main_f(APP)
# A script might have queues some entrypoints, etc.
# Analyze them now.
engine.analyze(progress_func)
if __name__ == "__main__":
argp = argparse.ArgumentParser(description="ScratchABit interactive disassembler")
argp.add_argument("file", help="Input file (binary or disassembly .def)")
argp.add_argument("--script", action="append", help="Run script from file after loading environment")
argp.add_argument("--save", action="store_true", help="Save DB after --script and quit; don't show UI")
args = argp.parse_args()
# Plugin dirs are relative to the dir where scratchabit.py resides.
# sys.path[0] below provide absolute path of this dir, resolved for
# symlinks.
plugin_dirs = ["plugins", "plugins/cpu", "plugins/loader"]
for d in plugin_dirs:
sys.path.append(os.path.join(sys.path[0], d))
log.basicConfig(filename="scratchabit.log", format='%(asctime)s %(message)s', level=log.DEBUG)
log.info("=" * 30 + " Started " + "=" * 30)
if args.file.endswith(".def"):
parse_disasm_def(args.file)
project_name = args.file.rsplit(".", 1)[0]
else:
import default_plugins
for loader_id in default_plugins.loaders:
loader = __import__(loader_id)
arch_id = loader.detect(args.file)
if arch_id: