Datasets:
Edward J. Schwartz
commited on
Commit
·
eafe4d4
1
Parent(s):
6abf84e
Add generation scripts
Browse files- scripts/gen-training.bash +5 -0
- scripts/gen-training.py +93 -0
scripts/gen-training.bash
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
4 |
+
|
5 |
+
find . -name '*.ground' | xargs -P 16 -I foo bash -c 'F=$(dirname foo)/$(basename foo .ground); echo $F; python3 '"$SCRIPT_DIR"'/gen-training.py $F{.ground,,.csv}'
|
scripts/gen-training.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/python3
|
2 |
+
|
3 |
+
#import json
|
4 |
+
import pandas
|
5 |
+
import re
|
6 |
+
import os
|
7 |
+
import subprocess
|
8 |
+
import sys
|
9 |
+
from tempfile import NamedTemporaryFile
|
10 |
+
|
11 |
+
gname = sys.argv[1]
|
12 |
+
bname = sys.argv[2]
|
13 |
+
oname = sys.argv[3]
|
14 |
+
|
15 |
+
linere = re.compile(r"symbol\(([^,]+), (func|method), '([^)]+)'\)\.")
|
16 |
+
|
17 |
+
|
18 |
+
anafile = NamedTemporaryFile(prefix=os.path.basename(bname) + "_", suffix=".bat_ana")
|
19 |
+
ananame = anafile.name
|
20 |
+
|
21 |
+
subprocess.check_output("/data/research/rose/install-latest/bin/bat-ana --no-post-analysis -o %s %s 2>/dev/null" % (ananame, bname), shell=True)
|
22 |
+
|
23 |
+
def get_all_dis():
|
24 |
+
|
25 |
+
output = subprocess.check_output("/data/research/rose/install-latest/bin/bat-dis --no-bb-cfg-arrows --color=off %s 2>/dev/null" % (ananame), shell=True)
|
26 |
+
output = re.sub(b' +', b' ', output)
|
27 |
+
|
28 |
+
func_dis = {}
|
29 |
+
last_func = None
|
30 |
+
current_output = []
|
31 |
+
|
32 |
+
for l in output.splitlines():
|
33 |
+
if l.startswith(b";;; function 0x"):
|
34 |
+
if last_func is not None:
|
35 |
+
func_dis[last_func] = b"\n".join(current_output)
|
36 |
+
last_func = int(l.split()[2], 16)
|
37 |
+
current_output.clear()
|
38 |
+
|
39 |
+
if not b";;" in l:
|
40 |
+
current_output.append(l)
|
41 |
+
|
42 |
+
if last_func is not None:
|
43 |
+
if last_func in func_dis:
|
44 |
+
print("Warning: Ignoring multiple functions at the same address")
|
45 |
+
else:
|
46 |
+
func_dis[last_func] = b"\n".join(current_output)
|
47 |
+
|
48 |
+
return func_dis
|
49 |
+
|
50 |
+
def get_dis_from_all_dis(addr):
|
51 |
+
if addr in all_dis:
|
52 |
+
return all_dis[addr]
|
53 |
+
else:
|
54 |
+
return None
|
55 |
+
|
56 |
+
def get_dis(addr):
|
57 |
+
try:
|
58 |
+
output = subprocess.check_output("/data/research/rose/install-latest/bin/bat-dis --no-bb-cfg-arrows --function %s --color=off %s 2>/dev/null | fgrep -v ';;'" % (addr, ananame), shell=True)
|
59 |
+
output = re.sub(b' +', b' ', output)
|
60 |
+
# print(output)
|
61 |
+
return output
|
62 |
+
except subprocess.CalledProcessError:
|
63 |
+
return None
|
64 |
+
|
65 |
+
all_dis = get_all_dis()
|
66 |
+
#objs = []
|
67 |
+
df = pandas.DataFrame(columns=['Binary', 'Addr', 'Name', 'Type', 'Disassembly'])
|
68 |
+
|
69 |
+
with open(gname, "r") as f:
|
70 |
+
for l in f:
|
71 |
+
|
72 |
+
if linere.match(l):
|
73 |
+
m = linere.match(l)
|
74 |
+
addr = m.group(1)
|
75 |
+
typ = m.group(2)
|
76 |
+
name = m.group(3)
|
77 |
+
#print([addr, typ, name])
|
78 |
+
|
79 |
+
dis = get_dis_from_all_dis(int(addr, 16))
|
80 |
+
#objs.append((addr, typ, name, dis))
|
81 |
+
if dis is not None:
|
82 |
+
dis = dis.decode("utf8")
|
83 |
+
df = df.append({'Binary': bname, 'Addr': addr, 'Name': name, 'Type': typ, 'Disassembly': dis}, ignore_index=True)
|
84 |
+
|
85 |
+
if False:
|
86 |
+
df.to_csv(oname, index=False)
|
87 |
+
#print(df.head())
|
88 |
+
|
89 |
+
df.to_csv(oname, index=False)
|
90 |
+
|
91 |
+
#with open(jname, "w") as f:
|
92 |
+
# output = {"file": bname, "d": objs}
|
93 |
+
# json.dump(output, f)
|