-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
attachments.py
371 lines (305 loc) · 12.6 KB
/
attachments.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2016 Fedele Mantuano (https://www.linkedin.com/in/fmantuano/)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import absolute_import, print_function, unicode_literals
try:
from UserList import UserList
except ImportError:
from collections import UserList
import base64
import copy
import datetime
import logging
import os
import shutil
import tempfile
from operator import itemgetter
import patoolib
from patoolib.util import PatoolError
import six
from .exceptions import HashError, ContentTypeError
from .post_processing import processors
from .utils import fingerprints, check_archive, contenttype, extension
log = logging.getLogger(__name__)
p_ordered = [i[0] for i in sorted(processors, key=itemgetter(1))]
class Attachments(UserList):
_kwargs = {}
def __getattr__(self, name):
try:
return self._kwargs[name]
except KeyError:
if name in ("commons"):
return {}
msg = "'{0}' object has no attribute '{1}'"
raise AttributeError(msg.format(type(self).__name__, name))
def __call__(self, intelligence=True):
self.run(intelligence)
def _intelligence(self):
"""
Post processing attachments with third party tools
"""
for p in p_ordered:
try:
p(getattr(self, p.__name__), self)
except AttributeError:
log.warning(
"AttributeError: {!r} doesn't exist".format(p.__name__))
def removeall(self):
"""Remove all items from object. """
del self[:]
def run(self, intelligence=True):
"""Run processing on items in memory. """
self._addmetadata()
self._filtercontenttypes()
self._filterforsize()
if intelligence:
self._intelligence()
def reload(self, **kwargs):
"""Reload all configuration parameters"""
self._kwargs = kwargs
def filenamestext(self):
"""Return a string with the filenames of all attachments. """
filenames = six.text_type()
for i in self:
try:
filenames += i["filename"] + "\n"
for j in i.get("files", []):
filenames += j["filename"] + "\n"
except UnicodeDecodeError:
continue
else:
return filenames.strip()
def payloadstext(self):
"""Return a string with the not binary payloads of all attachments. """
text = six.text_type()
for i in self:
try:
if i.get("is_filtered"):
continue
if not i.get("is_archive"):
if i["content_transfer_encoding"] == "base64":
text += base64.b64decode(i["payload"]) + "\n"
else:
text += i["payload"] + "\n"
continue
for j in i.get("files", []):
text += base64.b64decode(j["payload"]) + "\n"
except UnicodeDecodeError:
# This exception happens with binary payloads
continue
else:
return text.strip()
def popcontenttype(self, content_type):
""" Given a content type remove attachments with same content type,
also in files in archive.
"""
content_type = content_type.lower()
remove = []
for i in self:
filtered = i.get("is_filtered", False)
m_content_type = i["mail_content_type"].lower()
if not filtered:
try:
if i["Content-Type"].lower() == content_type:
remove.append(i)
continue
inner_remove = []
for j in i.get("files", []):
if j["Content-Type"].lower() == content_type:
inner_remove.append(j)
except KeyError:
raise ContentTypeError("Content-Type key missing. "
"Add metadata with method '.run()'")
else:
# Remove inner
for j in inner_remove:
i["files"].remove(j)
# Patch
# If attach is filtered and content_type in whitelist
# you should remove sample from results.
# You can't use Content-Type because we don't have payload, so
# we use mail_content_type
elif (filtered and m_content_type == content_type):
remove.append(i)
else:
# Remove
for i in remove:
self.remove(i)
def pophash(self, attach_hash):
"""Remove the item with attach_hash from object. """
len_hashes = dict([(32, "md5"), (40, "sha1"),
(64, "sha256"), (128, "sha512")])
to_remove = []
for i in self:
try:
key = len_hashes[len(attach_hash)]
except KeyError:
raise HashError("invalid hash {!r}".format(attach_hash))
else:
if key in i and i[key] == attach_hash:
to_remove.append(i)
else:
for i in to_remove:
self.remove(i)
def _filterforsize(self):
"""
Filter all attachments or archived files greater than fixed size
"""
if not self.commons.get("size.filter.enabled", False):
return
max_size = int(self.commons.get("max.size", 3145728))
for i in self:
if not i.get("is_filtered", False):
if i["size"] >= max_size:
i.pop("payload", None)
i.pop("files", None)
i["is_filtered"] = True
i["filter_reason"] = "greater that {} bytes".format(
max_size)
else:
if i.get("files", []):
files = []
for j in i["files"]:
if j["size"] < max_size:
files.append(j)
else:
log.warning(
"File {!r} greater than {} bytes".format(
j["sha1"], max_size))
if len(files) != len(i["files"]):
i["filter_files"] = True
if files:
i["files"] = files
else:
i.pop("files", None)
def _filtercontenttypes(self):
"""Filtering of all content types in
'filter_cont_types' parameter.
"""
for i in self.commons.get("blacklist_content_types", set()):
self.popcontenttype(i)
def filter(self, check_list, hash_type="sha1"):
"""Remove from memory the payloads with hash in check_list. """
check_list = set(check_list)
analyzed = set()
for i in self:
analyzed.add(i[hash_type])
if i[hash_type] in check_list:
i.pop("payload", None)
i["is_filtered"] = True
continue
i["is_filtered"] = False
else:
return analyzed
@staticmethod
def _metadata(raw_dict):
""" Return payload, file size and extension of raw data. """
if raw_dict["binary"]:
try:
payload = base64.b64decode(raw_dict["payload"])
except TypeError, e:
# https://gist.github.com/perrygeo/ee7c65bb1541ff6ac770
raw_dict["payload"] += "==="
log.warning(
"Added '===' to payload base64 for sample {!r}".format(
raw_dict["md5"]))
payload = base64.b64decode(raw_dict["payload"])
raw_dict.setdefault("errors", []).append(repr(e))
else:
payload = raw_dict["payload"]
size = len(payload)
ext = extension(raw_dict["filename"])
return (payload, size, ext)
def _addmetadata(self):
"""For each item in memory add extra informations as: file extension,
file size, content type, if is a archive and archived files.
"""
bl_list = self.commons.get("not_extract_content_types", set())
for i in self:
i["analisys_date"] = datetime.datetime.utcnow().isoformat()
if not i.get("is_filtered", False):
payload, size, ext = Attachments._metadata(i)
content_type = contenttype(payload)
flag, f = check_archive(payload, write_sample=True)
i.update({
"extension": ext,
"size": size,
"Content-Type": content_type,
"is_archive": flag})
if flag and content_type not in bl_list:
i["files"] = []
temp_dir = tempfile.mkdtemp()
try:
patoolib.extract_archive(
f, outdir=temp_dir, verbosity=-1)
except PatoolError as e:
log.warning(
"PatoolError archive md5 {!r}. Details: {}".format(
i["md5"], repr(e)))
else:
for path, subdirs, files in os.walk(temp_dir):
for name in files:
j = os.path.join(path, name)
with open(j, "rb") as a:
t = {}
payload = a.read()
content_type = contenttype(payload)
filename = os.path.basename(j)
t["analisys_date"] = \
datetime.datetime.utcnow().isoformat()
t["filename"] = filename
t["extension"] = extension(filename)
t["size"] = len(payload)
t["Content-Type"] = content_type
t["payload"] = payload.encode("base64")
t["is_filtered"] = False
t["md5"], t["sha1"], t["sha256"], \
t["sha512"], t["ssdeep"] = \
fingerprints(payload)
i["files"].append(t)
finally:
try:
# Remove temp dir for archived files
shutil.rmtree(temp_dir)
except OSError:
pass
try:
# Remove temp file from filesystem
os.remove(f)
except OSError:
pass
@classmethod
def withhashes(cls, attachments=[]):
"""Alternative costructor that add hashes to the items. """
r = copy.deepcopy(attachments)
for i in r:
if i.get("content_transfer_encoding") == "base64":
try:
payload = base64.b64decode(i["payload"])
except TypeError, e:
try:
# try to add === to decode base64
payload = base64.b64decode(i["payload"] + "===")
except TypeError:
# if fails maybe is text, with fake base64 header
payload = i["payload"]
finally:
i.setdefault("errors", []).append(
repr(e) + " Fake base64")
else:
payload = i["payload"]
i["md5"], i["sha1"], i["sha256"], i["sha512"], i["ssdeep"] = \
fingerprints(payload)
else:
return cls(r)