-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathpost_processing.py
More file actions
288 lines (222 loc) · 8.93 KB
/
post_processing.py
File metadata and controls
288 lines (222 loc) · 8.93 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/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
import logging
import os
from simplejson import JSONDecodeError
try:
from modules import register
except ImportError:
from ...modules import register
# The processors is a set of tuples (function, priority)
# You can use it to sort the post processing analysis
# Example:
#
# from operator import itemgetter
# p_ordered = [i[0] for i in sorted(processors, key=itemgetter(1))]
processors = set()
log = logging.getLogger(__name__)
"""
This module contains all post processors for mail attachments
(i.e.: VirusTotal, Thug, etc.).
The skeleton of function must be like this:
@register(processors, active=True)
def processor(conf, attachments):
if conf["enabled"]:
from module_x import y # import custom object for this processor
for a in attachments:
# check if sample is filtered
if not a.get("is_filtered", False):
pass
The function must be have the same name of configuration section in
conf/spamscope.yml --> attachments --> processor
The results will be added on attachments given as input.
Don't forget decorator @register()
You don't need anything else.
"""
@register(processors, active=True)
def tika(conf, attachments):
"""This method updates the attachments results
with the Tika reports.
Args:
attachments (list): all attachments of email
conf (dict): conf of this post processor
Returns:
This method updates the attachments list given
"""
if conf["enabled"]:
from tikapp import TikaApp
tika = TikaApp(
file_jar=conf["path_jar"],
memory_allocation=conf["memory_allocation"])
wtlist = conf.get("whitelist_content_types", [])
if not wtlist:
log.warning(
"Apache Tika analysis setted, without whitelist content types")
return
for a in attachments:
if not a.get("is_filtered", False):
if a["Content-Type"] in wtlist:
payload = a["payload"]
if a["content_transfer_encoding"] != "base64":
try:
payload = payload.encode("base64")
except UnicodeError:
# content_transfer_encoding': u'x-uuencode'
# it's not binary with strange encoding
continue
# tika-app only gets payload in base64
try:
results = tika.extract_all_content(
payload=payload,
convert_to_obj=True)
if results:
a["tika"] = results
except JSONDecodeError:
log.warning(
"JSONDecodeError for {!r} in Tika analysis".format(
a["md5"]))
@register(processors, active=True)
def virustotal(conf, attachments):
"""This method updates the attachments results
with the Virustotal reports.
Args:
attachments (list): all attachments of email
conf (dict): conf of this post processor
Returns:
This method updates the attachments list given
"""
if conf["enabled"]:
from virus_total_apis import PublicApi as VirusTotalPublicApi
from .utils import reformat_virustotal
vt = VirusTotalPublicApi(conf["api_key"])
wtlist = conf.get("whitelist_content_types", [])
# I don't have content types to analyze
if not wtlist:
log.warning(
"Virustotal analysis setted, without whitelist content types")
return
for a in attachments:
if not a.get("is_filtered", False) and a["Content-Type"] in wtlist:
# main/archive
result = vt.get_file_report(a["sha1"])
reformat_virustotal(result)
if result:
a["virustotal"] = result
# files in archive
for i in a.get("files", []):
if not i.get("is_filtered", False) \
and i["Content-Type"] in wtlist:
i_result = vt.get_file_report(i["sha1"])
reformat_virustotal(i_result)
if i_result:
i["virustotal"] = i_result
@register(processors, active=True)
def thug(conf, attachments):
"""This method updates the attachments results
with the Thug reports.
Args:
attachments (list): all attachments of email
conf (dict): conf of this post processor
Returns:
This method updates the attachments list given
"""
if conf["enabled"]:
from .thug_analysis import ThugAnalysis
thug = ThugAnalysis()
for a in attachments:
if not a.get("is_filtered", False):
if a["extension"] in conf["extensions"]:
a["thug"] = thug.run(a, **conf)
for i in a.get("files", []):
if i["extension"] in conf["extensions"]:
i["thug"] = thug.run(i, **conf)
@register(processors, active=False)
def zemana(conf, attachments): # pragma: no cover
"""This method updates the attachments results
with Zemana AntiMalware reports.
Args:
attachments (list): all attachments of email
Returns:
This method updates the attachments list given
"""
if conf["enabled"]:
try:
from zemana import Zemana
except ImportError:
raise ImportError("Zemana library not found. You should be Zemana "
"customer (https://www.zemana.com/)")
from requests.exceptions import HTTPError
z = Zemana(int(conf["PartnerId"]), conf["UserId"],
conf["ApiKey"], conf["useragent"])
for a in attachments:
if not a.get("is_filtered", False):
try:
result = z.query(a["md5"])
except HTTPError:
log.exception(
"HTTPError in Zemana query for md5 {!r}".format(
a["md5"]))
else:
if result:
a["zemana"] = result.json
a["zemana"]["type"] = result.type
for i in a.get("files", []):
try:
i_result = z.query(i["md5"])
except HTTPError:
log.exception(
"HTTPError in Zemana query for md5 {!r}".format(
i["md5"]))
else:
if i_result:
i["zemana"] = i_result.json
i["zemana"]["type"] = i_result.type
@register(processors, priority=999, active=True)
def store_samples(conf, attachments):
"""This method stores the attachments on file system.
Args:
attachments (list): all attachments of email
conf (dict): conf of this post processor
Returns:
This method updates the attachments list given
"""
if conf["enabled"]:
from .utils import write_sample
base_path = conf["base_path"]
for a in attachments:
if not a.get("is_filtered", False):
# commons
date_str = a["analisys_date"].split("T")[0]
path = os.path.join(base_path, date_str)
# do not write if has archived files
if not a.get("files", []):
write_sample(
binary=a["binary"],
payload=a["payload"],
path=path,
filename=a["filename"],
hash_=a["md5"],
)
# save file in archive
for i in a.get("files", []):
write_sample(
# All archived files have base64 payload
binary=True,
payload=i["payload"],
path=path,
filename=i["filename"],
hash_=i["md5"],
)