-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_system.py
96 lines (80 loc) · 2.36 KB
/
run_system.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
import csv
from subprocess import call
import os
import pandas as pd
import re
PATH = "./elf"
cur_path = os.getcwd()
try:
call("cd ELF\ Miner && rm results.csv", shell=True)
except Exception as e:
print e
flag = 0
for i in os.listdir(PATH):
try:
file = os.path.join(os.path.join(cur_path, PATH), i)
call('objdump -M intel -D "' + file + '"' + ' > hello.txt', shell=True)
if os.stat("hello.txt").st_size == 0:
raise Exception("incorrect format")
print file
## ELF Miner
call("cd ELF\ Miner && python ELFMiner.py " + "'" + file + "' && cp results.csv ../final.csv && cd " + cur_path, shell=True)
## Syscalls
headers = []
with open('./Syscalls/features_less.txt', 'r') as f:
for l in f:
a = l.split(': ')[0]
headers.append(a)
df = pd.read_csv('final.csv')
if flag == 0:
for i in headers:
df[i] = 0
flag = 1
s = re.compile('<.+>')
symbols = re.compile('"\w+"')
calls = dict()
row = [0]*len(headers)
with open('hello.txt', 'r') as f:
for l in f:
if 'call ' in l.strip():
ind = l.strip().index('call ')
new_str = l.strip()[ind:]
m = re.findall(s, new_str)
if m:
new_str = m[0]
if (('Base' in new_str and 'Base+' not in new_str) or ('plt' in new_str)) and new_str in headers:
if new_str not in calls.keys():
calls[new_str] = 1;
else:
calls[new_str] += 1
for i in calls.keys():
row[headers.index(i)] = calls[i]
ind = df.shape[0]-1
for i, h in enumerate(headers):
df.loc[df.index[-1], h] = row[i]
## Opcodes
opcodes_list = ['mov', 'push', 'call', 'pop', 'cmp', 'jz', 'lea', 'test', 'jmp', 'add', 'jnz', 'retn', 'xor', 'and', 'bt', 'fdivp', 'fild', 'fstcw', 'imul', 'int', 'nop', 'pushf', 'rdtsc', 'sbb', 'setb', 'setle', 'shld', 'std', '(bad)']
headers = []
headers.extend(opcodes_list)
headers.append('labels')
if flag == 1:
for i in headers:
df[i] = 0
flag = 2
row = [0]*(len(headers)-1)
# row[0] = file
with open('hello.txt', 'r') as f:
for l in f:
if ':' in l:
# print l
for i, opcode in enumerate(opcodes_list):
if opcode in l:
row[i] += 1
row.append('?')
for i, h in enumerate(headers):
df.loc[df.index[-1], h] = row[i]
df.to_csv('final.csv', index=False)
call('cp final.csv ./ELF\ Miner/results.csv', shell=True)
except Exception as e:
print e
call('python postprocessing.py', shell=True)