-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ParseEmailFilesV2_test.py
583 lines (504 loc) · 21.8 KB
/
ParseEmailFilesV2_test.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import pytest
import demistomock as demisto
from CommonServerPython import *
from ParseEmailFilesV2 import main, data_to_md, parse_nesting_level
def exec_command_for_file(
file_path,
info="RFC 822 mail text, with CRLF line terminators",
file_name=None,
file_type="",
):
"""
Return a executeCommand function which will return the passed path as an entry to the call 'getFilePath'
Arguments:
file_path {string} -- file name of file residing in test_data dir
Raises:
ValueError: if call with differed name from getFilePath or getEntry
Returns:
[function] -- function to be used for mocking
"""
if not file_name:
file_name = file_path
path = 'test_data/' + file_path
def executeCommand(name, args=None):
if name == 'getFilePath':
return [
{
'Type': entryTypes['note'],
'Contents': {
'path': path,
'name': file_name
}
}
]
elif name == 'getEntry':
return [
{
'Type': entryTypes['file'],
'FileMetadata': {
'info': info,
'type': file_type
}
}
]
else:
raise ValueError(f'Unimplemented command called: {name}')
return executeCommand
def test_eml_type(mocker):
"""
Given:
- A eml file
When:
- run the ParseEmailFilesV2 script
Then:
- Ensure its was parsed successfully
"""
def executeCommand(name, args=None):
if name == 'getFilePath':
return [
{
'Type': entryTypes['note'],
'Contents': {
'path': 'test_data/smtp_email_type.eml',
'name': 'smtp_email_type.eml'
}
}
]
elif name == 'getEntry':
return [
{
'Type': entryTypes['file'],
'FileMetadata': {
'info': 'SMTP mail, UTF-8 Unicode text, with CRLF terminators'
}
}
]
else:
raise ValueError(f'Unimplemented command called: {name}')
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand', side_effect=executeCommand)
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['SMTP mail, UTF-8 Unicode text, with CRLF terminators'])
mocker.patch.object(demisto, 'results')
# validate our mocks are good
assert demisto.args()['entryid'] == 'test'
# assert demisto.executeCommand('getFilePath', {})[0]['Type'] == entryTypes['note']
main()
assert demisto.results.call_count == 1
# call_args is tuple (args list, kwargs). we only need the first one
results = demisto.results.call_args[0]
assert len(results) == 1
assert results[0]['Type'] == entryTypes['note']
assert results[0]['EntryContext']['Email']['Subject'] == 'Test Smtp Email'
def test_eml_contains_eml(mocker):
"""
Given:
- A eml file contains eml
When:
- run the ParseEmailFilesV2 script
Then:
- Ensure the was parsed successfully
- Ensure both files was parsed
- Ensure the attachments was returned
"""
def executeCommand(name, args=None):
if name == 'getFilePath':
return [
{
'Type': entryTypes['note'],
'Contents': {
'path': 'test_data/Fwd_test-inner_attachment_eml.eml',
'name': 'Fwd_test-inner_attachment_eml.eml'
}
}
]
elif name == 'getEntry':
return [
{
'Type': entryTypes['file'],
'FileMetadata': {
'info': 'news or mail text, ASCII text'
}
}
]
else:
raise ValueError(f'Unimplemented command called: {name}')
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand', side_effect=executeCommand)
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['news or mail text, ASCII text'])
mocker.patch.object(demisto, 'results')
# validate our mocks are good
assert demisto.args()['entryid'] == 'test'
main()
assert demisto.results.call_count == 4
# call_args is tuple (args list, kwargs). we only need the first one
results = demisto.results.call_args_list
assert len(results) == 4
assert results[0].args[0]['File'] == 'ArcSight_ESM_fixes.yml'
assert results[1].args[0]['File'] == 'test - inner attachment eml.eml'
assert results[2].args[0]['EntryContext']['Email']['Subject'] == 'Fwd: test - inner attachment eml'
assert 'ArcSight_ESM_fixes.yml' in results[2].args[0]['EntryContext']['Email']['Attachments']
assert 'ArcSight_ESM_fixes.yml' in results[2].args[0]['EntryContext']['Email']['AttachmentsData'][0]['Name']
assert 'test - inner attachment eml.eml' in results[2].args[0]['EntryContext']['Email']['Attachments']
assert 'test - inner attachment eml.eml' in results[2].args[0]['EntryContext']['Email']['AttachmentsData'][1]['Name']
assert results[2].args[0]['EntryContext']['Email']['Depth'] == 0
assert results[3].args[0]['EntryContext']['Email']["Subject"] == 'test - inner attachment eml'
assert results[3].args[0]['EntryContext']['Email']['Depth'] == 1
def test_eml_contains_msg(mocker):
"""
Given:
- A eml file contains msg
When:
- run the ParseEmailFilesV2 script
Then:
- Ensure the was parsed successfully
- Ensure both files was parsed
- Ensure the attachments was returned
"""
def executeCommand(name, args=None):
if name == 'getFilePath':
return [
{
'Type': entryTypes['note'],
'Contents': {
'path': 'test_data/DONT_OPEN-MALICIOUS.eml',
'name': 'DONT_OPEN-MALICIOUS.eml'
}
}
]
elif name == 'getEntry':
return [
{
'Type': entryTypes['file'],
'FileMetadata': {
'info': 'news or mail text, ASCII text'
}
}
]
else:
raise ValueError(f'Unimplemented command called: {name}')
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand', side_effect=executeCommand)
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['news or mail text, ASCII text'])
mocker.patch.object(demisto, 'results')
# validate our mocks are good
assert demisto.args()['entryid'] == 'test'
main()
results = demisto.results.call_args_list
assert demisto.results.call_count == 3
assert len(results) == 3
assert results[0].args[0]['File'] == 'Attacker+email+.msg'
assert results[1].args[0]['EntryContext']['Email']['Subject'] == 'DONT OPEN - MALICIOS'
assert 'Attacker+email+.msg' in results[1].args[0]['EntryContext']['Email']['Attachments']
assert 'Attacker+email+.msg' in results[1].args[0]['EntryContext']['Email']['AttachmentsData'][0]['Name']
assert results[1].args[0]['EntryContext']['Email']['Depth'] == 0
assert results[2].args[0]['EntryContext']['Email']["Subject"] == 'Attacker email'
assert results[2].args[0]['EntryContext']['Email']['Depth'] == 1
def test_eml_contains_eml_depth(mocker):
"""
Given:
- A eml file contains eml
- depth = 1
When:
- run the ParseEmailFilesV2 script
Then:
- Ensure only the first mail is parsed
- Ensure the attachments of the first mail was returned
"""
def executeCommand(name, args=None):
if name == 'getFilePath':
return [
{
'Type': entryTypes['note'],
'Contents': {
'path': 'test_data/Fwd_test-inner_attachment_eml.eml',
'name': 'Fwd_test-inner_attachment_eml.eml'
}
}
]
elif name == 'getEntry':
return [
{
'Type': entryTypes['file'],
'FileMetadata': {
'info': 'news or mail text, ASCII text'
}
}
]
else:
raise ValueError(f'Unimplemented command called: {name}')
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test', 'max_depth': '1'})
mocker.patch.object(demisto, 'executeCommand', side_effect=executeCommand)
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['news or mail text, ASCII text'])
mocker.patch.object(demisto, 'results')
# validate our mocks are good
assert demisto.args()['entryid'] == 'test'
main()
assert demisto.results.call_count == 3
# call_args is tuple (args list, kwargs). we only need the first one
results = demisto.results.call_args_list
assert len(results) == 3
assert results[0].args[0]['File'] == 'ArcSight_ESM_fixes.yml'
assert results[1].args[0]['File'] == 'test - inner attachment eml.eml'
assert results[2].args[0]['EntryContext']['Email']['Depth'] == 0
assert 'ArcSight_ESM_fixes.yml' in results[2].args[0]['EntryContext']['Email']['Attachments']
assert 'ArcSight_ESM_fixes.yml' in results[2].args[0]['EntryContext']['Email']['AttachmentsData'][0]['Name']
assert 'test - inner attachment eml.eml' in results[2].args[0]['EntryContext']['Email']['Attachments']
assert 'test - inner attachment eml.eml' in results[2].args[0]['EntryContext']['Email']['AttachmentsData'][1]['Name']
def test_msg(mocker):
"""
Given:
- A msg file
When:
- run the ParseEmailFilesV2 script
Then:
- Ensure its was parsed successfully
"""
info = 'CDFV2 Microsoft Outlook Message'
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand', side_effect=exec_command_for_file('smime-p7s.msg', info=info))
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['CDFV2 Microsoft Outlook Message'])
mocker.patch.object(demisto, 'results')
# validate our mocks are good
assert demisto.args()['entryid'] == 'test'
main()
# assert demisto.results.call_count == 1
# call_args is tuple (args list, kwargs). we only need the first one
results = demisto.results.call_args[0]
assert len(results) == 1
assert results[0]['Type'] == entryTypes['note']
assert results[0]['EntryContext']['Email']['Subject'] == 'test'
def test_no_content_type_file(mocker):
"""
Given:
- A eml with no_content_type
When:
- run the ParseEmailFilesV2 script
Then:
- Ensure its was parsed successfully
"""
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand',
side_effect=exec_command_for_file('no_content_type.eml', info="ascii text"))
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['ascii text'])
mocker.patch.object(demisto, 'results')
main()
results = demisto.results.call_args[0]
assert len(results) == 1
assert results[0]['Type'] == entryTypes['note']
assert results[0]['EntryContext']['Email']['Subject'] == 'No content type'
def test_no_content_file(mocker):
"""
Given:
- A eml without content
When:
- run the ParseEmailFilesV2 script
Then:
- Ensure a error is returned
"""
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand',
side_effect=exec_command_for_file('no_content.eml', info="ascii text"))
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['ascii text'])
mocker.patch.object(demisto, 'results')
try:
main()
except SystemExit:
gotexception = True
assert gotexception
results = demisto.results.call_args[0]
assert len(results) == 1
assert 'Could not extract email from file' in results[0]['Contents']
def test_md_output_empty_body_text():
"""
Given:
- The input email_data where the value of the 'Text' field is None.
When:
- Running the data_to_md command on this email_data.
Then:
- Validate that output the md doesn't contain a row for the 'Text' field.
"""
email_data = {
'To': 'email1@paloaltonetworks.com',
'From': 'email2@paloaltonetworks.com',
'Text': None
}
expected = '### Results:\n' \
u'* From:\temail2@paloaltonetworks.com\n' \
u'* To:\temail1@paloaltonetworks.com\n' \
u'* CC:\t\n' \
u'* Subject:\t\n' \
u'* Attachments:\t\n\n\n' \
u'### HeadersMap\n' \
u'**No entries.**\n'
md = data_to_md(email_data)
assert expected == md
email_data = {
'To': 'email1@paloaltonetworks.com',
'From': 'email2@paloaltonetworks.com',
}
expected = '### Results:\n' \
u'* From:\temail2@paloaltonetworks.com\n' \
u'* To:\temail1@paloaltonetworks.com\n' \
u'* CC:\t\n' \
u'* Subject:\t\n' \
u'* Attachments:\t\n\n\n' \
u'### HeadersMap\n' \
u'**No entries.**\n'
md = data_to_md(email_data)
assert expected == md
def test_md_output_with_body_text():
"""
Given:
- The input email_data with a value in the 'Text' field.
When:
- Running the data_to_md command on this email_data.
Then:
- Validate that the output md contains a row for the 'Text' field.
"""
email_data = {
'To': 'email1@paloaltonetworks.com',
'From': 'email2@paloaltonetworks.com',
'Text': '<email text>'
}
expected = '### Results:\n' \
u'* From:\temail2@paloaltonetworks.com\n' \
u'* To:\temail1@paloaltonetworks.com\n' \
u'* CC:\t\n' \
u'* Subject:\t\n' \
u'* Body/Text:\t[email text]\n' \
u'* Attachments:\t\n\n\n' \
u'### HeadersMap\n' \
u'**No entries.**\n'
md = data_to_md(email_data)
assert expected == md
@pytest.mark.parametrize('nesting_level_to_return, output, res', [('All files', ['output1', 'output2', 'output3'],
['output1', 'output2', 'output3']),
('Outer file', ['output1', 'output2', 'output3'],
['output1']),
('Inner file', ['output1', 'output2', 'output3'],
['output3'])])
def test_parse_nesting_level(nesting_level_to_return, output, res):
"""
Given:
- parsed email output, nesting_level_to_return param - All files.
- parsed email output, nesting_level_to_return param - Outer file.
- parsed email output, nesting_level_to_return param - Inner file.
When:
calling the parse_nesting_level function.
Then:
- Validating the that all outputs are returned.
- Validating the that only output1 is returned.
- Validating the that only output3 is returned.
"""
assert parse_nesting_level(nesting_level_to_return, output) == res
@pytest.mark.parametrize('nesting_level_to_return, results_len, depth, results_index', [('All files', 4, 0, 2),
('Outer file', 3, 0, 2),
('Inner file', 1, 1, 0)])
def test_eml_contains_eml_nesting_level(mocker, nesting_level_to_return, results_len, depth, results_index):
"""
Given:
- A eml file contains eml, nesting_level_to_return param - All files.
- A eml file contains eml, nesting_level_to_return param - Outer file.
- A eml file contains eml, nesting_level_to_return param - Inner file.
When: parsing the eml file.
Then:
- Validating the that call_args_list length is 4 (2 parsed eml files and 2 attachments).
- Validating the that call_args_list length is 3 (the outer parsed eml file and is 2 attachments).
- Validating the that call_args_list length is 1 ( the Inner parsed eml file).
"""
def executeCommand(name, args=None):
if name == 'getFilePath':
return [
{
'Type': entryTypes['note'],
'Contents': {
'path': 'test_data/Fwd_test-inner_attachment_eml.eml',
'name': 'Fwd_test-inner_attachment_eml.eml'
}
}
]
elif name == 'getEntry':
return [
{
'Type': entryTypes['file'],
'FileMetadata': {
'info': 'news or mail text, ASCII text'
}
}
]
else:
raise ValueError(f'Unimplemented command called: {name}')
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test',
'nesting_level_to_return': nesting_level_to_return})
mocker.patch.object(demisto, 'context')
mocker.patch.object(demisto, 'dt', return_value=['news or mail text, ASCII text'])
mocker.patch.object(demisto, 'executeCommand', side_effect=executeCommand)
mocker.patch.object(demisto, 'results')
main()
# call_args is tuple (args list, kwargs). we only need the first one
results = demisto.results.call_args_list
assert len(results) == results_len
assert results[results_index].args[0]['EntryContext']['Email']['Depth'] == depth
def test_eml_contains_empty_htm_not_containing_file_data(mocker):
"""
Given: An email containing both an empty text file and a base64 encoded htm file.
When: Parsing a valid email file with default parameters.
Then: FileData is not one of the attachments' data attributes returned.
"""
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand',
side_effect=exec_command_for_file('eml_contains_emptytxt_htm_file.eml'))
mocker.patch.object(demisto, 'results')
assert demisto.args()['entryid'] == 'test'
main()
results = demisto.results.call_args[0]
assert results[0]['EntryContext']['Email']['AttachmentsData'][0]['FileData'] is None
def test_smime_without_to_from_subject(mocker):
"""
Given:
multipart/signed p7m file without "To"/"From"/"Subject" fields contains an eml attachment
When:
Parsing the file
Then:
The attachment files are saved to the war-room
"""
save_file = mocker.patch('ParseEmailFilesV2.save_file', return_value='mocked_file_path')
mocker.patch.object(demisto, 'args', return_value={'entryid': 'test'})
mocker.patch.object(demisto, 'executeCommand',
side_effect=exec_command_for_file('smime_without_fields.p7m',
info="ascii text",
file_type='multipart/signed; '
'protocol="application/pkcs7-signature";, ASCII text'))
mocker.patch.object(demisto, 'results')
expected_email_content = ('Return-Path: <testing@gmail.com>\n'
'Received: from [172.31.255.255] ([172.31.255.255])\n'
' by smtp.gmail.com with ESMTPSA id t6sm46056484wmb.29.2019.07.23.05.38.26\n'
' for <testing@gmail.com>\n'
' (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\n'
' Tue, 23 Jul 2019 05:38:26 -0700 (PDT)\n'
'To: testing@gmail.com\n'
'From: test ing <testing@gmail.com>\n'
'Subject: Testing Email Attachment\n'
'Message-ID: <a853a1b0-1ffe-4e37-d9a9-a27c6bc0bd5b@gmail.com>\n'
'Date: Tue, 23 Jul 2019 15:38:25 +0300\n'
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0)\n'
' Gecko/20100101 Thunderbird/60.8.0\n'
'MIME-Version: 1.0\n'
'Content-Type: text/plain; charset=utf-8; format=flowed\n'
'Content-Transfer-Encoding: 7bit\n'
'Content-Language: en-US\n'
'\n'
'This is the body of the attachment.')
main()
# Assert that save_file was called with the expected arguments
save_file.assert_called_once_with('Attachment.eml', expected_email_content)
results = demisto.results.call_args[0]
assert len(results) == 1
assert results[0]['EntryContext']['Email']['FileName'] == 'Attachment.eml'