Monday, March 23, 2015

Block all files in a folder in Windows Firewall

If you want to block all programs/files recursively in a folder from accessing the internet it is easier to use the following batch file (source).

@echo off
REM    BATCH FILE CREATED BY CHARLES DE HAVILLAND 20/02/2012

cls
If "%1"=="" GOTO :norulename
SET RULENAME=%1
ECHO Create in/out firewall rules for all *.exe files with the rulename of

"%RULENAME%" ?
ECHO.
ECHO.

pause
Echo.
FOR /r %%G in ("*.exe") Do (@echo %%G
NETSH advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=in program="%%G"

action="block" enable="yes")
FOR /r %%G in ("*.exe") Do (@echo %%G
NETSH advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=out program="%%G"

action="block" enable="yes")
Echo.
Echo done.
Echo.
GOTO :Finish
:norulename
Echo Error! - You did not specify a Rulename type - Addfwrs "Rulename"
Echo.
:Finish
Echo Batch ended...


Save it as addfwrs.bat. 

Place the file in a directory that is included in your system path (so it can be called easily), OR  place it in the root folder of the programs you want to block internet access for.

With Administrator access (type cmd, then right click on it and select 'Run as administrator'), change directory to the root directory of the programs you want to block internet access for (example: cd c:\Programs Files (x86)\YourProg\), run the batch file by typing: addfwrs [rulename]   example: addfwrs "YourProg"

N.B: It's important you add a firewall rule name, so that you can identify the rules when added to the firewall at a later date.

Follow the on screen information, it will advise you which rules it is creating.
 That's it.
 

No comments:

Post a Comment