如何通过PowerShell配置Windows Server 2022的防火墙策略?
美国、香港服务器
如何通过PowerShell配置Windows Server 2022的防火墙策略?
07-13 来源:
一、基础查看命令(先熟悉)
1. 查看全部入站规则
powershell
Get-NetFirewallRule -Direction Inbound | Select DisplayName,LocalPort,RemoteAddress,Enabled,Action
2. 查看指定端口规则(例如筛选 3389)
powershell
Get-NetFirewallRule | Where-Object {$_.LocalPort -eq "3389"}
3. 确认防火墙全局开启(云服务器必须打开防火墙,禁止关闭)
powershell
#开启防火墙全部配置文件
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled True
#查看防火墙开启状态
Get-NetFirewallProfile | Select Name,Enabled
二、常用实操命令(结合你的 Windows‑Server2022 服务器)
场景 1:修改远程桌面 3389(解决你账号被暴力破解的核心操作)
① 删除系统默认允许全网访问 3389 的旧规则
powershell
Remove-NetFirewallRule -DisplayName "远程桌面-TCP-入站" -Confirm:$false
② 只允许你的固定公网 IP 访问 3389(推荐生产配置)
把shturl.cc/HPIbwF5M替换成你自己电脑的公网 IP(百度搜索 IP 即可查到):
powershell
New-NetFirewallRule -DisplayName "RDP仅本人IP访问3389" `
-Direction Inbound `
-Protocol TCP `
-LocalPort 3389 `
-RemoteAddress "shturl.cc/HPIbwF5M" `
-Action Allow `
-Profile Any
-RemoteAddress "Any"代表全网所有人都可以访问,绝对不要给 3389 配置 Any。
场景 2:放行网站 80、443 端口
powershell
New-NetFirewallRule -DisplayName "放行80和443端口" `
-Direction Inbound `
-Protocol TCP `
-LocalPort 80,443 `
-RemoteAddress Any `
-Action Allow `
-Profile Any
场景 3:阻断高危端口 135,139,445(防止永恒之蓝勒索病毒,必做)
powershell
New-NetFirewallRule -DisplayName "阻断高危端口135‑139‑445" `
-Direction Inbound `
-Protocol TCP `
-LocalPort 135,139,445 `
-Action Block `
-Profile Any
场景 4:放行自定义端口(比如 8388 端口仅指定 IP 访问)
powershell
New-NetFirewallRule -DisplayName "8388仅指定IP访问" `
-Direction Inbound `
-Protocol TCP `
-LocalPort 8388 `
-RemoteAddress "shturl.cc/HPIbwF5M" `
-Action Allow `
-Profile Any
场景 5:删除一条防火墙规则(按规则名称删除)
powershell
Remove-NetFirewallRule -DisplayName "放行80和443端口" -Confirm:$false
‑Confirm:$false:跳过确认直接删除。
场景 6:禁用一条规则(暂时关闭,以后可以启用,不删除)
powershell
#禁用
Disable-NetFirewallRule -DisplayName "RDP仅本人IP访问3389"
#启用
Enable-NetFirewallRule -DisplayName "RDP仅本人IP访问3389"
三、高级配置(防火墙日志 + 默认安全策略)
1. 设置默认入站全部阻止(最佳安全规范:只放通你手动添加的端口)
Windows 防火墙默认策略:入站默认阻止,出站默认允许。
powershell
Set-NetFirewallProfile -Profile Domain,Private,Public -DefaultInboundAction Block
2. 开启防火墙日志,记录攻击 IP
powershell
Set-NetFirewallProfile -Profile Domain,Public,Private `
-LogBlocked True `
-LogAllowed False `
-LogFileName "%systemroot%\system32\LogFiles\Firewall\pfirewall.log" `
-LogMaxSizeKilobytes 16384
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快