1. Mr.xu 博客网首页
  2. 资源分享

批处理利用Windows防火墙阻止程序和服务联网

通过CMD命令录入“netsh advfirewall firewall add rule” 就会获取到有帮助内容,里面正好有设置端口的例子。

Windows防火墙其实是个好东西,但是好像很少有人好好利用起来,这次简单说说利用Windows防火墙阻止程序联网。

记得批处理里面是先删除再添加的,你注释掉添加那一句就只是删除了

netsh advfirewall firewall del rule name=%%i>nul 2>nul

netsh advfirewall firewall add rule name=”%%i” program=%%j action=block dir=out>nul

@echo off
setlocal ENABLEDELAYEDEXPANSION
mode con: cols=65 lines=25
color 0a
rem 批处理获取管理员权限
:-------------------------------------  
    %1 mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&&exit /b
    CD /D "%~dp0"
:-------------------------------------  

::添加阻止程序出站规则
(
        echo "Office_OfficeClickToRun","%ProgramFiles%\Common Files\microsoft shared\ClickToRun\OfficeClickToRun.exe"
        echo "Office_msoia","%ProgramFiles%\Microsoft Office\root\Office16\msoia.exe"
        echo "Office_WINWORD","%ProgramFiles%\Microsoft Office\root\Office16\WINWORD.EXE"
        echo "Office_EXCEL","%ProgramFiles%\Microsoft Office\root\Office16\EXCEL.EXE"
        echo "Office_POWERPNT","%ProgramFiles%\Microsoft Office\root\Office16\POWERPNT.EXE"
        echo "Office_VISIO","%ProgramFiles%\Microsoft Office\root\Office16\VISIO.EXE"
        echo "Office_WINPROJ","%ProgramFiles%\Microsoft Office\root\Office16\WINPROJ.EXE"
        echo "Office_MSPUB","%ProgramFiles%\Microsoft Office\root\Office16\MSPUB.EXE"
        echo "Office_MSACCESS","%ProgramFiles%\Microsoft Office\root\Office16\MSACCESS.EXE"
        echo "AutoCAD_LMU","%ProgramFiles%\Common Files\Autodesk Shared\AdLM\R11\LMU.exe"
        echo "AutoCAD_DADispatcherService","C:\Program Files\Autodesk\AutoCAD 2016\DADispatcherService.exe"
        echo "AutoCAD_acad","C:\Program Files\Autodesk\AutoCAD 2016\acad.exe"
)>list.txt
FOR /F "usebackq tokens=1,2 delims=," %%i in ( list.txt ) do (
        netsh advfirewall firewall del rule name="%%i">nul 2>nul
        netsh advfirewall firewall add rule name="%%i" program=%%j action=block dir=out>nul
        echo 阻止%%i程序出站规则已添加
)
del list.txt

::添加阻止服务出站规则
(
        echo "Windows Update","wuauserv"
)>list.txt
set svchost_exe="%%systemroot%%\system32\svchost.exe"
FOR /F "usebackq tokens=1,2 delims=," %%i in ( list.txt ) do (
        netsh advfirewall firewall del rule name=%%i>nul 2>nul
        netsh advfirewall firewall add rule name=%%i service=%%j action=block dir=out program="%svchost_exe%">nul
        echo 阻止%%i服务出站规则已添加
)
del list.txt
pause

历史上的今天

2015年:解决Windows8.1/10下安装secoway vpn client后启动时报虚拟网卡无法启动(0条评论)

原创文章《批处理利用Windows防火墙阻止程序和服务联网》,作者:Admin,未经允许不得转载。
转载或复制时,请注明出处:https://www.xuxiaokun.com/229.html,本文由 Mr.xu 博客网 整理。

发表评论

登录后才能评论
线上沟通

笔者期待与您共勉 · 互赢

有言必答:点击这里给我发消息

商务洽谈:交互沟通 · 共赢互利

邮件:admin@xuxiaokun.com

线上沟通:周一至周五 9:30 - 17:30

其余时间Mail或QQ我,有言必应。

微信关注
微信关注
投递稿件 我要留言
分享本页
返回顶部