Skip to content

Instantly share code, notes, and snippets.

@techlemur
Last active June 7, 2024 19:50
Show Gist options
  • Save techlemur/0eb3e2facd5442989827739a63972e6d to your computer and use it in GitHub Desktop.
Save techlemur/0eb3e2facd5442989827739a63972e6d to your computer and use it in GitHub Desktop.
Simple Firewall bat script for windows 10.
@echo off
setlocal enabledelayedexpansion
@REM Set directories to process here
set "DIRS=C:\Program Files\PowerToys"
@REM Add more folders like this
@REM set "DIRS=%DIRS%;PATH_GOES_HERE"
echo.
echo. ========================= DISCLAIMER =========================
echo.
echo. This script is provided as is without any guarantees or warranty.
echo. While the author of the script has made every effort to provide
echo. accurate and functioning code, the use of this script is at your
echo. own risk. The author is not responsible for any damage or losses of
echo. any kind caused by the use or misuse of the script. You are solely
echo. responsible for any effects, adverse or otherwise, that this script
echo. may have on your systems and operations.
echo.
if "%1"=="" (
echo.
echo. ---------------------------------------
echo. -------------- WARNING --------------
echo. ---------------------------------------
echo.
echo. You MUST edit this script before using!!!
echo.
echo. ---------------------------------------
echo.
set HELP=1
)
if "%1"=="-h" set HELP=1
if defined HELP (
echo.
echo. Basic script to block applications from accessing the internet/network
echo.
echo Options:
echo.
echo. -h Prints this and exits.
echo.
echo. -t test mode. Just lists the exe's that should be blocked.
echo.
echo. -a Add block rules for all exe's in the configured paths
echo.
echo. -d Deletes previously created block rules.
echo. NOTE: If you remove a folder from the DIRS variable any
echo. previously created rules will not be deleted.
exit /b
)
echo.
if "%1"=="-t" (
echo Test Mode...
) else if "%1"=="-d" (
echo Delete mode...
) else if "%1"=="-a" (
echo Add mode...
)
echo.
for %%D in ("%DIRS:;=" "%") do (
echo.=========================
echo.Processing exe's in !%%D!
echo.=========================
pushd "%%D"
for /R %%F in (*.exe) do (
set "exeName=%%~nF"
set "blockRuleName=0%%~nD block - !exeName!"
set "blockRuleNameIn=0%%~nD block in - !exeName!"
if "%1"=="-t" (
@REM List all exe's that will be blocked
echo Will add rule for "!exeName!" located at "!%%F!"
) else (
if "%1"=="-d" (
@REM Just delete the block rule
echo Deleting rule for !exeName!
netsh advfirewall firewall delete rule name="!blockRuleName!"
netsh advfirewall firewall delete rule name="!blockRuleNameIn!"
) else if "%1"=="-a" (
@REM Delete the block rule incase it's already there
echo Deleting rule for !exeName!
netsh advfirewall firewall delete rule name="!blockRuleName!"
netsh advfirewall firewall delete rule name="!blockRuleNameIn!"
@REM Add the block rule
echo Creating block rule for !exeName!
netsh advfirewall firewall add rule name="!blockRuleName!" dir=out action=block program="%%F" enable=yes
netsh advfirewall firewall add rule name="!blockRuleNameIn!" dir=in action=block program="%%F" enable=yes
)
)
)
popd
echo.
echo.
)
if "%1"=="-t" (
echo Done
) else (
echo All rules have been processed.
pause
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment