Created
August 29, 2024 00:33
-
-
Save 1ikeadragon/3f364ba56664166f7de15514211de2c9 to your computer and use it in GitHub Desktop.
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
# Author: 1ikeadragon, for GAMERANT | |
param ( | |
$InputFile = "BlockList.txt", | |
$RuleName, | |
$ProfileType = "any", | |
$InterfaceType = "any" | |
) | |
$file = get-item $InputFile -ErrorAction SilentlyContinue | |
if (-not $file) { | |
"`nCannot find $InputFile, quitting...`n" | |
exit | |
} | |
if (-not $RuleName) { | |
$RuleName = $file.basename | |
} | |
$description = "Rule created by script on $(get-date). Do not edit rule by hand, it will be overwritten when the script is run again. By default, the name of the rule is named after the input file." | |
"`nDeleting any inbound or outbound firewall rules named like '$RuleName-#*'`n" | |
$currentrules = netsh.exe advfirewall firewall show rule name=all | | |
select-string '^[Rule Name|Regelname]+:\s+(.+$)' | | |
foreach { $_.matches[0].groups[1].value } | |
if ($currentrules.Length -lt 3) { | |
"`nProblem getting a list of current firewall rules, quitting...`n" | |
exit | |
} | |
foreach ($rule in $currentrules) { | |
if ($rule -like "$RuleName-#*") { | |
netsh.exe advfirewall firewall delete rule name="$rule" | out-null | |
} | |
} | |
$ranges = get-content $file | where {($_.trim().length -ne 0) -and ($_ -match '^[0-9a-fA-F]{1,4}[\.\:]')} | |
if (-not $ranges) { | |
"`nCould not parse $file, quitting...`n" | |
exit | |
} | |
$linecount = $ranges.Count | |
if ($linecount -eq 0) { | |
"`nZero IP addresses to block, quitting...`n" | |
exit | |
} | |
$MaxRangesPerRule = 100 | |
$i = 1 | |
$start = 1 | |
$end = $MaxRangesPerRule | |
do { | |
$icount = $i.ToString().PadLeft(3, "0") | |
if ($end -gt $linecount) { | |
$end = $linecount | |
} | |
$textranges = [System.String]::Join(",", $ranges[$start - 1 .. $end - 1]) | |
"`nCreating an inbound firewall rule named '$RuleName-#$icount' for IP ranges $start - $end" | |
netsh.exe advfirewall firewall add rule name="$RuleName-#$icount" dir=in action=block program='E:\Games\Steam\steamapps\common\Call of Duty HQ\cod.exe' localip=any remoteip="$textranges" description="$description" profile="$ProfileType" interfacetype="$InterfaceType" | |
$i++ | |
$start += $MaxRangesPerRule | |
$end += $MaxRangesPerRule | |
} while ($start -le $linecount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment