This lesson builds the defence stack on a Windows server: the firewall, antivirus and exploit mitigations, credential protection and application control. Each layer assumes another has failed.
Audit first, block later. ASR, WDAC and AppLocker can stop legitimate software. Start every one in audit mode, read the logs, fix what would break, then enforce.
Windows Defender Firewall
The firewall should be on for all profiles, block inbound by default, and log drops.
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True -DefaultInboundAction Block -DefaultOutboundAction Allow -LogBlocked True -LogAllowed False -LogMaxSizeKilobytes 16384 -LogFileName '%systemroot%\system32\LogFiles\Firewall\pfirewall.log'
Get-NetFirewallRule -Enabled True -Direction Inbound | Select-Object DisplayName,Profile,Action | Sort-Object DisplayNameReview every enabled inbound rule. Disable the ones you do not need (Disable-NetFirewallRule -DisplayName "...") and scope the rest to the networks that need them (Set-NetFirewallRule -RemoteAddress ...). On high-value servers, consider restricting outbound traffic too, so malware cannot freely call home.
Microsoft Defender Antivirus
Get-MpComputerStatus | Select-Object AMServiceEnabled,RealTimeProtectionEnabled,IsTamperProtected,AntivirusSignatureLastUpdated
Update-MpSignature
Set-MpPreference -PUAProtection Enabled -EnableNetworkProtection Enabled -MAPSReporting Advanced -SubmitSamplesConsent SendSafeSamples- Real-time protection and cloud-delivered protection should be on.
- Tamper protection stops malware from switching Defender off. It is managed from the Windows Security app, Intune or Microsoft Defender for Endpoint, not from this PowerShell.
- If you run a third-party EDR, Defender goes passive by design. Make sure something is actively protecting every server.
Attack Surface Reduction (ASR) rules
ASR rules block behaviours that malware relies on. Roll out in audit mode, check the events, then block.
# Audit mode first (2 = audit, 1 = block). Rule GUIDs below are Microsoft's published IDs.
$rules = @{
'9e6c4e1f-7d60-472f-ba1a-a39ef669e4b2' = 'Block credential stealing from LSASS'
'd1e49aac-8f56-4280-b9ba-993a6d77406c' = 'Block process creations from PsExec and WMI commands'
'a8f5898e-1dc8-49a9-9878-85004b8a61e6' = 'Block web shell creation for servers'
}
foreach ($id in $rules.Keys) {
Add-MpPreference -AttackSurfaceReductionRules_Ids $id -AttackSurfaceReductionRules_Actions AuditMode
}
Get-MpPreference | Select-Object -ExpandProperty AttackSurfaceReductionRules_IdsRead what would have been blocked:
Get-WinEvent -FilterHashtable @{ LogName='Microsoft-Windows-Windows Defender/Operational'; Id=1122 } -MaxEvents 20 |
Select-Object TimeCreated,MessageEvent 1122 is audit (would have blocked) and 1121 is a real block. After a couple of weeks with no legitimate hits, switch to block:
Add-MpPreference -AttackSurfaceReductionRules_Ids 9e6c4e1f-7d60-472f-ba1a-a39ef669e4b2 -AttackSurfaceReductionRules_Actions EnabledThe PsExec/WMI rule can interfere with admin tooling and some management products. Add exclusions deliberately, never broad ones.
Protect credentials in memory
Attackers who get onto a server try to read credentials from LSASS memory. Two controls make that much harder.
LSA protection runs LSASS as a protected process:
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Lsa" /v RunAsPPL /t REG_DWORD /d 1 /f
# Reboot to applyCredential Guard isolates secrets using virtualisation-based security. Windows Server 2025 enables it by default on hardware that meets the requirements (domain controllers are excluded). Check it:
Get-CimInstance -ClassName Win32_DeviceGuard -Namespace root\Microsoft\Windows\DeviceGuard |
Select-Object SecurityServicesRunning,VirtualizationBasedSecurityStatusA value of 1 in SecurityServicesRunning means Credential Guard is running. It can break unconstrained Kerberos delegation and NTLMv1, so test applications that depend on those.
Application control: WDAC (App Control for Business) and AppLocker
The strongest control against unknown malware is to only allow approved code to run. Windows has two tools: WDAC (stronger, kernel-enforced) and AppLocker (simpler, older). Start in audit mode on a reference server.
# Build a policy that trusts what is installed now (example), then set it to audit mode
New-CIPolicy -FilePath .\Baseline.xml -Level Publisher -Fallback Hash -UserPEs -ScanPath "C:\Program Files"
Set-RuleOption -FilePath .\Baseline.xml -Option 3 # option 3 = Audit Mode
ConvertFrom-CIPolicy -XmlFilePath .\Baseline.xml -BinaryFilePath .\Baseline.cipDeploy the .cip with Group Policy, Intune or Microsoft's tooling (the App Control Policy Wizard is friendlier than raw PowerShell). Then read the events in Applications and Services Logs, Microsoft, Windows, CodeIntegrity, Operational: event 3076 is an audit hit and 3077 is a block. Add the legitimate items to the policy, and only switch to enforced mode when the audit log is quiet.
Also remove the old PowerShell engine if it is still present, since it bypasses modern logging:
Disable-WindowsOptionalFeature -Online -FeatureName MicrosoftWindowsPowerShellV2RootEvent IDs to remember
| Event | Source | Meaning |
|---|---|---|
| 1121 / 1122 | Defender Operational | ASR block / ASR audit |
| 5007 | Defender Operational | Defender configuration changed |
| 5001 | Defender Operational | Real-time protection disabled |
| 3076 / 3077 | CodeIntegrity Operational | WDAC audit / block |
🧪 Lab
- Set the firewall profiles as above and review the enabled inbound rules.
- Run
Get-MpComputerStatusand fix anything disabled. - Add the three ASR rules in audit mode, run a normal workload for a day, then read events 1122.
- Enable LSA protection, reboot, and check Credential Guard status.
- Create a WDAC audit policy for a test server and read the 3076 events.
Checkpoint
- Why start ASR and WDAC in audit mode?
- What is the difference between Defender events 1121 and 1122?
- What attack do LSA protection and Credential Guard defend against?
- How do you check whether Credential Guard is running?
- Why remove PowerShell v2?