-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathanalysis.py
executable file
·157 lines (128 loc) · 3.59 KB
/
analysis.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/python3
# -*- coding: utf-8 -*-
import json
import r2pipe
import hashlib
import sqlite3 as sql
import time
import sys
def get_bblocks(object):
return object.get('BasicBlocks')
def count():
output = []
for file in result:
bb = 0
fn = 0
for i in result[file]:
try:
i['Block_SHA256']
except KeyError:
fn += 1
else:
bb += 1
output.append({"File": file, "BasicBlocks": bb, "Functions": fn})
output.sort(key=get_bblocks, reverse=True)
print(output)
def analysis(data, a):
if a == 0:
cr.execute("Select * FROM Functions WHERE Func_SHA256 = ?", (data,))
for f in cr:
try:
result[f[0]]
except KeyError:
result[f[0]] = []
result[f[0]].append({
'Func_SHA256': f[1]
})
else:
result[f[0]].append({
'Func_SHA256': f[1]
})
elif a == 1:
cr.execute("Select * FROM Blocks WHERE Block_SHA256 = ?", (data,))
for b in cr:
try:
result[b[0]]
except KeyError:
result[b[0]] = []
result[b[0]].append({
'Block_SHA256': b[1]
})
else:
result[b[0]].append({
'Block_SHA256': b[1]
})
def extract_func(file):
uniq_hashes = []
r2 = r2pipe.open(file)
r2.cmd("aaaa")
function_list = r2.cmdj("aflj")
counter = 1
while function_list is None:
time.sleep(0.7)
function_list = r2.cmdj("aflj")
counter += 1
if counter > 10:
print("exit")
sys.exit()
for function in function_list:
try:
op_string = r2.cmd("p8f" + str(function["size"])+ " @ " + function["name"])
op = op_string[:-1]
op = op.encode('utf-8')
op_hash = hashlib.sha256(op).hexdigest()
if op_hash not in uniq_hashes:
analysis(op_hash, 0)
uniq_hashes.append(op_hash)
except:
print("exit")
sys.exit()
r2.quit()
def extract_bblock(file):
r2 = r2pipe.open(file)
r2.cmd("aaaa")
uniq_hashes = []
blocks_data = r2.cmd('pdbj @@ *')
counter = 1
while blocks_data == '':
time.sleep(0.7)
blocks_data = r2.cmd('pdbj @@ *')
counter += 1
if counter > 10:
print("exit")
sys.exit()
blocks_data = blocks_data.split('\n')
blocks_data.remove('')
blocks = set()
for bb in blocks_data:
blocks.add(bb)
blocks = list(blocks)
for block in blocks:
try:
block = json.loads(block)
code = b''
for b in block:
if b['type'] != 'invalid':
code += bytes.fromhex(b['bytes'])
code_hash = hashlib.sha256(code).hexdigest()
if code_hash not in uniq_hashes:
analysis(code_hash, 1)
uniq_hashes.append(code_hash)
except:
print("exit")
sys.exit()
r2.quit()
def main():
FILE = sys.argv[1]
extract_func(FILE)
extract_bblock(FILE)
count()
#fd = open('result', 'w', encoding='utf-8')
#json.dump(result, fd, ensure_ascii=False, indent=4)
#fd.close()
if __name__ == '__main__':
result = {}
db = sql.connect('dataset.db')
cr = db.cursor()
main()
db.close()