欢迎阅读本指南,本文旨在帮助系统管理员和IT专业人员通过命令行高效地管理和配置Windows防火墙。
启用与关闭防火墙
netsh.exe advfirewall set allprofiles state on
netsh advfirewall set allprofiles state off
防火墙初始化
netsh advfirewall reset
配置特定规则
netsh advfirewall firewall add rule name="Allow Inbound Telnet" dir=in program="%SystemRoot%System32tlntsvr.exe" remoteip=localsubnet action=allow
netsh advfirewall firewall add rule name="Block TCP 135 from 10.10.10.10" dir=in remoteip=10.10.10.10 protocol=tcp localport=135 action=block
netsh advfirewall firewall add rule name="Block Outbound Telnet" dir=out program="%SystemRoot%System32telnet.exe" protocol=tcp localport=23 action=block
netsh advfirewall firewall delete rule name="Allow TCP 135"
批量删除阻止规则
Remove-NetFirewallRule -Action Block
netsh advfirewall firewall /?
@echo off
chcp 65001 >nul
echo 启用Windows防火墙并添加拒绝指定端口的规则...
:: 检查是否以管理员权限运行
openfiles >nul 2>&1
if %errorlevel% neq 0 (
echo 请以管理员权限运行此脚本。
pause
exit /b 1
)
:: 启用所有配置文件的Windows防火墙
netsh advfirewall set allprofiles state on
if %errorlevel% neq 0 (
echo 无法启用Windows防火墙。
pause
exit /b 1
)
echo 规则开始添加
call :ADD_RULE "Deny TCP 135" TCP 135 in block
call :ADD_RULE "Deny TCP 137" TCP 137 in block
call :ADD_RULE "Deny UDP 137" UDP 137 in block
call :ADD_RULE "Deny TCP 138" TCP 138 in block
call :ADD_RULE "Deny UDP 138" UDP 138 in block
call :ADD_RULE "Deny TCP 139" TCP 139 in block
call :ADD_RULE "Deny TCP 445" TCP 445 in block
echo 操作完成,请按任意键退出
pause >nul
exit /b 0
:ADD_RULE
netsh advfirewall firewall add rule name=%1 protocol=%2 localport=%3 dir=%4 action=%5 enable=yes
if %errorlevel% neq 0 (
echo 无法添加规则: %1
pause
exit /b 1
)
echo Windows防火墙已启用,且已添加%1的规则。
goto :EOF
:: 结束标签
:EOF
原文始发于微信公众号(网络个人修炼):Windows 防火墙管理指南与脚本优化
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论