cmd命令增删防火墙规则
一、添加规则
命令:
netsh advfirewall firewall add rule
用法:
add rule name=<string> dir=in|out action=allow|block|bypass
[program=<program path>]
[service=<service short name>|any]
[description=<string>]
[enable=yes|no (default=yes)]
[profile=public|private|domain|any[,…]]
[localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
[remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
[localport=0–65535|<port range>[,…]|RPC|RPC-EPMap|IPHTTPS|any (default=any)]
[remoteport=0–65535|<port range>[,…]|any (default=any)]
[protocol=0–255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|tcp|udp|any (default=any)]
[interfacetype=wireless|lan|ras|any]
[rmtcomputergrp=<SDDL string>]
[rmtusrgrp=<SDDL string>]
[edge=yes|deferapp|deferuser|no (default=no)]
[security=authenticate|authenc|authdynenc|authnoencap|notrequired(default=notrequired)]
注意点:
– 规则名称可以不唯一的,删除时指定name一次性全删。
– 如果已指定远程计算机或用户组,则 security 必须为authenticate、authenc、authdynenc 或 authnoencap。
– 为 authdynenc 设置安全性可允许系统动态协商为匹配给定 Windows 防火墙规则的通信使用加密。
– 根据现有连接安全规则属性协商加密。选择此选项后,只要入站 IPSec 连接已设置安全保护,但未使用 IPSec 进行加密,计算机就能够接收该入站连接的第一个 TCP 或 UDP 包。一旦处理了第一个数据包,服务器将重新协商连接并对其进行升级,以便所有后续通信都完全加密。
– 如果 action=bypass,则 dir=in 时必须指定远程计算机组。
– 如果 service=any,则规则仅应用到服务。
– ICMP 类型或代码可以为 “any”。
– Edge 只能为入站规则指定。
– AuthEnc 和 authnoencap 不能同时使用。
– Authdynenc 仅当 dir=in 时有效。
– 设置 authnoencap 后,security=authenticate 选项就变成可选参数。
CMD示例:
A、为不具有封装的 messenger.exe 添加入站规则:
netsh advfirewall firewall add rule name=“allow messenger” dir=in program=“c:\programfiles\messenger\msmsgs.exe” security=authnoencap action=allow
B、为端口 80 添加出站规则:
netsh advfirewall firewall add rule name=“allow80” protocol=TCP dir=out localport=80 action=block
C、 为 TCP 端口 80 通信添加需要安全和加密的入站规则:
netsh advfirewall firewall add rule name=“Require Encryption for Inbound TCP/80” protocol=TCP dir=in localport=80 security=authdynenc action=allow
D、为 messenger.exe 添加需要安全的入站规则:
netsh advfirewall firewall add rule name=“allow messenger” dir=in program=“c:\program files\messenger\msmsgs.exe” security=authenticate action=allow
E、 为 SDDL 字符串标识的组 acmedomain\scanners 添加经过身份验证的防火墙跳过规则:
netsh advfirewall firewall add rule name=“allow scanners” dir=in rmtcomputergrp=<SDDL string> action=bypass security=authenticate
F、 为 udp- 的本地端口 5000-5010 添加出站允许规则:
Add rule name=“Allow port range” dir=out protocol=udp localport=5000–5010 action=allow
批处理示例:
@echo off
color 0A
echo 打开防火墙…
sc config sharedaccess start= auto > nul
net start sharedaccess > nul
echo 向防火墙添加禁用135,139,445端口tcp规则…
netsh advfirewall firewall add rule name=“forbid_tcp_rules” dir=in protocol=tcp localport=135,445,139 action=block
echo 向防火墙添加禁用135,139,445端口udp规则…
netsh advfirewall firewall add rule name=“forbid_udp_rules” dir=in protocol=udp localport=135,445,139 action=block
echo 完成
pause
二、删除规则
命令:
netsh advfirewall firewall delete rule /?
用法: delete rule name=<string>
[dir=in|out]
[profile=public|private|domain|any[,...]]
[program=<program path>]
[service=<service short name>|any]
[localip=any|<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
[remoteip=any|localsubnet|dns|dhcp|wins|defaultgateway|
<IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
[localport=0-65535|<port range>[,...]|RPC|RPC-EPMap|any]
[remoteport=0-65535|<port range>[,...]|any]
[protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|
tcp|udp|any]
注释:
- 删除按名称识别的规则,也可按终结点、端口、
协议和类型识别规则
- 如果找到多个匹配项,则删除所有匹配规则。
- 如果指定 name=all,则从指定的
类型和配置文件中删除所有规则。
示例:
删除本地端口 80 的所有入则:
netsh advfirewall firewall delete rule name=all protocol=tcp localport=80
删除名为 "allow80" 的规则:
netsh advfirewall firewall delete rule name="allow80"
2020-07-29 04:35:27
共有0条评论!