-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBitblazeTrace.py
197 lines (174 loc) · 6.09 KB
/
BitblazeTrace.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
#!/usr/bin/env python
import struct
import os
from capstone import *
md = Cs(CS_ARCH_X86, CS_MODE_32)
def read_format(_format, trace_file):
size = struct.calcsize(_format)
buf = trace_file.read(size)
result = struct.unpack(_format, buf)
return result, size
class OpType(object):
op_type = {30: ('TNone', 'TRegister', 'TMemLoc', 'TImmediate', 'TJump', 'TFloatRegister', 'TMemAddress')}
def __init__(self):
pass
class BitblazeReg(object):
reg_name = {30: {# 8 bits lower, upper
116: 'eax', 117: 'ecx', 118: 'edx', 119: 'ebx', 120: 'eax', 121: 'ecx', 122: 'edx', 123: 'ebx',
# 16bits
124: 'eax', 125: 'ecx', 126: 'edx', 127: 'ebx', 128:'esp', 129: 'ebp', 130: 'esi', 131: 'edi',
# 32bits
132: 'eax', 133: 'ecx', 134: 'edx', 135: 'ebx', 136:'esp', 137: 'ebp', 138: 'esi', 139: 'edi'
}
}
class ProcRecord30(object):
# typedef struct _process_record {
# char name[32];
# uint32_t pid;
# int n_mods;
# } ProcRecord;
def __init__(self, trace_file):
result, size = read_format('<32sII', trace_file)
self.name, self.pid, self.n_mods = result
self.size = size
self.mods = []
for mod_id in range(self.n_mods):
mod = ModRecord30(trace_file)
self.mods.append(mod)
self.size += mod.size
def __repr__(self):
repr_string = '''
Name: {}
PID: {}
n_mods: {}
'''.format(self.name, self.pid, self.n_mods)
return repr_string
class ModRecord30(object):
# typedef struct _module_record {
# char name[32];
# uint32_t base;
# uint32_t size;
# } ModuleRecord;
def __init__(self, trace_file):
result, size = read_format('<32sII', trace_file)
self.name, self.base, self.mod_size = result
self.size = size
class EntryHeader30(object):
# typedef struct {
# uint32_t addr;
# char rawbytes[15];
# ZL: 1 byte padding here (found in temu_trace.ml)
# OperandVal operand[5];
# OperandVal memregs[3][3];
# OperandVal esp;
# uint32_t eflags;
# uint32_t cc_op;
# uint32_t df;
# uint32_t hflags;
# uint32_t ldt;
# uint32_t gdt;
# uint32_t tr;
# uint32_t idt;
# } EntryHeader;
def __init__(self, trace_file):
result, size = read_format('<I16s', trace_file)
self.addr, self.rawbytes = result
self.size = size
(address, size, mnemonic, op_str) = md.disasm_lite(self.rawbytes, self.addr).next()
# truncate the rawbytes to just the current instruction
self.rawbytes = self.rawbytes[:size]
self.asm = '{} {}'.format(mnemonic, op_str)
self.tainted = False
self.ops = []
for _ in range(5):
op = OpVal30(trace_file)
if (op.tainted):
self.tainted = True
self.ops.append(op)
self.size += op.size
self.memregs = []
for x in range(3):
self.memregs.append([])
for _ in range(3):
op = OpVal30(trace_file)
self.memregs[x].append(op)
self.size += op.size
self.esp = OpVal30(trace_file)
self.size += op.size
result, size = read_format('<IIIIIIII', trace_file)
self.eflags, self.cc_op, self.df, self.hflags, self.ldt, self.gdt, self.tr, self.idt = result
self.size += size
def __repr__(self):
repr_string = '{} : {} : {}'.format(self.tainted, hex(self.addr), self.asm)
return repr_string
class OpVal30(object):
# typedef struct {
# enum OpType type;
# uint32_t addr;
# uint32_t value;
# uint64_t tainted;
# uint32_t origin[4];
# uint32_t offset[4];
# uint32_t source_id[4];
# char new_id[4];
# } OperandVal;
def __init__(self, trace_file):
result, size = read_format('<IIIQ', trace_file)
self.type, self.addr, self.value, self.tainted = result
self.type_str = OpType.op_type[30][self.type]
self.size = size
result, size = read_format('<4I', trace_file)
self.origin = result
self.size += size
result, size = read_format('<4I', trace_file)
self.offset = result
self.size += size
result, size = read_format('<4I', trace_file)
self.source_id = result
self.size += size
result, size = read_format('<4B', trace_file)
self.new_id = result
self.size += size
def __repr__(self):
repr_string = '''
Type: {}
Addr: {}
Value: {}
Tainted: {}
Origin: {}
Offset: {}
SourceID: {}
NewID: {}
'''.format(self.type, hex(self.addr), hex(self.value), self.tainted, self.origin, self.offset, self.source_id, self.new_id)
return repr_string
class BitblazeTrace(object):
def __init__(self, path):
# initialize the supported versions
self._read_header = {30: self._read_header_30}
self._read_procs = {30: self._read_procs_30}
self._read_instruction = {30: self._read_instruction_30}
self.trace_file = open(path, 'rb')
# get total size
self.trace_file.seek(0, os.SEEK_END)
self.trace_size = self.trace_file.tell()
self.trace_file.seek(0)
# get magicnumber and version
buf = self.trace_file.read(8)
self.magic_number, self.version = struct.unpack("II", buf)
self._read_header[self.version]()
self._read_procs[self.version]()
#self._read_procs[version]()
def _read_header_30(self):
buf = self.trace_file.read(4)
self.n_procs = struct.unpack("I", buf)[0]
def _read_procs_30(self):
self.procs = []
for proc_id in range(self.n_procs):
self.procs.append(ProcRecord30(self.trace_file))
def _read_instruction_30(self):
if (self.trace_file.tell() == self.trace_size):
return None
eh = EntryHeader30(self.trace_file)
return eh
def ReadInstruction(self):
return self._read_instruction[self.version]()