-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_variables.py
More file actions
101 lines (81 loc) · 4.12 KB
/
count_variables.py
File metadata and controls
101 lines (81 loc) · 4.12 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
import json
import delegator
from tqdm import tqdm
GHIDRAvarproblempath = './GHIDRAvarproblems' # path to file were to write function names of variable counting problems for GHIDRA
GHIDRAFOLDERPATH = './decomp_funcs_GHIDRA' # path to GHIDRA folder containing functions
angrFOLDERPATH = './decomp_funcs_angr' # path to angr folder containing functions
GHIDRAvaroutput = './varsGHIDRA.json' # JSON file to which you want to write GHIDRA var output to
angrvaroutput = './varsangr.json' # JSON file to which you want to write angr var output to
angrfunctionfilepath = './function-file angr.json' # path where output of filefunctioncorrangr.py is saved
GHIDRAfunctionfilepath = './function-file GHIDRA.json' # path where output of filefunctioncorrGHIDRA.py is saved
commonfunctionpath = './commonfunctions' # path to text file which contains a list of the name of commonly decompiled functions
GHIDRAdict = {}
angrdict = {}
'''
The purpose of this script is to find the number of variables in the unique functions of GHIDRA and angr
'''
def countGHIDRA():
c = delegator.run(f"joern --script joernscript.sc --params FolderPath='{GHIDRAFOLDERPATH}'") # output is string of filename and cpg.method(methodname).toJSON strings seperated by %
out_arr = c.out.split("%")
for i in range(1, len(out_arr), 2):
temp_path = out_arr[i-1]
temp_json = out_arr[i]
x = json.loads(temp_json)
name = temp_path.split("/")[-1] + "-vars"
try:
GHIDRAdict[name] = x[-1]['order'] #creates a dictionary with functionname + "-vars": # of variables
except:
with open(GHIDRAvarproblempath, "w") as GHIDRAvarproblems:
GHIDRAvarproblems.write(name)
GHIDRAdict[name] = 0
with open(GHIDRAvaroutput, "a") as GHIDRAjson:
json.dump(GHIDRAdict, GHIDRAjson)
def countangr():
c = delegator.run(f"joern --script joernscript.sc --params FolderPath='{angrFOLDERPATH}'") # output is string of filename and cpg.method(methodname).toJSON strings seperated by %
out_arr = c.out.split("%")
for i in range(1, len(out_arr), 2):
temp_path = out_arr[i-1]
temp_json = out_arr[i]
x = json.loads(temp_json)
name = temp_path.split("/")[-1] + "-vars"
try:
angrdict[name] = x[-1]['order']
except:
angrdict[name] = 0
with open(angrvaroutput, "a") as angrjson:
json.dump(angrdict, angrjson)
def process_all():
sumGHIDRA = 0
sumangr = 0
with open(GHIDRAvaroutput, "r") as GHIDRAjson:
GHIDRAvars = json.load(GHIDRAjson)
with open(angrvaroutput, "r") as angrjson:
angrvars = json.load(angrjson)
with open(angrfunctionfilepath, 'r') as angrff, open(GHIDRAfunctionfilepath, 'r') as GHIDRAff:
angrffdict =json.load(angrff)
GHIDRAffdict = json.load(GHIDRAff)
for value in GHIDRAffdict.values():
sumGHIDRA += GHIDRAvars[value + "-vars"]
for value in angrffdict.values():
sumangr += angrvars[value + "-vars"]
print("GHIDRA vars: " + str(sumGHIDRA))
print("angr vars: " + str(sumangr))
def process_common():
sumGHIDRA = 0
sumangr = 0
with open(GHIDRAvaroutput, "r") as GHIDRAjson:
GHIDRAvars = json.load(GHIDRAjson)
with open(angrvaroutput, "r") as angrjson:
angrvars = json.load(angrjson)
with open(angrfunctionfilepath, 'r') as angrff, open(GHIDRAfunctionfilepath, 'r') as GHIDRAff:
angrffdict =json.load(angrff)
GHIDRAffdict = json.load(GHIDRAff)
with open(commonfunctionpath, "r") as commonfunctions:
commonlist = commonfunctions.read().splitlines()
for filename in tqdm(commonlist):
filepathangr = angrffdict[filename]
filepathGHIDRA = GHIDRAffdict[filename]
sumGHIDRA += GHIDRAvars[filepathGHIDRA + "-vars"]
sumangr += angrvars[filepathangr + "-vars"]
print("GHIDRA vars: " + str(sumGHIDRA))
print("angr vars: " + str(sumangr))