-
Notifications
You must be signed in to change notification settings - Fork 6
/
pySigMaker.py
1244 lines (925 loc) · 34.9 KB
/
pySigMaker.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
# -*- coding: utf-8 -*-
PLUGIN_VERSION = '0.1.50'
# If True the starting address will be current function when making a sig for functions.
# SigMaker-x64 behavior is to look for 5 or more references before adding function start
FUNC_START_EA = True
# Can be set via Gui, this is just for a fallback default
PLUGIN_HOTKEY = 'Ctrl-Alt-S'
"""
pySigMaker:
Ported by: zoomgod - unknowncheats.me
IDAPython port for most of the origional compiled SigMaker-x64 IDA
plugin with some minor changes, bug-fix and new GUI.
Credits to the origional author/contributors of SigMaker-x64
https://github.com/ajkhoury/SigMaker-x64
See readme for IDA/Python requirements
"""
import sys, pickle, os, shutil
PLUGIN_DIR, PLUGIN_FILENAME = sys.argv[0].rsplit('/', 1)
HOTKEY_CONFLICT = False
SIGMAKER_X64_PLUGINS = []
if PLUGIN_DIR.find('plugins') == -1:
PLUGIN_DIR = ''
else:
if os.path.exists('%s/sigmaker.dll' % PLUGIN_DIR):
SIGMAKER_X64_PLUGINS.append('sigmaker.dll')
if os.path.exists('%s/sigmaker64.dll' % PLUGIN_DIR):
SIGMAKER_X64_PLUGINS.append('sigmaker64.dll')
HOTKEY_CONFLICT = len(SIGMAKER_X64_PLUGINS) > 0
try:
import tkinter
from enum import unique, IntEnum
except:
print('Python 3.4 > required.')
sys.exit(0)
# Gui
from PyQt5 import Qt, QtCore, QtGui, QtWidgets
import idc
import idaapi, ida_kernwin
from idaapi import BADADDR
# Ignore this, just for when I debug gui layout issues
GUI_DBG_ENABLED = False
try:
from SigMakerDebug import QTDebugHelper
GUI_DBG_ENABLED = True
except:
pass
@unique
class QueryTypes(IntEnum):
QUERY_FIRST = 0 # Return 1st match
QUERY_COUNT = 1 # Return count
QUERY_UNIQUE = 2 # Return True/False
@unique
class PatternType(IntEnum):
PT_INVALID = -1
PT_DIRECT = 0
PT_FUNCTION = 1
PT_REFERENCE = 2
@unique
class SigType(IntEnum):
SIG_IDA = 0
SIG_CODE = 1
SIG_OLLY = 2
@unique
class SigSelect(IntEnum):
OPT_LENGTH = 0
OPT_OPCODES = 1
OPT_WILDCARDS = 2
@unique
class LogOptions(IntEnum):
LOG_ERROR = 0
LOG_RESULT = 1
LOG_DEBUG = 2
#
#
# Utility functions
#
#
class QueryStruct:
def __init__(self, pattern=b'', mask = b'', startea=BADADDR, endea=BADADDR):
self.pattern = pattern
self.mask = mask
self.startea = startea
self.endea = endea
self.ea = BADADDR
if self.startea == BADADDR:
self.startea = idaapi.inf_get_min_ea()
if self.endea == BADADDR:
self.endea = idaapi.inf_get_max_ea()
def BinSearch(query) -> QueryStruct:
"""
Searches for matching sequence of bytes based on a pattern and mask
args:
QueryStruct
returns:
QueryStruct with ea filled in
"""
startea = query.startea
if query.startea == BADADDR:
query.startea = idaapi.inf_get_min_ea()
endea = query.endea
if query.startea == BADADDR:
query.startea = idaapi.inf_get_max_ea()
query.ea = idaapi.bin_search( query.startea, query.endea, query.pattern, query.mask,
idaapi.BIN_SEARCH_FORWARD,
idaapi.BIN_SEARCH_NOBREAK | idaapi.BIN_SEARCH_NOSHOW )
return query
def MakeBin(ida_pattern, startea=BADADDR, endea=BADADDR) -> QueryStruct:
"""
makeBin(ida_pattern)
Returns QueryStruct with bin_search compatible pattern and mask from an IDA style pattern
"""
patt = bytearray()
mask = bytearray()
for i in ida_pattern.split(' '):
if i == '?':
patt.append(0)
mask.append(0)
else:
patt.append(int(i, 16))
mask.append(1)
return QueryStruct(bytes(patt), bytes(mask), startea, endea)
def BinQuery(sig, flag = QueryTypes.QUERY_FIRST, startea=None, endea = None):
"""
Args:
sig : IDA style pattern string
flag: One of QueryTypes enum members
Return types:
flag == QUERY_FIRST returns ea, search stops when matches == 1
flag == QUERY_COUNT returns int, full search
flag == QUERY_UNIQUE returns boolean, search stops when matches > 1
"""
query = MakeBin(sig)
Result = []
query = BinSearch(query)
while query.ea != BADADDR:
Result.append(query.ea)
if flag == QueryTypes.QUERY_UNIQUE and len(Result) > 1:
break
if flag == QueryTypes.QUERY_FIRST:
return Result[0]
query.startea = query.ea + 1
ea = BinSearch(query)
if flag == QueryTypes.QUERY_UNIQUE:
return len(Result) == 1
elif flag == QueryTypes.QUERY_COUNT:
return len(Result)
elif flag == QueryTypes.QUERY_FIRST:
return BADADDR
raise ValueError('Invalid flag passed')
#
#
# Pattern converters
#
#
def Ida2Code(sig) -> str:
"""
Ida2Code(sig)
Convert an IDA sig to code pattern and mask
Arg:
sig: IDA style sig
Returns:
string, string
"""
mask = ''
patt = ''
for entry in sig.split(' '):
if entry == '?':
patt = patt + '\\x00'
mask = mask + '?'
else:
patt = patt + '\\x%s' % entry
mask = mask + 'x'
return patt, mask
def Ida2Olly(sig) -> str:
"""
Ida2Olly(sig)
Convert an IDA sig to an Olly Debugger compatible sig
Arg:
sig: IDA style sig
Return:
string
"""
pattern = []
for entry in sig.split(' '):
if entry == '?':
pattern.append('??')
else:
pattern.append(entry)
return " ".join(pattern)
def Code2Ida(patt, mask=None) -> str:
"""
Code2Ida(sig)
Convert an code style sig to an IDA sig
Note: When no mask is supplied any \x00 in pattern become a wildcards.
Arg:
sig : required, code style sig
mask: optional
Return:
string
"""
pattern = []
p = []
# convert binary string or regular string into a list of ints
# Since \ is an escape character in Python have to check
# for varying strings
if not type(patt) is type(b''):
if type(patt) is type('') and patt.find('\\') > -1:
p = [ int('0x%s' % x, 16) for x in patt.split('\\x')[1:] ]
else:
return ''
else:
# binary string, can just convert to list
p = list(patt)
if mask and len(mask) != len(p):
return ''
for i in range(len(p)):
if mask:
if mask[i] == 'x':
pattern.append('%02X' % p[i])
else:
pattern.append('?')
elif p[i] > 0:
pattern.append('%02X' % p[i])
else:
pattern.append('?')
return ' '.join(pattern)
def GetIdaSig(sig, mask = None) -> str:
"""
GetIdaSig(sig)
Converts Olly or Code style sigs to an IDA style sigs
Arg:
sig : required, olly or code style sig
mask: optional, valid only for code sigs
Return:
string
"""
# Only a code sig should be byte string
if type(sig) is type(b''):
return Code2Ida(sig, mask)
if sig.find(' ') > -1:
# an olly sig without wildcards would be same as an ida sig so this is safe
if sig.find(' ?? ') > -1:
return sig.replace('??', '?')
# Olly sig with no wildcards or already an ida sig
return sig
# Only supported type left is code sigs as a string
return Code2Ida(sig, mask)
def GetSigType(sig) -> SigType:
if type(sig) is type(b'') or sig.find('\\') > -1:
return SigType.SIG_CODE
if sig.find(' ') > -1:
if sig.find(' ?? ') > -1:
return SigType.SIG_OLLY
return SigType.SIG_IDA
return SigType.SIG_CODE
#
#
# SigMaker
#
#
class SigCreateStruct:
def __init__(self):
self.sig = []
self.dwOrigStartAddress = BADADDR #ea at cursor when started
self.dwStartAddress = BADADDR
self.dwCurrentAddress = BADADDR
self.bUnique = False
self.iOpCount = 0
self.eType = PatternType.PT_INVALID
class SigMaker:
"""
Public methods:
AutoFunction()
AutoAddress()
"""
def __init__(self, plugin):
self.__plugin = plugin
self.Sigs = []
def _reset(self):
self.Sigs = []
def _addBytesToSig(self, sigIndex, ea, size):
for i in range(0, size):
b = idaapi.get_byte( ea + i )
self.Sigs[sigIndex].sig.append('%02X' % b)
def _addWildcards(self, sigIndex, count):
for i in range(0, count):
self.Sigs[sigIndex].sig.append('?')
def _getCurrentOpcodeSize(self, cmd) -> (int, int):
count = 0
for i in range(0, idaapi.UA_MAXOP):
count = i
if cmd.ops[i].type == idaapi.o_void:
return 0, count
if cmd.ops[i].offb != 0:
return cmd.ops[i].offb, count
return 0, count
def _matchOperands(self, ea) -> bool:
if idaapi.get_first_dref_from(ea) != BADADDR:
return False
elif not self.__plugin.Settings.bOnlyReliable:
if idaapi.get_first_fcref_from(ea) != BADADDR:
return False
elif idaapi.get_first_cref_from(ea) != BADADDR:
return False
return True
def _addInsToSig(self, cmd, sigIndex):
size, count = self._getCurrentOpcodeSize(cmd)
if size == 0:
self._addBytesToSig(sigIndex, cmd.ea, cmd.size)
return
else:
self._addBytesToSig(sigIndex, cmd.ea, size)
if self._matchOperands(cmd.ea):
self._addBytesToSig(sigIndex, cmd.ea + size, cmd.size - size)
else:
self._addWildcards(sigIndex, cmd.size - size)
def _addToSig(self, sigIndex) -> bool:
cmd = idaapi.insn_t()
cmd.size = 0
sig = self.Sigs[sigIndex]
if not idaapi.can_decode(sig.dwCurrentAddress):
return False
count = idaapi.decode_insn(cmd, sig.dwCurrentAddress)
if count == 0 or cmd.size == 0:
return False
if cmd.size < 5:
self._addBytesToSig(sigIndex, sig.dwCurrentAddress, cmd.size)
else:
self._addInsToSig(cmd, sigIndex)
sig.dwCurrentAddress = sig.dwCurrentAddress + cmd.size
sig.iOpCount = sig.iOpCount + 1
self.Sigs[sigIndex] = sig
return True
def _haveUniqueSig(self) -> bool:
for i in range(0, len(self.Sigs)):
if self.Sigs[i].bUnique:
return True
return False
def _addRefs(self, startea) -> bool:
self.__plugin.log('Adding references', LogOptions.LOG_DEBUG)
if idaapi.get_func_num(startea) != -1:
sig = SigCreateStruct()
sig.dwStartAddress = startea
sig.dwCurrentAddress = startea
sig.eType = PatternType.PT_DIRECT
self.Sigs.append(sig)
self.__plugin.log('Added direct reference 0x%X' % startea, LogOptions.LOG_DEBUG)
eaCurrent = idaapi.get_first_cref_to(startea)
while eaCurrent != BADADDR:
if eaCurrent != startea:
sig = SigCreateStruct()
sig.dwStartAddress = eaCurrent
sig.dwCurrentAddress = eaCurrent
sig.eType = PatternType.PT_REFERENCE
self.Sigs.append(sig)
self.__plugin.log('Added reference 0x%X' % eaCurrent, LogOptions.LOG_DEBUG)
if self.__plugin.Settings.maxRefs > 0 and len(self.Sigs) >= self.__plugin.Settings.maxRefs:
break
eaCurrent = idaapi.get_next_cref_to(startea, eaCurrent)
if len(self.Sigs) < 5:
self.__plugin.log('Not enough references were found (%i so far), trying the function.' % len(self.Sigs), LogOptions.LOG_DEBUG)
func = idaapi.get_func(startea)
if not func or func.start_ea == BADADDR:
self.__plugin.log('Selected address not in a valid function.', LogOptions.LOG_ERROR)
return False
if func.start_ea != startea:
eaCurrent = idaapi.get_first_cref_to(func.start_ea)
while eaCurrent != BADADDR:
if eaCurrent != startea:
sig = SigCreateStruct()
sig.dwStartAddress = func.start_ea
sig.dwCurrentAddress = eaCurrent
sig.eType = PatternType.PT_FUNCTION
self.Sigs.append(sig)
self.__plugin.log('Added function 0x%X' % eaCurrent, LogOptions.LOG_DEBUG)
if self.__plugin.Settings.maxRefs > 0 and len(self.Sigs) >= self.__plugin.Settings.maxRefs:
break
eaCurrent = idaapi.get_next_cref_to(func.start_ea, eaCurrent)
if not len(self.Sigs):
self.__plugin.log('Automated signature generation failed, no references found.', LogOptions.LOG_ERROR)
return False
self.__plugin.log('Added %i references.' % len(self.Sigs), LogOptions.LOG_DEBUG)
return True
def _chooseSig(self) -> bool:
max = 9999
selected = -1
for sigIndex in range(0, len(self.Sigs)):
sig = self.Sigs[sigIndex]
# drop wildcards off end of sig
while sig.sig[-1] == '?':
sig.sig = sig.sig[:-1]
if sig.bUnique:
sigLen = len(sig.sig)
if self.__plugin.Settings.SigSelect == SigSelect.OPT_LENGTH:
if sigLen < max or (sig.eType == PatternType.PT_DIRECT and max == sigLen):
max = sigLen
selected = sigIndex
else:
if self.__plugin.Settings.SigSelect == SigSelect.OPT_OPCODES:
if sig.iOpCount < max or (sig.eType == PatternType.PT_DIRECT and max == sig.iOpCount):
max = sig.iOpCount
selected = sigIndex
else:
wildcards = ''.join(sig.sig).count('?')
if wildcards < max or sig.eType == PatternType.PT_DIRECT and max == wildcards:
selected = sigIndex
max = wildcards
if selected == -1:
self.__plugin.log('Failed to create signature.', LogOptions.LOG_ERROR)
return False
sig = self.Sigs[selected]
idaSig = ' '.join(sig.sig)
strSig = ''
if self.__plugin.Settings.SigType == SigType.SIG_CODE:
patt, mask = Ida2Code(idaSig)
strSig = patt + ' ' + mask
elif self.__plugin.Settings.SigType == SigType.SIG_OLLY:
strSig = Ida2Olly(idaSig)
else:
strSig = idaSig
#
# Testing sigs for now, may just leave it, it's quick
#
ea = BinQuery(idaSig, QueryTypes.QUERY_FIRST)
txt = ''
if sig.eType == PatternType.PT_DIRECT:
txt = 'result: matches @ 0x%X, sig direct: %s' % (ea, strSig)
elif sig.eType == PatternType.PT_FUNCTION:
txt = 'result: matches @ 0x%X, sig function: (+0x%X) %s' % (ea, startea - sig.dwStartAddress, strSig)
elif sig.eType == PatternType.PT_REFERENCE:
txt = 'result: matches @ 0x%X, sig reference: %s' % (ea, strSig)
self.__plugin.log(txt, LogOptions.LOG_RESULT)
#
# Qt has a clipboard widget but I didn't want to place a QT
# requirement on using the class since it has nothing to do
# with the Gui. TKinter is included with Python.
#
r = tkinter.Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(strSig)
r.update()
r.destroy()
return True
def AutoFunction(self) -> bool:
"""
Generate shortest unique signature possible to current function
"""
self._reset()
startea = idc.get_screen_ea()
if startea in [0, BADADDR]:
self.__plugin.log('Current ea == BADADDR.', LogOptions.LOG_ERROR)
return False
if FUNC_START_EA:
# Get function start
func = idaapi.get_func(startea)
if not func or func.start_ea == BADADDR:
self.__plugin.log('Must be in a function.', LogOptions.LOG_ERROR)
return False
elif startea != func.start_ea:
startea = func.start_ea
self.__plugin.log('Using function: 0x%X' % startea, LogOptions.LOG_DEBUG)
if not self._addRefs(startea):
return False
iCount = 0
bHaveUniqueSig = False
while not bHaveUniqueSig and len(self.Sigs):
for sigIndex in range(0, len(self.Sigs)):
if len(self.Sigs[sigIndex].sig) < self.__plugin.Settings.maxSigLength and self._addToSig(sigIndex):
if len(self.Sigs[sigIndex].sig) > 5:
self.Sigs[sigIndex].bUnique = BinQuery(' '.join(self.Sigs[sigIndex].sig), QueryTypes.QUERY_UNIQUE)
else:
#return False
if sigIndex == 0:
self.Sigs = self.Sigs[1:]
elif sigIndex == len(self.Sigs) - 1:
self.Sigs = self.Sigs[:-1]
else:
self.Sigs = self.Sigs[:sigIndex] + self.Sigs[sigIndex+1:]
sigIndex = sigIndex - 1
bHaveUniqueSig = self._haveUniqueSig()
return self._chooseSig()
def AutoAddress(self) -> bool:
"""
Rather than create a sig from selection this
gets current ea from screen and then creates
the shortest sig possible.
I don't really see a need for making sigs from a
selection but can add it if enough people need it.
"""
self._reset()
startea = idc.get_screen_ea()
if startea in [0, BADADDR]:
self.__plugin.log('Click on address you want sig for.', LogOptions.LOG_ERROR)
return False
sig = SigCreateStruct()
sig.dwStartAddress = startea
sig.dwCurrentAddress = startea
sig.eType = PatternType.PT_DIRECT
self.Sigs.append(sig)
while not self.Sigs[0].bUnique and len(self.Sigs[0].sig) < self.__plugin.Settings.maxSigLength:
sigIndex = 0
if self._addToSig(sigIndex):
if len(self.Sigs[sigIndex].sig) > 5:
self.Sigs[sigIndex].bUnique = BinQuery(' '.join(self.Sigs[sigIndex].sig), QueryTypes.QUERY_UNIQUE)
else:
self.__plugin.log('Unable to create sig at selected address', LogOptions.LOG_ERROR)
return False
self._chooseSig()
#
#
# QT Gui
#
#
class PluginGui(idaapi.PluginForm):
def __init__(self, plugin):
global GUI_DBG_ENABLED
idaapi.PluginForm.__init__(self)
self.__plugin = plugin
if GUI_DBG_ENABLED:
self._QtDbgHelper = QTDebugHelper(self)
else:
self._QtDbgHelper = None
self.closed = False
#
# IDA PluginForm overloaded methods
#
def Show(self, caption, options=None):
if options:
super().Show(caption, options)
return
# Floating window as default.
super().Show(caption, idaapi.PluginForm.WOPN_DP_FLOATING)
def OnCreate(self, form):
self.widget = self.FormToPyQtWidget(form)
self.PopulateForm()
# Bit hackish but is needed to restore form position/size.
# Parent widget isn't set until after this function returns.
# The passed form is a child under the main TWidget created by IDA
QtCore.QTimer.singleShot(1000, self._formState)
def OnClose(self, form):
self._formState(bSave=True)
self.closed = True
#
# Connected QT events
#
def _sigTypeIdaClick(self):
self.__plugin.Settings.SigType = SigType.SIG_IDA
self.__plugin.Settings.save()
def _sigTypeCodeClick(self):
self.__plugin.Settings.SigType = SigType.SIG_CODE
self.__plugin.Settings.save()
def _sigTypeOllyClick(self):
self.__plugin.Settings.SigType = SigType.SIG_OLLY
self.__plugin.Settings.save()
def _sigTest(self):
patt = self.patt.currentText()
mask = self.mask.text()
sig = ''
st = GetSigType(patt)
if st == SigType.SIG_CODE:
sig = GetIdaSig(patt, mask)
else:
sig = GetIdaSig(patt)
mask = ''
if not sig:
self.__plugin.log('Invalid sig: "%s"' % sig, LogOptions.LOG_ERROR)
return
self.__plugin.Settings.addHistory(patt, mask)
self.__plugin.Settings.save()
query = MakeBin(sig)
result = BinSearch(query)
#
# Always logging tests to output so set to LOG_ERROR
#
if result != BADADDR:
self.__plugin.log('Sig matched @ 0x%X' % result.ea, LogOptions.LOG_ERROR)
else:
self.__plugin.log('No match found', LogOptions.LOG_ERROR)
def _sigTestSelectChanged(self, index):
mask = ''
try:
mask = self.__plugin.Settings.getHistory()[index][1]
except:
pass
self.mask.setText(mask)
def _sigCurrentFunction(self):
self.__plugin.SigMaker.AutoFunction()
def _sigAtCursor(self):
self.__plugin.SigMaker.AutoAddress()
def _logLevelChanged(self, index):
self.__plugin.Settings.LogLevel = index
self.__plugin.Settings.save()
def _sigSelectChanged(self, index):
self.__plugin.Settings.SigSelect = index
self.__plugin.Settings.save()
def _safeDataChecked(self, checkedState):
#
# Checkboxes can be tristate so passed arg is not a bool
#
if checkedState == QtCore.Qt.Unchecked:
self.__plugin.Settings.bOnlyReliable = False
else:
self.__plugin.Settings.bOnlyReliable = True
self.__plugin.Settings.save()
def _archiveSigmaker(self):
global PLUGIN_DIR, HOTKEY_CONFLICT, SIGMAKER_X64_PLUGINS
bDidMove = False
for name in SIGMAKER_X64_PLUGINS:
if not os.path.exists('%s/orig_sigmaker' % PLUGIN_DIR):
self.__plugin.log('mkdir: %s/orig_sigmaker' % (PLUGIN_DIR), LogOptions.LOG_ERROR)
os.mkdir('%s/orig_sigmaker' % PLUGIN_DIR)
if os.path.isfile('%s/%s' % (PLUGIN_DIR, name)):
shutil.move('%s/%s' % (PLUGIN_DIR, name), '%s/orig_sigmaker/%s' % (PLUGIN_DIR, name))
bDidMove = True
self.__plugin.log('Moved: %s/%s to %s/orig_sigmaker/%s' % (PLUGIN_DIR, name, PLUGIN_DIR, name), LogOptions.LOG_ERROR)
if bDidMove:
self.__plugin.log('SigMaker-x64 archived, restart IDA to unload it', LogOptions.LOG_ERROR)
HOTKEY_CONFLICT = False
SIGMAKER_X64_PLUGINS = []
self.archiveBtn.setEnabled(False)
def _saveHotkey(self):
hotkey = self.hotkeyTxt.text()
if hotkey != self.__plugin.Settings.hotkey:
self.__plugin.Settings.hotkey = hotkey
self.__plugin.Settings.save()
self.__plugin.log('\npySigMaker hotkey changed to %s, IDA restart needed.' % hotkey, LogOptions.LOG_ERROR)
def _defaultHotkey(self):
global PLUGIN_HOTKEY
self.hotkeyTxt.setText(PLUGIN_HOTKEY)
if PLUGIN_HOTKEY != self.__plugin.Settings.hotkey:
self.__plugin.Settings.hotkey = PLUGIN_HOTKEY
self.__plugin.Settings.save()
self.__plugin.log('\npySigMaker hotkey changed to %s (default), IDA restart needed.' % PLUGIN_HOTKEY, LogOptions.LOG_ERROR)
#
# Save/Restore plugin form position and size.
#
def _formState(self, bSave=False):
def getWidget():
widget, parent = None, self.widget
while parent:
if parent.windowTitle() == self.widget.windowTitle():
widget = parent
parent = parent.parent()
return widget
widget = getWidget()
if not widget:
self.__plugin.log('Failed to save form info', LogOptions.LOG_ERROR)
return
if bSave:
qrect = widget.geometry()
x, y, w, h = qrect.x(), qrect.y(), qrect.width(), qrect.height()
self.__plugin.Settings.saveFormInfo(x, y, w, h)
self.__plugin.Settings.save()
self.__plugin.log('Form saved, x={}, y={}, w={}, h={}'.format(x, y, w, h), LogOptions.LOG_DEBUG)
else:
x, y, w, h = self.__plugin.Settings.getFormInfo()
if x > -1:
widget.setGeometry(x, y, w, h)
self.__plugin.log('Form restored: x={}, y={}, w={}, h={}'.format(x, y, w, h), LogOptions.LOG_DEBUG)
self.patt.setCurrentText('')
#
# QT widget creation
#
def _getSigTypesBox(self):
""" Sig type selector"""
setting = self.__plugin.Settings.SigType
grp = QtWidgets.QGroupBox("Sig Type")
r1 = QtWidgets.QRadioButton(" IDA ")
r2 = QtWidgets.QRadioButton(" Code ")
r3 = QtWidgets.QRadioButton(" Olly ")
r1.toggled.connect(self._sigTypeIdaClick)
r2.toggled.connect(self._sigTypeCodeClick)
r3.toggled.connect(self._sigTypeOllyClick)
if setting == SigType.SIG_IDA:
r1.setChecked(True)
elif setting == SigType.SIG_CODE:
r2.setChecked(True)
elif setting == SigType.SIG_OLLY:
r3.setChecked(True)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(r1)
layout.addWidget(r2)
layout.addWidget(r3)
grp.setLayout(layout)
return grp
def _initSettings(self, layout):
global HOTKEY_CONFLICT
formLayout = QtWidgets.QFormLayout()
#
# Log to output window options
#
self.logOpt = QtWidgets.QComboBox()
for s in ['Errors', 'Errors/Results', 'Debug']:
self.logOpt.addItem(s)
if self.__plugin.Settings.LogLevel > LogOptions.LOG_DEBUG:
self.__plugin.Settings.LogLevel = LogOptions.LOG_DEBUG
elif self.__plugin.Settings.LogLevel < LogOptions.LOG_ERROR:
self.__plugin.Settings.LogLevel = LogOptions.LOG_ERROR
self.logOpt.setCurrentIndex(self.__plugin.Settings.LogLevel)
self.logOpt.currentIndexChanged.connect(self._logLevelChanged)
#
# Selecting sig from results options
#
self.sigSelectorOpt = QtWidgets.QComboBox()
for s in ['Shortest Sig', 'Least Opcodes', 'Least Wildcards']:
self.sigSelectorOpt.addItem(s)
if self.__plugin.Settings.SigSelect > SigSelect.OPT_WILDCARDS:
self.__plugin.Settings.SigSelect = SigSelect.OPT_WILDCARDS
elif self.__plugin.Settings.SigSelect < SigSelect.OPT_LENGTH:
self.__plugin.Settings.SigSelect = SigSelect.OPT_LENGTH
self.sigSelectorOpt.setCurrentIndex(self.__plugin.Settings.SigSelect)
self.sigSelectorOpt.currentIndexChanged.connect(self._sigSelectChanged)
#
# Reliable/Unreliable data option
#
self.safeData = QtWidgets.QCheckBox()
self.safeData.setTristate(False)
if self.__plugin.Settings.bOnlyReliable:
self.safeData.setCheckState(QtCore.Qt.Checked)
else:
self.safeData.setCheckState(QtCore.Qt.Unchecked)
self.safeData.stateChanged.connect(self._safeDataChecked)
if HOTKEY_CONFLICT:
self.archiveBtn = QtWidgets.QPushButton('Archive SigMaker-x64')
self.archiveBtn.clicked.connect(self._archiveSigmaker)
formLayout.addRow('Output', self.logOpt)
formLayout.addRow('Sig Choice', self.sigSelectorOpt)
formLayout.addRow('Reliable Data Only', self.safeData)
layout.addLayout(formLayout)
layout2 = QtWidgets.QHBoxLayout()
lbl = QtWidgets.QLabel('Hotkey:')
self.hotkeyTxt = QtWidgets.QLineEdit()
self.hotkeyTxt.setText(self.__plugin.Settings.hotkey)
self.hotkeySetBtn = QtWidgets.QPushButton('Set')
self.hotkeyRestoreBtn = QtWidgets.QPushButton('Default')
self.hotkeySetBtn.clicked.connect(self._saveHotkey)
self.hotkeyRestoreBtn.clicked.connect(self._defaultHotkey)
layout2.addWidget(lbl)
layout2.addWidget(self.hotkeyTxt)
layout2.addWidget(self.hotkeySetBtn)
layout2.addWidget(self.hotkeyRestoreBtn)
layout.addLayout(layout2)
if HOTKEY_CONFLICT:
layout.addWidget(self.archiveBtn)
def _initMainTab(self):
tab = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
btn1 = QtWidgets.QPushButton(' Sig for current function ')