Security · 1 tool
Find mailboxes that forward mail outside the company
Summary
Finds forwarding set on the mailbox and in inbox rules that sends mail to external addresses, one of the most common signs of a compromised account and a common leftover after a migration.
Implementation
Choose your tool. Your choice is remembered on other samples.
Connect-ExchangeOnline -ShowBanner:$false
$accepted = (Get-AcceptedDomain).DomainName
function Test-External($addr) { $d = ($addr -replace "^smtp:", "" -split "@")[-1]; $d -and ($accepted -notcontains $d) }
Get-EXOMailbox -ResultSize Unlimited -Properties ForwardingSmtpAddress, DeliverToMailboxAndForward |
Where-Object { $_.ForwardingSmtpAddress -and (Test-External $_.ForwardingSmtpAddress) } |
Select-Object PrimarySmtpAddress, ForwardingSmtpAddress, DeliverToMailboxAndForward |
Export-Csv .\mailbox-forwarding.csv -NoTypeInformation
Get-EXOMailbox -ResultSize Unlimited | ForEach-Object {
$mbx = $_.PrimarySmtpAddress
Get-InboxRule -Mailbox $mbx -ErrorAction SilentlyContinue |
Where-Object { $_.ForwardTo -or $_.RedirectTo -or $_.ForwardAsAttachmentTo } |
Select-Object @{ n = "Mailbox"; e = { $mbx } }, Name, Enabled, ForwardTo, RedirectTo, ForwardAsAttachmentTo
} | Export-Csv .\inbox-rule-forwarding.csv -NoTypeInformationPrerequisites
- Exchange Administrator
- ExchangeOnlineManagement module 3.x
Output
Two CSVs: mailbox-level forwarding and inbox rules that forward or redirect.
Contributors
| Author | Role |
|---|---|
| Priyanshu Upadhyay | Solution Architect, Microsoft 365 and Azure |
InnovaticT