File size: 2,771 Bytes
eafe4d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3

#import json
import pandas
import re
import os
import subprocess
import sys
from tempfile import NamedTemporaryFile

gname = sys.argv[1]
bname = sys.argv[2]
oname = sys.argv[3]

linere = re.compile(r"symbol\(([^,]+), (func|method), '([^)]+)'\)\.")


anafile = NamedTemporaryFile(prefix=os.path.basename(bname) + "_", suffix=".bat_ana")
ananame = anafile.name

subprocess.check_output("/data/research/rose/install-latest/bin/bat-ana --no-post-analysis -o %s %s 2>/dev/null" % (ananame, bname), shell=True)

def get_all_dis():

    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)
    output = re.sub(b' +', b' ', output)

    func_dis = {}
    last_func = None
    current_output = []

    for l in output.splitlines():
        if l.startswith(b";;; function 0x"):
            if last_func is not None:
                func_dis[last_func] = b"\n".join(current_output)
            last_func = int(l.split()[2], 16)
            current_output.clear()

        if not b";;" in l:
            current_output.append(l)

    if last_func is not None:
        if last_func in func_dis:
            print("Warning: Ignoring multiple functions at the same address")
        else:
            func_dis[last_func] = b"\n".join(current_output)

    return func_dis

def get_dis_from_all_dis(addr):
    if addr in all_dis:
        return all_dis[addr]
    else:
        return None

def get_dis(addr):
    try:
        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)
        output = re.sub(b' +', b' ', output)
        # print(output)
        return output
    except subprocess.CalledProcessError:
        return None

all_dis = get_all_dis()
#objs = []
df = pandas.DataFrame(columns=['Binary', 'Addr', 'Name', 'Type', 'Disassembly'])

with open(gname, "r") as f:
    for l in f:

        if linere.match(l):
            m = linere.match(l)
            addr = m.group(1)
            typ = m.group(2)
            name = m.group(3)
            #print([addr, typ, name])

            dis = get_dis_from_all_dis(int(addr, 16))
            #objs.append((addr, typ, name, dis))
            if dis is not None:
                dis = dis.decode("utf8")
                df = df.append({'Binary': bname, 'Addr': addr, 'Name': name, 'Type': typ, 'Disassembly': dis}, ignore_index=True)
            
            if False:
                df.to_csv(oname, index=False)
            #print(df.head())
    
df.to_csv(oname, index=False)

#with open(jname, "w") as f:
#    output = {"file": bname, "d": objs}
#    json.dump(output, f)