forked from lwerdna/keypatch_binja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypatch.py
950 lines (797 loc) · 34.8 KB
/
keypatch.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
#!/usr/bin/env python
import math
# python stdlib stuff
import re
from PySide6.QtGui import QFont
# Qt stuff
from PySide6.QtWidgets import *
from binaryninja.enums import MessageBoxButtonSet, MessageBoxIcon
from binaryninja.interaction import show_message_box
from binaryninjaui import UIContext
# capstone/keystone stuff
from capstone import *
from keystone import *
# ------------------------------------------------------------------------------
# lookups
# ------------------------------------------------------------------------------
hexchars = '0123456789ABCDEFabcdef'
# (name, description, arch, mode, option)
architecture_infos = [('x16', 'X86 16bit, Intel syntax', CS_ARCH_X86, CS_MODE_16, KS_ARCH_X86, KS_MODE_16),
('x32', 'X86 32bit, Intel syntax', CS_ARCH_X86, CS_MODE_32, KS_ARCH_X86, KS_MODE_32),
('x64', 'X86 64bit, Intel syntax', CS_ARCH_X86, CS_MODE_64, KS_ARCH_X86, KS_MODE_64),
('x16att', 'X86 16bit, AT&T syntax', CS_ARCH_X86, CS_MODE_16, KS_ARCH_X86, KS_MODE_16),
('x32att', 'X86 32bit, AT&T syntax', CS_ARCH_X86, CS_MODE_32, KS_ARCH_X86, KS_MODE_32),
('x64att', 'X86 64bit, AT&T syntax', CS_ARCH_X86, CS_MODE_64, KS_ARCH_X86, KS_MODE_64),
('x16nasm', 'X86 16bit, NASM syntax', CS_ARCH_X86, CS_MODE_16, KS_ARCH_X86, KS_MODE_16),
('x32nasm', 'X86 32bit, NASM syntax', CS_ARCH_X86, CS_MODE_32, KS_ARCH_X86, KS_MODE_32),
('x64nasm', 'X86 64bit, NASM syntax', CS_ARCH_X86, CS_MODE_64, KS_ARCH_X86, KS_MODE_64),
('arm', 'ARM - little endian', CS_ARCH_ARM, CS_MODE_ARM, KS_ARCH_ARM, KS_MODE_ARM),
('armbe', 'ARM - big endian', CS_ARCH_ARM, CS_MODE_ARM, KS_ARCH_ARM, KS_MODE_ARM),
('thumb', 'Thumb - little endian', CS_ARCH_ARM, CS_MODE_THUMB, KS_ARCH_ARM, KS_MODE_THUMB),
('thumbbe', 'Thumb - big endian', CS_ARCH_ARM, CS_MODE_THUMB, KS_ARCH_ARM, KS_MODE_THUMB),
('armv8', 'ARM V8 - little endian', CS_ARCH_ARM, CS_MODE_ARM | CS_MODE_V8, KS_ARCH_ARM,
KS_MODE_ARM | KS_MODE_V8),
('armv8be', 'ARM V8 - big endian', CS_ARCH_ARM, CS_MODE_ARM | CS_MODE_V8, KS_ARCH_ARM,
KS_MODE_ARM | KS_MODE_V8), (
'thumbv8', 'Thumb V8 - little endian', CS_ARCH_ARM, CS_MODE_THUMB | CS_MODE_V8, KS_ARCH_ARM,
KS_MODE_THUMB | KS_MODE_V8), (
'thumbv8be', 'Thumb V8 - big endian', CS_ARCH_ARM, CS_MODE_THUMB | CS_MODE_V8, KS_ARCH_ARM,
KS_MODE_THUMB | KS_MODE_V8), ('arm64', 'AArch64', CS_ARCH_ARM64, 0, KS_ARCH_ARM64, 0),
('hexagon', 'Hexagon', None, 0, KS_ARCH_HEXAGON, 0),
('mips', 'Mips - little endian', CS_ARCH_MIPS, CS_MODE_MIPS32, KS_ARCH_MIPS, KS_MODE_MIPS32),
('mipsbe', 'Mips - big endian', CS_ARCH_MIPS, CS_MODE_MIPS32, KS_ARCH_MIPS, KS_MODE_MIPS32),
('mips64', 'Mips64 - little endian', CS_ARCH_MIPS, CS_MODE_MIPS64, KS_ARCH_MIPS, KS_MODE_MIPS64),
('mips64be', 'Mips64 - big endian', CS_ARCH_MIPS, CS_MODE_MIPS64, KS_ARCH_MIPS, KS_MODE_MIPS64),
('ppc32be', 'PowerPC32 - big endian', CS_ARCH_PPC, CS_MODE_32, KS_ARCH_PPC, KS_MODE_PPC32),
('ppc64', 'PowerPC64 - little endian', CS_ARCH_PPC, CS_MODE_64, KS_ARCH_PPC, KS_MODE_PPC64),
('ppc64be', 'PowerPC64 - big endian', CS_ARCH_PPC, CS_MODE_64, KS_ARCH_PPC, KS_MODE_PPC64),
('sparc', 'Sparc - little endian', CS_ARCH_SPARC, 0, KS_ARCH_SPARC, KS_MODE_SPARC32),
('sparcbe', 'Sparc - big endian', CS_ARCH_SPARC, 0, KS_ARCH_SPARC, KS_MODE_SPARC32),
('sparc64be', 'Sparc64 - big endian', None, 0, KS_ARCH_SPARC, KS_MODE_SPARC64),
('systemz', 'SystemZ (S390x)', None, 0, KS_ARCH_SYSTEMZ, 0),
('evm', 'Ethereum Virtual Machine', CS_ARCH_EVM, 0, KS_ARCH_EVM, 0)]
# map architecture names (capstone namespace) to capstone/keystone context
architecture_to_cs = {}
architecture_to_ks = {}
for (name, descr, cs_arch, cs_mode, ks_arch, ks_mode) in architecture_infos:
# default is little endian
# decide whether to set big endian
if name.endswith('be'):
cs_mode |= CS_MODE_BIG_ENDIAN
ks_mode |= KS_MODE_BIG_ENDIAN
else:
cs_mode |= CS_MODE_LITTLE_ENDIAN
ks_mode |= KS_MODE_LITTLE_ENDIAN
# initialize architecture
(cs, ks) = (None, None)
if cs_arch != None:
cs = Cs(cs_arch, cs_mode)
# print('Ks() on %s %s' % (name, descr))
ks = Ks(ks_arch, ks_mode)
# set special syntax if indicated
if 'AT&T syntax' in descr:
# ks_option(KS_OPT_SYNTAX, KS_OPT_SYNTAX_ATT)
ks.syntax = KS_OPT_SYNTAX_ATT
if name.endswith('nasm'):
# ks_option(KS_OPT_SYNTAX, KS_OPT_SYNTAX_NASM)
ks.syntax = KS_OPT_SYNTAX_NASM
architecture_to_cs[name] = cs
architecture_to_ks[name] = ks
font_mono = QFont('Courier New')
font_mono.setStyleHint(QFont.TypeWriter)
# ------------------------------------------------------------------------------
# utilities
# ------------------------------------------------------------------------------
# return the name of the architecture, in the capstone/keystone namespace
def determine_arch(bview):
arch = None
# determine the architecture name in the binja namespace
# if there's a current function, use that architecture, as it can distinguish
# between arm/thumb, whereas bv.arch would just be arm
try:
ac = UIContext.activeContext().contentActionHandler().actionContext()
current_function = ac.function
arch = current_function.arch
except Exception as e:
pass
# use architecture from binaryview
if arch == None:
arch = bview.arch
# sometimes the binaryview has no arch, like when File->New->Binary Data
if arch == None:
arch = binaryninja.Architecture['x86_64']
# map how binja names architectures to how keystone names architectures:
return {
'aarch64': 'arm64',
'armv7': 'arm',
'armv7eb': 'armbe',
'thumb2': 'thumb',
'thumb2eb': 'thumbbe',
'mipsel32': 'mips',
'mips32': 'mipsbe',
'ppc': 'ppc32be',
'ppc_le': 'ERROR',
'ppc64': 'ppc64be',
'ppc64_le': 'ppc64',
'sh4': 'ERROR',
'x86_16': 'x16nasm',
'x86': 'x32nasm',
'x86_64': 'x64nasm'
}[arch.name]
# test if given address is valid binaryview address
def is_valid_addr(bview, addr):
# if the binaryview has sections, validity is whether the address is in a section
if bview.sections:
for sname in bview.sections:
section = bview.sections[sname]
start = section.start
end = section.start + section.length
if addr >= start and addr < end:
return True
return False
# otherwise
else:
return addr >= bview.start and addr < (bview.start + bview.length)
# given a valid address, return least address after the valid that is invalid
def get_invalid_addr(bview, addr):
if not is_valid_addr(bview, addr):
return None
# if the binaryview has sections, next invalid address is end of current section
if bview.sections:
for sname in bview.sections:
section = bview.sections[sname]
start = section.start
end = section.start + section.length
if addr >= start and addr < end:
return end
# otherwise
else:
return bview.start + bview.length
# disassemble some given data
# returns (<instruction_string>, <instruction_length>)
# returns (None, None) if unable to disassemble
def disassemble(bview, arch, data, addr):
md = architecture_to_cs[arch]
try:
(addr, size, mnemonic, op_str) = next(md.disasm_lite(data, addr, 1))
if not size:
return (None, None)
except StopIteration:
return (None, None)
return (mnemonic + ' ' + op_str, size)
# disassemble from an address in the BinaryView, with the given arch
# returns (<instruction_string>, <instruction_length>)
# returns (None, None) if unable to disassemble
def disassemble_bview(bview, arch, addr):
if not is_valid_addr(bview, addr):
return (None, None)
end = get_invalid_addr(bview, addr)
length = min(16, end - addr)
data = bview.read(addr, length)
return disassemble(bview, arch, data, addr)
# get length of instruction at addr
# returns None if unable to disassemble
def disassemble_length(bview, arch, addr):
(instxt, length) = disassemble_bview(bview, arch, addr)
return length
def error(msg):
show_message_box('KEYPATCH', msg, MessageBoxButtonSet.OKButtonSet, MessageBoxIcon.ErrorIcon)
# b'\xaa\xbb\xcc\xdd' -> 'AA BB CC DD'
def bytes_to_str(data):
return ' '.join(['%02X' % x for x in data])
def strbytes_pretty(string):
return ' '.join(['%02X' % ord(x) for x in string])
def fixup(bview, arch_name, assembly):
reserved = {
'mips': [],
'arm': ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15', 'sp', 'lr', 'pc', 'apsr'],
'armbe': ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15', 'sp', 'lr', 'pc', 'apsr'],
'thumb': ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15', 'sp', 'lr', 'pc', 'apsr'],
'thumbbe': ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15', 'sp', 'lr', 'pc', 'apsr'],
'arm64': ['x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'x10', 'x11', 'x12', 'x13', 'x14', 'x15', 'x16', 'x17', 'x18', 'x19', 'x20', 'x21', 'x22', 'x23', 'x24', 'x25', 'x26', 'x27', 'x28', 'x29', 'x30', 'x31', 'fp', 'lr', 'pc', 'w0', 'w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8', 'w9', 'w10', 'w11', 'w12', 'w13', 'w14', 'w15', 'w16', 'w17', 'w18', 'w19', 'w20', 'w21', 'w22', 'w23', 'w24', 'w25', 'w26', 'w27', 'w28', 'w29', 'w30', 'w31'],
'x16nasm': ['ax', 'cx', 'dx', 'bx', 'sp', 'bp', 'si', 'di', 'ah', 'al', 'ch', 'cl', 'dh', 'dl', 'bh', 'bl', 'ss', 'cs', 'ds', 'es'],
'x32nasm': ['ax', 'cx', 'dx', 'bx', 'sp', 'bp', 'si', 'di', 'ah', 'al', 'ch', 'cl', 'dh', 'dl', 'bh', 'bl', 'ss', 'cs', 'ds', 'es', 'eax', 'ecx', 'edx', 'ebx', 'esp', 'ebp', 'esi', 'edi', 'fs'],
'x64nasm': ['ax', 'cx', 'dx', 'bx', 'sp', 'bp', 'si', 'di', 'ah', 'al', 'ch', 'cl', 'dh', 'dl', 'bh', 'bl', 'ss', 'cs', 'ds', 'es', 'eax', 'ecx', 'edx', 'ebx', 'esp', 'ebp', 'esi', 'edi', 'fs', 'rax', 'rcx', 'rdx', 'rbx', 'rsp', 'rbp', 'rsi', 'rdi']
}.get(arch_name)
# collect substitutions we'll make
substitutions = []
# loop over every word character token
for m in re.finditer(r'\w+', assembly):
if m.start() == 0: continue # do not replace mnemonic
symname = m.group(0)
# is reserved word? ignore
if symname in reserved:
continue
# is it just a hex constant? ignore
if re.match(r'^0x[a-fA-F0-9]+$', symname):
continue
# is in symbols? replace with first available address
if symname in bview.symbols:
# multiple symbols at given address possible
for sym in bview.symbols[symname]:
if hasattr(sym, 'address'):
substitutions.append((sym.name, sym.address))
break
# is a data variable?
m = re.match(r'^data_([a-fA-F0-9]+)$', symname)
if m:
addr = int(m.group(1), 16)
if addr in bview.data_vars:
substitutions.append((symname, addr))
# apply substitutions
for (name, addr) in substitutions:
assembly = assembly.replace(name, hex(addr))
# done
return assembly
# arch is keystone namespace
def get_nop(arch):
if arch in ['x16', 'x32', 'x64', 'x16att', 'x32att', 'x64att', 'x16nasm', 'x32nasm', 'x64nasm']:
return b'\x90'
elif arch in ['arm', 'armv8']:
return b'\x00\xf0\x20\xe3'
elif arch in ['armbe', 'armv8be']:
return b'\xe3\x20\xf0\x00'
elif arch in ['thumb', 'thumbv8']:
return b'\x00\xbf'
elif arch in ['thumbbe', 'thumbv8be']:
return b'\xbf\x00'
elif arch in ['arm64']:
return b'\x1f\x20\x03\xd5'
elif arch in ['hexagon']:
return b'\x00\xc0\x00\x7f'
elif arch in ['mips', 'mipsbe', 'mips64', 'mips64be']:
return b'\x00\x00\x00\x00'
elif arch in ['ppc64']:
return b'\x00\x00\x00\x60'
elif arch in ['ppc32be', 'ppc64be']:
return b'\x60\x00\x00\x00'
elif arch in ['sparc']:
return b'\x00\x00\x00\x01'
elif arch in ['sparcbe', 'sparc64be']:
return b'\x01\x00\x00\x00'
raise Exception('no NOP for architecture: %s' % arch)
# ------------------------------------------------------------------------------
# GUI
# ------------------------------------------------------------------------------
class AssembleTab(QWidget):
def __init__(self, context, parent=None):
super(AssembleTab, self).__init__(parent)
self.context = context
self.bview = context.binaryView
# other QLineEntry widgets to receive bytes upon assembling
self.qles_bytes = []
# ----------------------------------------------------------------------
# assemble tab
# ----------------------------------------------------------------------
layout = QVBoxLayout()
self.setLayout(layout)
form = QFormLayout()
self.qcb_arch = QComboBox()
form.addRow('Architecture:', self.qcb_arch)
self.qle_address = QLineEdit('00000000')
form.addRow('Address:', self.qle_address)
self.qle_assembly = QLineEdit()
form.addRow('Assembly:', self.qle_assembly)
# Add a new QLineEdit to receive bytes
self.edit_bytes = QLineEdit()
form.addRow('Edit Bytes:', self.edit_bytes)
self.check_nops = QCheckBox('NOPs padding until next instruction boundary')
form.addRow(self.check_nops)
self.check_save_original = QCheckBox('Save original instructions in binja comment')
form.addRow(self.check_save_original)
layout.addLayout(form)
groupbox = QGroupBox('Preview:')
form = QFormLayout()
self.qle_fixedup = QLineEdit()
form.addRow('Fixed Up:', self.qle_fixedup)
self.qle_data = QLineEdit()
form.addRow('Bytes:', self.qle_data)
self.qle_datasz = QLineEdit()
form.addRow('Size:', self.qle_datasz)
groupbox.setLayout(form)
layout.addWidget(groupbox)
btn_patch = QPushButton('Patch')
layout.addWidget(btn_patch)
# connect everything
self.qcb_arch.currentTextChanged.connect(self.re_assemble)
self.qle_address.textChanged.connect(self.re_assemble)
self.qle_assembly.textChanged.connect(self.re_assemble)
self.edit_bytes.textChanged.connect(self.re_disassemble)
btn_patch.clicked.connect(self.patch)
# ----------------
# set defaults
# ----------------
# default architecture dropdown in assemble
for (name, descr, _, _, _, _) in architecture_infos:
line = '%s: %s' % (name, descr)
self.qcb_arch.addItem(line)
ks_arch_name = determine_arch(context.binaryView)
self.qcb_arch.setCurrentIndex(([x[0] for x in architecture_infos]).index(ks_arch_name))
# default address to assemble
self.qle_address.setText(hex(self.context.address))
# default assembly
(instxt, length) = disassemble_bview(self.bview, self.arch(), context.address)
if instxt:
self.qle_assembly.setText(instxt)
self.qle_fixedup.setText(instxt)
self.qle_datasz.setText('%d' % length)
ok = True
else:
length = 10
self.qle_assembly.setText('nop')
data = context.binaryView.read(context.address, length)
self.qle_data.setText(' '.join(['%02X' % x for x in data]))
self.edit_bytes.setText(self.qle_data.text())
# assembly fields
self.qle_data.setReadOnly(True)
self.qle_data.setEnabled(False)
self.qle_datasz.setReadOnly(True)
self.qle_datasz.setEnabled(False)
self.qle_fixedup.setReadOnly(True)
self.qle_fixedup.setEnabled(False)
# default
self.check_nops.setChecked(True)
self.check_save_original.setChecked(True)
self.qle_assembly.setFocus()
btn_patch.setDefault(True)
#
# accessors
#
# get selected architecture from drop-down menu
# (this was set initially by sensing the environment)
def arch(self):
# 'thumbv8: Thumb V8 - little endian' -> 'thumbv8'
name_descr = self.qcb_arch.currentText()
return name_descr.split(':')[0]
def asm(self):
return self.qle_assembly.text()
def ks(self):
return architecture_to_ks.get(self.arch())
def addr(self):
return int(self.qle_address.text(), 16)
def data(self):
try:
values = [int(x, 16) for x in self.qle_data.text().split(' ')]
return bytes(values)
except Exception:
return None
# add another QLineEntry to receive assembled bytes
def add_qles_bytes(self, widget):
self.qles_bytes.append(widget)
# qle_assembly -> fixup -> qle_data
#
def re_assemble(self):
try:
# get input
(ks, assembly, addr) = (self.ks(), self.asm(), self.addr())
# apply fixup
fixedup = fixup(self.bview, self.arch(), assembly)
self.qle_fixedup.setText(fixedup)
# assemble
data, count = ks.asm(fixedup, addr)
if not data: return
for widget in self.qles_bytes:
widget.setText(bytes_to_str(data))
# pad with nops
if self.check_nops.isChecked():
length = disassemble_length(self.bview, self.arch(), self.addr())
if length != None and length > len(data):
nop = get_nop(self.arch())
sled = (length // len(nop)) * nop
sled = list(sled) # b'\xAA' -> [0xAA]
data = data + sled[len(data):]
# set
self.qle_data.setText(bytes_to_str(data))
self.edit_bytes.setText(bytes_to_str(data))
self.qle_datasz.setText(hex(len(data)))
except ValueError:
self.error('invalid address')
except KsError as e:
self.error(str(e))
except Exception as e:
self.error(str(e))
# update the self.qle_data.text() and self.qle_fixedup.text() through self.edit_bytes.text()
def re_disassemble(self):
try:
# get input
addr = self.addr()
data = bytes.fromhex(self.edit_bytes.text())
(disasm, length) = disassemble(self.bview, self.arch(), data, addr)
# pad with nops
if self.check_nops.isChecked():
length = disassemble_length(self.bview, self.arch(), self.addr())
if length != None and length > len(data):
nop = get_nop(self.arch())
sled = (length // len(nop)) * nop
data = data + sled[len(data):]
self.qle_data.setText(bytes_to_str(data))
self.qle_datasz.setText(hex(len(data)))
if disasm is not None:
self.qle_fixedup.setText(disasm)
else:
self.qle_fixedup.setText("...")
except KsError as e:
self.error(str(e))
except Exception as e:
self.error(str(e))
# qle_data -> binary view
#
def patch(self):
data = self.data()
comment = None
try:
if self.check_save_original.isChecked():
(instxt, length) = disassemble_bview(self.bview, self.arch(), self.addr())
if instxt and length:
comment = 'previously: ' + instxt
except Exception as e:
print(e)
pass
try:
with self.bview.undoable_transaction():
self.bview.write(self.addr(), data)
if comment:
self.bview.set_comment_at(self.addr(), comment)
except Exception as e:
return
# report error to the bytes
def error(self, msg):
self.qle_datasz.setText('')
if not ('error' in msg or 'ERROR' in msg):
msg = 'ERROR: ' + msg
self.qle_data.setText(msg)
self.qle_data.home(True)
# ------------------------------------------------------------------------------
# fill range tool
# ------------------------------------------------------------------------------
class FillRangeTab(QWidget):
def __init__(self, context, parent=None):
super(FillRangeTab, self).__init__(parent)
self.context = context
self.bview = context.binaryView
# this widget is VBox of QGroupBox
layoutV = QVBoxLayout()
self.setLayout(layoutV)
self.qle_end = QLineEdit('00000000')
self.qle_encoding = QLineEdit()
self.qle_encoding.setReadOnly(True)
self.qle_encoding.setEnabled(False)
self.qle_bytes = QLineEdit('00')
# not used
# self.qle_fill_size = QLineEdit()
# self.qle_fill_size.setReadOnly(True)
# self.qle_fill_size.setEnabled(False)
self.qle_preview = QLineEdit()
self.qle_preview.setReadOnly(True)
self.qle_preview.setEnabled(False)
self.qle_datasz = QLineEdit()
self.qcb_sections = QComboBox()
layoutV.addWidget(self.qcb_sections)
horiz = QHBoxLayout()
horiz.addWidget(QLabel('['))
self.qle_address = QLineEdit('00000000')
horiz.addWidget(self.qle_address)
horiz.addWidget(QLabel(','))
horiz.addWidget(self.qle_end)
horiz.addWidget(QLabel(')'))
layoutV.addLayout(horiz)
form = QFormLayout()
self.chk_encoding = QCheckBox('Assembled:')
form.addRow(self.chk_encoding, self.qle_encoding)
self.chk_manual = QCheckBox('Manual Entry:')
form.addRow(self.chk_manual, self.qle_bytes)
form.addRow('Preview:', self.qle_preview)
form.addRow('Size:', self.qle_datasz)
layoutV.addLayout(form)
# or QLayout::setAlignment
layoutV.addStretch()
btn_fill = QPushButton('Fill')
layoutV.addWidget(btn_fill)
# connect everything
self.qcb_sections.currentTextChanged.connect(self.section_chosen)
self.qle_address.textChanged.connect(self.preview)
self.qle_end.textChanged.connect(self.preview)
self.qle_datasz.textChanged.connect(self.preview_by_size_change)
self.chk_encoding.toggled.connect(self.encoding_checked_toggle)
self.chk_manual.toggled.connect(self.manual_checked_toggle)
self.qle_bytes.textChanged.connect(self.preview)
btn_fill.clicked.connect(self.fill)
# set defaults
nop = get_nop(determine_arch(self.bview))
if nop:
self.qle_bytes.setText(bytes_to_str(nop))
for (sname, section) in self.bview.sections.items():
section = self.bview.sections[sname]
line = '%s [0x%X, 0x%X)' % (sname, section.start, section.start + section.length)
self.qcb_sections.addItem(line)
self.chk_encoding.setChecked(False)
self.chk_manual.setChecked(True)
self.qle_address.setText(hex(self.context.address))
end = get_invalid_addr(self.bview, self.context.address)
if end != None:
self.qle_end.setText(hex(end))
btn_fill.setDefault(True)
def encoding_checked_toggle(self, is_check):
self.chk_manual.setChecked(not is_check)
self.preview()
def manual_checked_toggle(self, is_check):
self.chk_encoding.setChecked(not is_check)
self.preview()
# report error to the preview field
def error(self, msg):
self.qle_preview.setText('ERROR: ' + msg)
self.qle_preview.home(True)
# get the left, right, and length of the entered fill interval
# calculate length through left and right
def interval(self):
(a, b) = (0, 0)
errval = (None, None, None)
try:
a = int(self.qle_address.text(), 16)
except Exception:
self.error('malformed number: %s' % self.qle_address.text())
return errval
try:
b = int(self.qle_end.text(), 16)
except Exception:
return errval
if (a, b) == (0, 0):
# starting condition
return errval
if a == b:
self.error('empty interval')
return errval
if a > b:
self.error('negative interval')
return errval
if b - a > (16 * 1024 * 1024):
self.error('too large (>16mb) interval')
return errval
return (a, b, b - a)
# get the left, right, and length(size) of the entered fill interval
# calculate right through left and length(size)
def interval_by_size_change(self):
(a, b) = (0, 0)
errval = (None, None, None)
try:
a = int(self.qle_address.text(), 16)
except Exception:
self.error('malformed number: %s' % self.qle_address.text())
return errval
try:
size = int(self.qle_datasz.text(), 16)
except Exception:
return errval
if size <= 0:
self.error('length must > 0')
return errval
b = a + size
if (a, b) == (0, 0):
# starting condition
return errval
if a == b:
self.error('empty interval')
return errval
if a > b:
self.error('negative interval')
return errval
if b - a > (16 * 1024 * 1024):
self.error('too large (>16mb) interval')
return errval
return (a, b, size)
# get the data (as bytes) from either:
# - assembled bytes or manually
# - manually entered bytes
def data(self):
text = None
if self.chk_encoding.isChecked():
text = self.qle_encoding.text()
if self.chk_manual.isChecked():
text = self.qle_bytes.text()
buf = b''
if not text: return buf
for bstr in text.split(' '):
if bstr.startswith('0x'):
bstr = bstr[2:]
if len(bstr) != 2 or not bstr[0] in hexchars or not bstr[1] in hexchars:
self.error('malformed byte: %s' % bstr)
return None
buf += bytes([int(bstr, 16)])
return buf
# when user selects a section, populate [<address>, <end>)
def section_chosen(self):
line = self.qcb_sections.currentText()
m = re.match(r'.* \[(.*), (.*)\)$', line)
self.qle_address.setText(m.group(1))
self.qle_end.setText(m.group(2))
def encoding_checked_toggle(self, is_check):
self.chk_manual.setChecked(not is_check)
self.preview()
def manual_checked_toggle(self, is_check):
self.chk_encoding.setChecked(not is_check)
self.preview()
# elections -> preview field
def preview(self):
(_, _, fill_len) = self.interval()
if not fill_len: return None
data = self.data()
if not data: return None
self.qle_datasz.setText(hex(fill_len))
if fill_len <= 8:
self.qle_preview.setText(bytes_to_str(data[0:fill_len]))
else:
# fill length > 8
if len(data) == 1:
head = data * 6
tail = data * 2
else:
head = (data * math.ceil(6 / len(data)))[0:6]
idx = len(data) + (fill_len % len(data)) - 2
tail = (data + data)[idx:idx + 2]
self.qle_preview.setText(bytes_to_str(head) + ' ... ' + bytes_to_str(tail))
self.qle_preview.home(True)
def preview_by_size_change(self):
(st, ed, fill_len) = self.interval_by_size_change()
if not fill_len: return None
data = self.data()
if not data: return None
self.qle_end.setText(hex(ed))
if fill_len <= 8:
self.qle_preview.setText(bytes_to_str(data[0:fill_len]))
else:
# fill length > 8
if len(data) == 1:
head = data * 6
tail = data * 2
else:
head = (data * math.ceil(6 / len(data)))[0:6]
idx = len(data) + (fill_len % len(data)) - 2
tail = (data + data)[idx:idx + 2]
self.qle_preview.setText(bytes_to_str(head) + ' ... ' + bytes_to_str(tail))
self.qle_preview.home(True)
def fill(self):
# get, validate the interval
left, right, length = self.interval()
#print(f'filling [0x{left:X}, 0x{right:X}) length 0x{length:X}')
if left == None: return None
for a in [left, right - 1]:
if not is_valid_addr(self.bview, a):
self.error('0x%X invalid write address' % a)
return
# get, validate data
data = self.data()
if not data: return None
# form the final write
while len(data) < length:
data = data * 2
data = data[0:length]
# write it
#print('writing 0x%X bytes to 0x%X' % (len(data), left))
with self.bview.undoable_transaction():
self.bview.write(left, data)
# ------------------------------------------------------------------------------
# search tool
# ------------------------------------------------------------------------------
class SearchTab(QWidget):
def __init__(self, context, parent=None):
super(SearchTab, self).__init__(parent)
self.context = context
self.bview = context.binaryView
# this widget is VBox of QGroupBox
layoutV = QVBoxLayout()
form = QFormLayout()
self.chk_encoding = QCheckBox('Assembled:')
self.qle_encoding = QLineEdit()
self.qle_encoding.setReadOnly(True)
self.qle_encoding.setEnabled(False)
form.addRow(self.chk_encoding, self.qle_encoding)
self.chk_manual = QCheckBox('Bytes Regex:')
self.qle_bytes = QLineEdit('48 (8b|8d)+ . . (F8|10)')
form.addRow(self.chk_manual, self.qle_bytes)
layoutV.addLayout(form)
self.list_results = QListWidget()
self.list_results.setFont(font_mono)
layoutV.addWidget(self.list_results)
btn_search = QPushButton('Search')
layoutV.addWidget(btn_search)
layoutV.addLayout(form)
self.setLayout(layoutV)
# connect everything
self.chk_encoding.toggled.connect(self.encoding_checked_toggle)
self.chk_manual.toggled.connect(self.manual_checked_toggle)
self.list_results.itemDoubleClicked.connect(self.results_clicked)
btn_search.clicked.connect(self.search)
# set defaults
self.chk_encoding.setChecked(False)
self.chk_manual.setChecked(True)
self.qle_bytes.setFocus()
btn_search.setDefault(True)
def encoding_checked_toggle(self, is_check):
self.chk_manual.setChecked(not is_check)
def manual_checked_toggle(self, is_check):
self.chk_encoding.setChecked(not is_check)
def search(self):
# get search expression
sexpr = None
if self.chk_encoding.isChecked():
sexpr = self.qle_encoding.text()
if self.chk_manual.isChecked():
sexpr = self.qle_bytes.text()
# get rid of bad chars and whitespace in input line
sexpr = sexpr.strip()
sexpr = re.sub(r'\s+', '', sexpr)
# parse regex input line
my_encoding = 'utf8'
regex = b''
while sexpr:
if len(sexpr) >= 2 and sexpr[0] in hexchars and sexpr[1] in hexchars:
regex += re.escape(bytes.fromhex(sexpr[0:2]))
sexpr = sexpr[2:]
else:
regex += sexpr[0:1].encode(my_encoding)
sexpr = sexpr[1:]
# validate regex
try:
regobj = re.compile(regex)
except Exception:
error('invalid regex')
return
# clear old results
self.list_results.clear()
self.list_results.show()
# loop through each section, searching it
bview = self.bview
intervals = []
if bview.sections:
width = max([len(sname) for sname in bview.sections])
intervals = [(n, s.start, s.start + s.length) for n,s in bview.sections.items()]
else:
# some views don't have sections, like File -> New Binary Data
width = 3
intervals = [('raw', bview.start, bview.end)]
for name, start, end in intervals:
#print(f'searching bytes in [0x{start:X}, 0x{end:X})')
buf = bview.read(start, end - start)
for m in regobj.finditer(buf):
addr = start + m.start()
# m.group is bytes type
info = '%s %08X: %s' % (name.rjust(width), addr, bytes_to_str(m.group()))
self.list_results.addItem(QListWidgetItem(info))
def results_clicked(self, item):
# print('you double clicked %s' % item.text())
m = re.match(r'^.* ([a-fA-F0-9]+):', item.text())
addr = int(m.group(1), 16)
self.bview.navigate(self.bview.view, addr)
# ------------------------------------------------------------------------------
# top level tab gui
# ------------------------------------------------------------------------------
class KeypatchDialog(QDialog):
def __init__(self, context, parent=None):
super(KeypatchDialog, self).__init__(parent)
self.tab1 = AssembleTab(context)
self.tab2 = FillRangeTab(context)
self.tab3 = SearchTab(context)
tabs = QTabWidget()
tabs.addTab(self.tab1, "Assemble")
tabs.addTab(self.tab2, "Fill")
tabs.addTab(self.tab3, "Search")
layout = QVBoxLayout()
layout.addWidget(tabs)
self.setLayout(layout)
self.setWindowTitle('Keypatch')
#
self.tab1.add_qles_bytes(self.tab2.qle_encoding)
self.tab1.add_qles_bytes(self.tab3.qle_encoding)
self.tab1.re_assemble()
self.tab1.setFocus()
self.tab1.qle_assembly.setFocus()
# ------------------------------------------------------------------------------
# exported functions
# ------------------------------------------------------------------------------
# input: binaryninjaui.UIActionContext (struct UIActionContext from api/ui/action.h)
def launch_keypatch(context):
if context.binaryView == None:
error('no binary')
return
dlg = KeypatchDialog(context)
dlg.exec()