Windows防火墙配置详解

admin 2026-03-12 23:18:19 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文详细介绍了Windows防火墙的配置方法,涵盖基础知识、图形界面操作、Netsh命令行管理、高级安全设置及PowerShell管理等。文档提供了大量实用命令示例,包括端口规则、程序规则、ICMP规则的创建与管理,以及连接安全规则和组策略集成。内容结构清晰,适合运维人员学习Windows主机防火墙的安全加固与日常管理。 综合评分: 81 文章分类: 安全建设,终端安全,安全工具,安全运营


cover_image

Windows 防火墙配置详解

原创

刘军军 刘军军

运维星火燎原

2026年3月12日 00:01 山西

目录

  1. Windows 防火墙基础
  2. 图形界面配置
  3. 命令行配置(Netsh)
  4. 高级安全配置
  5. 规则管理详解
  6. 网络配置文件
  7. 日志与监控
  8. 故障排除
  9. 安全最佳实践
  10. 实用脚本模板

一、Windows 防火墙基础

1.1 什么是 Windows 防火墙?

Windows 防火墙(Windows Defender Firewall)是 Windows 操作系统内置的主机防火墙,用于控制进出计算机的网络流量,保护系统免受未经授权的访问和恶意软件攻击。

1.2 防火墙版本演进

| | | | | — | — | — | | Windows 版本 | 防火墙名称 | 主要特性 | | Windows XP SP2 | Windows Firewall | 基础入站过滤 | | Windows Vista/7 | Windows Firewall with Advanced Security | 入站+出站过滤 | | Windows 8/10/11 | Windows Defender Firewall | 集成 Windows Defender |

1.3 防火墙工作原理

┌─────────────────────────────────────────────────────────────┐
│                    应用程序层                               │
└───────────────────────────┬─────────────────────────────────┘
                            │
┌───────────────────────────▼─────────────────────────────────┐
│                  Windows 防火墙                           │
│  ┌─────────────┐    ┌─────────────┐    ┌─────────────┐   │
│  │ 入站规则     │    │ 出站规则     │    │ 连接安全规则 │   │
│  └─────────────┘    └─────────────┘    └─────────────┘   │
└───────────────────────────┬─────────────────────────────────┘
                            │
┌───────────────────────────▼─────────────────────────────────┐
│                    网络适配器层                             │
└─────────────────────────────────────────────────────────────┘

1.4 防火墙状态类型

  • 域网络(Domain):连接到域控制器的网络
  • 专用网络(Private):家庭或工作网络
  • 公用网络(Public):公共场所网络(最严格)

二、图形界面配置

2.1 访问防火墙设置

:: 打开防火墙控制面板
wf.msc

:: 或通过控制面板
control.exe /name Microsoft.WindowsFirewall

:: 或通过设置(Windows 10/11)
start ms-settings:windowsdefender

2.2 基本配置界面

2.2.1 防火墙状态管理

Windows Defender 防火墙
├── 专用网络设置
│   ├── 打开或关闭 Windows Defender 防火墙
│   └── 通知设置
├── 公用网络设置
│   ├── 打开或关闭 Windows Defender 防火墙
│   └── 通知设置
└── 还原默认设置

2.2.2 允许应用通过防火墙

允许应用或功能通过 Windows Defender 防火墙
├── 更改设置(需要管理员权限)
├── 允许其他应用...
└── 应用列表
    ├── 名称
    ├── 专用(√/×)
    └── 公用(√/×)

2.3 高级安全配置(wf.msc)

2.3.1 主界面结构

Windows Defender 防火墙 with Advanced Security
├── 入站规则
├── 出站规则
├── 连接安全规则
├── 监视
│   ├── 安全关联
│   ├── 安全事件
│   └── 筛选的连接
└── 属性
    ├── 域配置文件
    ├── 专用配置文件
    └── 公用配置文件

2.3.2 创建新规则向导

入站规则创建步骤:

  1. 规则类型选择
  • 程序:基于特定程序
  • 端口:基于端口号
  • 预定义:系统预定义规则
  • 自定义:高级自定义规则
  1. 程序指定
  • 此程序路径:C:\Program Files\MyApp\app.exe
  • 所有程序
  1. 协议和端口
  • 协议类型:TCP/UDP/ICMPv4/ICMPv6/任何
  • 特定本地端口:80, 443, 3389
  • 远程端口:任意/特定
  1. 作用域
  • 本地 IP 地址:任意/特定 IP 范围
  • 远程 IP 地址:任意/特定 IP 范围
  1. 操作
  • 允许连接
  • 允许连接(如果安全)
  • 阻止连接
  1. 配置文件
  • 域:√
  • 专用:√
  • 公用:×
  1. 名称和描述
  • 名称:Web Server HTTP
  • 描述:允许 HTTP 流量通过端口 80

三、命令行配置(Netsh)

3.1 Netsh 基础命令

:: 查看防火墙状态
netsh advfirewall show allprofiles state

:: 启用/禁用防火墙
netsh advfirewall set allprofiles state on
netsh advfirewall set allprofiles state off

:: 查看详细配置
netsh advfirewall show allprofiles

:: 重置防火墙到默认设置
netsh advfirewall reset

3.2 规则管理命令

3.2.1 查看规则

:: 查看出站规则
netsh advfirewall firewall show rule name=all dir=out

:: 查看入站规则
netsh advfirewall firewall show rule name=all dir=in

:: 查看特定规则
netsh advfirewall firewall show rule name="Remote Desktop"

:: 查看启用的规则
netsh advfirewall firewall show rule name=all enable=yes

:: 导出所有规则
netsh advfirewall firewall show rule name=all > firewall_rules.txt

3.2.2 添加规则

:: ========== 端口规则 ==========
:: 允许 TCP 端口 80(HTTP)
netsh advfirewall firewall add rule name="HTTP" dir=in action=allow protocol=TCP localport=80

:: 允许 TCP 端口 443(HTTPS)
netsh advfirewall firewall add rule name="HTTPS" dir=in action=allow protocol=TCP localport=443

:: 允许 UDP 端口 53(DNS)
netsh advfirewall firewall add rule name="DNS" dir=in action=allow protocol=UDP localport=53

:: 允许多个端口
netsh advfirewall firewall add rule name="Web Services" dir=in action=allow protocol=TCP localport=80,443,8080

:: 允许端口范围
netsh advfirewall firewall add rule name="Custom Range" dir=in action=allow protocol=TCP localport=8000-8100

:: ========== 程序规则 ==========
:: 允许特定程序
netsh advfirewall firewall add rule name="My Application" dir=in action=allow program="C:\MyApp\app.exe"

:: 允许所有程序(危险!)
netsh advfirewall firewall add rule name="All Programs" dir=in action=allow program="any"

:: ========== ICMP 规则 ==========
:: 允许 Ping(ICMP Echo Request)
netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" dir=in action=allow protocol=icmpv4:8,any

:: 允许所有 ICMP
netsh advfirewall firewall add rule name="ICMP All" dir=in action=allow protocol=icmpv4:any,any

3.2.3 高级规则配置

:: ========== 作用域限制 ==========
:: 仅允许特定 IP 访问
netsh advfirewall firewall add rule name="Restricted Access" dir=in action=allow protocol=TCP localport=3389 remoteip=192.168.1.100,10.0.0.0/24

:: 仅允许本地特定 IP
netsh advfirewall firewall add rule name="Local Restriction" dir=in action=allow protocol=TCP localport=80 localip=192.168.1.50

:: ========== 配置文件指定 ==========
:: 仅在专用网络启用
netsh advfirewall firewall add rule name="Private Only" dir=in action=allow protocol=TCP localport=80profile=private

:: 在域和专用网络启用
netsh advfirewall firewall add rule name="Domain Private" dir=in action=allow protocol=TCP localport=80profile=domain,private

:: ========== 安全设置 ==========
:: 要求加密连接
netsh advfirewall firewall add rule name="Secure Connection" dir=in action=allow protocol=TCP localport=443 security=authenticate

:: 仅允许已认证的连接
netsh advfirewall firewall add rule name="Authenticated Only" dir=in action=allow program="C:\MyApp\app.exe" security=authenticate

3.2.4 修改和删除规则

:: ========== 修改规则 ==========
:: 启用/禁用规则
netsh advfirewall firewall set rule name="HTTP"newenable=yes
netsh advfirewall firewall set rule name="HTTP"newenable=no

:: 修改规则操作
netsh advfirewall firewall set rule name="HTTP"newaction=block

:: 修改端口
netsh advfirewall firewall set rule name="HTTP"new localport=8080

:: ========== 删除规则 ==========
:: 删除特定规则
netsh advfirewall firewall delete rule name="HTTP"

:: 删除所有匹配规则
netsh advfirewall firewall delete rule name=all protocol=TCP localport=80

:: 按组删除
netsh advfirewall firewall delete rule group="My Application Group"

3.3 配置文件管理

:: ========== 查看配置文件 ==========
netsh advfirewall show domainprofile
netsh advfirewall show privateprofile
netsh advfirewall show publicprofile

:: ========== 配置文件设置 ==========
:: 设置域配置文件
netsh advfirewall set domainprofile state on
netsh advfirewall set domainprofile firewallpolicy blockinbound,blockoutbound
netsh advfirewall set domainprofile logging filename %systemroot%\system32\LogFiles\Firewall\domain.log
netsh advfirewall set domainprofile logging maxfilesize 16384

:: 设置专用配置文件
netsh advfirewall set privateprofile state on
netsh advfirewall set privateprofile firewallpolicy allowinbound,blockoutbound

:: 设置公用配置文件(最严格)
netsh advfirewall set publicprofile state on
netsh advfirewall set publicprofile firewallpolicy blockinbound,blockoutbound
netsh advfirewall set publicprofile settings inboundusernotification enable

四、高级安全配置

4.1 连接安全规则

连接安全规则用于配置 IPsec 策略,要求通信双方进行身份验证和加密。

4.1.1 创建连接安全规则

:: 允许经过身份验证的连接
netsh advfirewall consec add rule name="Authenticated Traffic" endpoint1=localsubnet endpoint2=any auth1=computerkerb auth1crypto=sha256 action=requireinboundandoutbound

:: 要求加密的连接
netsh advfirewall consec add rule name="Encrypted Traffic" endpoint1=192.168.1.0/24 endpoint2=10.0.0.0/24 auth1=computerntlm auth1crypto=aes128 action=requireinboundandoutbound

4.1.2 连接安全规则类型

| | | | — | — | | 规则类型 | 说明 | | 隔离 | 要求域成员之间的所有连接都经过身份验证 | | 身份验证免除 | 允许特定服务器无需身份验证即可连接 | | 服务器到服务器 | 在特定服务器之间要求身份验证 | | 隧道 | 通过 IPsec 隧道转发流量 |

4.2 组策略集成

4.2.1 通过组策略管理防火墙

计算机配置
└── 策略
    └── Windows 设置
        └── 安全设置
            └── Windows 防火墙 with Advanced Security
                ├── Windows 防火墙 with Advanced Security - 域配置文件
                ├── Windows 防火墙 with Advanced Security - 专用配置文件
                └── Windows 防火墙 with Advanced Security - 公用配置文件

4.2.2 组策略设置示例

  • 阻止所有入站连接:启用”阻止所有入站连接”
  • 通知用户:配置”显示通知”
  • 日志设置:配置日志文件路径和大小
  • 规则部署:批量部署防火墙规则到域内计算机

4.3 PowerShell 防火墙管理

# 查看防火墙状态
Get-NetFirewallProfile | Select-Object Name, Enabled

# 启用防火墙
Set-NetFirewallProfile -Profile Domain,Private,Public -Enabled True

# 查看规则
Get-NetFirewallRule | Where-Object {$_.Enabled -eq 'True'}

# 创建新规则
New-NetFirewallRule -DisplayName "PowerShell HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow

# 删除规则
Remove-NetFirewallRule -DisplayName "PowerShell HTTP"

# 导出规则
Get-NetFirewallRule | Export-Clixml -Path "C:\firewall_rules.xml"

# 导入规则
Import-Clixml -Path "C:\firewall_rules.xml" | ForEach-Object {New-NetFirewallRule @_}

五、规则管理详解

5.1 规则优先级

Windows 防火墙规则按以下优先级处理:

  1. 显式拒绝规则(Block rules with specific criteria)
  2. 显式允许规则(Allow rules with specific criteria)
  3. 默认策略(Default policy for the profile)

注意:更具体的规则优先于更通用的规则

5.2 规则属性详解

| | | | | — | — | — | | 属性 | 说明 | 示例值 | | Name | 规则名称 | “Web Server” | | Description | 规则描述 | “Allow HTTP traffic” | | Direction | 流量方向 | Inbound/Outbound | | Action | 规则操作 | Allow/Block | | Protocol | 协议类型 | TCP/UDP/ICMPv4/Any | | LocalPort | 本地端口 | 80, 443, RPC | | RemotePort | 远程端口 | Any, 53 | | LocalIP | 本地 IP | Any, 192.168.1.100 | | RemoteIP | 远程 IP | Any, 10.0.0.0/24 | | Program | 程序路径 | C:\app.exe, System | | Service | 服务名称 | Spooler, Dnscache | | Profile | 网络配置文件 | Domain/Private/Public | | InterfaceType | 接口类型 | Wired/Wireless/RemoteAccess | | Security | 安全要求 | Authenticate/Encrypt |

5.3 常用服务端口参考

| | | | | | — | — | — | — | | 服务 | 端口 | 协议 | 规则示例 | | HTTP | 80 | TCP | netsh … localport=80 | | HTTPS | 443 | TCP | netsh … localport=443 | | RDP | 3389 | TCP | netsh … localport=3389 | | SSH | 22 | TCP | netsh … localport=22 | | FTP | 21 | TCP | netsh … localport=21 | | DNS | 53 | TCP/UDP | netsh … localport=53 | | SMB | 445 | TCP | netsh … localport=445 | | SQL Server | 1433 | TCP | netsh … localport=1433 | | MySQL | 3306 | TCP | netsh … localport=3306 |

5.4 预定义规则管理

:: 查看预定义规则
netsh advfirewall firewall show rule name=all | findstr "Core Networking"

:: 启用预定义规则
netsh advfirewall firewall set rule group="Remote Desktop"newenable=Yes

:: 禁用预定义规则
netsh advfirewall firewall set rule group="File and Printer Sharing"newenable=No

:: 常见预定义规则组
:: - Remote Desktop
:: - Fileand Printer Sharing
:: - Core Networking
:: - Network Discovery
:: - Windows Media Player Network Sharing Service
:: - BranchCache

六、网络配置文件

6.1 配置文件类型

6.1.1 域配置文件(Domain Profile)

  • 适用场景:计算机连接到 Active Directory 域

  • 安全级别:中等(信任域内其他计算机)

  • 典型设置:

  • 入站:允许域内必要的管理流量

  • 出站:通常允许所有流量

6.1.2 专用配置文件(Private Profile)

  • 适用场景:家庭网络、工作网络(非域环境)

  • 安全级别:中等偏高

  • 典型设置:

  • 入站:允许局域网内的文件共享、打印机共享

  • 出站:通常允许所有流量

6.1.3 公用配置文件(Public Profile)

  • 适用场景:咖啡厅、机场、酒店等公共场所

  • 安全级别:最高(最严格)

  • 典型设置:

  • 入站:几乎全部阻止

  • 出站:通常允许所有流量

  • 启用用户通知

6.2 配置文件自动切换

Windows 根据网络环境自动切换配置文件:

网络检测流程:
1. 检测是否能联系域控制器 → 域配置文件
2. 检测网络位置类型(用户设置)→ 专用/公用配置文件
3. 默认使用公用配置文件(最安全)

6.3 手动设置网络位置

:: Windows 10/11 设置网络位置
:: 设置 → 网络和 Internet → 以太网/WiFi → 网络配置文件类型

:: 通过 PowerShell 设置
Set-NetConnectionProfile -InterfaceAlias "以太网" -NetworkCategory Private
Set-NetConnectionProfile -InterfaceAlias "WiFi" -NetworkCategory Public

七、日志与监控

7.1 启用日志记录

:: 启用域配置文件日志
netsh advfirewall set domainprofile logging filename %systemroot%\system32\LogFiles\Firewall\domain.log
netsh advfirewall set domainprofile logging maxfilesize 32768
netsh advfirewall set domainprofile logging droppedconnections enable

:: 启用专用配置文件日志
netsh advfirewall set privateprofile logging filename %systemroot%\system32\LogFiles\Firewall\private.log
netsh advfirewall set privateprofile logging droppedconnections enable

:: 启用公用配置文件日志
netsh advfirewall set publicprofile logging filename %systemroot%\system32\LogFiles\Firewall\public.log
netsh advfirewall set publicprofile logging droppedconnections enable

7.2 日志格式解析

防火墙日志采用标准格式,包含以下字段:

Field # | 字段名 | 说明
--------|--------|------
1| Date | 日期 (YYYY-MM-DD)
2| Time | 时间 (HH:MM:SS)
3| Action | 操作 (DROP/ALLOW)
4| Protocol | 协议 (TCP/UDP/ICMP)
5| Source IP | 源 IP 地址
6| Destination IP | 目标 IP 地址
7| Source Port | 源端口
8| Destination Port | 目标端口
9| Size | 数据包大小 (字节)
10| TCP Flags | TCP 标志位
11| TCP Seq/Ack | TCP 序列号/确认号
12| TCP Window | TCP 窗口大小
13| ICMP Type | ICMP 类型
14| ICMP Code | ICMP 代码
15| Info | 附加信息
16| Path | 路径 (SEND/RECEIVE/FORWARD)

7.3 日志分析示例

# 允许的连接
2024-01-15 10:30:45ALLOWTCP 192.168.1.100 192.168.1.1 52345 80 0 ------SEND

# 阻止的连接
2024-01-15 10:31:22DROPTCP 203.0.113.50 192.168.1.100 12345 3389 0 ------RECEIVE

7.4 实时监控

:: 使用 netsh 监控
netsh advfirewall monitor show securityassociations
netsh advfirewall monitor show currentsecurityrules

:: 使用 PowerShell 监控
Get-NetEventSession
New-NetEventSession -Name FirewallMonitor
Add-NetEventPacketCaptureProvider -SessionName FirewallMonitor
Start-NetEventSession -Name FirewallMonitor
# ... 执行网络操作 ...
Stop-NetEventSession -Name FirewallMonitor
Get-NetEventPacketCapture -SessionName FirewallMonitor

八、故障排除

8.1 常见问题诊断

8.1.1 无法访问服务

症状:可以 ping 通但无法访问 Web 服务

诊断步骤:

:: 1. 检查服务是否运行
netstat -ano | findstr ":80"

:: 2. 检查防火墙规则
netsh advfirewall firewall show rule name=all | findstr "80"

:: 3. 临时禁用防火墙测试
netsh advfirewall set allprofiles state off
:: 测试连接...
netsh advfirewall set allprofiles state on

:: 4. 检查网络配置文件
netsh advfirewall show currentprofile

8.1.2 规则不生效

可能原因:

  • 规则配置文件不匹配当前网络类型
  • 存在更高优先级的阻止规则
  • 规则被组策略覆盖
  • 服务未正确绑定到端口

解决方案:

:: 检查当前激活的配置文件
powershell "Get-NetConnectionProfile"

:: 检查是否有冲突规则
netsh advfirewall firewall show rule name=all dir=in | findstr "3389"

:: 强制刷新组策略
gpupdate /force

8.2 防火墙重置

:: 完全重置防火墙到默认状态
netsh advfirewall reset

:: 重置特定配置文件
netsh advfirewall set domainprofile firewallpolicy blockinbound,allowoutbound
netsh advfirewall set privateprofile firewallpolicy blockinbound,allowoutbound
netsh advfirewall set publicprofile firewallpolicy blockinbound,allowoutbound

:: 清除所有自定义规则
netsh advfirewall firewall delete rule name=all

8.3 性能影响排查

防火墙对性能的影响:

  • 规则数量过多会影响处理速度
  • 复杂的作用域条件会增加 CPU 使用率
  • 日志记录会占用磁盘 I/O

优化建议:

  • 定期清理不用的规则
  • 使用更具体的规则而不是通用规则
  • 限制日志文件大小
  • 避免在规则中使用复杂的 IP 范围

九、安全最佳实践

9.1 最小权限原则

  • 只开放必要的端口和服务
  • 限制源 IP 地址范围
  • 使用专用网络配置文件进行内部通信
  • 公用网络配置文件保持最严格设置

9.2 分层安全策略

安全层次:
1. 网络边界防火墙(路由器/硬件防火墙)
2. 主机防火墙(Windows Defender Firewall)
3. 应用程序级别安全(应用自身安全机制)
4. 端点保护(防病毒软件、EDR)

9.3 规则管理最佳实践

9.3.1 命名规范

命名格式:[服务]-[方向]-[端口]-[描述]
示例:
- Web-In-80-HTTP
- DB-Out-1433-SQLServer
- RDP-In-3389-RemoteDesktop
- Custom-In-8080-AppServer

9.3.2 文档化

  • 为每个规则添加详细描述
  • 记录规则的业务需求和负责人
  • 定期审查和清理过期规则
  • 维护规则变更日志

9.3.3 测试流程

规则部署流程:
1. 在测试环境中验证规则
2. 在非生产时间部署到生产环境
3. 监控日志确认规则正常工作
4. 收集用户反馈并调整
5. 文档化最终配置

9.4 审计与合规

:: 导出当前防火墙配置用于审计
netsh advfirewall export "C:\audit\firewall_config.wfw"

:: 生成规则报告
netsh advfirewall firewall show rule name=all > "C:\audit\rules_report.txt"

:: 检查合规性(示例:检查是否允许 RDP)
netsh advfirewall firewall show rule name="Remote Desktop" | findstr "Enable.*Yes"
if errorlevel 1 (
    echo 合规:RDP 未启用
) else (
    echo 警告:RDP 已启用,需要额外安全措施
)

十、实用脚本模板

10.1 基础安全加固脚本

@echo off
title Windows 防火墙安全加固
color 0c

echo ====================================
echo   Windows 防火墙安全加固脚本
echo ====================================

:: 检查管理员权限
net session >nul 2>&1
if %errorlevel% neq 0 (
echo 错误:需要管理员权限!
    pause
exit /b1
)

echo.
echo [1/6] 启用所有配置文件的防火墙...
netsh advfirewall set allprofiles state on

echo [2/6] 设置默认阻止策略...
netsh advfirewall set domainprofile firewallpolicy blockinbound,blockoutbound
netsh advfirewall set privateprofile firewallpolicy blockinbound,blockoutbound
netsh advfirewall set publicprofile firewallpolicy blockinbound,blockoutbound

echo [3/6] 允许必要服务...
netsh advfirewall firewall add rule name="Essential-DNS-Out" dir=out action=allow protocol=UDP localport=53
netsh advfirewall firewall add rule name="Essential-DHCP-Out" dir=out action=allow protocol=UDP localport=67,68
netsh advfirewall firewall add rule name="Essential-ICMP" dir=in action=allow protocol=icmpv4:8,any

echo [4/6] 启用日志记录...
netsh advfirewall set allprofiles logging droppedconnections enable
netsh advfirewall set allprofiles logging allowedconnections disable

echo [5/6] 禁用危险的预定义规则...
netsh advfirewall firewall set rule group="File and Printer Sharing"new enable=No
netsh advfirewall firewall set rule group="Network Discovery"new enable=No

echo [6/6] 显示当前配置...
netsh advfirewall show allprofiles

echo.
echo ====================================
echo   防火墙加固完成!
echo   注意:请根据实际需求调整规则
echo ====================================
pause

10.2 Web 服务器防火墙配置

@echo off
title Web 服务器防火墙配置
setlocal enabledelayedexpansion

:: 配置参数
set WEB_PORTS=80,443,8080
set ADMIN_IP=192.168.1.100
set DB_SERVER=10.0.0.50

echo 配置 Web 服务器防火墙规则...

:: 允许 HTTP/HTTPS 流量(来自任何地方)
netsh advfirewall firewall add rule name="Web-HTTP-In" dir=inaction=allow protocol=TCP localport=%WEB_PORTS% profile=private,domain

:: 仅允许管理员 IP 访问 RDP
netsh advfirewall firewall add rule name="Admin-RDP-In" dir=inaction=allow protocol=TCP localport=3389 remoteip=%ADMIN_IP% profile=private,domain

:: 允许访问数据库服务器
netsh advfirewall firewall add rule name="DB-Out" dir=outaction=allow protocol=TCP remoteport=1433 remoteip=%DB_SERVER% profile=private,domain

:: 允许 DNS 查询
netsh advfirewall firewall add rule name="DNS-Out" dir=outaction=allow protocol=UDP localport=53

:: 允许 NTP 时间同步
netsh advfirewall firewall add rule name="NTP-Out" dir=outaction=allow protocol=UDP remoteport=123

:: 阻止所有其他入站连接
netsh advfirewall firewall add rule name="Block-All-In" dir=inaction=block

echo Web 服务器防火墙配置完成!
pause

10.3 防火墙规则备份与恢复

@echo off
title 防火墙规则备份与恢复工具
color 0a

set BACKUP_DIR=C:\FirewallBackup
set TIMESTAMP=%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%
set TIMESTAMP=%TIMESTAMP: =0%
set BACKUP_FILE=%BACKUP_DIR%\firewall_%TIMESTAMP%.wfw

if not exist %BACKUP_DIR% mkdir %BACKUP_DIR%

echo ====================================
echo   防火墙规则备份与恢复工具
echo ====================================
echo1. 备份防火墙规则
echo2. 恢复防火墙规则
echo3. 查看备份文件
echo4. 退出
echo ====================================
set /p choice=请选择操作 (1-4):

if"%choice%"=="1"goto backup
if"%choice%"=="2"goto restore
if"%choice%"=="3"gotolist
if"%choice%"=="4"exit

:backup
echo 备份防火墙规则到:%BACKUP_FILE%
netsh advfirewall export "%BACKUP_FILE%"
if %errorlevel% equ 0 (
echo 备份成功!
) else (
echo 备份失败!
)
pause
goto :eof

:restore
echo 可用的备份文件:
dir /b"%BACKUP_DIR%\*.wfw"
echo.
set /p restore_file=请输入要恢复的文件名(完整路径):
if exist "%restore_file%" (
echo 恢复防火墙规则:%restore_file%
    netsh advfirewall import "%restore_file%"
if %errorlevel% equ 0 (
echo 恢复成功!
    ) else (
echo 恢复失败!
    )
) else (
echo 文件不存在!
)
pause
goto :eof

:list
echo 备份文件列表:
dir "%BACKUP_DIR%\*.wfw" /tc
pause
goto :eof

10.4 端口扫描检测脚本

@echo off
title 端口扫描检测脚本
color 0c

:: 启用详细的日志记录
netsh advfirewall set allprofiles logging droppedconnections enable
netsh advfirewall set allprofiles logging allowedconnections enable

echo 监控防火墙日志中的可疑活动...
echo 按 Ctrl+C 停止监控

:monitor
:: 分析最近的日志条目
powershell -Command "
$logfile = '%systemroot%\system32\LogFiles\Firewall\pfirewall.log'
if (Test-Path $logfile) {
    $recent = Get-Content $logfile | Select-Object -Last 50
    $suspicious = $recent | Where-Object { $_ -match 'DROP' } | Group-Object { ($_ -split ' ')[4] } | Where-Object { $_.Count -gt 5 }
    if ($suspicious) {
        Write-Host '检测到可疑的端口扫描活动:' -ForegroundColor Red
        $suspicious | ForEach-Object { Write-Host '源IP:' $_.Name '尝试次数:' $_.Count }
    }
}
"
timeout /t 30 /nobreak >nul
goto monitor

附录:常用命令速查表

防火墙状态管理

| | | | — | — | | 命令 | 说明 | | netsh advfirewall show allprofiles | 查看所有配置文件状态 | | netsh advfirewall set allprofiles state on | 启用防火墙 | | netsh advfirewall set allprofiles state off | 禁用防火墙 | | netsh advfirewall reset | 重置到默认设置 |

规则管理

| | | | — | — | | 命令 | 说明 | | netsh advfirewall firewall add rule | 添加新规则 | | netsh advfirewall firewall delete rule | 删除规则 | | netsh advfirewall firewall set rule | 修改规则 | | netsh advfirewall firewall show rule | 查看规则 |

常用规则示例

| | | | — | — | | 用途 | 命令 | | 允许 HTTP | add rule name=”HTTP” dir=in action=allow protocol=TCP localport=80 | | 允许 HTTPS | add rule name=”HTTPS” dir=in action=allow protocol=TCP localport=443 | | 允许 RDP | add rule name=”RDP” dir=in action=allow protocol=TCP localport=3389 | | 允许 Ping | add rule name=”ICMP” dir=in action=allow protocol=icmpv4:8,any | | 限制 IP 访问 | add rule name=”Restricted” dir=in action=allow protocol=TCP localport=3389 remoteip=192.168.1.100 |


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:运维星火燎原 刘军军 刘军军《Windows 防火墙配置详解》

    评论:0   参与:  0