-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
178 lines (149 loc) · 7.35 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import PySimpleGUI as sg
import predict_api
import predict_traffic
import numpy as np
import os
import hashlib
import _thread
from cuckoo_report_analyzer.move_report import move_1
from cuckoo_report_analyzer.split_reports import split_1
# sg.theme('Reddit') # Add a touch of color
sg.theme('Material1')
def file_hash(file_path: str, hash_method) -> str:
if not os.path.isfile(file_path):
print('文件不存在')
return ''
h = hash_method()
with open(file_path, 'rb') as f:
while True:
b = f.read(8192)
if not b:
break
h.update(b)
return h.hexdigest()
def get_hash(json_path: str) -> str:
return file_hash(json_path, hashlib.sha256)
def cuckoo_run(file_path):
cmd_submit = 'cuckoo submit ' + file_path
os.popen(cmd_submit)
report_path = ''
while not os.path.isfile(report_path):
continue
print('---report has been made---')
move_1()
split_1()
def draw_window() -> sg.Window:
BORDER_COLOR = '#C7D5E0'
DARK_HEADER_COLOR = 'white'
BPAD_TOP = ((20, 20), (20, 10))
BPAD_LEFT = ((20, 10), (0, 10))
BPAD_LEFT_INSIDE = (0, 10)
BPAD_RIGHT = ((10, 20), (10, 20))
top_banner = [[sg.Text('恶意代码检测', font='Any 20', background_color=DARK_HEADER_COLOR)]]
bottom_banner = [[sg.Text('日志文件:/xx/yy.csv', background_color=DARK_HEADER_COLOR)]]
top = [[sg.Text('请输入被检测文件名(e.g. xx.exe)', size=(50, 1), justification='c', pad=BPAD_TOP, font='Any 20')],
[sg.Text('file name: '), sg.InputText(key='file_name')]]
block_2 = [[sg.Text('动态获取运行报告', font='Any 20')],
[sg.T('')],
[sg.T('请输入被检测文件路径,生成报告时长预计1min')],
[sg.Text('file path: '),
sg.InputText(key='file_path'),
sg.FileBrowse(key='file_path_browse', target='file_path', file_types=(("exe files", "*.exe"),))],
[sg.Text('json file path: ')],
[sg.Text(key='json_file_path')],
[sg.Button('show json file path'), sg.Button('refresh_file'), sg.Button('submit_to_model')]]
block_3 = [[sg.Text('基于API调用序列,文件被判定为:', font='Any 20')],
[sg.Text(' ' * 5), sg.Text(key='api_result', font='Any 15')]]
# 流量处理的部分,后续添加,目前只实现直接对流量图片分类
# block_4 = [[sg.Text('流量处理&检测', font='Any 20')],
# [sg.T('')],
# [sg.T('请输入被检测流量文件路径')],
# [sg.Text('traffic file path: '), sg.InputText(key='traffic_path')],
# [sg.Button('submit_traffic'), sg.Button('Refresh_2')],
# [sg.Image(key='traffic_pic')],
# [sg.Text('基于会话流量,文件被判定为:', font='Any 20')],
# [sg.Text('BENIGN', key='traffic_result', font='Any 15')],
# ]
block_4 = [[sg.Text('流量处理&检测', font='Any 20')],
[sg.T('')],
[sg.T('请输入被检测流量文件路径')],
[sg.Text('traffic file path: '), sg.InputText(key='traffic_file_path')],
[sg.FileBrowse(key='traffic_browse', target='traffic_file_path', file_types=(("png files", "*.png"),)),
sg.Button('submit_traffic'), sg.Button('refresh_pic')],
[sg.Image(key='traffic_pic')],
[sg.Text('基于会话流量,文件被判定为:', font='Any 20')],
[sg.Text(' ' * 5), sg.Text(key='traffic_result', font='Any 15')],
]
layout = [
# [sg.Column(top_banner, size=(960, 40), pad=(0, 0), background_color=DARK_HEADER_COLOR)],
[sg.Column(top, size=(920, 90), pad=BPAD_TOP)],
[sg.Column([[sg.Column(block_2, size=(450, 210), pad=BPAD_LEFT_INSIDE)],
[sg.Column(block_3, size=(450, 90), pad=BPAD_LEFT_INSIDE)]], pad=BPAD_LEFT,
background_color=BORDER_COLOR),
sg.Column(block_4, size=(450, 320), pad=BPAD_RIGHT)
],
[sg.Column(bottom_banner, size=(960, 30), pad=(0, 0), background_color=DARK_HEADER_COLOR)]
]
window = sg.Window('malware detection based on dynamic features', layout, margins=(0, 0),
background_color=BORDER_COLOR,
no_titlebar=False, grab_anywhere=True)
return window
if __name__ == "__main__":
my_window = draw_window()
file_name = ''
json_file_path = ''
traffic_pic_path = ''
while True: # Event Loop
event, values = my_window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'show json file path':
file_name = values['file_name']
# terminal commands, need value['file_path']
json_file_path = 'D:\\VMShare\\cuckoo_report_analyzer-master\\storage\\' + file_name + '\\processes.json'
my_window['json_file_path'].update(json_file_path)
# _thread.start_new_thread(cuckoo_run())
if event == 'refresh_file':
file_name = values['file_name']
json_file_path = 'D:\\VMShare\\cuckoo_report_analyzer-master\\storage\\' + file_name + '\\processes.json'
if not os.path.isfile(json_file_path):
my_window['json_file_path'].update('not saved yet')
else:
my_window['json_file_path'].update('already saved')
if event == 'submit_to_model':
file_name = values['file_name']
# terminal commands, need value['file_path']
json_file_path = 'D:\\VMShare\\cuckoo_report_analyzer-master\\storage\\' + file_name + '\\processes.json'
pred = predict_api.api_predict('./api_reader/api.csv', json_file_path,
'./classification_api/behavioral-malware-detection-based-on-api-calls_model.h5')
if np.argmax([pred[0]]) == 0:
my_window['api_result'].update('BENIGN')
else:
my_window['api_result'].update('MALWARE')
# 写入日志
csv_path = './log.csv'
csv_file = open(csv_path, 'a')
csv_file.write(
'API,' + file_name + ',' + str(get_hash(json_file_path)) + ',' + str(np.argmax([pred[0]])) + '\n')
csv_file.close()
if event == 'submit_traffic':
file_name = values['file_name']
traffic_pic_path = values['traffic_file_path']
my_window['traffic_pic'].update(filename=traffic_pic_path)
model_p = './classification_traffic/traffic_class.h5'
pic = predict_traffic.load_traffic_data(traffic_pic_path)
pred = predict_traffic.traffic_predict(pic, model_p)
if np.argmax([pred[0]]) == 0:
my_window['traffic_result'].update('BENIGN')
else:
my_window['traffic_result'].update('MALWARE')
# 写入日志
csv_path = './log.csv'
csv_file = open(csv_path, 'a')
csv_file.write(
'Traffic,' + file_name + ',' + str(get_hash(traffic_pic_path)) + ',' + str(np.argmax([pred[0]])) + '\n')
csv_file.close()
if event == 'refresh_pic':
traffic_pic_path = values['traffic_file_path']
my_window['traffic_pic'].update(filename=traffic_pic_path)
my_window.close()