|
|
|
|
|
|
|
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) |
|
|
|
return output |
|
except subprocess.CalledProcessError: |
|
return None |
|
|
|
|
|
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) |
|
|
|
|
|
parsed_data.append((addr,typ,name)) |
|
|
|
|
|
|
|
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)) |
|
|
|
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) |
|
|
|
df.to_csv(oname, index=False) |
|
|
|
|
|
|
|
|
|
|