-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphishingformat.py
207 lines (182 loc) · 6.32 KB
/
phishingformat.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
import json
import traceback
import html as h
def dicttable(j, html, prepend=None, cell='cell'):
for k in j:
html += "<tr>"
key = k
if prepend:
key = prepend + k
try:
if not isinstance(j[k], dict):
html += "<td id='key' class='" + cell + "'>" + key + \
"</td><td id='value' class='" + cell + \
"'>" + h.escape(str(j[k])) + "</td>"
else:
html += "<td id='key' class='" + cell + "'>" + key
html += "</td><td id='value' class='" + cell + "'><pre>" + \
h.escape(
json.dumps(
j[k],
sort_keys=True,
indent=4)) + "</pre></td>"
except BaseException:
print j
traceback.print_exc()
raise
html += "</tr>"
return html
def misprow(value, conf):
if not value["results"]:
return ""
for r in value["results"]:
r = r["Event"]
h = "<tr><td id='misp'>" + value['item'] + "</td>"
h += "<td id='misp'>" + r['date'] + "</td>"
h += "<td id='misp'><a href='" + \
conf['mispui'] + "/events/view/" + \
r['id'] + "'>" + r['info'] + "</a></td>"
h += "<td id='misp'>"
if not 'Tag' in r:
r['Tag'] = []
for tag in r['Tag']:
h += "<span id='tag' style='background-color:" + \
tag['colour'] + ";'>" + tag['name'] + "</span>"
h += "</td><td id='misp'>" + str(r['attribute_count']) + "</td>"
h += "</tr>"
h += "</tr>"
return h
def phishingformat(mail, j, lh, conf):
if not "m" in j:
return
lh.debug("Email analysis formatting started for:" + mail.subject)
style = None
with open("phishing.css") as f:
style = "<style>\n"
style += f.read()
style += "\n</style>"
feye = falcon = misp = []
phish = adinfo = falconhost = {}
mispfound = False
try:
if "FalconDetections" in j:
falcon = j.pop("FalconDetections")
if "FireEyeDetections" in j:
feye = j.pop("FireEyeDetections")
if "adenrichment" in j:
adinfo = j.pop("adenrichment")
if "m" in j:
phish = j.pop("m")
if not phish or len(phish) < 1:
phish = j
if 'misp' in j and j['misp']:
misp = j.pop('misp')
html = ""
# html+="<div id='detection'>"
# html+="<div id='title'><h3> Phishing Email Submission </h3></div>"
# html+="<table>"
# if "m" in j:
# html=dicttable(j["m"],html,cell='detection')
# else:
# html=dicttable(j,html,cell='detection')
#
# html+="</table></div>"
if misp:
html += "<div id='misp'>"
html += "<div id='title'><h3> MISP matches</h3></div>"
html += "<table>"
html += "<tr><b><td id='misp'>Indicator of attack</td><td id='misp'>Date</td><td id='misp'>MISP Event</td><td id='misp'>Tags</td><td id='misp'>Attribute count</td></b></tr>\n"
# lh.debug(json.dumps(misp,sort_keys=True,indent=4))
for m in misp:
mr = misprow(m, conf)
html += mr
if mr:
mispfound = True
html += "</table></div>"
html += "<div id='userinfo'>"
html += "<div id='title'><h3> AD Information for User</h3></div>"
html += "<table>"
blacklist = [
"instanceType",
"codePage",
"msExchRecipientTypeDetails",
"countryCode",
"msExchUserAccountControl",
"lastLogoff"]
info = {}
for i in adinfo:
if not i in blacklist:
info[i] = adinfo[i]
html = dicttable(info, html, cell='userinfo')
html += "</table></div>"
# html+="<div id='computerinfo'>"
# html+="<div id='title'><h3> AD Information for Computer</h3></div>"
# html+="<table>"
# html=dicttable(computerinfo,html,cell='computerinfo')
# html+="</table></div>"
# html+="<div id='falconhost'>"
# html+="<div id='title'><h3> FalconHost Data</h3></div>"
# html+="<table>"
# for d in falconhost:
# html=dicttable(d,html,cell='falconhost')
# html+="</table></div>"
html += "<div id='falcon'>"
html += "<div id='title'><h3> Recent Crowdstrike Falcon Detections </h3></div>"
for d in falcon[:3]:
html += "<table>"
e = d
if "event" in d:
e = d["event"]
detect = {}
if 'adenrichment' in d:
d.pop('adenrichment')
blacklist = [
"ScanResults",
"QuarantineFiles",
"adenrichment",
"PatternDispositionDescription",
"PatternDispositionFlags"]
for k in e:
if not k in blacklist:
detect[k] = e[k]
html = dicttable(detect, html, 'falcon')
html += "</table><br><br>"
html += "<div id='fireeye'>"
html += "<div id='title'><h3> Recent FireEye Detections</h3></div>"
for d in feye[:3]:
html += "<table>"
if 'adenrichment' in d:
d.pop('adenrichment')
html = dicttable(d, html, cell='fireeye')
html += "</table><br><br>"
html += "</div>"
html += "</body></html>"
# feye=falcon=phish=adinfo=falconhost={}
if feye:
j["FalconDetections"] = falcon
if phish:
j["FireEyeDetections"] = feye
if adinfo:
j["adenrichment"] = adinfo
mail.body += "<html><head>" + style + "</head><body>" + html
lh.debug("Done formatting email analysis")
cats = ["Processed"]
if mispfound:
cats.append("MISP Match")
if not isinstance(mail.categories, list):
mail.categories = cats # ["Processed"]
else:
mail.categories += cats
mail.save()
return html
except Exception as e:
lh.exception(str(e))
traceback.print_exc()
print j
def phishingformatmd(j, mail):
if not isinstance(mail.categories, list):
mail.categories = ["Processed"]
else:
mail.categories.append("Processed")
mail.save()
return json.dumps(j, indent=4, sort_keys=True)