-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest_attachments_post_processing.py
More file actions
323 lines (255 loc) · 11.1 KB
/
test_attachments_post_processing.py
File metadata and controls
323 lines (255 loc) · 11.1 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
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
#!/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.
"""
import logging
import os
import six
import unittest
import mailparser
try:
from collections import ChainMap
except ImportError:
from chainmap import ChainMap
from context import attachments, DEFAULTS
MailAttachments = attachments.MailAttachments
base_path = os.path.realpath(os.path.dirname(__file__))
mail = os.path.join(base_path, 'samples', 'mail_malformed_1')
mail_thug = os.path.join(base_path, 'samples', 'mail_thug')
mail_test_4 = os.path.join(base_path, 'samples', 'mail_test_4')
mail_test_9 = os.path.join(base_path, 'samples', 'mail_test_9')
mail_test_10 = os.path.join(base_path, 'samples', 'mail_test_10')
OPTIONS = ChainMap(os.environ, DEFAULTS)
logging.getLogger().addHandler(logging.NullHandler())
class TestPostProcessing(unittest.TestCase):
def setUp(self):
# Init
p = mailparser.parse_from_file(mail)
self.attachments = p.attachments
p = mailparser.parse_from_file(mail_thug)
self.attachments_thug = p.attachments
@unittest.skipIf(OPTIONS["VIRUSTOTAL_ENABLED"].capitalize() == "False",
"VirusTotal test skipped: "
"set env variable 'VIRUSTOTAL_ENABLED' to True")
def test_virustotal(self):
"""Test add VirusTotal processing."""
from src.modules.attachments import virustotal
conf = {"enabled": True,
"api_key": OPTIONS["VIRUSTOTAL_APIKEY"],
"whitelist_content_types": [
"application/octet-stream",
"application/x-dosexec",
"application/zip",
]}
attachments = MailAttachments.withhashes(self.attachments)
attachments(intelligence=False)
virustotal(conf, attachments)
# VirusTotal analysis
for i in attachments:
self.assertIn('virustotal', i)
self.assertEqual(i['virustotal']['response_code'], 200)
self.assertEqual(i['virustotal']['results']['sha1'],
'2a7cee8c214ac76ba6fdbc3031e73dbede95b803')
self.assertIsInstance(i["virustotal"]["results"]["scans"], list)
for j in i["files"]:
self.assertIn('virustotal', j)
self.assertEqual(j['virustotal']['response_code'], 200)
self.assertEqual(j['virustotal']['results']['sha1'],
'ed2e480e7ba7e37f77a85efbca4058d8c5f92664')
self.assertIsInstance(
j["virustotal"]["results"]["scans"], list)
@unittest.skipIf(OPTIONS["ZEMANA_ENABLED"].capitalize() == "False",
"Zemana test skipped: "
"set env variable 'ZEMANA_ENABLED' to True")
def test_zemana(self):
"""Test add Zemana processing."""
from src.modules.attachments import zemana
conf = {"enabled": True,
"PartnerId": OPTIONS["ZEMANA_PARTNERID"],
"UserId": OPTIONS["ZEMANA_USERID"],
"ApiKey": OPTIONS["ZEMANA_APIKEY"],
"useragent": "SpamScope"}
attachments = MailAttachments.withhashes(self.attachments)
attachments(intelligence=False)
zemana(conf, attachments)
# Zemana analysis
for i in attachments:
self.assertIn("zemana", i)
self.assertIn("type", i["zemana"])
self.assertIn("aa", i["zemana"])
self.assertIsInstance(i["zemana"]["aa"], list)
for j in i["files"]:
self.assertIn("zemana", j)
self.assertIn("type", j["zemana"])
self.assertIn("aa", j["zemana"])
self.assertIsInstance(j["zemana"]["aa"], list)
def test_tika(self):
"""Test add Tika processing."""
from src.modules.attachments import tika
# Complete parameters
conf = {"enabled": True,
"path_jar": OPTIONS["TIKA_APP_JAR"],
"memory_allocation": None,
"whitelist_content_types": ["application/zip"]}
attachments = MailAttachments.withhashes(self.attachments)
attachments(intelligence=False)
tika(conf, attachments)
for i in attachments:
self.assertIn("tika", i)
self.assertEqual(len(attachments[0]["tika"]), 2)
self.assertEqual(
int(attachments[0]["tika"][0]["Content-Length"]),
attachments[0]["size"])
# tika disabled
conf["enabled"] = False
attachments = MailAttachments.withhashes(self.attachments)
attachments(intelligence=False)
tika(conf, attachments)
for i in attachments:
self.assertNotIn("tika", i)
conf["enabled"] = True
# attachments without run()
with self.assertRaises(KeyError):
attachments = MailAttachments.withhashes(self.attachments)
tika(conf, attachments)
# attachments a key of conf
conf_inner = {
"enabled": True,
"path_jar": OPTIONS["TIKA_APP_JAR"],
"memory_allocation": None}
attachments = MailAttachments.withhashes(self.attachments)
tika(conf_inner, attachments)
for i in attachments:
self.assertNotIn("tika", i)
def test_tika_bug_incorrect_padding(self):
"""Test add Tika processing."""
from src.modules.attachments import tika
# Complete parameters
conf = {"enabled": True,
"path_jar": OPTIONS["TIKA_APP_JAR"],
"memory_allocation": None,
"whitelist_content_types": ["application/zip"]}
p = mailparser.parse_from_file(mail_test_4)
attachments = MailAttachments.withhashes(p.attachments)
attachments(intelligence=False)
tika(conf, attachments)
for i in attachments:
self.assertIn("tika", i)
def test_tika_bug_unicode_error(self):
"""Test add Tika processing."""
from src.modules.attachments import tika
# Complete parameters
conf = {"enabled": True,
"path_jar": OPTIONS["TIKA_APP_JAR"],
"memory_allocation": None,
"whitelist_content_types": [
"application/zip", "application/octet-stream"]}
p = mailparser.parse_from_file(mail_test_10)
attachments = MailAttachments.withhashes(p.attachments)
attachments(intelligence=False)
tika(conf, attachments)
self.assertNotIn("tika", attachments[0])
@unittest.skipIf(OPTIONS["THUG_ENABLED"].capitalize() == "False",
"Thug test skipped: "
"set env variable 'THUG_ENABLED' to True")
def test_thug(self):
"""Test add Thug processing."""
from src.modules.attachments import thug
# Complete parameters
conf = {"enabled": True,
"extensions": [".html", ".js", ".jse"],
"user_agents": ["win7ie90", "winxpie80"],
"referer": "http://www.google.com/",
"timeout": 300}
attachments = MailAttachments.withhashes(self.attachments_thug)
attachments(intelligence=False)
first_attachment = attachments[0]
self.assertNotIn('thug', first_attachment)
thug(conf, attachments)
# Thug attachment
thug_attachment = first_attachment['files'][0]
self.assertIn('thug', thug_attachment)
thug_analysis = thug_attachment['thug']
self.assertIsInstance(thug_analysis, list)
self.assertEqual(len(thug_analysis), 2)
first_thug_analysis = thug_analysis[0]
self.assertIn('files', first_thug_analysis)
self.assertIn('code', first_thug_analysis)
self.assertIn('exploits', first_thug_analysis)
self.assertIn('url', first_thug_analysis)
self.assertIn('timestamp', first_thug_analysis)
self.assertIn('locations', first_thug_analysis)
self.assertIn('connections', first_thug_analysis)
self.assertIn('logtype', first_thug_analysis)
self.assertIn('behavior', first_thug_analysis)
self.assertIn('thug', first_thug_analysis)
self.assertIn('classifiers', first_thug_analysis)
self.assertEqual(
first_thug_analysis['thug']['personality']['useragent'],
'win7ie90')
self.assertEqual(
first_thug_analysis['thug']['options']['referer'],
'http://www.google.com/')
def test_store_samples_unicode_error(self):
from datetime import datetime
import shutil
from src.modules.attachments import store_samples
# Complete parameters
conf = {"enabled": True,
"base_path": "/tmp"}
p = mailparser.parse_from_file(mail_test_9)
attachments = MailAttachments.withhashes(p.attachments)
attachments(intelligence=False)
store_samples(conf, attachments)
now = six.text_type(datetime.utcnow().date())
sample = os.path.join(
"/tmp",
now,
"43573896890da36e092039cf0b3a92f8")
self.assertTrue(os.path.exists(sample))
shutil.rmtree(os.path.join("/tmp", now))
p = mailparser.parse_from_file(mail_test_10)
attachments = MailAttachments.withhashes(p.attachments)
attachments(intelligence=False)
store_samples(conf, attachments)
sample = os.path.join(
"/tmp",
now,
"2ea90c996ca28f751d4841e6c67892b8_REQUEST FOR QUOTE.zip")
self.assertTrue(os.path.exists(sample))
shutil.rmtree(os.path.join("/tmp", now))
def test_store_samples(self):
"""Test add store file system processing."""
from datetime import datetime
import shutil
from src.modules.attachments import store_samples
# Complete parameters
conf = {"enabled": True,
"base_path": "/tmp"}
attachments = MailAttachments.withhashes(self.attachments)
attachments(intelligence=False)
store_samples(conf, attachments)
now = six.text_type(datetime.utcnow().date())
sample = os.path.join(
"/tmp",
now,
"1e38e543279912d98cbfdc7b275a415e_20160523_916527.jpg_.zip")
sample_child = os.path.join(
"/tmp",
now,
"495315553b8af47daada4b279717f651_20160523_211439.jpg_.jpg.exe")
self.assertFalse(os.path.exists(sample))
self.assertTrue(os.path.exists(sample_child))
shutil.rmtree(os.path.join("/tmp", now))
if __name__ == '__main__':
unittest.main(verbosity=2)