-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeatures.py
213 lines (174 loc) · 8.03 KB
/
features.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
209
210
211
212
213
import os
import json
class APICalls:
"""
Feature class for API calls made by the binary.
"""
def __init__(self):
pass
def processFeatures(self, jsonFile):
"""
Parses the json trace file to fetch total calls, successful calls and failed calls for every API.
:param jsonFile: trace file in json format.
"""
self.dataDict = {}
with open(jsonFile) as f:
data = json.load(f)
for processName in data["behavior"]["apistats"]:
for key in data["behavior"]["apistats"][processName].keys():
keyDict = key + "_total"
if keyDict not in self.dataDict:
self.dataDict[keyDict] = data["behavior"]["apistats"][processName][key]
else:
self.dataDict[keyDict] += data["behavior"]["apistats"][processName][key]
for prefix in ["_success", "_fail"]:
keyDict = key + prefix
if keyDict not in self.dataDict:
self.dataDict[keyDict] = 0
for process in data["behavior"]["processes"]:
if "calls" in process:
for apiCall in process["calls"]:
if apiCall["return_value"] == 0:
keyDict = apiCall["api"] + "_success"
else:
keyDict = apiCall["api"] + "_fail"
self.dataDict[keyDict] += 1
return self.dataDict
class FileActions:
"""
Feature class for file-related actions performed by the binary.
"""
def __init__(self):
pass
def processFeatures(self, jsonFile):
"""
Parses the json trace file to fetch total file actions performed.
:param jsonFile: trace file in json format.
"""
self.dataDict = {}
with open(jsonFile) as f:
data = json.load(f)
actions = ["file_failed", "file_copied", "file_exists", "file_opened", "file_read", "file_written"]
for action in actions:
self.dataDict[action] = 0
for process in data["behavior"]["generic"]:
summary = process["summary"]
for action in actions:
if action in summary:
self.dataDict[action] = len(summary[action])
return self.dataDict
class RegistryActions:
"""
Feature class for registry-related actions performed by the binary.
"""
def __init__(self):
pass
def processFeatures(self, jsonFile):
"""
Parses the json trace file to fetch total registry actions performed.
:param jsonFile: trace file in json format.
"""
self.dataDict = {}
with open(jsonFile) as f:
data = json.load(f)
actions = ["regkey_read", "regkey_opened", "regkey_deleted", "regkey_written"]
for action in actions:
self.dataDict[action] = 0
for process in data["behavior"]["generic"]:
summary = process["summary"]
for action in actions:
if action in summary:
self.dataDict[action] = len(summary[action])
return self.dataDict
class DLLLoads:
"""
Feature class for the DLLs loaded by the binary.
"""
def __init__(self):
pass
def processFeatures(self, jsonFile):
"""
Parses the json trace file to fetch the list of DLLs loaded.
:param jsonFile: trace file in json format.
"""
self.dataDict = {}
with open(jsonFile) as f:
data = json.load(f)
for process in data["behavior"]["generic"]:
summary = process["summary"]
if "dll_loaded" in summary:
self.dataDict["dll_loaded"] = len(summary["dll_loaded"])
return self.dataDict
class APICallSequences:
"""
Feature class for the API call sequence signature of the binary.
"""
def __init__(self):
"""
Intialise sets of API calls to create a signature.
"""
self.setFile = ["CreateFile", "ReadFile", "WriteFile", "OpenFile", "GetFile", "CopyFile", "FindFile", "DeleteFile", "GetPath", "SearchPath", "CreateDirectory", "GetDirectory", "OpenDirectory", "SetFile", "GetFolder", "MoveFile", "QueryFile", "RemoveDirectory", "NtdeviceIOControlFile"]
self.A = self.setFile
self.setSystem = ["NtClose", "NtDelayExecution", "Exception", "GetTime", "GetSystem", "Crypt", "NtQuery", "QuerySystem", "Exit", "Initialize", "CreateInstance", "SetObject", "Hook", "CreateObject", "Debugger", "Service", "Anomaly", "SetError", "Cert", "Privilege", "GetName", "Shellexecute", "Shutdown", "GetObject", "Manager"]
self.B = self.setSystem
self.setReg = ["CreateKey", "OpenKey", "CloseKey", "RegGetValue", "RegEnumValue", "RegQuery", "RegEnum", "RegDelete", "RegSet"]
self.C = self.setReg
self.setKernel = ["Ldr", "Resource", "Func", "Load", "Uuid", "Hwnd", "Section", "Module", "Dll", "Libm"]
self.D = self.setKernel
self.setMemory = ["Memory", "Volume", "Space", "Buffer"]
self.E = self.setMemory
self.setProcess = ["Mutant", "OpenProcess", "AssignProcess", "Thread", "SnapShot", "Module", "Process32", "SetUnhandledException", "TerminateProcess", "CreateProcess"]
self.F = self.setProcess
self.setWindow = ["GetSystemMetrics", "GetForeGroundWindow", "Console", "KeyState", "Cursor", "RegisterHotKey", "EnumWindows", "SendNotifyMessage", "FindWindow", "CreateCtcCtx", "MessageBox", "State", "Key"]
self.G = self.setWindow
self.setNetwork = ["Internet", "Http", "Internal", "WSA", "Adapter", "Host", "DNS", "Addr", "Sock", "Listen", "Recv", "Send", "Select", "Connect", "Bind", "URL", "Interface", "Accept", "NetUser", "NetShare", "NetGet", "Information"]
self.H = self.setNetwork
self.setDevice = ["DeviceIOControl", "StdHandle"]
self.I = self.setDevice
self.setText = ["String", "Text", "Char"]
self.J = self.setText
self.sets = [
self.setFile,
self.setSystem,
self.setReg,
self.setKernel,
self.setMemory,
self.setProcess,
self.setWindow,
self.setNetwork,
self.setDevice,
self.setText
]
def processFeatures(self, jsonFile):
"""
Parses the json trace file to fetch the API call sequence signature.
:param jsonFile: trace file in json format.
"""
self.signature = ""
with open(jsonFile) as f:
data = json.load(f)
for process in data["behavior"]["processes"]:
for call in process["calls"]:
for section in self.sets:
if any(ele in call["api"] for ele in section):
if section == self.A:
signature += "A"
elif section == self.B:
signature += "B"
elif section == self.C:
signature += "C"
elif section == self.D:
signature += "D"
elif section == self.E:
signature += "E"
elif section == self.F:
signature += "F"
elif section == self.G:
signature += "G"
elif section == self.H:
signature += "H"
elif section == self.I:
signature += "I"
elif section == self.J:
signature += "J"
return self.signature