This repository was archived by the owner on Mar 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpost_process.py
More file actions
351 lines (285 loc) · 12.7 KB
/
post_process.py
File metadata and controls
351 lines (285 loc) · 12.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
# Copyright (C) 2019 Intel Corporation
#
# SPDX-License-Identifier: BSD-3-Clause
import subprocess
import os
import os.path
import re
import argparse
import json
import operator
def load_tainted_address_set(filename):
tainted_addr_set = set()
with open(filename, 'r') as f:
for full_line in f:
line = full_line.strip()
if line == "TraceStart" or line == "TraceEnd":
pass
else:
colon_index = line.find(':')
addr = line[0:colon_index]
operands = line[colon_index:]
arrow_index = operands.find('-->')
read_operands = operands[0:arrow_index]
# write_operands = operands[arrow_index:]
if (read_operands.find(".T") >= 0):
tainted_addr_set.add(addr)
return tainted_addr_set
def parse_line(line):
if line.startswith("IP:"):
return line[4:]
elif line.startswith("MemRead:") or line.startswith("MemWrite:"):
first_space_index = line.find(' ')
second_space_index = line.find(' ', first_space_index + 1)
return line[first_space_index+1: second_space_index]
elif line == "TraceMemStart":
return None
elif line == "TraceMemEnd":
return None
elif line == "TraceIPStart":
return None
elif line == "TraceIPEnd":
return None
else:
print("Unknown line: %s" % line)
return None
def parse_diff_for_mem(diff_str):
left_addr_list = []
right_addr_list = []
start_index = 0
last_line = False
while not last_line:
end_index = diff_str.find("\n", start_index)
if end_index == -1:
line = diff_str[start_index:]
last_line = True
else:
line = diff_str[start_index: end_index]
start_index = end_index + 1
if line == "":
pass
elif line.startswith('@@') or line.startswith('++') or line.startswith('--'):
pass
elif line.startswith('-'):
left_addr_list.append(parse_line(line[1:]))
elif line.startswith('+'):
right_addr_list.append(parse_line(line[1:]))
return (left_addr_list, right_addr_list)
def parse_diff_for_ip(diff_str):
left_addr_list = []
right_addr_list = []
start_index = 0
last_line = False
top_context = True
while not last_line:
end_index = diff_str.find("\n", start_index)
if end_index == -1:
line = diff_str[start_index:]
last_line = True
else:
line = diff_str[start_index: end_index]
start_index = end_index + 1
if line == "":
pass
elif line.startswith('@@') or line.startswith('++') or line.startswith('--'):
top_context = True
elif line.startswith('-'):
left_addr_list.append(parse_line(line[1:]))
top_context = False
elif line.startswith('+'):
right_addr_list.append(parse_line(line[1:]))
top_context = False
else:
if top_context:
left_addr_list.append(parse_line(line[1:]))
right_addr_list.append(parse_line(line[1:]))
return (left_addr_list, right_addr_list)
def diff_files_for_mem(filename1, filename2):
cmd = ["diff", "--speed-large-files", "-U0", filename1, filename2]
proc = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE)
return parse_diff_for_mem(proc.stdout.decode("utf-8"))
def diff_files_for_ip(filename1, filename2):
cmd = ["diff", "--speed-large-files", "-U1", filename1, filename2]
proc = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE)
return parse_diff_for_ip(proc.stdout.decode("utf-8"))
def lookup_symbol(symtab, addr):
for (start, size, name, image_address, image_name) in symtab:
if (addr >= start) and (addr < start + size):
return (name, addr - image_address, image_name)
return ("???", 0, "")
def filter_trace(trace_filename, taint_filename, out_filename):
tainted_addr_set = load_tainted_address_set(taint_filename)
with open(out_filename, 'w') as f_out:
with open(trace_filename, 'r') as f_in:
for line in f_in:
addr = parse_line(line.strip())
if addr is not None and addr in tainted_addr_set:
f_out.write(line)
def analyze_all_files_for_mem(trace_path, taint_path, symtab,
verbose, result_file):
ref_filename = "trace0"
ref_filepath = os.path.join(trace_path, ref_filename)
ref_taint_filename = "taint0"
ref_taint_filepath = os.path.join(taint_path, ref_taint_filename)
if verbose:
print("Filtering memory reference trace...")
ref_temp_filepath = os.path.join(trace_path, "temp0")
filter_trace(ref_filepath, ref_taint_filepath, ref_temp_filepath)
addr_set = set()
temp_filepath = os.path.join(trace_path, "tempX")
with os.scandir(trace_path) as scanner:
for item in scanner:
if item.is_file() and item.name != ref_filename and item.name.startswith("trace"):
if verbose:
print("Filtering memory %s..." % item.name)
trace_filepath = os.path.join(trace_path, item.name)
trace_index = int(item.name[5:])
taint_filename = "taint%d" % trace_index
taint_filepath = os.path.join(taint_path, taint_filename)
filter_trace(trace_filepath, taint_filepath, temp_filepath)
if verbose:
print("Diffing memory %s..." % item.name)
(left_tainted_addr_list,
right_tainted_addr_list) = diff_files_for_mem(ref_temp_filepath, temp_filepath)
if len(left_tainted_addr_list) > 0 or len(right_tainted_addr_list) > 0:
for addr in left_tainted_addr_list:
dot_index = addr.find('.')
addr_set.add(int(addr[0:dot_index], 16))
for addr in right_tainted_addr_list:
dot_index = addr.find('.')
addr_set.add(int(addr[0:dot_index], 16))
print()
# Cleanup temp files (temp0 and tempX)
if os.path.isfile(ref_temp_filepath):
os.remove(ref_temp_filepath)
if os.path.isfile(temp_filepath):
os.remove(temp_filepath)
if len(addr_set) > 0:
print("Addresses with tainted memory access differences:")
result_file.write("Addresses with tainted memory access differences:\n")
for addr in sorted(addr_set):
(sym_name, offset, image_name) = lookup_symbol(symtab, addr)
print("%X --> %s (%s @ %X)" % (addr, sym_name, image_name, offset))
result_file.write("%X --> %s (%s @ %X)\n" % (addr, sym_name, image_name, offset))
else:
print("No addresses with tainted memory access differences.")
result_file.write("No addresses with tainted memory access differences.\n")
print()
def analyze_all_files_for_ip(trace_path, taint_path, symtab, verbose, result_file):
ref_filename = "trace0"
ref_filepath = os.path.join(trace_path, ref_filename)
ref_taint_filename = "taint0"
ref_taint_filepath = os.path.join(taint_path, ref_taint_filename)
if verbose:
print("Filtering IP reference trace...")
ref_temp_filepath = os.path.join(trace_path, "temp0")
filter_trace(ref_filepath, ref_taint_filepath, ref_temp_filepath)
addr_set = set()
temp_filepath = os.path.join(trace_path, "tempX")
with os.scandir(trace_path) as scanner:
for item in scanner:
if item.is_file() and item.name != ref_filename and item.name.startswith("trace"):
if verbose:
print("Filtering IP %s..." % item.name)
trace_filepath = os.path.join(trace_path, item.name)
trace_index = int(item.name[5:])
taint_filename = "taint%d" % trace_index
taint_filepath = os.path.join(taint_path, taint_filename)
filter_trace(trace_filepath, taint_filepath, temp_filepath)
if verbose:
print("Diffing IP %s..." % item.name)
(left_tainted_addr_list,
right_tainted_addr_list) = diff_files_for_ip(ref_temp_filepath, temp_filepath)
if len(left_tainted_addr_list) > 0 or len(right_tainted_addr_list) > 0:
for addr in left_tainted_addr_list:
dot_index = addr.find('.')
addr_set.add(int(addr[0:dot_index], 16))
for addr in right_tainted_addr_list:
dot_index = addr.find('.')
addr_set.add(int(addr[0:dot_index], 16))
print()
# Cleanup temp files (temp0 and tempX)
if os.path.isfile(ref_temp_filepath):
os.remove(ref_temp_filepath)
if os.path.isfile(temp_filepath):
os.remove(temp_filepath)
if len(addr_set) > 0:
print("Addresses with tainted execution differences:")
result_file.write("Addresses with tainted execution differences:\n")
for addr in sorted(addr_set):
(sym_name, offset, image_name) = lookup_symbol(symtab, addr)
print("%X --> %s (%s @ %X)" % (addr, sym_name, image_name, offset))
result_file.write("%X --> %s (%s @ %X)\n" % (addr, sym_name, image_name, offset))
else:
print("No addresses with tainted execution differences.")
result_file.write("No addresses with tainted execution differences.\n")
print()
def parse_image_map(image_map_filename, verbose=False):
global_symtab = []
with open(image_map_filename, 'r') as image_map_file:
image_map = json.load(image_map_file)
for (image_name, image_offset_str) in image_map.items():
image_offset = int(image_offset_str, 16)
symtab = parse_symbols(image_name, offset=image_offset, verbose=verbose)
global_symtab.extend(symtab)
global_symtab.sort(key=operator.itemgetter(0))
return global_symtab
def parse_symbols(image_name, offset=0, verbose=False):
cmd = ["nm", "-nS", "--defined-only", image_name]
proc = subprocess.run(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
nm_output = proc.stdout.decode("utf-8")
if verbose:
print(proc.stderr.decode("utf-8"))
line_re = re.compile("([0-9a-f]+) ([0-9a-f]+) . (.+)")
symtab = []
for line in nm_output.split("\n"):
m = line_re.match(line)
if m is not None:
start = int(m.group(1), 16) + offset
size = int(m.group(2), 16)
name = m.group(3)
symtab.append((start, size, name, offset, image_name))
return symtab
def branch_check_file(filename):
addr_set = set()
line_re = re.compile(r"([0-9a-f]+)\.[0-9a-f]+\: \[(.+)\] --> \[(.+)\]")
with open(filename, 'r') as f_in:
for line in f_in:
m = line_re.match(line.strip())
if m is not None:
addr = int(m.group(1), 16)
read_operands = m.group(2)
write_operands = m.group(3)
if read_operands.find('.T') != -1 and write_operands.find('REG(rip)') != -1:
addr_set.add(addr)
return addr_set
def branch_check_all_files(taint_path, symtab, verbose):
addr_set = set()
with os.scandir(taint_path) as scanner:
for item in scanner:
if item.is_file():
file_addr_set = branch_check_file(os.path.join(taint_path, item.name))
addr_set.update(file_addr_set)
if len(addr_set) > 0:
print("Tainted branches:")
for addr in sorted(addr_set):
(sym_name, offset, image_name) = lookup_symbol(symtab, addr)
print("%X --> %s (%s @ %X)" % (addr, sym_name, image_name, offset))
else:
print("No tainted branches.")
print()
def main(verbose, branch_check, result_filename):
symtab = parse_image_map("image_map", verbose=verbose)
with open(result_filename, 'w') as result_file:
analyze_all_files_for_mem("memtrace", "taint", symtab, verbose, result_file)
analyze_all_files_for_ip("iptrace", "taint", symtab, verbose, result_file)
if branch_check:
branch_check_all_files("taint", symtab, verbose)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Process some traces.")
parser.add_argument("result_file", help="Name of output file to print to (as well as stdout).")
parser.add_argument("--verbose", default=False, action='store_true')
parser.add_argument("--branch", default=False, action='store_true',
help="Report any conditional branches that depend on secret data (experimental).")
args = parser.parse_args()
main(args.verbose, args.branch, args.result_file)