<# .SYNOPSIS Lab 08.3 — Microsoft Defender for Endpoint Official Benign DIY Detection Test .DESCRIPTION Verifies MDE Sense sensor health (OnboardingState = 1) and triggers Microsoft's official benign loopback detection test (http://127.0.0.1/1.exe) strictly on an isolated lab workstation. Classification : Validation trigger & cleanup (ISOLATED LAB ONLY) Run On : WIN11-01 ONLY (Never run on DC01, SYNC01, or production hosts) .NOTES Author : Caner Cetinkaya (canercetinkaya.com/labs/08-attack-and-validate/) Ref : https://learn.microsoft.com/en-us/defender-endpoint/run-detection-test Safety : ISOLATED LAB ONLY (Proves EDR Detection & Investigation, NOT Prevention) #> [CmdletBinding()] param( [string]$AllowedHostPrefix = 'WIN11-' ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' if (-not $env:COMPUTERNAME.StartsWith($AllowedHostPrefix, [System.StringComparison]::OrdinalIgnoreCase)) { throw "SAFETY GUARD: This validation script may only run on '$AllowedHostPrefix*' workstations. Current host: $env:COMPUTERNAME" } Write-Host '[1/3] Pre-Flight: Verifying MDE Sense service and OnboardingState...' -ForegroundColor Cyan $sense = (Get-Service Sense -ErrorAction Stop).Status $onboarding = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows Advanced Threat Protection\Status' -ErrorAction Stop).OnboardingState [pscustomobject]@{ Host = $env:COMPUTERNAME SenseService = $sense OnboardingState = $onboarding } | Format-List if ($sense -ne 'Running' -or $onboarding -ne 1) { throw 'Pre-Flight FAILED: MDE sensor is not onboarded/running. Complete Lab 05.1 first.' } Write-Host '[2/3] Triggering Microsoft official benign loopback DIY test (127.0.0.1/1.exe)...' -ForegroundColor Cyan $testDir = 'C:\test-WDATP-test' New-Item -ItemType Directory -Force -Path $testDir | Out-Null $diyCmd = "powershell.exe -NoExit -ExecutionPolicy Bypass -WindowStyle Hidden `$ErrorActionPreference='silentlycontinue';(New-Object System.Net.WebClient).DownloadFile('http://127.0.0.1/1.exe','$testDir\invoice.exe');Start-Process '$testDir\invoice.exe'" $proc = Start-Process -FilePath 'cmd.exe' -ArgumentList "/c $diyCmd" -PassThru -WindowStyle Hidden Write-Host "Triggered at $(Get-Date -Format o) | PID=$($proc.Id). Alert surfaces in security.microsoft.com within 5-15 minutes." -ForegroundColor Green Start-Sleep -Seconds 4 Write-Host '[3/3] Cleaning up spawned test process and temporary directory...' -ForegroundColor Cyan Get-Process powershell -ErrorAction SilentlyContinue | Where-Object { $_.Id -ne $PID } | Stop-Process -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 1 Remove-Item -Path $testDir -Recurse -Force -ErrorAction SilentlyContinue Write-Host "CleanupVerified (TestDirRemoved): $(-not (Test-Path $testDir))" -ForegroundColor Green