-
Notifications
You must be signed in to change notification settings - Fork 71
/
pykd_engine.py
225 lines (168 loc) · 4.87 KB
/
pykd_engine.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
# shadow - De Mysteriis Dom jemalloc
import sys
true = True
false = False
none = None
sys.path.append('.')
try:
import pykd
except ImportError:
print('[shadow] pykd_engine is only usable from within windbg/pykd')
sys.exit()
address_separator = ' '
# cache
cache_dword_size = None
cache_int_size = None
cache_page_size = None
cache_offsets = {}
cache_values= {}
cache_sizes = {}
# XXX: this function is getting complicated; needs to be re-written with
# pykd's typed memory access features
def to_int(val):
sval = str(val)
start = sval.find('0x')
if start != -1:
end = sval.find('\n')
if end == -1:
return int(sval[start:].replace('`', ''), 16)
else:
return int(sval[start:end].replace('`', ''), 16)
elif sval.startswith('unsigned int'):
if sval.startswith('unsigned int64'):
return int(sval[len('unsigned int64'):])
else:
return int(sval[len('unsigned int'):])
else:
return int(sval.replace('`', ''))
def buf_to_le(buf):
# this function is from seanhn's tcmalloc_gdb
tmp = 0
for i in range(0, len(buf)):
tmp |= (buf[i] << i * 8)
return tmp
def get_page_size():
global cache_page_size
if not cache_page_size:
cache_page_size = pykd.pageSize()
return cache_page_size
def get_xul_version():
version = pykd.loadCStr(pykd.module('xul').offset('gToolkitVersion'))
return version
def get_arch():
if pykd.is64bitSystem():
return 'x86-64'
return 'x86'
def get_dword_size():
global cache_dword_size
if not cache_dword_size:
arch = get_arch()
if arch == 'x86':
cache_dword_size = 4
if arch == 'x86-64':
cache_dword_size = 8
return cache_dword_size
def int_size():
global cache_int_size
if not cache_int_size:
cache_int_size = 4
return cache_int_size
def offsetof(struct_name, member_name):
global cache_offsets
k = struct_name + "." + member_name
if k not in cache_offsets:
# speed up
s = 'mozglue!' + struct_name
cache_offsets[k] = pykd.typeInfo(s).fieldOffset(member_name)
return cache_offsets[k]
def sizeof(type_name):
global cache_sizes
k = type_name
if k not in cache_sizes:
type_name = 'mozglue!' + type_name # speedup
cache_sizes[k] = pykd.typeInfo(type_name).size()
return cache_sizes[k]
def get_value(symbol):
global cache_values
k = symbol
if k not in cache_values:
mozglue = pykd.module('mozglue')
symbol_addr = mozglue.offset(symbol)
cache_values[k] = pykd.ptrDWord(symbol_addr)
return cache_values[k]
def addressof(symbol):
mozglue = pykd.module('mozglue')
return mozglue.offset(symbol)
def eval_expr(expr):
sval = pykd.dbgCommand('?? %s' % (expr))
return sval
def execute(expr):
sval = pykd.dbgCommand(expr)
return sval
def read_memory(addr, size):
return buf_to_le(pykd.loadBytes(addr, size))
# xxx
def read_bytes(addr, size):
try:
return bytearray(pykd.loadBytes(addr, size))
except:
b = []
off = 0
while len(b) < size:
try:
b += pykd.loadBytes(addr + off, 0x1000)
except:
b += [0] * 0x1000
off += 0x1000
return b
def read_dwords(addr, size):
if get_dword_size() == 4:
return pykd.loadDWords(addr, size)
else:
return pykd.loadQWords(addr, size)
def read_dword(addr):
return read_dwords(addr, 1)[0]
def dword_from_buf(buf, off):
dword = 0
for i in range(0, get_dword_size()):
dword |= buf[off+i] << i * 8
return dword
def int_from_buf(buf, off):
dword = 0
for i in range(0, int_size()):
dword |= buf[off+i] << i * 8
return dword
def read_struct_member(buf, struct_name, member_name, size):
off = offsetof(struct_name, member_name)
val_bytes = buf[off:off+size]
if size > get_dword_size():
return val
val = 0
for i in range(0, size):
val |= (val_bytes[i] << i * 8)
return val
def search(start_addr, end_addr, dword):
search_expr = 's -[1]d %x %x %s'
results = []
search_str = search_expr % (start_addr, end_addr, dword)
out_str = pykd.dbgCommand(search_str)
str_results = out_str.split('\n')
for str_result in str_results:
if str_result.startswith('0x'):
results.append((str_result, start_addr))
return results
def modules_dict():
"""
modules_dict[objfile] = (start, end)
"""
modules_dict = {}
for module in pykd.getModulesList():
objfile = module.name()
start = module.begin()
end = module.end()
if objfile not in modules_dict:
modules_dict[objfile] = [(start, end),]
else:
modules_dict[objfile].append((start, end))
return modules_dict
# EOF