Web Restriction Script For Windows

by | Nov 22, 2020

Code in windows using Bats

Time Saver Code is Here, Allows To Force Prevent Accessing Websites.

Have you ever thought, how to make a quick and easy way to prevent yourself from social network websites that we tend to binge all the time? I did. From time to time, I get overwhelmed by information social networks deliver, it’s addictive and simply too much. [Windows Only]

What It Does

Basically, corrects your hosts file connection and instantly uses IP address that you have defined to be associated with website.  

More Details Please

Ok, You specify name of website and define fixed IP. Within code here IP is set to be 8.8.8.8 which is google DNS server IP. Every time you get redirected via other web or decide to open some sort of shady link if that link is going anywhere near those websites that is listed within this code, it will simply stop due to fact that IP is fixed and it no longer receives further instructions what it should do. So it stops, and presents you with a wonderful blank page.

StopSocial.bat:

@echo off
TITLE Modifying your HOSTS file
COLOR 3b
ECHO.


:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%system32cacls.exe" "%SYSTEMROOT%system32configsystem"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%getadmin.vbs"
    set params = %*:"="
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%getadmin.vbs"

    "%temp%getadmin.vbs"
    del "%temp%getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop


:REJECTED
ECHO Your HOSTS file was left unchanged>>%systemroot%TemphostFileUpdate.log
ECHO Finished.
GOTO END


:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of host domains
set LIST=(facebook.com www.facebook.com m.facebook.com instagram.com m.instagram.com m.facebook.com messenger.com youtube )
::Set the ip of the domains you set in the list above

:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
::ECHO %WINDIR%System32driversetchosts > tmp.txt
for  %%G in (%_list%) do (
    set  _name=%%G
    set  _value=!%%G!
    SET NEWLINE=^& echo.
    ECHO Carrying out requested modifications to your HOSTS file
    ::strip out this specific line and store in tmp file
    type %WINDIR%System32driversetchosts | findstr /v !_name! > tmp.txt
    ::re-add the line to it
    ECHO %NEWLINE%^!_value! !_name!>>tmp.txt
    ::overwrite host file
    copy /b/v/y tmp.txt %WINDIR%System32driversetchosts
    del tmp.txt
)
ipconfig /flushdns
ECHO.
ECHO.
ECHO Finished, you may close this window now.
ECHO You should now open Chrome and go to "chrome://net-internals/#dns" (without quotes)
ECHO     then click the "clear host cache" button
GOTO END

:END
ECHO.
ping -n 11 192.0.2.2 > nul
EXIT

How To Use It

Code is written as batch file using JCL language. In order for this script to work, you need to do following: 

1. Open Notepad or any IDE
2. Copy code
3. Paste code into notepad
4. Select File, then “Save as”
5. Type “stopsocial.BAT” (Mandatory “.bat” extension”) Otherwise code won’t work.
6. Launch the file, file will ask you for administrator privileges to modify HOSTS File. After you grant it, changes will be made within seconds.  If you are not granting access as admin, code won’t work.

 

StartSocial.bat:

I understand, everyone wants the best and want’s to play arround with features and scripts but not everything is bound to be permanent. Script bellow clears out HOSTS file for you to access any sort of website you choose.

@echo off
TITLE Modifying your HOSTS file
COLOR 3b
ECHO.


:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%system32cacls.exe" "%SYSTEMROOT%system32configsystem"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%getadmin.vbs"
    set params = %*:"="
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%getadmin.vbs"

    "%temp%getadmin.vbs"
    del "%temp%getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"

IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%

ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop


:REJECTED
ECHO Your HOSTS file was left unchanged>>%systemroot%TemphostFileUpdate.log
ECHO Finished.
GOTO END


:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of host domains
set LIST=(facebook.com www.facebook.com m.facebook.com instagram.com m.instagram.com m.facebook.com messenger.com youtube )
::Set the ip of the domains you set in the list above

:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
::ECHO %WINDIR%System32driversetchosts > tmp.txt
for  %%G in (%_list%) do (
    set  _name=%%G
    set  _value=!%%G!
    SET NEWLINE=^& echo.
    ECHO Carrying out requested modifications to your HOSTS file
    ::strip out this specific line and store in tmp file
    type %WINDIR%System32driversetchosts | findstr /v !_name! > tmp.txt
    ::re-add the line to it
    ECHO %NEWLINE%^!_value! !_name!>>tmp.txt
    ::overwrite host file
    copy /b/v/y tmp.txt %WINDIR%System32driversetchosts
    del tmp.txt
)
ipconfig /flushdns
ECHO.
ECHO.
ECHO Finished, you may close this window now.
ECHO You should now open Chrome and go to "chrome://net-internals/#dns" (without quotes)
ECHO     then click the "clear host cache" button
GOTO END

:END
ECHO.
ping -n 11 192.0.2.2 > nul
EXIT

Use

This script may be used in several ways. Corporate, prevention of kids accessing websites you know and dont want to be accessing, any person you tend to share your pc with.

guest
0 Comments
Inline Feedbacks
View all comments

Become Patron, be exclusive:

  •            Sceduled content discussion pre-release
  •            Release planing
  •            Suggestions
  •            Coaching one on one
  •            And more
© 2020 Wifilobster.com All rights reserved.