Batch Script to block social media from network

Discussion in 'Programming/Html' started by Loobyluggs, May 23, 2024.

  1. Loobyluggs

    Loobyluggs Ancient Guru

    Messages:
    5,309
    Likes Received:
    1,659
    GPU:
    RTX 3060 12GB
    Code:
    @echo off
    color F0
    REM FILEPATH: %UserProfile%\Desktop\block_me.bat
    REM Display a warning to the user
    echo WARNING: This script needs to be run with administrative privileges because it modifies the system's firewall rules.
    setlocal EnableDelayedExpansion
    
    :: ASCII start banner
    set "banner[0]= ________          ___  ___          ________          ___  ___          ________          ________"
    set "banner[1]=|\   ____\        |\  \|\  \        |\   __  \        |\  \|\  \        |\_____  \        |\   ___ \"
    set "banner[2]=\ \  \___|        \ \  \\\  \       \ \  \|\  \       \ \  \\\  \       \|____|\ /_       \ \  \_|\ \"
    set "banner[3]= \ \  \  ___       \ \  \\\  \       \ \   _  _\       \ \  \\\  \            \|\  \       \ \  \ \\ \"
    set "banner[4]=  \ \  \|\  \       \ \  \\\  \       \ \  \\  \|       \ \  \\\  \          __\_\  \       \ \  \_\\ \"
    set "banner[5]=   \ \_______\       \ \_______\       \ \__\\ _\        \ \_______\        |\_______\       \ \_______\"
    set "banner[6]=    \|_______|        \|_______|        \|__|\|__|        \|_______|        \|_______|        \|_______|"
    
    :: Display ASCII start banner with time delay
    for /L %%i in (0,1,6) do (
        echo !banner[%%i]!
        ping localhost -n 2 >nul
    )
    REM Check if the script is running with administrative privileges
    net session >nul 2>&1
    if %errorLevel% neq 0 (
        echo ERROR: This script must be run with administrative privileges.
        exit /b
    )
    
    REM Define the domain names to block
    set "Domains=facebook.com youtube.com whatsapp.com wechat.com instagram.com tiktok.com qq.com tumblr.com linkedin.com twitter.com reddit.com baidu.com snapchat.com pinterest.com viber.com line.me telegram.org medium.com twitch.tv quora.com discord.com vimeo.com slideshare.net flickr.com meetup.com"
    
    REM Loop through each domain
    for %%D in (%Domains%) do (
        REM Ask the user whether they want to block the website
        set /p "UserInput=Do you want to block %%D? (y/n)"
    
        REM Check if the user input is 'y'
        if /i "%UserInput%"=="y" (
            REM Check if nslookup command is available
            where nslookup >nul 2>&1
            if %errorLevel% neq 0 (
                echo ERROR: nslookup command not found. Please make sure it is installed.
                exit /b
            )
    
            REM Resolve the domain name to an IP address
            for /f "tokens=2 delims=: " %%I in ('nslookup %%D ^| findstr /i "address"') do (
                REM Create a new outbound firewall rule to block all traffic to the IP address
                netsh advfirewall firewall add rule name="Block Outbound Traffic to %%D" dir=out remoteip=%%I action=block
            )
        )
    )
    
    REM Ask the user whether they want to generate a new script to undo the changes
    set /p "UserInput=Do you want to generate a new script to undo the changes? (y/n)"
    
    REM Check if the user input is 'y'
    if /i "%UserInput%"=="y" (
        REM Generate a new batch script called 'unblock me.bat' on the user's desktop
        >"%UserProfile%\Desktop\unblock me.bat" (
            echo @echo off
            echo REM This script needs to be run with administrative privileges because it modifies the system's firewall rules.
            echo net session ^>nul 2^>^&1
            echo if %%errorLevel%% neq 0 (
            echo     echo ERROR: This script must be run with administrative privileges.
            echo     exit /b
            echo )
            for %%D in (%Domains%) do (
                echo netsh advfirewall firewall delete rule name^="Block Outbound Traffic to %%D"
            )
        )
    )
    
    set "EncodedMessage=SGFsZi1MaWZlIDIgRXBpc29kZSAzIERyYWZ0"
    powershell -Command "Write-Host ([Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('%EncodedMessage%')))"
    timeout /t 1 >nul
    exit /b
    
     
    Last edited: Jun 4, 2024
    Valken, 386SX and mbk1969 like this.
  2. RealNC

    RealNC Ancient Guru

    Messages:
    5,492
    Likes Received:
    3,749
    GPU:
    4070 Ti Super
    It's "batch," not "bath" :p
     
    mbk1969 likes this.
  3. mbk1969

    mbk1969 Ancient Guru

    Messages:
    15,895
    Likes Received:
    13,841
    GPU:
    GF RTX 4070
    And it needs a second batch file to remove those created firewall rules.
     
  4. Loobyluggs

    Loobyluggs Ancient Guru

    Messages:
    5,309
    Likes Received:
    1,659
    GPU:
    RTX 3060 12GB
    Valid point, made changes to generate a script onto the desktop to undo the cmds, let me know how it goes.
     
    mbk1969 likes this.

  5. Loobyluggs

    Loobyluggs Ancient Guru

    Messages:
    5,309
    Likes Received:
    1,659
    GPU:
    RTX 3060 12GB
    :cool: Typos are cool, it shows I am human
     
    eGGroLLiO likes this.
  6. 386SX

    386SX Ancient Guru

    Messages:
    2,143
    Likes Received:
    2,295
    GPU:
    AMD Vega64 RedDevil
    Code:
    REM Check if the script is running with administrative privileges
    net session >nul 2>&1
    if %errorLevel% neq 0 (
        echo ERROR: This script must be run with administrative privileges.
        exit /b
    )
    If you need to run this as admin, I would add a part to auto-elevate. Something like this:

    Code:
    :CHECKADMIN
    REM Intended privilege escalation to admin ...
    
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    if '%errorlevel%' NEQ '0' (
        echo Requesting Admin access...
        goto GETADMIN
       )
    goto ISADMIN
    
    :GETADMIN
        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
    
    :ISADMIN
     
  7. wavetrex

    wavetrex Ancient Guru

    Messages:
    2,556
    Likes Received:
    2,697
    GPU:
    ROG RTX 6090 Ultra
    Might as well do this, pretty much achieves the same result.

    [​IMG]

    I mean, when I saw the 2nd entry ... "youtube.com", really ?
     
  8. Loobyluggs

    Loobyluggs Ancient Guru

    Messages:
    5,309
    Likes Received:
    1,659
    GPU:
    RTX 3060 12GB
    Some people have issues with google, all things being equal, that is healthy paranoia...no debate needed in the programming sub-forum...
     

Share This Page