Skip to content

Instantly share code, notes, and snippets.

@jimdiroffii
Last active August 26, 2023 16:40
Show Gist options
  • Save jimdiroffii/09c946f85d75e8e77b913e49b294171e to your computer and use it in GitHub Desktop.
Save jimdiroffii/09c946f85d75e8e77b913e49b294171e to your computer and use it in GitHub Desktop.
Block all executables in a directory using firewall rules in Windows
# Check for administrator privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
# Relaunch the script as an administrator
Start-Process powershell -Verb runAs -ArgumentList "-File `"$($MyInvocation.MyCommand.Path)`""
exit
}
# init directory existence check variable
$dirExists = $false
# Ask the user for the directory
while (-not $dirExists) {
$directoryPath = Read-Host -Prompt 'Please enter the directory path to search for .exe files (or press Enter for directory browser)'
if ([string]::IsNullOrEmpty($directoryPath)) {
# Load necessary assembly for showing the dialog
Add-Type -AssemblyName System.Windows.Forms
# Create and show folder browser dialog
$folderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$folderBrowser.Description = "Select a folder"
$folderBrowser.RootFolder = [System.Environment+SpecialFolder]::MyComputer
$dialogResult = $folderBrowser.ShowDialog()
# Get the selected folder
if ($dialogResult -eq "OK") {
$selectedFolder = $folderBrowser.SelectedPath
} else {
Write-Host "No folder selected. Exiting..."
pause
exit
}
}
$directoryPath = $selectedFolder
if ($directoryPath) {
Write-Host "You have selected: $($directoryPath)"
} else {
Write-Host "No directory was selected."
pause
exit
}
if (Test-Path -Path $directoryPath -PathType Container) {
$dirExists = $true
} else {
Write-Host "Directory does not exist. Please retry."
}
}
# add a rule prefix for identifying rules
$rulePrefix = Read-Host -Prompt 'Enter a text prefix for rules [i.e. AB3] (or press Enter to skip)'
# Get all the specified file types recursively
$fileTypes = @("*.exe", "*.dll", "*.bat", "*.cmd", "*.ps1", "*.scr", "*.com", "*.vbs", "*.js", "*.jar", "*.py", "*.pl", "*.sh", "*.msi", "*.msp", "*.app", "*.rb")
$files = Get-ChildItem -Path $directoryPath -Include $fileTypes -Recurse -File
# Get all the .exe files recursively
#$files = Get-ChildItem -Path $directoryPath -Filter *.exe -Recurse
# Check for no exes
if ([string]::IsNullOrEmpty($files)) {
Write-Host "No exe found in $($directoryPath)"
pause
exit
}
# Add counter
$counter = 0;
# Loop through each .exe file and create a new outbound firewall rule
foreach ($file in $files) {
$counter++
Write-Host "Processing file: $($file.FullName)"
if ([string]::IsNullOrEmpty($rulePrefix)) {
$ruleName = "Block $($file.Name) $($counter)"
} else {
$ruleName = "$($rulePrefix): Block $($file.Name) $($counter)"
}
# Create new outbound rule
New-NetFirewallRule -DisplayName $ruleName -Direction Outbound -Program $file.FullName -Action Block
Write-Host "Created outbound firewall rule: $($ruleName)"
}
Write-Host "Process completed. $($counter) rule(s) created with prefix: $($rulePrefix)"
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment