Last active
December 18, 2024 20:38
-
-
Save ph33nx/48a0147c6fdf9fefb38734fe0d3b3ab5 to your computer and use it in GitHub Desktop.
Batch script to block internet access for all .exe files in a folder recursively using Windows Firewall (inbound and outbound rules). Includes dynamic folder path input, usage instructions, and automation.
This file contains 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
@echo off | |
:: Batch Script: block_folder.bat | |
:: Author: https://github.com/ph33nx | |
:: Description: Blocks all .exe files in the specified folder (and subfolders) from accessing the internet (both inbound and outbound) using Windows Firewall. | |
:: Usage: | |
:: block_folder.bat [FolderPath] | |
:: - Pass the folder path containing the .exe files to block. | |
:: block_folder.bat -h | |
:: - Displays this help message. | |
setlocal enabledelayedexpansion | |
REM Check if a parameter is passed | |
if "%~1"=="" goto help | |
if "%~1"=="-h" goto help | |
REM Get the folder path from the argument | |
set "folderPath=%~1" | |
REM Verify if the folder exists | |
if not exist "%folderPath%" ( | |
echo [ERROR] The specified folder does not exist: %folderPath% | |
exit /b 1 | |
) | |
REM Extract base folder name from the provided path | |
for %%A in ("%folderPath%") do set "baseFolderName=%%~nA" | |
REM Display a confirmation message | |
echo [INFO] Blocking all .exe files in folder: %folderPath% | |
REM Loop through all .exe files in the folder and create firewall rules | |
for /r "%folderPath%" %%F in (*.exe) do ( | |
set "filePath=%%F" | |
set "fileName=%%~nxF" | |
REM Enable delayed expansion inside the loop to use updated variables | |
call :AddFirewallRules "!filePath!" "!baseFolderName!" "!fileName!" | |
) | |
REM Final message | |
echo [INFO] All .exe files in %folderPath% have been blocked (inbound and outbound). | |
echo [INFO] You can view the rules in Windows Defender Firewall with Advanced Security by opening it from the Start menu. | |
exit /b 0 | |
:AddFirewallRules | |
REM Arguments: %1 = filePath, %2 = baseFolderName, %3 = fileName | |
set "filePath=%~1" | |
set "baseFolderName=%~2" | |
set "fileName=%~3" | |
REM Generate rule names with base folder name and file name | |
set "ruleName=Block %baseFolderName% %fileName% (automated)" | |
echo [INFO] Adding outbound block rule for: %filePath% | |
netsh advfirewall firewall add rule name="%ruleName% OUT" dir=out program="%filePath%" action=block enable=yes >nul | |
echo [INFO] Adding inbound block rule for: %filePath% | |
netsh advfirewall firewall add rule name="%ruleName% IN" dir=in program="%filePath%" action=block enable=yes >nul | |
goto :eof | |
:help | |
echo Usage: block_folder.bat [FolderPath] | |
echo. | |
echo This script blocks all .exe files in the specified folder and its subfolders from accessing the internet (inbound and outbound) using Windows Firewall. | |
echo. | |
echo Parameters: | |
echo FolderPath - Path to the folder containing .exe files to block. | |
echo -h - Display this help message. | |
echo. | |
echo Example: | |
echo block_folder.bat "C:\Program Files\Adobe" | |
exit /b 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment