This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 399
/
disassembler.py
382 lines (295 loc) · 12.8 KB
/
disassembler.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
# Rekall Memory Forensics
# Copyright (C) 2012 Michael Cohen <scudette@gmail.com>
# Copyright 2013 Google Inc. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Provides the primitives needed to disassemble code using capstone,
unless it's not available on the system, then the code falls back to
using diStorm3.
"""
# This stuff is just here to make pyinstaller pick up on it. Due to the way
# distorm3 uses ctypes, pyinstaller misses the imports. Note that the following
# patch should also be applied to distorm3/__init__.py to support freezing:
#
# # Guess the DLL filename and load the library.
# if getattr(sys, "frozen", None):
# _distorm_path = '.'
# else:
# _distorm_path = split(__file__)[0]
try:
from ctypes import cdll
cdll.LoadLibrary("distorm3.dll")
except Exception:
pass
try:
import capstone
except Exception:
pass
# For now we still always use distorm3.
import distorm3
import re
import binascii
from rekall import plugin
from rekall import obj
from rekall import testlib
class Disassembler(object):
__abstract = True
def __init__(self, mode):
self.mode = mode
def disasm(self, data, offset):
""" Starts disassembly of data """
def decode(self, insn):
""" Decodes the current instruction """
class Capstone(Disassembler):
def __init__(self, mode):
super(Capstone, self).__init__(mode)
if self.mode == "I386":
self.cs = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_32)
elif self.mode == "AMD64":
self.cs = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
elif self.mode == "MIPS":
self.cs = capstone.Cs(capstone.CS_ARCH_MIPS, capstone.CS_MODE_32 +
capstone.CS_MODE_BIG_ENDIAN)
else:
raise NotImplementedError(
"No disassembler available for this arch.")
def disasm(self, data, offset):
return self.cs.disasm(data, int(offset))
def decode(self, insn):
instruction = Instruction(
"%s %s" % (insn.mnemonic, insn.op_str))
hexdump = unicode(binascii.hexlify(insn.bytes))
return insn.address, insn.size, instruction, hexdump
class Distorm3(Disassembler):
def __init__(self, mode):
super(Distorm3, self).__init__(mode)
if self.mode == "I386":
self.distorm_mode = distorm3.Decode32Bits
elif self.mode == "AMD64":
self.distorm_mode = distorm3.Decode64Bits
else:
raise NotImplementedError(
"No disassembler available for this arch.")
def disasm(self, data, offset):
return distorm3.DecodeGenerator(
int(offset), data, self.distorm_mode)
def decode(self, insn):
(offset, size, instruction, hexdump) = insn
instruction = Instruction(instruction)
hexdump = unicode(hexdump)
return offset, size, instruction, hexdump
class DisasmFactory(object):
@classmethod
def get(cls, mode):
return Distorm3(mode)
try:
return Capstone(mode)
except Exception:
pass
return Distorm3(mode)
class Instruction(unicode):
"""A Decoded instruction."""
class Disassemble(plugin.Command):
"""Disassemble the given offset."""
__name = "dis"
@classmethod
def args(cls, parser):
super(Disassemble, cls).args(parser)
parser.add_argument(
"offset", type="SymbolAddress",
help="An offset to disassemble. This can also be the name of "
"a symbol with an optional offset. For example: "
"tcpip!TcpCovetNetBufferList.")
parser.add_argument("-a", "--address_space", default=None,
help="The address space to use.")
parser.add_argument(
"-l", "--length", type="IntParser",
help="The number of instructions (lines) to disassemble.")
parser.add_argument(
"-e", "--end", default=None, type="IntParser",
help="The end address to disassemble up to.")
parser.add_argument(
"--mode", default="auto", choices=["auto", "I386", "AMD64", "MIPS"],
type="Choices",
help="Disassemble Mode (AMD64 or I386). Defaults to profile arch.")
parser.add_argument(
"--suppress_headers", default=False, type="Boolean",
help="If set we do not write table headers.")
parser.add_argument(
"--branch", default=False, type="Boolean",
help="If set we follow all branches to cover all code.")
def __init__(self, offset=0, address_space=None, length=None, end=None,
mode="auto", suppress_headers=False, branch=False,
**kwargs):
super(Disassemble, self).__init__(**kwargs)
load_as = self.session.plugins.load_as(session=self.session)
self.address_space = load_as.ResolveAddressSpace(address_space)
resolver = self.session.address_resolver
if resolver:
offset = resolver.get_address_by_name(offset)
# Normalize the offset to an address.
offset = obj.Pointer.integer_to_address(offset)
self.offset = offset
self.length = length
self.end = end
# All the visited addresses (for branch analysis).
self._visited = set()
self._visited_count = 0
self.follow_branches = branch
self.suppress_headers = suppress_headers
if mode == "auto":
mode = self.session.profile.metadata("arch") or "I386"
self.dis = DisasmFactory.get(mode)
def disassemble(self, offset, depth=0):
"""Disassemble the number of instructions required.
Yields:
A tuple of (Address, Opcode, Instructions).
"""
# Disassemble the data one page at the time.
while 1:
# The start of the disassembler buffer.
buffer_offset = obj.Pointer.integer_to_address(offset)
# By default read 2 pages.
data = self.address_space.read(buffer_offset, 0x2000)
for insn in self.dis.disasm(data, int(offset)):
offset, size, instruction, hexdump = self.dis.decode(insn)
if offset in self._visited:
return
yield depth, offset, size, hexdump, instruction
self._visited_count += 1
if self.follow_branches:
self._visited.add(offset)
# If the user asked for full branch analysis we follow all
# branches. This gives us full code coverage for a function - we
# just disassemble until the function exists from all branches.
if self.follow_branches:
# A return stops this branch.
if instruction.startswith("RET"):
return
m = self.BRANCH_REFERENCE.match(instruction)
if m:
# Start disassembling the branch. When the branch is
# exhausted we resume disassembling the continued
# branch.
for x in self.disassemble(
int(m.group(2), 16), depth=depth+1):
yield x
# A JMP stops disassembling this branch.
if instruction.startswith("JMP"):
return
# Exit condition can be specified by length.
if (self.length is not None and
self._visited_count > self.length):
return
# Exit condition can be specified by end address.
if self.end and offset > self.end:
return
# If we disassemble past one page, we read another two
# pages. This guarantees that we have enough data for full
# instructions.
if offset - buffer_offset > 0x1000:
break
def format_indirect(self, operand):
target = self.session.profile.Object(
"address", offset=operand, vm=self.address_space).v()
resolver = self.session.address_resolver
target_name = ", ".join(resolver.format_address(target))
operand_name = ", ".join(resolver.format_address(operand))
if target_name:
return "0x%x %s -> %s" % (target, operand_name, target_name)
else:
return "0x%x %s" % (target, operand_name)
BRANCH_REFERENCE = re.compile("(J|B)[^ ]{1,2} (0x[^ ]+)")
SIMPLE_REFERENCE = re.compile("0x[0-9a-fA-F]+$")
INDIRECT_REFERENCE = re.compile(r"\[(0x[0-9a-fA-F]+)\]")
RIP_REFERENCE = re.compile(r"\[RIP([+-]0x[0-9a-fA-F]+)\]")
def find_reference(self, offset, size, instruction):
match = self.RIP_REFERENCE.search(instruction)
if match:
operand = int(match.group(1), 16)
return self.format_indirect(offset + size + operand) or ""
match = self.INDIRECT_REFERENCE.search(instruction)
if match:
operand = int(match.group(1), 16)
return self.format_indirect(operand) or ""
match = self.SIMPLE_REFERENCE.search(instruction)
if match:
operand = int(match.group(0), 16)
resolver = self.session.address_resolver
return ", ".join(resolver.format_address(operand))
return ""
def render(self, renderer):
"""Disassemble code at a given address.
Disassembles code starting at address for a number of bytes
given by the length parameter (default: 128).
Note: This feature requires capstone, available at
http://www.capstone-engine.org/
or distorm, available at
http://www.ragestorm.net/distorm/
The mode is '32bit' or '64bit'. If not supplied, the disasm
mode is taken from the profile.
"""
# If length nor end are specified only disassemble one pager output.
if self.end is None and self.length is None:
self.length = self.session.GetParameter("paging_limit") - 5
# If we are doing branch analysis we can not suspend this plugin. We
# must do everything all the time.
if self.follow_branches:
self.length = self.end = None
renderer.table_header(
[dict(type="TreeNode", name="Address", cname="cmd_address",
child=dict(style="address")),
('Rel', "relative_address", '[addr]'),
('Op Codes', "opcode", '<20'),
('Instruction', "instruction", '<30'),
('Comment', "comment", "")],
suppress_headers=self.suppress_headers)
offset = 0
self._visited.clear()
self._visited_count = 0
for depth, offset, size, hexdump, instruction in self.disassemble(
self.offset):
relative = None
comment = ""
resolver = self.session.address_resolver
if resolver:
(f_offset, f_names) = resolver.get_nearest_constant_by_address(
offset)
f_name = ", ".join(f_names)
self.session.report_progress(
"Disassembled %s: 0x%x", f_name, offset)
if offset - f_offset == 0:
renderer.table_row("------ %s ------\n" % f_name,
annotation=True)
comment = self.find_reference(offset, size, instruction)
if offset - f_offset < 0x1000:
relative = offset - f_offset
renderer.table_row(
offset, relative, hexdump,
instruction, Instruction(comment), depth=depth)
# Continue from where we left off when the user calls us again with the
# v() plugin.
self.offset = offset
class TestDisassemble(testlib.SimpleTestCase):
PARAMETERS = dict(
# We want to test symbol discovery via export table detection so turn it
# on.
commandline=("dis -l %(length)s %(func)s "
"--name_resolution_strategies Export"),
func=0x805031be,
length=20
)