Manipulate hosts file and add block rule to the firewall

Manipulate hosts file and add block rule to the firewall

Write Record to Hosts File

With this batch file we add any entries to the hosts file. This batch file does not have to be executed with elevated rights. If elevated rights are required, the script executes itself with elevated rights.

The batch also creates a block rule in the firewall. The red fields have to be adjusted individually.

@ Echo off
set ver = v5.3
title Entry in the host file
REM filter management
fltmc> nul 2> & 1 || (
echo Set UAC = CreateObject ^ (“Shell.Application” ^)> “% temp% \ Admin.vbs”
echo UAC.ShellExecute “% ~ fs0”, “”, “”, “runas”, 1 >> “% temp% \ Admin.vbs”
cmd / u / c type “% temp% \ Admin.vbs”> ”% temp% \ AdminCode.vbs”
cscript // nologo “% temp% \ Admincode.vbs”
del / f / q “% temp% \ Admin.vbs”> nul 2> & 1
del / f / q “% temp% \ AdminCode.vbs”> nul 2> & 1
exit
)

@ Echo off
SET hosts =% windir% \ system32 \ drivers \ etc \ hosts
attrib -r% hosts%
echo. >>% hosts%
FOR %% A IN (

Example.com
Example2.com

) DO (
echo 0.0.0.0 %% A >>% hosts%
)
attrib + r% hosts%
echo The entry was created

@ Echo off
netsh advfirewall firewall add rule name = ”Application.exe”Dir = out program =”C: \ Temp \ Application.exe”Action = block
echo The software is now blocked

Batch - Hosts file add entry