forked from cuckoosandbox/cuckoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic.py
More file actions
265 lines (219 loc) · 8.89 KB
/
Copy pathstatic.py
File metadata and controls
265 lines (219 loc) · 8.89 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (C) 2010-2014 Cuckoo Foundation.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.
import os
try:
import magic
HAVE_MAGIC = True
except ImportError:
HAVE_MAGIC = False
try:
import pefile
import peutils
HAVE_PEFILE = True
except ImportError:
HAVE_PEFILE = False
from lib.cuckoo.common.abstracts import Processing
from lib.cuckoo.common.constants import CUCKOO_ROOT
from lib.cuckoo.common.objects import File
from lib.cuckoo.common.utils import convert_to_printable
# Partially taken from
# http://malwarecookbook.googlecode.com/svn/trunk/3/8/pescanner.py
class PortableExecutable:
"""PE analysis."""
def __init__(self, file_path):
"""@param file_path: file path."""
self.file_path = file_path
self.pe = None
def _get_filetype(self, data):
"""Gets filetype, uses libmagic if available.
@param data: data to be analyzed.
@return: file type or None.
"""
if not HAVE_MAGIC:
return None
try:
ms = magic.open(magic.MAGIC_NONE)
ms.load()
file_type = ms.buffer(data)
except:
try:
file_type = magic.from_buffer(data)
except Exception:
return None
return file_type
def _get_peid_signatures(self):
"""Gets PEID signatures.
@return: matched signatures or None.
"""
if not self.pe:
return None
try:
sig_path = os.path.join(CUCKOO_ROOT, "data",
"peutils", "UserDB.TXT")
signatures = peutils.SignatureDatabase(sig_path)
return signatures.match(self.pe, ep_only=True)
except:
return None
def _get_imported_symbols(self):
"""Gets imported symbols.
@return: imported symbols dict or None.
"""
if not self.pe:
return None
imports = []
if hasattr(self.pe, "DIRECTORY_ENTRY_IMPORT"):
for entry in self.pe.DIRECTORY_ENTRY_IMPORT:
try:
symbols = []
for imported_symbol in entry.imports:
symbol = {}
symbol["address"] = hex(imported_symbol.address)
symbol["name"] = imported_symbol.name
symbols.append(symbol)
imports_section = {}
imports_section["dll"] = entry.dll
imports_section["imports"] = symbols
imports.append(imports_section)
except:
continue
return imports
def _get_exported_symbols(self):
"""Gets exported symbols.
@return: exported symbols dict or None.
"""
if not self.pe:
return None
exports = []
if hasattr(self.pe, "DIRECTORY_ENTRY_EXPORT"):
for exported_symbol in self.pe.DIRECTORY_ENTRY_EXPORT.symbols:
symbol = {}
symbol["address"] = hex(self.pe.OPTIONAL_HEADER.ImageBase +
exported_symbol.address)
symbol["name"] = exported_symbol.name
symbol["ordinal"] = exported_symbol.ordinal
exports.append(symbol)
return exports
def _get_sections(self):
"""Gets sections.
@return: sections dict or None.
"""
if not self.pe:
return None
sections = []
for entry in self.pe.sections:
try:
section = {}
section["name"] = convert_to_printable(entry.Name.strip("\x00"))
section["virtual_address"] = "0x{0:08x}".format(entry.VirtualAddress)
section["virtual_size"] = "0x{0:08x}".format(entry.Misc_VirtualSize)
section["size_of_data"] = "0x{0:08x}".format(entry.SizeOfRawData)
section["entropy"] = entry.get_entropy()
sections.append(section)
except:
continue
return sections
def _get_resources(self):
"""Get resources.
@return: resources dict or None.
"""
if not self.pe:
return None
resources = []
if hasattr(self.pe, "DIRECTORY_ENTRY_RESOURCE"):
for resource_type in self.pe.DIRECTORY_ENTRY_RESOURCE.entries:
try:
resource = {}
if resource_type.name is not None:
name = str(resource_type.name)
else:
name = str(pefile.RESOURCE_TYPE.get(resource_type.struct.Id))
if hasattr(resource_type, "directory"):
for resource_id in resource_type.directory.entries:
if hasattr(resource_id, "directory"):
for resource_lang in resource_id.directory.entries:
data = self.pe.get_data(resource_lang.data.struct.OffsetToData, resource_lang.data.struct.Size)
filetype = self._get_filetype(data)
language = pefile.LANG.get(resource_lang.data.lang, None)
sublanguage = pefile.get_sublang_name_for_lang(resource_lang.data.lang, resource_lang.data.sublang)
resource["name"] = name
resource["offset"] = "0x{0:08x}".format(resource_lang.data.struct.OffsetToData)
resource["size"] = "0x{0:08x}".format(resource_lang.data.struct.Size)
resource["filetype"] = filetype
resource["language"] = language
resource["sublanguage"] = sublanguage
resources.append(resource)
except:
continue
return resources
def _get_versioninfo(self):
"""Get version info.
@return: info dict or None.
"""
if not self.pe:
return None
infos = []
if hasattr(self.pe, "VS_VERSIONINFO"):
if hasattr(self.pe, "FileInfo"):
for entry in self.pe.FileInfo:
try:
if hasattr(entry, "StringTable"):
for st_entry in entry.StringTable:
for str_entry in st_entry.entries.items():
entry = {}
entry["name"] = convert_to_printable(str_entry[0])
entry["value"] = convert_to_printable(str_entry[1])
infos.append(entry)
elif hasattr(entry, "Var"):
for var_entry in entry.Var:
if hasattr(var_entry, "entry"):
entry = {}
entry["name"] = convert_to_printable(var_entry.entry.keys()[0])
entry["value"] = convert_to_printable(var_entry.entry.values()[0])
infos.append(entry)
except:
continue
return infos
def _get_imphash(self):
"""Gets imphash.
@return: imphash string or None.
"""
if not self.pe:
return None
try:
return self.pe.get_imphash()
except AttributeError:
return None
def run(self):
"""Run analysis.
@return: analysis results dict or None.
"""
if not os.path.exists(self.file_path):
return None
try:
self.pe = pefile.PE(self.file_path)
except pefile.PEFormatError:
return None
results = {}
results["peid_signatures"] = self._get_peid_signatures()
results["pe_imports"] = self._get_imported_symbols()
results["pe_exports"] = self._get_exported_symbols()
results["pe_sections"] = self._get_sections()
results["pe_resources"] = self._get_resources()
results["pe_versioninfo"] = self._get_versioninfo()
results["pe_imphash"] = self._get_imphash()
results["imported_dll_count"] = len([x for x in results["pe_imports"] if x.get("dll")])
return results
class Static(Processing):
"""Static analysis."""
def run(self):
"""Run analysis.
@return: results dict.
"""
self.key = "static"
static = {}
if HAVE_PEFILE:
if self.task["category"] == "file":
if "PE32" in File(self.file_path).get_type():
static = PortableExecutable(self.file_path).run()
return static