This guide helps you determine whether your Microsoft Exchange Server is vulnerable to the Hafnium exploits, a set of zero-day vulnerabilities actively exploited in 2021. You will learn how to check for indicators of compromise and verify your patch status. You need administrative access to your Exchange Server and, ideally, the ability to run PowerShell commands.
What You’ll Need
- Administrative access to the Exchange Server (local admin or Exchange Organization Management role).
- PowerShell access on the server (Exchange Management Shell or standard PowerShell with Exchange modules).
- Internet access to download Microsoft’s official detection scripts (or a local copy of them).
- A text editor (like Notepad) to review log files.
Step-by-Step
- Identify your Exchange Server version and build. Open the Exchange Management Shell and run:
Get-ExchangeServer | Select Name, AdminDisplayVersion. Note the version number. The vulnerable builds are those before the March 2021 security updates. If you are on a later Cumulative Update (CU) that includes the fixes, you are likely safe. - Check for the presence of the March 2021 security updates. In the same shell, run:
Get-HotFix | Where-Object {$_.HotFixID -match 'KB5000871|KB5000872|KB5000873|KB5000874|KB5000875|KB5000876|KB5000877|KB5000878'}. If any of these KB numbers appear, the server has the official patches applied. If none appear, your server is likely unpatched and vulnerable. - Download Microsoft’s official Hafnium detection scripts. Microsoft released two scripts:
Test-Hafnium.ps1andScanHafnium.ps1. Download them from the Microsoft Security Response Center (MSRC) blog or the Microsoft Download Center. Save them to a local folder on the server, e.g.,C:\HafniumCheck. - Run the Test-Hafnium.ps1 script. In an elevated PowerShell window, navigate to the folder where you saved the script and run:
.\Test-Hafnium.ps1. This script checks for the presence of web shells and other indicators of compromise. It will output a list of files that match known malicious patterns. If it finds nothing, you are likely clean. - Run the ScanHafnium.ps1 script for a deeper scan. This script scans all Exchange web directories for suspicious files. Run:
.\ScanHafnium.ps1. It will produce a CSV report (by default in the same folder) listing all files that need manual review. Look for files with names likeerror.aspx,help.aspx, or any .aspx files in unusual locations (e.g., in the\inetpub\wwwroot\aspnet_clientfolder). - Manually inspect IIS logs for signs of exploitation. Open IIS Manager, select the default web site, and click on “Logging”. Note the log file directory (typically
C:\inetpub\logs\LogFiles\W3SVC1). Open the most recent log files in a text editor. Look for POST requests to/ecp/or/owa/from external IP addresses, especially with unusual query strings or with a User-Agent that is not a standard browser. Also look for requests to files likeshell.aspxorweb.aspx. - Check for newly created or modified files in Exchange directories. Open a command prompt and run:
dir /s /o-d C:\inetpub\wwwroot\*.aspxanddir /s /o-d C:\Program Files\Microsoft\Exchange Server\V15\FrontEnd\HttpProxy\*.aspx. Sort by date and look for any files created or modified after January 1, 2021. Pay special attention to files in the\owa\auth\and\ecp\folders. - Check for suspicious scheduled tasks. Run
schtasks /query /fo LIST /vand look for tasks with names that are random strings or that run PowerShell or cmd.exe with encoded commands. Hafnium often creates scheduled tasks for persistence. - If you find any indicators, immediately isolate the server. Disconnect the server from the network (physical or virtual) and contact your incident response team or a qualified security professional. Do not attempt to delete files or clean the system yourself, as this may destroy evidence.
- If you are unpatched but find no indicators, apply the security updates immediately. Even if you find no signs of compromise, you must patch the server as soon as possible. The exploits were actively used in the wild, and unpatched servers remain at high risk.
Common Mistakes
- Assuming a later Cumulative Update means you are patched. The security updates were released as standalone patches and were also included in later CUs. However, if you are on a CU released before March 2021, you need to install the specific security update. Check the KB numbers, not just the CU version.
- Only checking for the presence of web shells. Hafnium also used other techniques like credential dumping and data exfiltration. Even if no web shells are found, you may still be compromised. Review logs and check for unusual activity.
- Running the detection scripts but not reviewing the output. The scripts only list suspicious files; they do not automatically clean them. You must manually review every file listed and determine if it is legitimate.
- Ignoring the IIS logs. Logs are your best evidence of what happened. If you don’t review them, you may miss the attack entirely.
Tips
- Use the official Microsoft scripts. They are regularly updated to detect the latest variants. Always download the latest version from the MSRC blog.
- Check all Exchange servers in your organization. If you have multiple servers, run the checks on each one. An attacker may have compromised only one.
- Monitor your network for outbound connections. Hafnium often exfiltrates data to external IPs. Check your firewall logs for connections from your Exchange server to unknown IP addresses on ports 443 or 80.
- Enable verbose logging on the Exchange server. If you are not already logging, enable detailed logging for IIS and Exchange to aid in future investigations.
Troubleshooting
Script fails to run due to execution policy. If you get an error about running scripts, open PowerShell as administrator and run: Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process. This allows the script to run for the current session only.
Script does not output anything. Some scripts may require you to specify the Exchange installation path. Try running: .\Test-Hafnium.ps1 -ExchangePath "C:\Program Files\Microsoft\Exchange Server\V15" (adjust the path if your Exchange is installed elsewhere).
You cannot find the IIS logs. The logs are stored by default in C:\inetpub\logs\LogFiles\W3SVC1. If you have multiple sites, look for folders like W3SVC2, W3SVC3, etc. You can also check the IIS Manager to see the exact path.
You find a suspicious file but are not sure if it is malicious. Do not delete it. Copy it to a safe location (e.g., a USB drive or a separate folder) and contact a security professional. You can also upload it to a malware analysis service, but do this only if you are sure the file is not actively being used by the attacker.
Frequently Asked Questions
Q: What are the Hafnium exploits?
A: Hafnium is a Chinese state-sponsored hacking group that exploited four zero-day vulnerabilities in Microsoft Exchange Server (CVE-2021-26855, CVE-2021-26857, CVE-2021-26858, CVE-2021-27065) to access email accounts and deploy web shells.
Q: How do I know if my server is vulnerable?
A: If your Exchange Server is running a version before the March 2021 security updates, it is vulnerable. Check your build number and compare it to the patched versions listed in Microsoft’s documentation.
Q: Can I use the detection scripts after I have patched?
A: Yes, you can and should run the detection scripts even after patching. They check for indicators of compromise, and if you were compromised before patching, the scripts can help you find the web shells left behind.
Q: What should I do if I find a web shell?
A: Immediately isolate the server from the network, preserve all logs and files, and contact a professional incident response team. Do not try to remove the web shell yourself, as you may miss other backdoors.
Q: Are there any free tools to help with the check?
A: Yes, Microsoft provides the official detection scripts for free. Additionally, there are community-created scripts, but it is best to use Microsoft’s official ones to ensure accuracy.
Q: How long does the scan take?
A: The scan time varies depending on the size of your Exchange environment and the number of files. On a typical server, it may take a few minutes. The ScanHafnium.ps1 script can take longer because it scans all web directories.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- MacBook Charger Not Working? Fix It Now
- Fix Windows Cannot Be Installed to This Disk
- Zoom on iPhone & iPad: Host or Join a Meeting
- Fix iPhone SE 4 Unable to Activate: Step-by-Step
- Keep Important Emails From Getting Buried in iPhone Mail Categories
- How to Clear RAM on iPhone: Speed It Up