-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrocorruption_loader.py
More file actions
377 lines (308 loc) · 10.7 KB
/
microcorruption_loader.py
File metadata and controls
377 lines (308 loc) · 10.7 KB
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
import angr
import cle
from construct import (
Byte,
Const,
Validator,
Select,
GreedyRange,
NullTerminated,
StringEncoded,
GreedyBytes,
Struct,
Optional,
FocusedSeq,
Check,
len_,
this,
Computed,
Adapter,
ListContainer,
)
from angr_platforms.msp430 import lift_msp430, simos_msp430
_MC_Address = Byte[4]
_MC_ByteVal = Byte[2]
class _IsHexValidator(Validator):
chars = set(b"abcdefABCDEF0123456789")
def _validate(self, obj, context, path):
return all(o in self.chars for o in obj)
_MC_HexAddress = _IsHexValidator(_MC_Address)
_MC_HexByteVal = _IsHexValidator(_MC_ByteVal)
class _BigEndHexAdapter(Adapter):
def _decode(self, obj, context, path):
return int("".join(chr(x) for x in obj), 16)
def _encode(self, obj, context, path):
return ("%02x" % obj).encode("ascii")
_MC_BigEndAddress = _BigEndHexAdapter(_MC_HexAddress)
_MC_BigEndByte = _BigEndHexAdapter(_MC_HexByteVal)
_NewLine = Select(Const(b"\x0d\x0a"), Const(b"\x0a"))
_Spaces = GreedyRange(Const(b" "))
TerminatedString = lambda term, encoding="utf8": StringEncoded(
NullTerminated(GreedyBytes, term=bytes(term, encoding)), encoding
)
TerminatedStringOrEOF = lambda term, encoding="utf8": StringEncoded(
NullTerminated(GreedyBytes, term=bytes(term, encoding), require=False), encoding
)
class _RemoveWindowsNewLine(Adapter):
def _decode(self, obj, ctx, path):
if len(obj) > 0 and obj[-1] == "\r":
obj = obj[:-1]
return obj
def _encode(self, obj, ctx, path):
return obj
_LinuxNewLineTerminatedString = lambda encoding="utf8": TerminatedString("\n", encoding)
_LinuxNewLineTerminatedStringOrEOF = lambda encoding="utf8": TerminatedStringOrEOF(
"\n", encoding
)
NewLineTerminatedString = lambda encoding="utf8": _RemoveWindowsNewLine(
_LinuxNewLineTerminatedString(encoding)
)
NewLineTerminatedStringOrEOF = lambda encoding="utf8": _RemoveWindowsNewLine(
_LinuxNewLineTerminatedStringOrEOF(encoding)
)
_MC_BytePair = Struct(
"b1" / _MC_BigEndByte, "b2" / Optional(_MC_BigEndByte), Const(b" ")
)
class _FlattenByteList(Adapter):
@staticmethod
def _mc_get_byte_pair_as_list(obj):
result = []
result.append(obj.b1)
if obj.b2 is not None:
result.append(obj.b2)
return result
def _decode(self, obj, context, path):
result = ListContainer()
for o in obj:
result.extend(self._mc_get_byte_pair_as_list(o))
return result
def _encode(self, obj, context, path):
raise NotImplementedError()
_MC_ByteList = _FlattenByteList(GreedyRange(_MC_BytePair + _Spaces))
_MC_Dump_Line = Struct(
"address" / _MC_BigEndAddress,
Const(b":"),
_Spaces,
"bytevals" / _MC_ByteList,
Check(len_(this.bytevals) > 0),
Check(len_(this.bytevals) <= 16),
NewLineTerminatedStringOrEOF(),
)
_MC_Section_End = Struct(
"address" / _MC_BigEndAddress, Const(b":"), _Spaces, Const(b"*"), _Spaces, _NewLine
)
_MC_Section = Struct(
"lines" / GreedyRange(_MC_Dump_Line),
Check(len_(this.lines) > 0),
Optional(_MC_Section_End),
"address" / Computed(this.lines[0].address),
"length"
/ Computed(
this.lines[-1].address - this.lines[0].address + len_(this.lines[-1].bytevals)
),
)
MC_Dump_Parser = Struct("sections" / GreedyRange(_MC_Section))
class MC_Loader(cle.Backend):
def __init__(
self,
path,
binary_stream,
safe_area=-1,
explicit_sections=False,
*args,
**kwargs,
):
super(MC_Loader, self).__init__(
path,
binary_stream=binary_stream,
arch="msp430",
entry_point=0x4400,
*args,
**kwargs,
)
if not explicit_sections:
self._load_from_initial_state(path, safe_area)
else:
self._load_explicit_sections(path)
def _load_from_initial_state(self, path, safe_area):
parsed = MC_Dump_Parser.parse_file(path)
self.arch.bits = 32
self._min_addr = 0x0
self._max_addr = 0xFFFF
self.memory.add_backer(0, b"\x00" * 0x10000)
for section in parsed.sections:
print(
"Loading data with 0x%0x bytes at address 0x%04x"
% (section.length, section.address)
)
self.memory.store(section.address, self._make_section(section))
# This is the return from the interrupt: might be more nicely done with
# a syscall or hook? It doesn't show up in the memory dump, so we add it
# in manually here.
self.memory.store(0x10, b"\x30\x41")
def _load_explicit_sections(self, path):
parsed = MC_Dump_Parser.parse_file(path)
self.arch.bits = 16
self._max_addr = 0xFFFF
self._min_addr = 0x0
for section in parsed.sections:
print(
"Loading data with 0x%0x bytes at address 0x%04x"
% (section.length, section.address)
)
self.memory.add_backer(section.address, b"\x00" * section.length)
self.memory.store(section.address, self._make_section(section))
self.memory.add_backer(0x10, b"\x00" * 16)
self.memory.store(0x10, b"\x30\x41")
def make_symbols(self, disassembly_path):
print("parsing")
parsed = MC_Disassembly_Parser.parse_file(disassembly_path)
for sym_location in parsed.symbols:
self._make_symbol(sym_location.address, sym_location.symbol)
def _make_symbol(self, address, name):
if name in self._symbol_cache:
return
print(f"Making {name} at {address:x}")
new_sym = angr.cle.backends.symbol.Symbol(
owner=self,
name=name,
relative_addr=address,
size=2,
sym_type=angr.cle.backends.symbol.SymbolType.TYPE_FUNCTION,
)
self._symbol_cache[name] = new_sym
def get_symbol(self, name):
return self._symbol_cache.get(name, None)
@staticmethod
def _make_section(section):
bytevals = b""
for line in section.lines:
bytevals += bytes(line.bytevals)
return bytevals
@property
def max_addr(self):
return self._max_addr
@property
def min_addr(self):
return self._min_addr
@staticmethod
def is_compatible(stream):
stream.seek(0)
parsed = MC_Dump_Parser.parse_stream(stream)
stream.seek(0)
return len(parsed.sections) > 0
angr.cle.register_backend("microcorruption", MC_Loader)
def mc_project(
path,
disassembly_path=None,
hook_standard=True,
explicit_sections=False,
safe_area=-1,
*args,
**kwargs,
):
"""Load and return a microcorruption angr project.
Arguments:
path {Str} -- Path to the microcorruption memory dump file.
Keyword Arguments:
disassembly_path {Str} -- If given, path to the disassembly file for hooking symbols. (default: {None})
hook_standard {Bool} -- Hooks getsn, puts, and __stop_progExec__ with angr_platforms implementations if True. (default: {True})
explicit_sections {Bool} -- If set to True, only create backed memory for the bytes that are explicitly specified in the memory dump. (default: {False})
safe_area {int} - If given, an area of 0x200 bytes in memory which angr can safely use for bookkeeping. (default: {-1})
Returns:
angr.Project -- A loaded project.
"""
# Create the project
proj = angr.Project(
path,
*args,
main_opts={
"backend": "microcorruption",
"explicit_sections": explicit_sections,
"safe_area": safe_area,
},
load_options={"rebase_granularity": 8},
**kwargs,
)
# Set the number of bits in the architecture back to 16: see the hack in
# the MC_Loader __init__ function above.
proj.loader.main_object.arch.bits = 16
# If we have a path to a disassembly file, parse it and possibly do some
# hooking.
if disassembly_path is not None:
if hook_standard:
hook_mc_symbols(disassembly_path, proj)
else:
parse_symbols(proj, disassembly_path)
return proj
_simprocs = {
"puts": simos_msp430.MCputs,
"getsn": simos_msp430.MCgetsn,
"__stop_progExec__": simos_msp430.MCstopexec,
"strcpy": angr.SIM_PROCEDURES["libc"]["strcpy"],
"memset": angr.SIM_PROCEDURES["libc"]["memset"],
}
_MC_Symbol = Select(
FocusedSeq("name", Const(b"<"), "name" / TerminatedString(">")),
FocusedSeq("name", Const(b"."), "name" / TerminatedString(":")),
)
_MC_Symbol_Line = Struct(
"address" / _MC_BigEndAddress,
_Spaces,
"symbol" / _MC_Symbol,
NewLineTerminatedStringOrEOF(),
)
_MC_Instruction = FocusedSeq(
"i",
"i" / GreedyRange(FocusedSeq("x", "x" / _MC_BigEndAddress, Const(b" "))),
Check(lambda ctx: (len(ctx.i) > 0 and len(ctx.i) < 4)),
)
_MC_Instruction_Line = Struct(
"address" / _MC_BigEndAddress,
Const(b":"),
_Spaces,
"disassembly"
/ Struct(
"instruction_bytes" / _MC_Instruction,
_Spaces,
"disassembly" / NewLineTerminatedStringOrEOF(),
),
)
_MC_String_Line = Struct(
"address" / _MC_BigEndAddress,
Const(b":"),
_Spaces,
Const(b'"'),
"string" / TerminatedString('"'),
NewLineTerminatedStringOrEOF(),
)
_MC_Bad_Line = "badline" / NewLineTerminatedString()
_MC_Disassembly_Line = Select(
_MC_Symbol_Line, _MC_Instruction_Line, _MC_String_Line, _MC_Bad_Line
)
MC_Disassembly_Parser = Struct(
"lines" / GreedyRange(_MC_Disassembly_Line),
"symbols" / Computed(lambda ctx: [l for l in ctx.lines if hasattr(l, "symbol")]),
"disassembly"
/ Computed(lambda ctx: [l for l in ctx.lines if hasattr(l, "disassembly")]),
"strings" / Computed(lambda ctx: [l for l in ctx.lines if hasattr(l, "string")]),
)
def parse_symbols(proj, disassembly_path):
proj.loader.main_object.make_symbols(disassembly_path)
def get_default_hooks():
"""Get the default hooking map of symbols to hook implementations.
This is the default mapping of symbol strings to SimProcedure sub-classes
which will be hooked by hook_mc_symbols. If you want to use different
hooks, modify what this function returns then pass it to hook_mc_symbols.
Returns:
Dictionary -- Maps strings to SimProcedure implementations.
"""
return _simprocs.copy()
def hook_mc_symbols(disassembly_path, angr_project, simprocs=None):
parse_symbols(angr_project, disassembly_path)
if simprocs is None:
simprocs = _simprocs
for sym_name in simprocs.keys():
print(f"Hooking {sym_name} with {simprocs[sym_name]}: ", end="")
success = angr_project.hook_symbol(sym_name, simprocs[sym_name]())
print(f"{success}")