CMD
添加示例
netsh advfirewall firewall add rule name="規則名稱"dir=in action=allow protocol=TCP localport=1030
一些參數說明:
name:規則名稱
dir:方向 (in入口方向 out出口方向)
action:行為(allow允許 block 阻止)
protocol:協議類型(TCP/UDP)
localport:本地端口
program:程序路徑
security:安全(使用默認或者不指定即可)(authenticate|authenc|authdynenc|authnoencap|notrequired(default=notrequired))
authenticate - 要求雙向身份驗證,但不加密
authenc - 要求雙向身份驗證和加密
authdynenc - 要求身份驗證,加密可選
authnoencap - 要求身份驗證,禁用ESP封裝
notrequired - 不要求安全措施
以下是一些示例:
為 messenger.exe 添加入站規則:
1 netsh advfirewall firewall add rule name="allow messenger" dir=in program="c:\programfiles\messenger\msmsgs.exe" security=authnoencap action=allow
為端口 80 添加出站規則(禁止):
netsh advfirewall firewall add rule name="allow80" protocol=TCP dir=out localport=80 action=block
為 TCP 端口 80 通信添加需要安全和加密的入站規則:
netsh advfirewall firewall add rule name="Require Encryption for Inbound TCP/80" protocol=TCP dir=in localport=80 security=authdynenc action=allow
為 messenger.exe 添加需要安全的入站規則:
netsh advfirewall firewall add rule name="allow messenger"dir=in program="c:\program files\messenger\msmsgs.exe" security=authenticate action=allow
為 SDDL 字符串標識的組 acmedomain\scanners 添加經過身份驗證的防火墻跳過規則:
netsh advfirewall firewall add rule name="allow scanners"dir=in rmtcomputergrp=<SDDL string> action=bypass security=authenticate
為 udp- 的本地端口 5000-5010 添加出站允許規則
netsh advfirewall firewall Add rule name="Allow port range"dir=out protocol=udp localport=5000-5010 action=allow
啟用名為 "MyRule" 的防火墻規則:
netsh advfirewall firewall set rule name="MyRule" new enable=yes
將名為 "MyRule" 的規則的本地端口更改為 8080:
netsh advfirewall firewall set rule name="MyRule" new localport=8080
將名為 "MyRule" 的規則的遠程 IP 地址限制為 192.168.1.1:
netsh advfirewall firewall set rule name="MyRule" new remoteip=192.168.1.1
將名為 "MyRule" 的規則的協議更改為 UDP:
netsh advfirewall firewall set rule name="MyRule" new protocol=UDP
運行以下命令查看規則是否已正確修改:
netsh advfirewall firewall show rule name="MyRule"
刪除示例
刪除本地端口 80 的所有入則:
netsh advfirewall firewall delete rule name=all protocol=tcp localport=80
刪除名為 "allow80" 的規則:
netsh advfirewall firewall delete rule name="allow80"
PowerShell
New-NetFirewallRule -DisplayName '規則名稱' -Profile 'Private' -Direction Inbound -Action Allow -Protocol TCP -LocalPort 6124
詳細的使用介紹可以查看參考資料中的鏈接,非常詳細 。
參考資料:
https://learn.microsoft.com/zh-cn/windows/security/operating-system-security/network-security/windows-firewall/configure-with-command-line?tabs=cmd
https://learn.microsoft.com/zh-cn/troubleshoot/windows-server/networking/netsh-advfirewall-firewall-control-firewall-behavior
閱讀原文:原文鏈接
該文章在 2025/7/18 11:02:59 編輯過