Powershell/CMD添加Windows防火墙规则

CMD

添加示例

1 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))

 

以下是一些示例:

为不具有封装的 messenger.exe 添加入站规则:

1 netsh advfirewall firewall add rule name="allow messenger" dir=in program="c:\programfiles\messenger\msmsgs.exe" security=authnoencap action=allow

 

为端口 80 添加出站规则:

1 netsh advfirewall firewall add rule name="allow80" protocol=TCP dir=out localport=80 action=block

 

为 TCP 端口 80 通信添加需要安全和加密的入站规则:

1 netsh advfirewall firewall add rule name="Require Encryption for Inbound TCP/80" protocol=TCP dir=in localport=80 security=authdynenc action=allow

 

为 messenger.exe 添加需要安全的入站规则:

1 netsh advfirewall firewall add rule name="allow messenger" dir=in program="c:\program files\messenger\msmsgs.exe" security=authenticate action=allow

 

为 SDDL 字符串标识的组 acmedomain\scanners 添加经过身份验证的防火墙跳过规则:

1 netsh advfirewall firewall add rule name="allow scanners" dir=in rmtcomputergrp=<SDDL string> action=bypass security=authenticate

 

为 udp- 的本地端口 5000-5010 添加出站允许规则

1 Add rule name="Allow port range" dir=out protocol=udp localport=5000-5010 action=allow

 

删除示例

删除本地端口 80 的所有入则:

1 netsh advfirewall firewall delete rule name=all protocol=tcp localport=80

 

删除名为 "allow80" 的规则:

1 netsh advfirewall firewall delete rule name="allow80"

 

 

PowerShell

1 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

posted @ 2024-07-17 10:34  zhaotianff  阅读(191)  评论(0编辑  收藏  举报