-
Notifications
You must be signed in to change notification settings - Fork 19
/
Remove-SpamEmail.ps1
282 lines (220 loc) · 13.9 KB
/
Remove-SpamEmail.ps1
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
<#
.NAME
Remove-SpamEmail
.SYNOPSIS
This cmdlet is used for removing a malicious or spam email message from all inboxes the email was sent too.
For Exchange Servers in the cloud this cmdlet assumes you are in the United States. The -ConnectionUri parameter
will vary for certain countries such as Germany and may require modifying this cmdlets -ConnectionUri default value.
.DESCRIPTION
This cmdlet is used to easily search for and remove any spam emails found in all inboxes in an organization.
.PARAMETER ConnectionUri
The ConnectionUri parameter specifies the connection endpoint for the remote Exchange Online PowerShell session.
For Exchange Online PowerShell in Microsoft 365 or Microsoft 365 GCC, you don't use this parameter.
For Exchange Online PowerShell in Office 365 Germany, use the value https://outlook.office.de/PowerShell-LiveID for this parameter.
For Exchange Online PowerShell in Office 365 operated by 21Vianet, use the value https://partner.outlook.cn/PowerShell for this parameter.
For Exchange Online PowerShell in Microsoft 365 GCC High, use the value https://outlook.office365.us/powershell-liveid for this parameter.
For Exchange Online PowerShell in Microsoft 365 DoD, use the value https://webmail.apps.mil/powershell-liveid for this parameter.
Note: If your organization is on-premises Exchange, and you have Exchange Enterprise CAL with Services licenses for EOP, use the this cmdlet without the ConnectionUri parameter to connect to EOP PowerShell (the same connection instructions as Exchange Online PowerShell).
.PARAMETER ContentMatchQuery
The ContentMatchQuery parameter specifies a content search filter.
This parameter uses a text search string or a query that's formatted by using the Keyword Query Language (KQL). For more information about KQL, see Keyword Query Language syntax reference (https://go.microsoft.com/fwlink/?LinkId=269603).
.PARAMETER MFA
The MFA parameter specifies whether Multi Factor Authentication is used for authentication in your environment. If this switch parameter exists then the MFA parmaeter -AzureADAuthorizationEndPointUri is used and defined automatically.
.EXAMPLE
.EXAMPLE 1
Remove-SpamEmail -ContentMatchQuery '(Received:4/13/2020..4/14/2020) AND (Subject:`"Action required`")'
Remove-SpamEmail -ExchangeOnline -ContentMatchQuery '(Received:4/13/2020..4/14/2020) AND (Subject:`"Action required`")'
The above two commands do the same thing. They find every email received from April 13-14 2020 with the Subject
"Action Required" and then removes it from everyones inbox on the Exchange Server. The -ExchangeOnline parameter
is the default parameter set name and does not need to be specified. It signifies that your Exchange Server is
not on site and managed by Microsoft.
.EXAMPLE 2
Remove-SpamEmail -OnPremise -ContentMatchQuery '(Received:4/13/2020) AND (Subject:`"Action required`")' -ConnectionUri "https://exchangeserver.domain.com/Powershell"
This example finds every email received from April 13 2020 with the Subject Action Required and removes it from every ones inbox.
The OnPremise parameter specifies that your Exchange server is managed on site. As such the -ConnectionUri parameter will also need
to be defined containing a link to your Exchange Server. This has not been tested so if you experience issues with this please inform me
what they are.
.EXAMPLE 3
Remove-SpamEmail -MFA -OnPremise -ContentMatchQuery '(Received:4/13/2020) AND (Subject:`"Action required`")' -ConnectionUri "https://exchangeserver.domain.com/Powershell"
This example finds every email received from April 13 2020 with the Subject Action Required and removes it from every ones inbox.
The -MFA parameter is to be used when your environment is using Multi Factor Authenticaiton. This gets used in the cmdlet New-PsSession.
The OnPremise parameter specifies that your Exchange server is managed on site. As such the -ConnectionUri parameter will also need
to be defined containing a link to your Exchange Server. On-Prem has not been tested so if you experience issues with this please inform me
what they are.
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.INPUTS
None
.OUTPUTS
None
.LINK
https://docs.microsoft.com/en-us/powershell/exchange/connect-to-exchange-servers-using-remote-powershell?view=exchange-ps
https://docs.microsoft.com/en-us/microsoft-365/compliance/search-for-and-delete-messages-in-your-organization?view=o365-worldwide
https://docs.microsoft.com/en-us/powershell/exchange/connect-to-exchange-online-powershell?view=exchange-ps
https://writeups.osbornepro.com
https://osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
#>
Function Remove-SpamEmail {
[CmdletBinding(DefaultParameterSetName="ExchangeOnline")]
param(
[Parameter(
ParameterSetName="On-Premise",
Mandatory=$True,
ValueFromPipeLine=$False,
HelpMessage="ConnectionURI should be in URL format. This needs to be a link to your On-Premise Exchange server. The default value if not specified connects to Exchange Onlines URI`nON-PREMISE EXAMPLE: http://<ServerFQDN>/PowerShell/")]
[Parameter(
ParameterSetName="ExchangeOnline",
Mandatory=$False,
ValueFromPipeLine=$False,
HelpMessage="ConnectionURI should be in URL format. This needs to be a link to your Exchange Online Server. The default value if not specified connects to the Exchange Onlines URI`nON-PREMISE EXAMPLE: http://<ServerFQDN>/PowerShell/")]
[String]$ConnectionUri = "https://nam02b.ps.compliance.protection.outlook.com/Powershell-LiveId?BasicAuthToOAuthConversion=true&HideBannerMessage=true;PSVersion=5.1.18362.752",
[Parameter(
ParameterSetName="On-Premise",
Mandatory=$False,
ValueFromPipeLine=$False)]
[Switch][Bool]$MFA,
[Parameter(
ParameterSetName="On-Premise")] # End Parameter
[Switch][Bool]$OnPremise,
[Parameter(
ParameterSetName="ExchangeOnline")]
[Switch]$ExchangeOnline,
[Parameter(
ParameterSetName="On-Premise",
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="This parameter is for defining the search query that discovers the malicious email to be deleted.`nEXAMPLE: (Received:4/13/2020..4/14/2020) AND (Subject:`"Action required`")")]
[Parameter(
ParameterSetName="ExchangeOnline",
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="This parameter is for defining the search query that discovers the malicious email to be deleted.`nEXAMPLE: '(Received:4/13/2020..4/14/2020) AND (Subject:`"Action required`")'")]
[String]$ContentMatchQuery
) # End param
BEGIN
{
Write-Verbose "Prompting for Gloabl Admin Credentials"
$UserCredential = Get-Credential -Message "Enter your Office365 Global Administrator Credentials"
$Date = Get-Date -Format "yyyy_MM_dd"
Write-Verbose "Connecting to Exchange Online Manamgement"
Install-Module -Name ExchangeOnlineManagement -Force
Import-Module -Name ExchangeOnlineManagement -Force
If ($OnPremise.IsPresent)
{
If ($MFA.IsPresent)
{
Write-Verbose "Attempting connection to On-Premise Environment server at $ConnectionUri using MFA"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ConnectionUri -Authentication Kerberos -Credential $UserCredential -AzureADAuthorizationEndPointUri https://login.microsoftonline.us/common
} # End If
Else
{
Write-Verbose "Attempting connection to On-Premise Environment server at $ConnectionUri"
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ConnectionUri -Authentication Kerberos -Credential $UserCredential
} # End Else
} # End If
Else
{
Write-Verbose "Connecting to Exchange Online"
Connect-ExchangeOnline -Credential $UserCredential -ShowProgress $True
Write-Verbose "Connecting to Exchange Security and Compliance PowerShell center."
Connect-IPPSSession -Credential $UserCredential
} # End Else
} # End BEGIN
PROCESS
{
$Search = New-ComplianceSearch -Name "$Date Remove Phishing Message" -ExchangeLocation All -ContentMatchQuery $ContentMatchQuery
Start-ComplianceSearch -Identity $Search.Identity
Write-Output “[*] Waiting 4 minutes for the search to complete”
Start-Sleep -Seconds 240
Write-Verbose "Deleting the inbox messages discovered in the previous search results from mailboxes where the spam email exists."
New-ComplianceSearchAction -SearchName "$Date Remove Phishing Message" -Purge -PurgeType SoftDelete
} # End PROCESS
END
{
Disconnect-ExchangeOnline
If (Get-PsSession)
{
Remove-PSSession * -ErrorAction SilentlyContinue
} # End If
} # End END
} # End Function Remove-SpamEmail
# SIG # Begin signature block
# MIIM9AYJKoZIhvcNAQcCoIIM5TCCDOECAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU7KCP0UUErTFLtNRP9boT66vN
# ANqgggn7MIIE0DCCA7igAwIBAgIBBzANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UE
# BhMCVVMxEDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxGjAY
# BgNVBAoTEUdvRGFkZHkuY29tLCBJbmMuMTEwLwYDVQQDEyhHbyBEYWRkeSBSb290
# IENlcnRpZmljYXRlIEF1dGhvcml0eSAtIEcyMB4XDTExMDUwMzA3MDAwMFoXDTMx
# MDUwMzA3MDAwMFowgbQxCzAJBgNVBAYTAlVTMRAwDgYDVQQIEwdBcml6b25hMRMw
# EQYDVQQHEwpTY290dHNkYWxlMRowGAYDVQQKExFHb0RhZGR5LmNvbSwgSW5jLjEt
# MCsGA1UECxMkaHR0cDovL2NlcnRzLmdvZGFkZHkuY29tL3JlcG9zaXRvcnkvMTMw
# MQYDVQQDEypHbyBEYWRkeSBTZWN1cmUgQ2VydGlmaWNhdGUgQXV0aG9yaXR5IC0g
# RzIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC54MsQ1K92vdSTYusw
# ZLiBCGzDBNliF44v/z5lz4/OYuY8UhzaFkVLVat4a2ODYp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