-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathget_all_opcode.py
140 lines (117 loc) · 3.79 KB
/
get_all_opcode.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import os.path
from idaapi import *
import time
import idc
from idc import *
def my_get_reg_value(register):
rv = ida_idd.regval_t()
ida_dbg.get_reg_val(register, rv)
regval = rv.ival
return regval
class MyDbgHooks(DBG_Hooks):
def __init__(self, output_path, enter_ea, fetch_ea, exit_ea):
DBG_Hooks.__init__(self)
self.__output_path = output_path
self.__output = None
self.__cur_opset = None
self.__opstack = []
self.__enter_ea = enter_ea
self.__fetch_ea = fetch_ea
self.__exit_ea = exit_ea
#
def dbg_process_attach(self, pid, tid, ea, name, base, size):
print("Process attach pid=%d tid=%d ea=0x%x name=%s base=%x size=%x" % (pid, tid, ea, name, base, size))
self.__output = open(self.__output_path, "w")
self.__tm = time.time()
#
def __print_res(self):
base = int(get_imagebase())
tmp_list = list(self.__cur_opset)
tmp_list.sort()
head = "begin dump opcode base 0x%08x\n"%int(base)
self.__output.write(head)
for opcode in tmp_list:
line = "0x%08x\n"%int(opcode)
self.__output.write(line)
if (self.__output != None):
self.__output.flush()
#
#
def dbg_bpt(self, tid, ea):
base = int(get_imagebase())
addr = int(ea)
fetch_abs_ea = base + self.__fetch_ea
enter_abs_ea = base + self.__enter_ea
exit_abs_ea = base + self.__exit_ea
#print("%x %x"%(addr, fetch_abs_ea))
if (addr == enter_abs_ea):
print("enter vmp...")
if (self.__cur_opset != None):
self.__opstack.append(self.__cur_opset)
#
self.__cur_opset = set()
elif (addr == fetch_abs_ea):
cur_opcode = my_get_reg_value("R0")
self.__cur_opset.add(cur_opcode)
print("opcode off 0x%08x"%int(cur_opcode))
#
elif (addr == exit_abs_ea):
print("exit vmp dump...")
self.__print_res()
self.__cur_opset = self.__opstack.pop()
if (len(self.__opstack) == 0):
print("finish...")
#
#
return 0
#
def dbg_suspend_process(self):
print ("Process suspended")
if (self.__output != None):
self.__output.flush()
#
#
def dbg_process_exit(self, pid, tid, ea, code):
print("call Process exited pid=%d tid=%d ea=0x%x code=%d" % (pid, tid, ea, code))
if (self.__output != None):
self.__output.close()
#
#
#
if __name__ == "__main__":
# Remove an existing debug hook
try:
if debughook:
print("Removing previous hook ...")
debughook.unhook()
#
#
except:
pass
#
script_path = os.path.split(os.path.realpath(__file__))[0]
name = os.path.splitext(idc.get_root_filename())[0]
file_path = "%s/opcodes-%s.txt"%(script_path, name)
fetch_ea = 0x0004C0CC
fetch_abs_ea = get_imagebase() + fetch_ea
AddBpt(fetch_abs_ea)
SetBptAttr(fetch_abs_ea, BPTATTR_FLAGS, BPT_ENABLED|BPT_TRACE)
enter_ea = 0x0004C028
enter_abs_ea = get_imagebase() + enter_ea
AddBpt(enter_abs_ea)
SetBptAttr(enter_abs_ea, BPTATTR_FLAGS, BPT_ENABLED|BPT_TRACE)
#用0x4DDB4会崩溃
exit_ea = 0x4DDB0
exit_abs_ea = get_imagebase() + exit_ea
AddBpt(exit_abs_ea)
SetBptAttr(exit_abs_ea, BPTATTR_FLAGS, BPT_ENABLED|BPT_TRACE)
#Install the debug hook
debughook = MyDbgHooks(file_path, enter_ea, fetch_ea, exit_ea)
debughook.hook()
debughook.steps = 0
print ("script run...")
#