-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
test_phishing.py
141 lines (109 loc) · 4.52 KB
/
test_phishing.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2017 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 copy
import os
import unittest
import mailparser
from pyfaup.faup import Faup
from context import mails
from context import utils
phishing = mails.phishing
base_path = os.path.realpath(os.path.dirname(__file__))
mail_thug = os.path.join(base_path, 'samples', 'mail_thug')
mail_form = os.path.join(base_path, 'samples', 'mail_form')
mail_test_5 = os.path.join(base_path, 'samples', 'mail_test_5')
mail_test_6 = os.path.join(base_path, 'samples', 'mail_test_6')
logging.getLogger().addHandler(logging.NullHandler())
class TestPhishing(unittest.TestCase):
faup = Faup()
def setUp(self):
parser = mailparser.parse_from_file(mail_thug)
self.email = parser.mail
self.attachments = parser.attachments
parser = mailparser.parse_from_file(mail_form)
self.email_form = parser.mail
body = self.email_form.get("body")
self.urls = utils.urls_extractor(body, self.faup)
d = {"generic": "conf/keywords/targets.example.yml",
"custom": "conf/keywords/targets_english.example.yml"}
self.targets = utils.load_keywords_dict(d)
d = {"generic": "conf/keywords/subjects.example.yml",
"custom": "conf/keywords/subjects_english.example.yml"}
self.subjects = utils.load_keywords_list(d)
def test_ParserError(self):
parser = mailparser.parse_from_file(mail_test_6)
body = parser.mail.get("body")
flag_form = phishing.check_form(body)
self.assertFalse(flag_form)
def test_none_values(self):
email = copy.deepcopy(self.email)
email.pop("body", None)
email.pop("subjects", None)
email.pop("from", None)
phishing.check_phishing(
email=email,
attachments=self.attachments,
urls_body=self.urls,
urls_attachments=self.urls,
target_keys=self.targets,
subject_keys=self.subjects)
def test_check_form(self):
body = self.email_form.get("body")
flag_form = phishing.check_form(body)
self.assertTrue(flag_form)
body = self.email.get("body")
flag_form = phishing.check_form(body)
self.assertFalse(flag_form)
def test_form_value_error(self):
parser = mailparser.parse_from_file(mail_test_5)
body = parser.mail.get("body")
flag_form = phishing.check_form(body)
self.assertFalse(flag_form)
def test_check_urls(self):
flag = False
if any(phishing.check_urls(self.urls, i)
for i in self.targets.values()):
flag = True
self.assertTrue(flag)
def test_check_phishing(self):
results = phishing.check_phishing(
email=self.email,
attachments=self.attachments,
urls_body=self.urls,
urls_attachments=self.urls,
target_keys=self.targets,
subject_keys=self.subjects)
self.assertIsInstance(results, dict)
self.assertEqual(results["score"], 123)
self.assertIn("filename_attachments", results["score_expanded"])
self.assertIn("mail_subject", results["score_expanded"])
self.assertIn("mail_body", results["score_expanded"])
self.assertIn("mail_from", results["score_expanded"])
self.assertIn("urls_body", results["score_expanded"])
self.assertIn("urls_attachments", results["score_expanded"])
self.assertIn("Test", results["targets"])
self.assertTrue(results["with_phishing"])
def test_check_phishing_form(self):
results = phishing.check_phishing(
email=self.email_form,
attachments=self.attachments,
urls_body=self.urls,
urls_attachments=self.urls,
target_keys=self.targets,
subject_keys=self.subjects)
self.assertIn("mail_form", results["score_expanded"])
if __name__ == '__main__':
unittest.main(verbosity=2)