forked from bsdphk/PyRevEng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyreveng.py
172 lines (142 loc) · 3.33 KB
/
pyreveng.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
#!/usr/local/bin/python
#
# This is the central container-class for a PyRevEng job.
#
# Services rendered:
# todo Todo list, things to do, one thing after another
#----------------------------------------------------------------------
# Check the python version
import sys
assert sys.version_info[0] >= 3 or "Need" == "Python v3"
#----------------------------------------------------------------------
# Python dist imports
# PyRevEng imports
import tree
class pyreveng(object):
def __init__(self, m, lo=None, hi=None):
""" The container class for a reverse engineering task.
m:
A memory class
lo:
The lowest address of interest
hi:
The higest address of interest
"""
self.m = m
self.lo = lo
self.hi = hi
if self.lo == None:
self.lo = m.start
if self.hi == None:
self.hi = m.end
assert type(self.lo) == int
assert type(self.hi) == int
self.t = tree.tree(self.lo, self.hi)
self.c = dict()
# @todo
self.__tlist = list()
# @label
self.label = dict()
# Random attributes
self.a = dict()
# @hints
self.hints = dict()
self.cmt_start = 56
###############################################################
# HINT processing
#
def hint(self, adr):
if not adr in self.hints:
self.hints[adr] = dict()
return self.hints[adr]
###############################################################
# TODO list processing
#
def todo(self, adr, func, priv = None):
assert type(adr) == int
if adr < self.lo or adr > self.hi:
if False:
print("WARNING: Ignoring todo at " +
self.m.afmt(adr), func, priv)
return
try:
self.m.chkadr(adr)
except:
return
self.__tlist.append((adr, func, priv))
def run(self):
if len(self.__tlist) == 0:
return False
while len(self.__tlist) > 0:
c = self.__tlist[0]
del self.__tlist[0]
if True:
c[1](self, c[0], c[2])
return True
try:
c[1](self, c[0], c[2])
except:
print("FAILED %x\n\t" % c[0], c[1], "\n\t", c[2])
try:
print(c[2].debug())
except:
pass
return True
###############################################################
def setlabel(self, a, lbl):
self.label[a] = lbl
###############################################################
# Load labels from file
#
def loadlabels(self, filename):
fi = open(filename, "r")
for i in fi.readlines():
i = i.strip()
if i == "" or i[0] == "#":
continue
j = i.split()
self.setlabel(int(j[1], 0), j[0])
fi.close()
###############################################################
# A general purpose hexdumping routine
#
def hexdump(self, start = None, end = None, fo = sys.stdout, wid=16):
if start == None:
start = self.m.start
if end == None:
end = self.m.end
# Must be power of two
assert wid & (wid -1) == 0
adr = start
while adr < end:
s = self.m.afmt(adr)
s += " "
t = ""
b = adr & ~(wid -1)
e = b + wid
if e > end:
e = end
if self.m.tstflags(b, e, self.m.undef):
adr = b + wid
continue
for i in range(0, wid):
if i == 8:
s += " "
if i < (adr & (wid-1)) or b + i >= end:
s += " .."
t += "."
continue
try:
x = self.m.rd(b + i)
except:
s += " --"
t += "-"
continue
s += " %02x" % x
if x >= 32 and x <= 126:
t += "%c" % x
else:
t += "."
s += " |" + t + "|\n"
fo.write(s)
adr = b + 16