Is it possible to Block An Application from Accessing the Internet with cmd?

6,921

You could add firewall rule to block any incoming and outgoing traffic before running the application and disable (or delete) the firewall rules after exiting.

Setup block rules

:: Variables
set RULE_NAME=TemporaryBlock
set PROGRAM=C:\Program Files (x86)\App\app.exe

netsh advfirewall firewall add rule name="%RULE_NAME%" dir=in action=block profile=any program="%PROGRAM%"
netsh advfirewall firewall add rule name="%RULE_NAME%" dir=out action=block profile=any program="%PROGRAM%"

Run the app with internet blocked

@echo off

:: Variables
set RULE_NAME=TemporaryBlock
set PROGRAM=C:\Program Files (x86)\App\app.exe

:: Block the app
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=in new enable=yes
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=out new enable=yes

:: Running the app
start "" /wait "%PROGRAM%"

:: Disable the firewall rules
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=in new enable=no
netsh advfirewall firewall set rule name="%RULE_NAME%" dir=out new enable=no

Details

  • documentation: netsh advfirewall firewall add rule -?
  • you could also create and delete the rules everytime you run the app, but I like creating the rules once and then just enabling or disabling it more
  • you need admin rights
Share:
6,921

Related videos on Youtube

123iamking
Author by

123iamking

Updated on September 18, 2022

Comments

  • 123iamking
    123iamking over 1 year

    Normally, I would use FireWall to Block An Application from Accessing the Internet. But I don't want to setup my FireWall system for just an application.

    What I want is: create an bat file to start an application. The bat file similar to this.

    If I run the bat file, the application is blocked from accessing internet. If I run the application directly, the application can access internet.

    Is it possible to do that?

  • 123iamking
    123iamking over 5 years
    I see, Windows didn't design this feature so we have to do it manually for now. But how to use this batch file? Run it to start the application and remember to run it again after exit the application to delete firewall?
  • Vlastimil Ovčáčík
    Vlastimil Ovčáčík over 5 years
    The intention was that you will run Setup block rules script once. And then you will start your app using the Run the app with internet blocked script which does 3 things: enables the firewall block rules, starts your app and waits for it to exit (i.e. start /wait) and lastly disables the firewall rules.
  • 123iamking
    123iamking over 5 years
    What if I run 2 instances of the application, then exit the first instance but still let the second instance running.