-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathO365-EmailHeaderAnalyzer.ps1
More file actions
285 lines (242 loc) · 9.91 KB
/
O365-EmailHeaderAnalyzer.ps1
File metadata and controls
285 lines (242 loc) · 9.91 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
#> Functions
function Get-ExternalSenderIP {
param ($headers)
# Check for "Received:" lines first (IPv4)
$matches = [regex]::Matches($headers, "Received: from \S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\)")
if ($matches.Count -gt 0) {
foreach ($match in $matches) {
$ip = $match.Groups[1].Value
if ($ip -notmatch "^127\." -and $ip -notmatch "^10\." -and $ip -notmatch "^192\." -and $ip -notmatch "^172\.") {
return $ip
}
}
}
# Check for "Received:" lines first (IPv6)
$matches = [regex]::Matches($headers, "Received: from \S+ \(\[?([a-fA-F0-9:]+)\]?\)")
if ($matches.Count -gt 0) {
foreach ($match in $matches) {
$ip = $match.Groups[1].Value
if ($ip -notmatch "^::1$" -and $ip -notmatch "^fc00:" -and $ip -notmatch "^fd00:" -and $ip -notmatch "^fe80:") {
return $ip
}
}
}
# Check for "Received-SPF:" lines (IPv4)
$matches = [regex]::Matches($headers, "Received-SPF: \S+ \((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\)")
if ($matches.Count -gt 0) {
foreach ($match in $matches) {
$ip = $match.Groups[1].Value
if ($ip -notmatch "^127\." -and $ip -notmatch "^10\." -and $ip -notmatch "^192\." -and $ip -notmatch "^172\.") {
return $ip
}
}
}
# Check for "Received-SPF:" lines (IPv6)
$matches = [regex]::Matches($headers, "Received-SPF: \S+ \(\[?([a-fA-F0-9:]+)\]?\)")
if ($matches.Count -gt 0) {
foreach ($match in $matches) {
$ip = $match.Groups[1].Value
if ($ip -notmatch "^::1$" -and $ip -notmatch "^fc00:" -and $ip -notmatch "^fd00:" -and $ip -notmatch "^fe80:") {
return $ip
}
}
}
return "not found"
}
function Get-Headers {
$headers = $txtHeaders.Text
if ([string]::IsNullOrWhiteSpace($headers)) { return }
$textboxes["Sender IP"].Text = Get-ExternalSenderIP $headers
$spfStatus = if ($headers -match "spf=(pass|fail|softfail|neutral)") { $matches[1] } else { "Unknown" }
$dkimStatus = if ($headers -match "dkim=(pass|fail|none)") { $matches[1] } else { "Unknown" }
$dmarcStatus = if ($spfStatus -eq "pass" -and $dkimStatus -eq "pass") { "Compliant" } else { "Non-Compliant" }
$textboxes["SPF"].Text = $spfStatus
$textboxes["DKIM"].Text = $dkimStatus
$textboxes["DMARC"].Text = $dmarcStatus
$emojiLabels["SPF"].Text = if ($spfStatus -eq "pass") { " $greenCheck" } else { " $redCross" }
$emojiLabels["DKIM"].Text = if ($dkimStatus -eq "pass") { " $greenCheck" } else { " $redCross" }
$emojiLabels["DMARC"].Text = if ($dmarcStatus -eq "Compliant") { " $greenCheck" } else { " $redCross" }
$classification = "External"
if ($headers -match "X-MS-Exchange-Organization-AuthAs:\s*(\w+)") {
switch ($matches[1]) {
"Internal" { $classification = "Internal" }
"Partner" { $classification = "Internal (Partner)" }
"Anonymous" { $classification = "External" }
"Authenticated" { $classification = "External" }
}
}
$textboxes["O365 Classification"].Text = $classification
$textboxes["Message Source"].Text = $classification
# Subject (raw MIME-safe, compliance copy/paste)
$subject = "ERROR / NOT FOUND"
if ($headers -match "(?m)^Subject:\s*(.+)$") {
$rawSubject = $matches[1].Trim()
if ($rawSubject -match "^\=\?utf-8\?q\?(.+?)\?=$") {
# Extract without decoding, preserve underscores
$subject = $matches[1]
} else {
$subject = $rawSubject
}
}
$textboxes["Subject (Header)"].Text = $subject
# Sender (Auth)
$senderAuth = "NOT FOUND (Potentially Spoofed/Spam/Phishing)"
if ($headers -match "(?m)^Sender:\s*(.+)$") {
$senderAuth = $matches[1].Trim()
if ($senderAuth -match "<(.+?)>") {
$senderAuth = $matches[1]
}
} elseif ($headers -match "(?m)^From:\s*(.+)$") {
$senderAuth = $matches[1].Trim()
if ($senderAuth -match "<(.+?)>") {
$senderAuth = $matches[1]
}
}
if ($headers -match "X-MS-Exchange-Organization-AuthAs:\s*Anonymous") {
$senderAuth += " (Unauthenticated / Likely Spoofed)"
}
$textboxes["Sender (Auth)"].Text = $senderAuth
# Sender (Envelope / Return Path)
$envelopeSender = if ($headers -match "Return-Path:\s*<?([^>\s]+)>?") {
$matches[1].Trim()
} else {
"NOT FOUND (Potentially Spoofed/Spam/Phishing)"
}
$textboxes["Sender (Envelope)"].Text = $envelopeSender
# Sender (Header / Outlook)
$headerSender = "NOT FOUND (Potentially Spoofed/Spam/Phishing)"
if ($headers -match "(?m)^From:\s*(.+)$") {
$headerSender = $matches[1].Trim()
if ($headerSender -match "<(.+?)>") {
$headerSender = $matches[1]
}
}
$textboxes["Sender (Header)"].Text = $headerSender
}
#> GUI
Add-Type -AssemblyName System.Windows.Forms
$greenCheck = [char]::ConvertFromUtf32(0x2705)
$redCross = [char]::ConvertFromUtf32(0x274C)
$form = New-Object System.Windows.Forms.Form
$form.Text = "O365 - Email Header Analyzer"
#$form.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\GitHub\O365-EmailHeaderAnalyzer\email_icon.ico")
$form.Size = New-Object System.Drawing.Size(600, 580)
$form.StartPosition = "CenterScreen"
#$form.TopMost = $true
$lblMessageDetails = New-Object System.Windows.Forms.Label
$lblMessageDetails.Text = "Message Details:"
$lblMessageDetails.Location = New-Object System.Drawing.Point(10, 10)
$lblMessageDetails.AutoSize = $true
$form.Controls.Add($lblMessageDetails)
$form.Controls.Add($lblMessageDetails)
$btnPaste = New-Object System.Windows.Forms.Button
$btnPaste.Size = New-Object System.Drawing.Size(30, 25)
$btnPaste.Location = New-Object System.Drawing.Point(540, 5)
$btnPaste.Font = New-Object System.Drawing.Font("Segoe UI Emoji", 12)
$btnPaste.Text = [char]::ConvertFromUtf32(0x1F4CB)
$btnPaste.Add_Click({
if ([Windows.Forms.Clipboard]::ContainsText()) {
$txtHeaders.Text = [Windows.Forms.Clipboard]::GetText()
}
})
$form.Controls.Add($btnPaste)
$txtHeaders = New-Object System.Windows.Forms.TextBox
$txtHeaders.Multiline = $true
$txtHeaders.ScrollBars = "Vertical"
$txtHeaders.Size = New-Object System.Drawing.Size(560, 150)
$txtHeaders.Location = New-Object System.Drawing.Point(10, 30)
$txtHeaders.Add_Click({ $txtHeaders.Clear() })
$form.Controls.Add($txtHeaders)
$labels = @("Sender IP", "SPF", "DKIM", "DMARC", "Subject (Header)", "Sender (Auth)", "Sender (Envelope)", "Sender (Header)", "O365 Classification", "Message Source")
$textboxes = @{}
$emojiLabels = @{}
$yPos = 200
foreach ($label in $labels) {
$lbl = New-Object System.Windows.Forms.Label
$lbl.Text = "${label}:"
$lbl.Location = New-Object System.Drawing.Point(10, $yPos)
$lbl.AutoSize = $true
$form.Controls.Add($lbl)
$txt = New-Object System.Windows.Forms.TextBox
$txt.Size = New-Object System.Drawing.Size(150, 20)
if ($label -in @("Subject (Header)", "Sender (Auth)","Sender (Envelope)", "Sender (Header)")) {
$txt.Size = New-Object System.Drawing.Size(450, 20)
} else {
$txt.Size = New-Object System.Drawing.Size(150, 20)
}
$txt.Location = New-Object System.Drawing.Point(120, $yPos)
$txt.ReadOnly = $true
$form.Controls.Add($txt)
$textboxes[$label] = $txt
if ($label -eq "Sender IP") {
# Copy Button
$btnCopyIP = New-Object System.Windows.Forms.Button
$btnCopyIP.Text = "Copy"
$btnCopyIP.Size = New-Object System.Drawing.Size(50, 20)
$btnCopyIP.Location = New-Object System.Drawing.Point(280, $yPos)
$btnCopyIP.Add_Click({
[System.Windows.Forms.Clipboard]::SetText($textboxes["Sender IP"].Text)
})
$form.Controls.Add($btnCopyIP)
# IP Info Button
$btnIPInfo = New-Object System.Windows.Forms.Button
$btnIPInfo.Text = "Info"
$btnIPInfo.Size = New-Object System.Drawing.Size(50, 20)
$btnIPInfo.Location = New-Object System.Drawing.Point(340, $yPos)
$btnIPInfo.Add_Click({
$ip = $textboxes["Sender IP"].Text
if ($ip -and $ip -ne "not found") {
Start-Process "https://ipinfo.io/$ip"
}
})
$form.Controls.Add($btnIPInfo)
}
if ($label -in @("SPF", "DKIM", "DMARC")) {
$emojiLbl = New-Object System.Windows.Forms.Label
$emojiLbl.Location = New-Object System.Drawing.Point(280, $yPos)
$emojiLbl.AutoSize = $true
$emojiLbl.Font = New-Object System.Drawing.Font("Segoe UI Emoji", 10)
$form.Controls.Add($emojiLbl)
$emojiLabels[$label] = $emojiLbl
}
$yPos += 30
}
$lblSpacing = New-Object System.Windows.Forms.Label
$lblSpacing.Text = ""
$lblSpacing.Location = New-Object System.Drawing.Point(10, 400)
$lblSpacing.AutoSize = $true
$form.Controls.Add($lblSpacing)
$form.Add_Shown({ $txtHeaders.Focus() })
$form.add_FormClosed({
param($sender, $e)
$form.Dispose()
})
####################
$btnAnalyze = New-Object System.Windows.Forms.Button
$btnAnalyze.Text = "Analyze"
$btnAnalyze.Location = New-Object System.Drawing.Point(10, 505)
$btnAnalyze.Add_Click({ Get-Headers })
$form.Controls.Add($btnAnalyze)
$btnReset = New-Object System.Windows.Forms.Button
$btnReset.Text = "Reset"
$btnReset.Location = New-Object System.Drawing.Point(100,505)
$btnReset.Add_Click({
$txtHeaders.Text = ""
foreach ($key in $textboxes.Keys) { $textboxes[$key].Text = "" }
foreach ($key in $emojiLabels.Keys) { $emojiLabels[$key].Text = "" }
})
$form.Controls.Add($btnReset)
$txtHeaders.Add_KeyDown({
param ($sender, $e)
if ($e.KeyCode -eq "Enter") {
Get-Headers
$e.SuppressKeyPress = $true
}
})
$txtHeaders.Add_TextChanged({
if (-not [string]::IsNullOrWhiteSpace($txtHeaders.Text)) {
Start-Sleep -Milliseconds 1000
Get-Headers
}
})
$form.ShowDialog()