Use mail flow rules to block messages with executable attachments in EOP
In Microsoft 365 organizations with mailboxes in Exchange Online or standalone Exchange Online Protection (EOP) organizations without Exchange Online mailboxes, EOP uses anti-malware policies to block harmful messages, including messages with executable attachments. For more information, see Anti-malware protection in EOP.
To further enhance protection, you can use mail flow rules (also known as transport rules) to identify and block messages that contain executable attachments as described in this article.
For example, following a malware outbreak, a company could apply this rule with a time limit so that affected users can get back to sending attachments after a specified length of time.
What do you need to know before you begin?
You need to be assigned permissions in Exchange Online or Exchange Online Protection before you can do the procedures in this article. Specifically, you need the Transport Rules role, which is assigned to the Organization Management, Compliance Management, and Records Management role groups by default.
For more information, see the following topics:
For more information about mail flow rules in Exchange Online and standalone EOP, see the following topics:
Use the EAC to create a rule that blocks messages with executable attachments
In the EAC, go to Mail flow > Rules.
Click Add and then select Create a new rule.
In the New rule page that opens, configure the following settings:
Name: Enter a unique, descriptive name for the rule.
Click More Options.
Apply this rule if: Select Any attachment > has executable content.
Do the following: Select Block the message and then choose the action you want:
reject the message and include an explanation: In the Specify reject reason dialog that appears, enter the text you want to appear in the non-delivery report (also known as an NDR or bounce message). The default enhanced status code that's used is 5.7.1.
reject the message with the enhanced status code of: In the Enter enhanced status code dialog that appears, enter the enhanced status code that you want to appear in the NDR. Valid values are 5.7.1 or a value from 5.7.900 to 5.7.999. The default rejection text is: Delivery not authorized, message refused.
reject the message without notifying anyone
When you're finished, click Save. Your attachment blocking rule is now in force.
Use PowerShell to create a rule that blocks messages with executable attachments
Use the following syntax to create a rule to blocks messages that contain executable attachments:
New-TransportRule -Name "<UniqueName>" -AttachmentHasExecutableContent $true [-RejectMessageEnhancedStatusCode <5.7.1 | 5.7.900 to 5.7.999>] [-RejectMessageReasonText "<Text>"] [-DeleteMessage $true]
If you use the RejectMessageEnhancedStatusCode parameter without the RejectMessageReasonText parameter, the default text is: Delivery not authorized, message refused.
If you use the RejectMessageReasonText parameter without the RejectMessageEnhancedStatusCode parameter, the default code is 5.7.1.
This example creates a new rule named Block Executable Attachments that silently deletes messages that contain executable attachments.
New-TransportRule -Name "Block Executable Attachments" -AttachmentHasExecutableContent $true -DeleteMessage $true
For detailed syntax and parameter information, see New-TransportRule.
How do you know this worked?
To verify that you've successfully create a mail flow rule to block messages that contain executable attachments, do any of the following steps:
In the EAC, go to Mail flow > Rules > select the rule > click Edit , and verify the settings.
In PowerShell, run the following command to verify the settings:
Get-TransportRule -Identity "<Rule Name>" | Format-List Name,AttachmentHasExecutableContent,RejectMessage*,DeleteMessage