This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheasyRE.py
1225 lines (1073 loc) · 42.7 KB
/
easyRE.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
import contextlib
import json
import logging
import os
import signal
from statistics import variance
import struct
import sys
import threading
import time
from atexit import register
from collections import OrderedDict
from concurrent.futures import thread
from dataclasses import dataclass
from tkinter.messagebox import askokcancel
from typing import List
import easygui
import ida_bytes
import ida_dbg
import ida_hexrays
import ida_nalt
import idaapi
import idautils
import idc
import ida_xref
import PyQt5
from idaapi import PluginForm
from idna import valid_label_length
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
from functools import wraps
from enum import IntEnum
import pprint
"""_summary_ = "EasyRE"
_author_ = "Antoine Blaud (@d0raken)"
"""
@dataclass
class GarbageValues:
"""_summary_
Contains the garbage global values used in this script.
"""
x64_register_names = [
'RAX', 'RBX', 'RCX', 'RDX', 'RSP', 'RBP', 'RSI', 'RDI', 'R8', 'R9',
'R10', 'R11', 'R12', 'R13', 'R14', 'R15'
]
x86_register_names = [
'EAX', 'EBX', 'ECX', 'EDX', 'ESP', 'EBP', 'ESI', 'EDI', 'R8D', 'R9D',
'R10D', 'R11D', 'R12D', 'R13D', 'R14D', 'R15D'
]
ret = [
'ret', 'retn', 'retf', 'iret', 'iretn', 'iretf', 'sysret', 'sysretn',
'sysretf'
]
modules = [
'kernel32.dll', 'user32.dll', 'ws2_32.dll', 'advapi32.dll', 'ntdll.dll'
]
windowsapi_functions = [
'GetKeyboardState', 'LoadResource', 'CreateThread', 'OpenProcessToken',
'GetCommandLineA', 'SendInput', 'RaiseException', 'VirtualAlloc',
'GetStartupInfoA', 'SetKeyboardState', 'RegQueryInfoKeyW',
'GetFileAttributesA', 'CreateMutexA', 'NtOpenProcess', 'PeekNamedPipe',
'GetVersionExA', 'PostMessageA', 'VkKeyScanW', 'GetAsyncKeyState',
'UnhandledExceptionFilter', 'FlushFileBuffers', 'OpenProcess',
'RegDeleteKeyW', 'RegQueryInfoKeyA', 'VirtualAllocEx',
'SetFileAttributesA', 'DeviceIoControl', 'NtMapViewOfSection',
'NtUnmapViewOfSection', 'RtlCreateUserProcess', 'VirtualProtect',
'GetForegroundWindow', 'GlobalMemoryStatus', 'GetModuleHandle',
'RegOpenKeyExA', 'RegCloseKey', 'ReadProcessMemory',
'NtCreateProcessEx', 'FreeEnvironmentStringsA', 'CreateProcess',
'RegQueryValueExA', 'FindResource', 'NtOpenSection', 'LoadLibraryA',
'CallNextHookEx', 'RegDeleteValueW', 'SetUnhandledExceptionFilter',
'NtCreateSection', 'DispatchMessageA', 'CreateFileA',
'NtAllocateVirtualMemory', 'NtWriteVirtualMemory', 'RegSetValueExA',
'VirtualFree', 'SetFileTime', 'UnhookWindowsHookEx', 'GetKeyState',
'send', 'HeapCreate', 'NtCreateProcess', 'RegSetValueExW',
'RegSetValueEx', 'SetStdHandle', 'RegDeleteKeyA', 'FreeLibrary',
'CreateService', 'PeekMessageA', 'MoveFileACreateProcess',
'GetTickCount', 'GetExitCodeProcess', 'RegDeleteValueA',
'RegCreateKeyExA', 'RegEnumKeyExA', 'CreateRemoteThread',
'CreateProcessInternal', 'RegOpenKeyExW', 'GetSystemDirectoryA',
'ReadFile', 'RegCreateKeyExW', 'RegQueryValueExW', 'FindClose',
'CreateProcessA', 'connect', 'GetFileType', 'FindResourceA',
'GetCurrentProcess', 'SetWindowsHookExA', 'GetProcAddress',
'InternetOpen', 'InternetConnect', 'HTTPSendRequest',
'HttpOpenRequest', 'InternetReadFile', 'URLDownloadToFile',
'ShellExecute', 'WinExec', 'send', 'bind'
]
maxparents = 1
discoverytries = 0
class GarbageHelper:
"""_summary_
Contains miscellaneous functions used in this script. Most of them are used to transform
registers or variables into a new format, in order to be printed or saved into a file.
"""
@staticmethod
def ConvertRegistersValues(regs_values):
""""_summary_
Convert registers values to a printable dict format.
"""
values = OrderedDict()
zfill = 16 if idaapi.get_inf_structure().is_64bit() else 8
for reg in regs_values:
values[reg] = hex(regs_values[reg])[2:].zfill(zfill).upper()
return values
@staticmethod
def ConvertVariablesValues(variables, regs_values):
""""_summary_
Convert variables values to a printable dict format.
"""
def Extract(pattern, regs_values, zfill, var, reg):
# use pattern to try to find the register name and setting value
with contextlib.suppress(Exception):
value = hex(regs_values[pattern])[2:].zfill(zfill).upper()
string = f'{var.name}: {reg} -> {value}'
found = True
return found, string
values = OrderedDict()
zfill = 16 if idaapi.get_inf_structure().is_64bit() else 8
for var in variables:
# if variable is saved into a register
if len(var.references) > 0:
reg = var.references[0].upper()
reg = reg[1:] + 'D' if reg[0] == 'E' else reg
rv = idaapi.regval_t()
# following code test if the register exists, and then get the value
if (idaapi.get_reg_val(reg, rv)):
found, string = False, None
with contextlib.suppress(Exception):
found, string = Extract(
reg[:2] if reg[2] == 'D' else reg, regs_values,
zfill, var, reg)
if not found:
found, string = Extract(reg, regs_values, zfill,
var, reg)
if not found:
found, string = Extract(
'R' +
reg if idaapi.get_inf_structure().is_64bit()
else 'E' + reg, regs_values, zfill, var, reg)
if not found:
string = reg
values[var.name] = string
else:
try:
offset = hex(int(reg[1:]))
reg = f"[rsp+{offset}]" if idaapi.get_inf_structure(
).is_64bit() else f"[esp+{offset}]"
string = f'{var.name}: {reg}'
#TODO: fix this
#values[var.name] = string
except ValueError:
continue
return values
@staticmethod
def FormatDataDump(**kwargs):
"""_summary_
Use the dumped data to show the values in different formats (int , str, ptr)
"""
#addr, ea, functionname, traceevents, index, timestotrace
addr = kwargs['addr']
ea = kwargs['ea']
functionname = kwargs['functionname']
traceevents = kwargs['traceevents']
index = kwargs['index']
timestotrace = kwargs['timestotrace']
for trace in traceevents:
if trace.functionname != functionname:
continue
for k, entry in trace.entries.items():
if k != ea:
continue
index = 0 if timestotrace == 1 else index % timestotrace
if addr in entry[index].mem.dump:
for word in entry[index].mem.dump[addr]:
yield GarbageHelper.FormatWord(word)
return
@staticmethod
def CreatePrintableBlock(string):
block = 18
size = len(string)
string = string.ljust(int(size + (block - size) / 2), ).rjust(
(block), )
return string
@staticmethod
def IntToStr(integer, size):
integer = hex(integer)[2:].zfill(size * 2)
result = []
for e in range(0, 2 * size, 2):
value = int(integer[e:e + 2], 16)
if value < 32 or value > 127:
result.append(chr(46))
continue
result.append(chr(value))
return ''.join(result[::-1])
@staticmethod
def FormatWord(word):
zfill = 8 if idaapi.get_inf_structure().is_64bit() else 4
hexa = hex(word)[2:].zfill(zfill * 2)
q = struct.pack('<Q', word)
signed_int = struct.unpack('<q', q)[0]
double = struct.unpack('<d', q)[0]
string = GarbageHelper.IntToStr(word, zfill)
s = f"{hexa.upper()} "
s += f"{string} ".rjust(10, ' ')
s += GarbageHelper.CreatePrintableBlock("{:e}".format(signed_int))
s += GarbageHelper.CreatePrintableBlock("{:e}".format(double))
return s
class IdaHelper:
"""_summary_
Contains miscellaneous functions.
"""
@staticmethod
def GetRegistersNames() -> list:
if idaapi.get_inf_structure().is_64bit():
return GarbageValues.x64_register_names
else:
return GarbageValues.x86_register_names
@staticmethod
def GetRegistersValues() -> dict:
regs = IdaHelper.GetRegistersNames()
regs_values = {}
rv = idaapi.regval_t()
for reg in regs:
idaapi.get_reg_val(reg, rv)
regs_values[reg] = rv.ival
return regs_values
@staticmethod
def GetEA() -> tuple:
rv = idaapi.regval_t()
if idaapi.get_inf_structure().is_64bit():
idaapi.get_reg_val('RIP', rv)
else:
idaapi.get_reg_val('EIP', rv)
functionname = idaapi.get_func_name(rv.ival)
return rv.ival, functionname
@staticmethod
def IsPointer(ea, segments):
return any(sgm.Contains(ea) for sgm in segments)
@staticmethod
def ReadPointer(ea):
if idaapi.get_inf_structure().is_64bit():
return idaapi.get_qword(ea)
return idaapi.get_dword(ea)
@staticmethod
def GetStackAddr():
rv = idaapi.regval_t()
if idaapi.get_inf_structure().is_64bit():
return idaapi.get_reg_val('RSP', rv).ival
return idaapi.get_reg_val('ESP', rv).ival
@staticmethod
def GrabFunctionCode(ea):
code = []
func = idaapi.get_func(ea)
while ea < func.end_ea:
code.append([ea, idc.GetDisasm(ea)])
ea = idc.next_head(ea)
return code
@staticmethod
def Resume():
idaapi.continue_process()
event = ida_dbg.wait_for_next_event(ida_dbg.WFNE_SUSP, -1)
return IdaHelper.GetEA()
@staticmethod
def StepOver():
idaapi.step_over()
event = ida_dbg.wait_for_next_event(ida_dbg.WFNE_SUSP, -1)
return IdaHelper.GetEA()
@staticmethod
def StepInto():
idaapi.step_into()
event = ida_dbg.wait_for_next_event(ida_dbg.WFNE_SUSP, -1)
return IdaHelper.GetEA()
@staticmethod
def FindFunctionInTrace(functionname, traceevents):
return next(
(trace
for trace in traceevents if trace.functionname == functionname),
None)
@staticmethod
def Count(dictionary, key):
if key in dictionary:
dictionary[key] += 1
else:
dictionary[key] = 1
return dictionary[key]
@staticmethod
def DiscoverSegments():
# build list of segment and maybe sort them
#ici regrouper les segments afin d'améliorer la rapiditié
segments = []
for s in idautils.Segments():
start = idc.get_segm_start(s)
end = idc.get_segm_end(s)
name = idc.get_segm_name(s)
segments.append(TraceCollection.Segment(start, end, name))
return segments
@staticmethod
def AskConfigurationValues():
pass
@staticmethod
def GetNames(base, size, desirednames):
currentaddress = base
result = {}
while currentaddress <= base + size:
for fname in desirednames:
if fname in idc.get_name(currentaddress):
result[fname] = currentaddress
idaapi.refresh_idaview_anyway()
QtWidgets.QApplication.processEvents()
currentaddress = idc.next_head(currentaddress)
return result
# Enumerate modules
@staticmethod
def FindRoutines(themodule, desirednames):
for m in idautils.Modules():
if themodule.lower() in m.name.lower():
base = m.base
size = m.size
idc.plan_and_wait(base, base + size)
return IdaHelper.GetNames(base, size, desirednames)
return {}
class Watchdog():
"""_summary_
Wathdog prevents the program from freezing.
This class is used in long loop like trace functions.
"""
def __init__(self, timeout=60):
self.timeout = timeout
print("Watchdog started")
def _Expire(self):
raise Exception("Watchdog expired")
def Start(self):
self.started_time = time.time()
def CheckExpired(self):
if time.time() - self.started_time > self.timeout:
self._Expire()
return False
class TraceCollection:
"""_summary_
Containes garbage class for collecting data from the memory and saving it.
"""
class Segment:
def __init__(self, start, end, name):
self.start = start
self.end = end
self.name = name
def Contains(self, addr) -> bool:
return (addr >= self.start and addr <= self.end)
class MemoryZone:
"""_summary_
Represents a memory zone dumped because a register or a pointer found was pointing to it.
"""
def __init__(self, regs_values):
self.dump = {}
self.regs_values = regs_values
def Dump(self):
self.DumpRegistersPoiters()
def DumpAddr(self, addr, depth=0, size=50):
"""_summary_
Dump a memory zone from an address. If inside the zone there is a pointer,
it will be dumped to by calling this function again.
"""
if addr in self.dump or depth > 1:
return
self.dump[addr] = []
incr = 8 if idaapi.get_inf_structure().is_64bit() else 4
for i in range(size):
try:
value = idaapi.get_qword(
addr + i * incr) if idaapi.get_inf_structure(
).is_64bit() else idaapi.get_dword(addr + i * incr)
self.dump[addr].append(value)
self.DumpAddr(value, depth + 1, 30)
except Exception as e:
print(e)
break
def DumpRegistersPoiters(self):
for regname, regvalue in self.regs_values.items():
self.DumpAddr(regvalue, size=50)
class EaEntry:
"""_summary_
Single entry in the EA list. Each line of code is an entry.
"""
def __init__(self, registers, variables, mem):
self.registers = registers
self.variables = variables
self.mem = mem
class FunctionEntry:
"""_summary_
Wrapper for EaEntry enlarged with the function name.
"""
def __init__(self, functionname):
self.entries = OrderedDict()
self.functionname = functionname
def Clone(self):
trace = TraceCollection.FunctionEntry(self.functionname)
trace.entries = self.entries
return trace
def AddEntry(self, addr, registers, variables, segments):
regs_values = IdaHelper.GetRegistersValues()
memdump = TraceCollection.MemoryZone(regs_values)
memdump.Dump()
entry = TraceCollection.EaEntry(registers, variables, memdump)
if addr not in self.entries:
self.entries[addr] = [entry]
else:
self.entries[addr].append(entry)
def AddEntrySaved(self, addr, registers, variablessaved, memdumpsaved):
memdump = TraceCollection.MemoryZone(registers)
memdumpsaved = {int(k): v for k, v in memdumpsaved.items()}
memdump.dump = memdumpsaved
variables = TraceCollection.Variables()
variables.extend([
TraceCollection.VariableEntry.FromJSON(json)
for json in variablessaved
])
entry = TraceCollection.EaEntry(registers, variables, memdump)
if addr not in self.entries:
self.entries[addr] = [entry]
else:
self.entries[addr].append(entry)
class VariableEntry:
"""_summary_
Variable entry is saved into this class.
"""
def __init__(self, name=None, defea=None, vdloc=None):
self.name = name
self.defea = defea
self.vdloc = vdloc
self.references = []
def AppendReference(self, location):
if location not in self.references:
self.references.append(location)
def DeleteReference(self, location):
self.references.remove(location)
def ToJSON(self):
return json.dumps(self,
default=lambda o: o.__dict__,
sort_keys=True,
indent=4)
@staticmethod
def FromJSON(jsonString):
variableentry = TraceCollection.VariableEntry()
variableentry.__dict__ = json.loads(jsonString)
return variableentry
class Variables(List):
"""_summary_
Wrapper for VariableEntry , collections of variables.
"""
def __init__(self):
super().__init__()
def AppendVariable(self, name, defea, vdloc):
self.append(TraceCollection.VariableEntry(name, defea, vdloc))
@staticmethod
def FetchVariablesValues(varscollection):
"""_summary_
Try to get variable values and save them into the variables_collection.
"""
ea, _ = IdaHelper.GetEA()
func = idaapi.get_func(ea)
variables = None
init = False
try:
if len(varscollection) == 0 or ea == func.start_ea:
varscollection.append(TraceCollection.Variables())
init = True
variables = varscollection[-1]
if idc.print_insn_mnem(ea) in GarbageValues.ret:
varscollection.pop()
decompilation = idaapi.decompile(func.start_ea)
if init:
for var in decompilation.lvars:
if var.name != "":
variables.AppendVariable(
var.name, var.defea,
ida_hexrays.print_vdloc(
var.location, int(var.width)))
TraceCollection.Variables.Instanciate(ea, variables)
except Exception as e:
return varscollection
return variables
@staticmethod
def Instanciate(ea, variables):
for var in variables:
# instantiate variables when they are created in the code
if var.defea >= ea and ea < var.defea + 0x32 and len(
var.references) == 0:
var.AppendReference(var.vdloc)
if str(idc.print_operand(ea, 0)) == str(var.vdloc):
var.AppendReference(idc.print_operand(ea, 0))
for ref in var.references:
if idc.print_insn_mnem(ea) == "mov" and idc.print_operand(
ea, 1) == ref:
var.AppendReference(idc.print_operand(ea, 1))
break
@staticmethod
def LoadTrace():
file = easygui.fileopenbox(filetypes=["*.json"])
last_trace_index = -1
if not file:
return
f = open(file, 'r')
content = json.load(f)
traceevents = []
last_invoke = content["last_invoke"]
timestotrace = content["timestotrace"]
for entry in content["trace"]:
trace_index = entry["trace_index"]
if trace_index > last_trace_index:
last_trace_index = trace_index
functraced = TraceCollection.FunctionEntry(
entry["functionname"])
traceevents.append(functraced)
entry_content = entry["content"]
ea = int(entry["ea"])
registers = entry_content["registers"]
variables = entry_content["variables"]
memdump = entry_content["memdump"]
functraced.AddEntrySaved(ea, registers, variables, memdump)
f.close()
return traceevents, last_invoke, timestotrace
@staticmethod
def SaveTrace(traceevents, last_invoke, timestotrace):
file = easygui.filesavebox("Save file",
default='trace',
filetypes=['*.json'])
content = {}
file = open(file, 'w')
content["last_invoke"] = last_invoke
content["timestotrace"] = timestotrace
content["trace"] = []
for i, trace in enumerate(traceevents):
for k, entry in trace.entries.items():
for j in range(len(entry)):
registersentry = entry[j].registers
registers = registersentry
variables = [
variable.ToJSON() for variable in entry[j].variables
]
content["trace"].append({
"ea": k,
"trace_index": i,
"functionname": trace.functionname,
"content": {
"registers": registers,
"variables": variables,
"memdump": entry[j].mem.dump
}
})
json.dump(content, file)
file.close()
class UI(PluginForm):
"""_summary_
This class manages the UI of the plugin.
"""
def AttachTracer(self, valuetracer):
self.valuetracer = valuetracer
self.stack_pointer_printed = {}
def OnCreate(self, form):
self.parent = self.FormToPyQtWidget(form)
self.PopulateForm()
def PopulateForm(self):
btnpostions = [(0, i * 50, 140, 40) for i in range(10)]
# Create layout
layout = QtWidgets.QHBoxLayout()
self.callsW = QtWidgets.QListWidget()
self.callsW.setSelectionMode(
QtWidgets.QAbstractItemView.ExtendedSelection)
self.callsW.currentItemChanged.connect(self.ShowCode)
self.callsW.setFixedWidth(200)
self.codeW = QtWidgets.QListWidget()
self.codeW.setSelectionMode(
QtWidgets.QAbstractItemView.ExtendedSelection)
self.codeW.currentItemChanged.connect(self.ShowTrace)
self.codeW.setFixedWidth(400)
self.dump = QtWidgets.QHBoxLayout()
self.dataW = QtWidgets.QListWidget()
self.dataW.setSelectionMode(
QtWidgets.QAbstractItemView.ExtendedSelection)
self.dataW.currentItemChanged.connect(self.ShowMemoryDump)
self.dataW.setFixedWidth(400)
self.stackL = QtWidgets.QListWidget()
self.stackL.setSelectionMode(
QtWidgets.QAbstractItemView.ExtendedSelection)
self.stackL.setFont(QtGui.QFont('Consolas', 9))
self.dataW.setFixedWidth(300)
self.stackL.currentItemChanged.connect(self.ShowStackDumpedPointer)
self.widget = QtWidgets.QWidget()
self.widget.setFixedWidth(150)
self.restartBtn = QtWidgets.QPushButton('Reset', self.widget)
self.restartBtn.clicked.connect(self.valuetracer.ActionReset)
self.restartBtn.setGeometry(*btnpostions[1])
self.cleanBtn = QtWidgets.QPushButton('Clean Bpts', self.widget)
self.cleanBtn.setGeometry(*btnpostions[6])
self.cleanBtn.clicked.connect(self.valuetracer.CleanBreakpoints)
self.saveBtn = QtWidgets.QPushButton('Save', self.widget)
self.saveBtn.clicked.connect(self.valuetracer.Save)
self.saveBtn.setGeometry(*btnpostions[8])
self.traceBtnGlobal = QtWidgets.QPushButton('Function Tracing',
self.widget)
self.traceBtnGlobal.setGeometry(*btnpostions[0])
self.traceBtnGlobal.clicked.connect(self.valuetracer.ActionGlobalTrace)
self.traceBtnSurgical = QtWidgets.QPushButton(
'Surgical Tracing', self.widget)
self.traceBtnSurgical.setGeometry(*btnpostions[5])
self.traceBtnSurgical.clicked.connect(
self.valuetracer.ActionSurgicalTrace)
self.loadBtn = QtWidgets.QPushButton('Load', self.widget)
self.loadBtn.setGeometry(*btnpostions[7])
self.loadBtn.clicked.connect(self.valuetracer.Load)
self.stepOverBtn = QtWidgets.QPushButton('Step Over', self.widget)
self.stepOverBtn.setGeometry(*btnpostions[2])
self.stepOverBtn.clicked.connect(self.valuetracer.ActionStepOver)
self.stepIntoBtn = QtWidgets.QPushButton('Step Into', self.widget)
self.stepIntoBtn.setGeometry(*btnpostions[3])
self.stepIntoBtn.clicked.connect(self.valuetracer.ActionStepInto)
self.stepIntoBtn = QtWidgets.QPushButton('Resume', self.widget)
self.stepIntoBtn.setGeometry(*btnpostions[4])
self.stepIntoBtn.clicked.connect(self.valuetracer.ActionResume)
self.stepIntoBtn = QtWidgets.QPushButton('Hook WinAPI', self.widget)
self.stepIntoBtn.setGeometry(*btnpostions[9])
self.stepIntoBtn.clicked.connect(self.valuetracer.ActionHookWin)
self.dump.addWidget(self.dataW)
self.dump.addWidget(self.stackL)
layout.addWidget(self.callsW)
layout.addWidget(self.codeW)
layout.addLayout(self.dump)
layout.addWidget(self.widget)
# make our created layout the dialogs layout
self.parent.setLayout(layout)
def AddCall(self, item):
self.callsW.addItem(item)
def AddCode(self, item):
self.codeW.addItem(item)
def AddData(self, item):
self.dataW.addItem(item)
def AddDump(self, item):
self.stackL.addItem(item)
def Reset(self):
with contextlib.suppress(Exception):
self.dataW.clear()
self.callsW.clear()
self.codeW.clear()
self.stackL.clear()
self.stack_pointer_printed = {}
self.callsW.setCurrentRow(0)
self.codeW.setCurrentRow(0)
def ShowMemoryDump(self, traceevents):
"""_summary_
Show the memory dump of the current instruction.
"""
self.stackL.clear()
try:
functionname = self.callsW.currentItem().text()
addr = self.dataW.currentItem().text()
ea = int(self.codeW.currentItem().text().split(" ")[0], 16)
except AttributeError:
functionname = self.dataW.item(0).text()
addr = self.dataW.item(0).text()
ea = int(self.codeW.item(0).text().split(" ")[0], 16)
try:
addr = int(addr.split(" ")[-1], 16)
except ValueError:
return
idaapi.jumpto(ea)
indexcode = self.codeW.currentRow()
# addr, ea, functionname, self.valuetracer.traceevents, index, self.valuetracer.timestotrace
params = {
"addr": addr,
"ea": ea,
"functionname": functionname,
"traceevents": self.valuetracer.traceevents,
"index": indexcode,
"timestotrace": self.valuetracer.timestotrace
}
mult = 8 if idaapi.get_inf_structure().is_64bit() else 4
for offset, dump in enumerate(GarbageHelper.FormatDataDump(**params)):
offset = offset * mult
self.AddDump("0x%03x %s" % (offset, dump))
def ShowCode(self):
"""_summary_
Show the code of the function the user selected in the GUI
"""
self.codeW.clear()
code = []
if self.valuetracer.last_invoke == Invoke.CHIRUGICAL_TRACER:
for trace in self.valuetracer.traceevents:
for ea, entry in trace.entries.items():
code.extend([ea, idc.GetDisasm(ea)] for _ in entry)
else:
try:
funcName = self.callsW.currentItem().text()
ea = idc.get_name_ea_simple(funcName)
code = IdaHelper.GrabFunctionCode(ea)
except AttributeError:
pass
self._ShowCode(code)
def _ShowCode(self, code):
for code_entry in code:
ea = code_entry[0]
functionname = idc.get_func_name(ea)
string = hex(ea) + " " * 4 + code_entry[1]
self.AddCode(string)
line = self.codeW.item(self.codeW.count() - 1)
if self.valuetracer.last_invoke == Invoke.GLOBAL_TRACER:
if (ida_dbg.get_bpt(code_entry[0], ida_dbg.bpt_t())):
line.setBackground(QtGui.QColor(102, 0, 12))
else:
line.setBackground(QtGui.QColor(102, 105, 110))
for trace in self.valuetracer.traceevents:
if trace.functionname != functionname:
continue
for k, v in trace.entries.items():
if k != ea:
continue
line.setBackground(QtGui.QColor(0, 102, 12))
break
def ShowTrace(self):
"""_summary_
Show the trace in the GUI
"""
self.dataW.clear()
try:
functionname = self.callsW.currentItem().text()
ea = int(self.codeW.currentItem().text().split(" ")[0], 16)
except AttributeError:
functionname = self.callsW.item(0).text()
ea = int(self.codeW.item(0).text().split(" ")[0], 16)
idaapi.jumpto(ea)
for trace in self.valuetracer.traceevents:
if trace.functionname != functionname:
continue
for k, entry in trace.entries.items():
if k != ea:
continue
# special case for chirugical tracing
index = 0 if self.valuetracer.last_invoke != Invoke.CHIRUGICAL_TRACER else self.codeW.currentRow(
) % self.valuetracer.timestotrace
if index > len(entry) - 1:
raise Exception("Index out of range")
for regname, regvalue in GarbageHelper.ConvertRegistersValues(
entry[index].registers).items():
self.AddData(f"{regname}: {regvalue}")
for _, vstring in GarbageHelper.ConvertVariablesValues(
entry[index].variables,
entry[index].registers).items():
self.AddData(f"{vstring}")
break
def ShowStackDumpedPointer(self):
"""_summary_
When we click on a valid pointer into the memory dump, we trigger this function that show the memory dump
of the pointed address
"""
stack = self.stackL.currentItem().text()
item = self.stackL.findItems(stack, QtCore.Qt.MatchExactly)[0]
index = self.stackL.indexFromItem(item).row()
addr = int(stack.split(" ")[4], 16)
# if index in self.stack_pointer_printed:
# return
self.stack_pointer_printed[index] = True
ea = int(self.codeW.currentItem().text().split(" ")[0], 16)
functionname = self.callsW.currentItem().text()
indexcode = self.codeW.currentRow()
params = {
"addr": addr,
"ea": ea,
"functionname": functionname,
"traceevents": self.valuetracer.traceevents,
"index": indexcode,
"timestotrace": self.valuetracer.timestotrace
}
mult = 8 if idaapi.get_inf_structure().is_64bit() else 4
for offset, dump in enumerate(GarbageHelper.FormatDataDump(**params)):
offset = offset * mult
self.stackL.insertItem(index + 1,
" " * 8 + "0x%03x %s" % (offset, dump))
index += 1
def OnClose(self, form):
"""
Called when the widget is closed
"""
with contextlib.suppress(Exception):
self.valuetracer.CleanBreakpoints()
idaapi.clear_trace()
class EasyRe(idaapi.plugin_t):
flags = idaapi.PLUGIN_UNL
comment = "Plugin for improve the reverse engineering speed of IDA when you want tog focus on a Surgical part of the code"
help = "See https://github.com/"
wanted_name = "EasyRe"
wanted_hotkey = "Ctrl+Shift+U"
def __init__(self):
super(EasyRe, self).__init__()
self.discoveredfuncs = []
self.bpts = []
self.varscollection = []
self.traceevents = []
self.segments = []
self.prevfuncea = 0
self.last_invoke = None
self.watchDog = Watchdog()
self.timestotrace = 1
self.AttachUi()
def Save(self):
"""_summary_
Save the current state of the plugin
"""
TraceCollection.SaveTrace(self.traceevents, self.last_invoke,
self.timestotrace)
def AttachUi(self):
"""_summary_
Attach the UI to the plugin
"""
self.UI = UI()
self.UI.AttachTracer(self)
def run(self, arg):
"""_summary_
Run the plugin
"""
for i in range(10):
with contextlib.suppress(Exception):
self.UI.Show(f"EasyRe {hex(i)}")
break
def init(self):
return idaapi.PLUGIN_OK
def term(self):
pass
def Load(self):
"""_summary_
Load the saved state of the plugin
"""
self.Reset()
self.traceevents, self.last_invoke, self.timestotrace = TraceCollection.LoadTrace(
)
for trace in self.traceevents:
self.UI.AddCall(trace.functionname)
def Reset(self):
self.UI.Reset()
for ea in self.bpts:
idaapi.del_bpt(ea)
self.bpts = []
self.varscollection = []
self.prevfuncea = 0
self.traceevents = []
def PrepareTrace(func):
def wrap(self, *args, **kw):
self.timestotrace = 1
if self.last_invoke == Invoke.CHIRUGICAL_TRACER:
self.Reset()
return func(self)
return wrap
def ActionReset(self):
self.Reset()
def ActionGlobalTrace(self):
"""_summary_
Trrigger the global trace action
"""
self.Reset()
self.last_invoke = Invoke.GLOBAL_TRACER
self.SetupGlobalTrace()
self.GlobalTrace()
self.CleanBreakpoints()
def ActionSurgicalTrace(self):
"""_summary_
Trrigger the Surgical trace action
"""
self.Reset()
IdaHelper.Resume()
self.CleanBreakpoints()
self.last_invoke = Invoke.CHIRUGICAL_TRACER
self.timestotrace = idaapi.ask_long(10, "Number of times to trace:")
self.SurgicalTrace()
@PrepareTrace
def ActionStepOver(self):
"""_summary_
Trigger the step over action
"""
self.last_invoke = Invoke.GLOBAL_TRACER