-
Notifications
You must be signed in to change notification settings - Fork 3
/
xml_to_csv.py
208 lines (160 loc) · 7.8 KB
/
xml_to_csv.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import xml.etree.ElementTree as ET
from datetime import datetime
import pandas as pd
from basic_function import make_dir
def get_file_list_in_dir(path):
return [name for name in os.listdir(path) if not os.path.isdir(os.path.join(path, name))]
def save_file_info(path, csv_name):
file = get_file_list_in_dir(path=path)
print("file number:", len(file))
file_data = []
for i in range(len(file)):
if i % 200 == 0:
print("percent:", i / len(file) * 100)
file_name = file[i]
root = ET.parse(os.path.join(path, file_name)).getroot()
file_list_node = root.findall('file_list')[0]
file_error = int(file_list_node.get('file_error'))
file_name = file_list_node.get('file_name')
file_uid = file_list_node.get('file_uid')
file_data.append([file_error, file_name, file_uid])
file_data_df = pd.DataFrame(data=file_data, columns=['file_error', 'file_name', 'file_uid'])
file_data_df.to_csv(csv_name)
def save_action_data_to_csv(action_data, csv_name, columns=None):
if columns is None:
action_data_df = pd.DataFrame(data=action_data,
columns=['file_name', 'api_name', 'call_name', 'call_pid', 'ret_value',
'apiArg_list_count',
'exInfo_list_count', 'call_time'])
action_data_df.to_csv(csv_name)
else:
action_data_df = pd.DataFrame(data=action_data, columns=columns)
action_data_df.to_csv(csv_name)
def time_string_to_ns(time_string):
time_format = '%H:%M:%S.%f'
datetime_object = datetime.strptime(time_string, time_format)
return (datetime_object - datetime(1970, 1, 1)).total_seconds()
def is_dll(name):
return name[-3:] == "dll" or name[-3:] == "DLL"
def is_hkey(name):
return name[:4] == "HKEY"
def save_action_stream(path, csv_name, dir_name="", mode=0):
if dir_name != "":
make_dir(os.path.join("features", dir_name))
file = get_file_list_in_dir(path=path)
print("file number:", len(file))
action_data = []
for i in range(len(file)):
if i % 200 == 0:
print("percent:", i / len(file) * 100)
if i % 1000 == 0 and mode == 1 and i != 0:
save_action_data_to_csv(action_data, os.path.join("features", dir_name, str(i) + csv_name))
action_data = []
file_name = file[i]
root = ET.parse(os.path.join(path, file_name)).getroot()
action_list = root.findall('./file_list/file/start_boot/action_list/action')
start = 0
start_time = 0
last = 0
for action in action_list:
api_name = action.get('api_name')
call_name = action.get('call_name')
call_pid = action.get('call_pid')
call_time = action.get('call_time')
ret_value = action.get('ret_value')
# convert time string to seconds since software start
try:
call_time = time_string_to_ns(call_time)
last = call_time
except ValueError:
print(call_time)
call_time = last
if start == 0:
start_time = call_time
start += 1
call_time = call_time - start_time
try:
apiArg_list = action.findall('apiArg_list')[0]
apiArg_list_count = apiArg_list.get('count')
except IndexError:
apiArg_list_count = -1
print(action.attrib)
exInfo_list = action.findall('exInfo_list')[0]
exInfo_list_count = exInfo_list.get('count')
action_data.append(
[file_name, api_name, call_name, call_pid, ret_value, apiArg_list_count, exInfo_list_count, call_time])
if mode == 1:
save_action_data_to_csv(action_data, os.path.join("features", dir_name, 'final_' + csv_name))
else:
save_action_data_to_csv(action_data, csv_name)
def save_attribute_list(path, csv_name, dir_name="", mode=0, dll_or_hkey="dll"):
if dir_name != "":
make_dir(os.path.join("features", dir_name))
file = get_file_list_in_dir(path=path)
print("file number:", len(file))
column_name = ['file_name', 'api_name', 'call_name', 'call_pid', 'ret_value', 'value', "call_time"]
determination = is_hkey
if dll_or_hkey == "dll":
determination = is_dll
elif dll_or_hkey == "hkey":
determination = is_hkey
action_data = []
for i in range(len(file)):
if i % 200 == 0:
print("percent:", i / len(file) * 100)
if i % 1000 == 0 and mode == 1 and i != 0:
save_action_data_to_csv(action_data, os.path.join("features", dir_name, str(i) + csv_name), column_name)
action_data = []
file_name = file[i]
root = ET.parse(os.path.join(path, file_name)).getroot()
action_list = root.findall('./file_list/file/start_boot/action_list/action')
for action in action_list:
api_name = action.get('api_name')
call_name = action.get('call_name')
call_pid = action.get('call_pid')
call_time = action.get('call_time')
ret_value = action.get('ret_value')
apiArg_list = action.findall("./apiArg_list/apiArg")
exInfo_list = action.findall('./exInfo_list/exInfo')
for apiArg in apiArg_list:
value = apiArg.get('value')
# print(value)
if determination(value):
print(1)
action_data.append(
[file_name, api_name, call_name, call_pid, ret_value, value, call_time])
for exInfo in exInfo_list:
value = exInfo.get('value')
# print(value)
if determination(value):
action_data.append(
[file_name, api_name, call_name, call_pid, ret_value, value, call_time])
if mode == 1:
save_action_data_to_csv(action_data, os.path.join("features", dir_name, 'final_' + csv_name), column_name)
else:
save_action_data_to_csv(action_data, csv_name, column_name)
if __name__ == '__main__':
save_action_stream('stage2_dataset', "stage2.csv", dir_name="stage2", mode=1)
save_attribute_list('stage2_dataset', "stage2.csv",
dir_name="stage2_dll", mode=1, dll_or_hkey="dll")
save_attribute_list('stage2_dataset', "stage2.csv",
dir_name="stage2_hkey", mode=1, dll_or_hkey="hkey")
# save_action_stream('stage1_dataset\\test', "test.csv", dir_name="test", mode=1)
# save_action_stream('stage1_dataset\\train\\white', "white.csv", dir_name="white",
# mode=1)
# save_action_stream('stage1_dataset\\stage1_dataset\\train\\black', "black.csv", dir_name="black",
# mode=1)
# save_attribute_list('stage1_dataset\\stage1_dataset\\train\\white', "white.csv",
# dir_name="white_dll", mode=1, dll_or_hkey="dll")
# save_attribute_list('stage1_dataset\\stage1_dataset\\train\\black', "black.csv",
# dir_name="black_dll", mode=1, dll_or_hkey="dll")
# save_attribute_list('stage1_dataset\\stage1_dataset\\test', "test.csv",
# dir_name="test_dll", mode=1, dll_or_hkey="dll")
#
# save_attribute_list('stage1_dataset\\stage1_dataset\\train\\white', "white.csv",
# dir_name="white_hkey", mode=1, dll_or_hkey="hkey")
# save_attribute_list('stage1_dataset\\stage1_dataset\\train\\black', "black.csv",
# dir_name="black_hkey", mode=1, dll_or_hkey="hkey")
# save_attribute_list('stage1_dataset\\stage1_dataset\\test', "test.csv",
# dir_name="test_hkey", mode=1, dll_or_hkey="hkey")