File size: 3,055 Bytes
eafe4d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c21998d
eafe4d4
c21998d
 
 
 
 
 
 
 
eafe4d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c21998d
 
eafe4d4
 
 
 
 
 
 
 
 
 
c21998d
 
eafe4d4
c21998d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/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


def get_all_dis(addrs=None):

    addrstr = ""
    if addrs is not None:
        addrstr = " ".join([f"--function-at {x}" for x in addrs])

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


    output = subprocess.check_output(f"/data/research/rose/install-latest/bin/bat-dis --no-insn-address --no-bb-cfg-arrows --color=off {ananame} 2>/dev/null", 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

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

parsed_data = []

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])

            parsed_data.append((addr,typ,name))

            #print(df.head())

all_addrs = set(x[0] for x in parsed_data)

all_dis = get_all_dis(all_addrs)

for addr, typ, name in parsed_data:

    dis = get_dis_from_all_dis(int(addr, 16))
    #objs.append((addr, typ, name, dis))
    if dis is None:
        dis = ""
        typ = "notfound"
    else:
        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)
    
df.to_csv(oname, index=False)

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