Last active
February 28, 2026 13:40
-
-
Save st1vms/bcafc3cf66765b22801335e8b256dbb0 to your computer and use it in GitHub Desktop.
Powershell script to create Windows Firewall rules for blocking Amazon networks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| !Amazonaws WF rules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Define the URL for the official AWS IP ranges | |
| $AWS_IP_URL = "https://ip-ranges.amazonaws.com/ip-ranges.json" | |
| $RuleName = "Block-Amazon-Networks" | |
| Write-Host "Fetching latest AWS IP ranges..." -ForegroundColor Cyan | |
| try { | |
| $Response = Invoke-RestMethod -Uri $AWS_IP_URL | |
| # Filter for IPv4 prefixes. | |
| $Subnets = $Response.prefixes | Where-Object { $_.service -eq "AMAZON" } | Select-Object -ExpandProperty ip_prefix -Unique | |
| Write-Host "Found $($Subnets.Count) subnets to block." -ForegroundColor Yellow | |
| # Check if the rule already exists | |
| $ExistingRule = Get-NetFirewallRule -DisplayName $RuleName -ErrorAction SilentlyContinue | |
| if ($ExistingRule) { | |
| Write-Host "Updating existing firewall rule..." -ForegroundColor Cyan | |
| Set-NetFirewallRule -DisplayName $RuleName -RemoteAddress $Subnets | |
| } else { | |
| Write-Host "Creating new firewall rule..." -ForegroundColor Cyan | |
| New-NetFirewallRule -DisplayName $RuleName ` | |
| -Direction Inbound ` | |
| -Action Block ` | |
| -RemoteAddress $Subnets ` | |
| -Description "Blocks all IP from Amazon/AWS ranges" | |
| New-NetFirewallRule -DisplayName "$RuleName-Out" ` | |
| -Direction Outbound ` | |
| -Action Block ` | |
| -RemoteAddress $Subnets | |
| } | |
| Write-Host "Success" -ForegroundColor Green | |
| } | |
| catch { | |
| Write-Error "Failed to download or apply IP ranges: $($_.Exception.Message)" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment