SaferComputing.com

Reference · Accounts and sessions

Hidden inbox rules and forwarding in Microsoft 365, Outlook.com and Gmail

Find the inbox rules attackers hide in a taken-over mailbox: rules that forward mail out, delete it, or move invoices to the RSS folder and mark them read; mailbox SMTP forwarding; Gmail filters; and third-party apps with OAuth access.

Published
Last checked
Applies to
Microsoft 365, Outlook.com and Gmail
Technical specification
Target artifactInbox rules, including hidden ones; mailbox forwarding to an outside address; Gmail filters, forwarding addresses and delegates; third-party apps granted access to the mailbox.
Default behaviourRules run silently on every new message. After getting into a mailbox, attackers add rules that forward mail out, delete it, or move messages containing words such as "invoice" to a folder nobody reads and mark them read. Some rules do not appear in Outlook.
Safe stateEvery rule, filter and forwarding address is one you created; automatic forwarding to outside addresses is off for the organization unless someone needs it; only apps you recognize have access.
Check in Microsoft 365Get-InboxRule -Mailbox user@example.com -IncludeHidden | Format-List Name, Enabled, SubjectOrBodyContainsWords, ForwardTo, RedirectTo, MoveToFolder, MarkAsRead, DeleteMessage
Check across the organizationGet-HostedOutboundSpamFilterPolicy | Format-List Name, AutoForwardingMode
Check in GmailSettings, See all settings, then Forwarding and POP/IMAP and Filters and Blocked Addresses
Fix in Microsoft 365Disable-InboxRule -Mailbox user@example.com -Identity 'RULE NAME', after saving the rules as evidence
Fix across the organizationSet-HostedOutboundSpamFilterPolicy -Identity Default -AutoForwardingMode Off
Fix in Gmailchoose Disable forwarding, and delete the filters you did not create

What a malicious rule looks like

Microsoft's guide for investigating suspicious inbox rules lists these signs:

SignWhy it matters
Forwards or redirects to an outside addressCopies every matching message to the attacker
Deletes messagesHides replies and security alerts from the owner
Moves messages to a little-used folder, such as RSS, and marks them readThe owner never sees them, but the attacker can
Looks for words such as "invoice", "phish", "do not reply", "suspicious email" or "spam"Hides payment conversations and warnings
Has no condition at all, or deletes every incoming messageCatches everything; Microsoft calls a rule that deletes all incoming mail with no keyword filter an indicator of malicious activity

In Microsoft 365, the conditions sit in SubjectContainsWords, BodyContainsWords and SubjectOrBodyContainsWords. For a rule that moves messages and marks them read, Microsoft suggests checking whether the destination folder has anything to do with the words the rule looks for.

Microsoft 365

Connect, list and save the evidence
Windows PowerShell, with the ExchangeOnlineManagement module installed
Connect-ExchangeOnline
Get-InboxRule -Mailbox user@example.com -IncludeHidden | Format-List Name, Enabled, From, SubjectContainsWords, BodyContainsWords, SubjectOrBodyContainsWords, ForwardTo, ForwardAsAttachmentTo, RedirectTo, MoveToFolder, MarkAsRead, DeleteMessage
Get-InboxRule -Mailbox user@example.com -IncludeHidden | Export-Clixml -Path .\inbox-rules-evidence.xml

-IncludeHidden matters: attackers sometimes create rules that do not show up in Outlook.

Mailbox forwarding (SMTP forwarding)

Forwarding can also be set on the mailbox itself, outside any rule:

Windows PowerShell, connected to Exchange Online
Get-Mailbox -Identity user@example.com | Format-List Forwarding*Address, DeliverTo*
Set-Mailbox -Identity user@example.com -ForwardingAddress $null -ForwardingSmtpAddress $null -DeliverToMailboxAndForward $false
Turn off automatic forwarding to outside addresses

The outbound spam policy's Automatic forwarding setting controls both inbox rules and mailbox forwarding that send mail to outside addresses. Its default, Automatic, meant on when it was introduced and was changed to off in 2021, but organizations that were already using it can still have forwarding on. Microsoft advises setting On or Off explicitly:

Windows PowerShell, connected to Exchange Online
Get-HostedOutboundSpamFilterPolicy | Format-List Name, AutoForwardingMode
Set-HostedOutboundSpamFilterPolicy -Identity Default -AutoForwardingMode Off

With Off, a forwarded message bounces with the error 5.7.520 Access denied, Your organization does not allow external forwarding. Forwarding between internal users is not affected. If someone needs to forward outside, give them a separate policy rather than turning it on for everyone.

When were rules created or changed?
Windows PowerShell, connected to Exchange Online
Search-UnifiedAuditLog -StartDate (Get-Date).AddDays(-30) -EndDate (Get-Date) -Operations New-InboxRule, Set-InboxRule, UpdateInboxRules -ResultSize 1000 | Select-Object CreationDate, UserIds, Operations, AuditData

Microsoft has announced the retirement of Search-UnifiedAuditLog on 15 October 2027 in favour of the Microsoft Graph audit log query API.

Apps the user gave access

Users can review and revoke their own grants at myapplications.microsoft.com: hover over an app, select the three dots, Manage your application, then Revoke Permissions. Permissions an administrator consented to can only be removed by an administrator, who can list a user's grants with Microsoft Graph PowerShell:

Windows PowerShell, connected with Connect-MgGraph
Get-MgUserOauth2PermissionGrant -UserId user@example.com | Select-Object ClientId, ConsentType, Scope

Warning Outlook client rules

Changing inbox rules with PowerShell removes any client-side rules the user created in Outlook. Tell the user before you do it.

Outlook.com and Gmail

Outlook.com

In Settings, open Mail, then check Forwarding and Rules. Turn off forwarding to any address you did not add, and delete rules you did not create.

Gmail

In Settings, choose See all settings, then:

  • Forwarding and POP/IMAP: choose Disable forwarding if there is an address you did not add.
  • Filters and Blocked Addresses: delete filters that forward, delete, archive or mark as read mail you did not set up.
  • Accounts and Import, under Grant access to your account: remove any delegate you do not recognize.

Then review Linked apps at myaccount.google.com/linkedapps and choose Remove access for anything unfamiliar.

Remove the rules only after the account is secured: change the password and end every session first, or the attacker can put them back. See Account compromise triage.

Sources