netsh advfirewall export <file.wfw> 导出完整防火墙策略到`.wfw`格式文件;netsh advfirewall import <file.wfw>导入`.wfw`备份文件,恢复整套防火墙策略,.wfw 是 Windows 防火墙配置文件的扩展名, "Windows Firewall" 的配置文件。包含了防火墙的设置和规则,还原或复制防火墙设置。

PS C:\WINDOWS\system32>  netsh advfirewall help

下列指令有效:

此上下文中的命令:
`netsh advfirewall ?`显示当前 advfirewall 上下文帮助,列出全部一级子命令,等价于 help 命令Microsoft ...。

`netsh advfirewall consec`                进入**连接安全规则(IPsec)子上下文**;管理计算机间 IPsec 连接安全规则;支持 add/delete/set/show/dump,用于 IPsec 加密、身份验证策略配置Microsoft ...。

`netsh advfirewall dump`                    输出当前高级防火墙完整配置脚本文本,可重定向保存为 txt,用于查看完整策略配置;**注意:advfirewall 顶层 dump 不输出规则,要在 consec/firewall 子上下文内执行 dump**Microsoft ...。

`netsh advfirewall export <file.wfw>`        导出完整防火墙策略到`.wfw`格式文件;二进制格式,供 import 恢复,用于备份整套防火墙配置(域 / 专用 / 公用配置文件、所有规则)Microsoft ...。

`netsh advfirewall firewall`            进入**防火墙规则子上下文**;管理入站 / 出站防火墙规则;add/delete/set/show;日常放行端口、放行程序、阻断流量都在该子上下文操作,最常用Microsoft ...。

`netsh advfirewall help`            上下文帮助,与`?`功能完全相同,打印一级命令列表说明Microsoft ...。

`netsh advfirewall import <file.wfw>`        导入`.wfw`备份文件,恢复整套防火墙策略,与 export 配对使用Microsoft ...。

`netsh advfirewall mainmode`        进入**IPsec 主模式子上下文**;管理 IPsec IKE 主模式协商规则,IKE 密钥交换算法、身份验证方法配置;add/delete/set/show/dumpMicrosoft ...。

`netsh advfirewall monitor`        进入**监控子上下文**;查看实时 IPsec SA 安全关联、IKE 主模式会话、防火墙运行统计;排查 IPsec 连接问题;可查看 mmsa、qmsa 活动安全联盟Microsoft ...。

`netsh advfirewall reset`        **重置高级安全防火墙为系统出厂默认值**;可加 export 参数先备份再重置;等价 MMC 控制台 “还原默认设置”,会清除全部自定义规则,谨慎执行Microsoft ...。

`netsh advfirewall set`                设置防火墙全局、各配置文件(domainprofile/privateprofile/publicprofile/currentprofile)参数;包含:防火墙开关、默认入站 / 出站行为、日志配置、存储位置、组策略存储等Microsoft ...。

`netsh advfirewall show`                查看防火墙配置;可查看 allprofiles /currentprofile/domain /private/public 配置文件状态、防火墙策略、日志设置、全局 IPsec 参数等Microsoft ...        

下列的子上下文可用:
 consec firewall mainmode monitor

若需要命令的更多帮助信息,请键入命令,接着是空格,后面跟 ?。
PS C:\WINDOWS\system32> netsh advfirewall monitor

下列指令有效:

此上下文中的命令:
`netsh advfirewall monitor ?`            显示 `advfirewall monitor` 监控上下文帮助,列出该层级全部可用子命令,等价 help。

`netsh advfirewall monitor delete`        删除 IPsec 安全关联(SA);可删除主模式 MMSA、快速模式 QMSA 会话,用于强制断开已建立的 IPsec 隧道。

`netsh advfirewall monitor dump`        输出监控相关 IPsec 会话、全局 IPsec 参数的可读文本,可重定向保存用于审计排错;仅文本查看,不可用于 import 恢复策略。

`netsh advfirewall monitor help`        显示 monitor 上下文帮助,功能与 `?` 完全一致。

`netsh advfirewall monitor show`        查询展示 IPsec 监控信息;支持 show consec(IPsec 全局配置)、show mmsa(主模式 SA)、show qmsa(快速模式 SA),用于 IPsec 隧道排错,查看活动加密会话。        

若需要命令的更多帮助信息,请键入命令,接着是空格,
后面跟 ?。

PS C:\WINDOWS\system32> netsh advfirewall monitor show

下列指令有效:

此上下文中的命令:
`netsh advfirewall monitor show consec`            查看 IPsec 全局连接安全参数、IKE 主模式 / 快速模式加密套件配置,同时列出当前活动 IPsec 安全关联 SA。

`netsh advfirewall monitor show currentprofile`    查看本机当前网卡生效的网络配置文件(域 / 专用 / 公用)、对应防火墙实时状态。

`netsh advfirewall monitor show firewall`        查看防火墙实时运行状态,包含全局防火墙开关、默认入站 / 出站策略、日志相关运行参数。

`netsh advfirewall monitor show mainmode`        查看 IPsec IKE 主模式全局协商配置,包含密钥生存期、主模式加密算法套件。

`netsh advfirewall monitor show mmsa`            查看**IKE 主模式安全关联 MMSA**,展示已建立的 IKE 第一阶段会话,排查 IPsec 第一阶段协商问题。

`netsh advfirewall monitor show qmsa`            查看**IKE 快速模式安全关联 QMSA**,展示 IPsec 第二阶段数据加密隧道会话,排查业务流量加密隧道问题。     

PS C:\WINDOWS\system32>  netsh advfirewall monitor show mainmode help

用法:   netsh advfirewall monitor show mainmode  显示主模式
       [rule name=<string> [profile=public|private|domain|active|any[,...]] ][verbose]


备注:

      - 显示所有可用网络配置文件的主模式安全配置
      - [profile=] 命令使管理员能够将输出筛选到系统上的特定配置文件或仅返回活动或非活动配置文件的结果
      - [rule] 命令允许管理员将规则输出范围限制于某些规则名称和状态以限制输出的范围
      - Verbose 命令添加对显示详细安全和高级规则“源名称”信息的支持

示例:

      显示公用配置文件的当前主模式信息:
      netsh advfirewall monitor show mainmode rule name=all profile=public

PS C:\WINDOWS\system32>  netsh advfirewall monitor show mmsa help

用法: netsh advfirewall monitor show mmsa [(source  destination)|all]

备注:

      - 该命令显示安全关联或(source destination)对筛选的安全关联。
      - 源和目标都是一个 IPv4 或 IPv6地址。

示例:

      显示所有主模式 SA:
      netsh advfirewall monitor show mmsa

      显示两个地址之间的主模式 SA:
      netsh advfirewall monitor show mmsa 192.168.0.3 192.168.0.4

PS C:\WINDOWS\system32>  netsh advfirewall monitor show qmsa help

用法: netsh advfirewall monitor show qmsa [(source  destination)|all]

备注:

      - 该命令显示安全关联或(source destination)对筛选的安全关联。
      - 源和目标都是一个 IPv4 或 IPv6地址。

示例:

      显示所有快速模式 SA:
      netsh advfirewall monitor show qmsa

      显示两个地址之间的快速模式 SA:
      netsh advfirewall monitor show qmsa 192.168.0.3 192.168.0.4
PS C:\WINDOWS\system32>  netsh advfirewall consec add rule help

用法: netsh advfirewall consec add rule name=<string>
      endpoint1=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>
      endpoint2=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>
      action=requireinrequestout|requestinrequestout|
         requireinrequireout|requireinclearout|noauthentication
      [description=<string>]
      [mode=transport|tunnel (default=transport)]
      [enable=yes|no (default=yes)]
      [profile=public|private|domain|any[,...] (default=any)]
      [type=dynamic|static (default=static)]
      [localtunnelendpoint=any|<IPv4 address>|<IPv6 address>]
      [remotetunnelendpoint=any|<IPv4 address>|<IPv6 address>]
      [port1=0-65535|<port range>[,...]|any (default=any)]
      [port2=0-65535|<port range>[,...]|any (default=any)]
      [protocol=0-255|tcp|udp|icmpv4|icmpv6|any (default=any)]
      [interfacetype=wiresless|lan|ras|any (default=any)]
      [auth1=computerkerb|computercert|computercertecdsap256|
         computercertecdsap384|computerpsk|computerntlm|anonymous[,...]]
      [auth1psk=<string>]
      [auth1kerbproxyfqdn=<fully-qualified dns name>]
      [auth1ca="<CA Name> [certmapping:yes|no] [excludecaname:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         |..."]
      [auth1healthcert=yes|no (default=no)]
      [auth1ecdsap256ca="<CA Name> [certmapping:yes|no]
         [excludecaname:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth1ecdsap256healthcert=yes|no (default=no)]
      [auth1ecdsap384ca="<CA Name> [certmapping:yes|no]
         [excludecaname:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth1ecdsap384healthcert=yes|no (default=no)]
      [auth2=computercert|computercertecdsap256|computercertecdsap384|
         userkerb|usercert|usercertecdsap256|usercertecdsap384|userntlm|
         anonymous[,...]]
      [auth2kerbproxyfqdn=<fully-qualified dns name>]
      [auth2ca="<CA Name> [certmapping:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth2ecdsap256ca="<CA Name> [certmapping:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth2ecdsap384ca="<CA Name> [certmapping:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [qmpfs=dhgroup1|dhgroup2|dhgroup14|dhgroup24|ecdhp256|ecdhp384|
         mainmode|none (default=none)]
      [qmsecmethods=authnoencap:<integrity>+[valuemin]+[valuekb]|
         ah:<integrity>+esp:<integrity>-<encryption>+[valuemin]+[valuekb]
         |default]
      [exemptipsecprotectedconnections=yes|no (default=no)]
      [applyauthz=yes|no (default=no)]

备注:

      - 规则名称应该唯一且不能为 "all"。
      - 当 mode=tunnel 时,必须指定隧道终结点,操作为 noauthentication 时除外。当输入特定 IP 地址时,这些地址必须是相同的 IP 版本。 此外,当配置动态隧道时:可以将隧道终结点设置为 any。不需要为 客户端策略指定本地隧道终结点(即 any)。 不需要为网关策略指定远程隧道终结点(即 any)。 此外,操作必须为 requireinrequireout、requireinclearout 或 noauthentication。
      - 当 mode=Transport 时 requireinclearout 无效。
      - 必须至少指定一个身份验证。
      - Auth1 和 auth2 可以是用逗号分隔的选项列表。
      - 不可以为 auth1 同时指定 Computerpsk 和 computerntlm 方法。
      - 不可以将 Computercert 与 auth2 的用户凭据一起指定。
      - 仅在 Windows Vista SP1 及更高版本上 支持 Certsigning 选项 ecdsap256 和 ecdsap384。
      - Qmsecmethods 可以是由 "," 分隔的建议列表。
      - 对于 qmsecmethods,integrity=md5|sha1|sha256|aesgmac128|aesgmac192|aesgmac256|aesgcm128|aesgcm192|aesgcm256 和 encryption=3des|des|aes128|aes192|aes256|aesgcm128|aesgcm192|aesgcm256
      - 如果指定 aesgcm128、aesgcm192 或 aesgcm256,则它必须用于 ESP 完整性和加密。
      - 仅在 Windows Vista SP1 及更高版本上支持 Aesgmac128、aesgmac192、aesgmac256、aesgcm128、aesgcm192、aesgcm256、sha256。
      - Qmpfs=mainmode 使用 PFS 的主模式密钥交换设置。
      - 不建议使用 DES、MD5 和 DHGroup1。提供这些加密算法仅用于向下兼容。
      - certmapping 和 excludecaname 的默认值为 "no"。
      - 必须将 CA 名称中的 " 字符替换为 \'
      - 对于 auth1ca 和 auth2ca,CA 名称必须以 "CN=" 为前缀。
      - 可以使用 catype 来指定证书颁发机构类型 - catype=root/intermediate
      - 在 Windows 7 及更高版本上支持 authnoencap。
      - authnoencap 表示计算机将仅使用身份验证,将不使用任何每个数据包封装或加密算法来保护随后作为此连接的一部分交换的网络数据包。
      - 不能在同一规则上同时使用 QMPFS 和 authnoencap。
      - AuthNoEncap 必须至少和一个 AH 或 ESP 完整性套件一起使用。
      - 只能为隧道模式规则指定 applyauthz。
      - 只能为隧道模式规则指定 exemptipsecprotectedconnections。通过将此标志设置为“是”,将从隧道中免除 ESP 流量。仅 AH 流量将不会从隧道中免除。
      - qmsecmethod 的 Valuemin(指定时)应介于 5-2880 分钟之间。qmsecmethod 的 Valuekb(指定时)应介于 20480-2147483647 KB 之间。
      - Certhash 指定证书的指纹或证书的哈希。
      - Followrenewal 指定是否自动跟随证书中的可续订链接。仅适用于证书部分(需要 certhash)。
      - Certeku 指定要在证书中匹配的用逗号分隔的 EKU OID 列表。
      - Certname 指定要匹配证书名称的字符串(需要 certnametype)。
      - Certnametype 指定要匹配的 certname 的证书字段(需要 certname)。
      - Certcriteriatype 指定当选择本地证书、验证对等证书或两者都执行 时是否对证书采取操作。
      - 在 computercert 身份验证映射内,可以通过用 "|" 字符分隔每个条目来引用多个证书。

示例:

      使用默认值为域隔离添加规则:
      netsh advfirewall consec add rule name="isolation" endpoint1=any endpoint2=any action=requireinrequestout

      使用自定义快速模式建议添加规则:
      netsh advfirewall consec add rule name="custom" endpoint1=any endpoint2=any  qmsecmethods=ah:sha1+esp:sha1-aes256+60min+20480kb,ah:sha1  action=requireinrequestout

      使用自定义快速模式建议添加规则:
      netsh advfirewall consec add rule name="custom" endpoint1=any endpoint2=any  qmsecmethods=authnoencap:sha1,ah:aesgmac256+esp:aesgmac256-none  action=requireinrequestout

      创建从子网 A (192.168.0.0, external ip=1.1.1.1)到 子网 B (192.157.0.0, external ip=2.2.2.2)的隧道模式规则:
      
      netsh advfirewall consec add rule name="my tunnel" mode=tunnel  endpoint1=192.168.0.0/16 endpoint2=192.157.0.0/16 remotetunnelendpoint=2.2.2.2 localtunnelendpoint=1.1.1.1 action=requireinrequireout

      创建从子网 A (192.168.0.0/16)到子网 B (192.157.0.0, remoteGW=2.2.2.2)的动态隧道模式规则
      
      客户端策略:
      netsh advfirewall consec add rule name="dynamic tunnel" mode=tunnel endpoint1=any endpoint2=192.157.0.0/16  remotetunnelendpoint=2.2.2.2 action=requireinrequireout
      
      网关策略(仅适用于网关设备):
      netsh advfirewall consec add rule name="dynamic tunnel" mode=tunnel endpoint1=192.157.0.0/16 endpoint2=any localtunnelendpoint=2.2.2.2 action=requireinrequireout

      使用 CA 名称添加规则:
      netsh advfirewall consec add rule name="cert rule" endpoint1=any endpoint2=any action=requireinrequestout auth1=computercert auth1ca="C=US, O=MSFT, CN=\'Microsoft North, South, East, and West Root Authority\'"

      使用各种证书条件以及多种身份验证方法 添加规则:
      netsh advfirewall consec add rule name="cert rule" endpoint1=any endpoint2=any action=requireinrequireout auth1=computercert auth1ca="CN=\'CN1\' certcriteriatype:Selection certname:MyGroup  certnametype:SubjectOU certeku:1.2.3.4.5|CN=\'CN2\'  certcriteriatype:Validation certeku:2.3.4.5.6,9.10.11.12|CN=\'CN3\' certhash:0123456789abcdef01234567890ABCDEF0123456"
PS C:\WINDOWS\system32>  netsh advfirewall consec help

下列指令有效:

此上下文中的命令:
`netsh advfirewall consec ?`                显示 consec 上下文帮助信息,列出全部可用子命令,等价 help。

`netsh advfirewall consec add rule`        新建 [IPsec 连接安全规则];设置端点、动作(请求 / 强制认证)、传输 / 隧道模式、加密算法、配置文件等参数。

`netsh advfirewall consec delete rule`        删除匹配的 IPsec 连接安全规则,按规则名称进行删除。

`netsh advfirewall consec dump`                输出本机全部 IPsec 连接安全规则的可读脚本文本,可重定向保存为 txt;仅文本查看,不能用于 import 恢复完整策略,完整备份使用上层 netsh advfirewall export 生成 wfw 文件。

`netsh advfirewall consec help`                显示 consec 上下文帮助,与`?`功能完全一致。

`netsh advfirewall consec set rule`            修改[已存在]的 IPsec 连接安全规则;更新启用状态、动作、端点、加密、配置文件等属性。

`netsh advfirewall consec show rule`            查询展示指定 IPsec 连接安全规则,可输出规则完整参数。          

若需要命令的更多帮助信息,请键入命令,接着是空格,后面跟 ?。

PS C:\WINDOWS\system32>  netsh advfirewall consec dump help

用法: netsh advfirewall consec dump 

`netsh advfirewall consec dump`  输出[IPsec 连接安全规则(consec 上下文)]的可读配置脚本文本;仅导出本机所有连接安全规则,输出内容为可直接执行的 netsh 脚本,可重定向保存文本用于查阅、复现配置;⚠️仅文本输出,**不能用 import 恢复,完整备份请使用上层`netsh advfirewall export`生成 wfw 二进制文件**Microsoft ...。

说明:
    创建一个包含当前配置的脚本文件。如果保存到文件,此脚本可以用来还原更改的配置设置。
    
PS C:\WINDOWS\system32> netsh advfirewall consec delete rule help

用法:  netsh advfirewall consec delete rule  name=<string>
      [type=dynamic|static]
      [profile=public|private|domain|any[,...] (default=any)]
      [endpoint1=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
      [endpoint2=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
      [port1=0-65535|<port range>[,...]|any (default=any)]
      [port2=0-65535|<port range>[,...]|any (default=any)]
      [protocol=0-255|tcp|udp|icmpv4|icmpv6|any]

注释:

      - 删除按名称识别的规则,也可按配置文件、终结点、端口、协议和类型识别。
      - 如果找到多个匹配项,则删除所有匹配规则。

示例:

      从所有配置文件中删除名称为 "rule1" 的规则:
      netsh advfirewall consec delete rule name="rule1"

      从所有配置文件中删除所有动态规则:
      netsh advfirewall consec delete rule name=all type=dynamic

 

PS C:\WINDOWS\system32>  netsh advfirewall consec set rule help

用法:  netsh advfirewall consec set rule 
      group=<string> | name=<string>
      [type=dynamic|static]
      [profile=public|private|domain|any[,...] (default=any)]
      [endpoint1=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
      [endpoint2=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
      [port1=0-65535|<port range>[,...]|any]
      [port2=0-65535|<port range>[,...]|any]
      [protocol=0-255|tcp|udp|icmpv4|icmpv6|any]
      new
      [name=<string>]
      [profile=public|private|domain|any[,...]]
      [description=<string>]
      [mode=transport|tunnel]
      [endpoint1=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
      [endpoint2=any|localsubnet|dns|dhcp|wins|defaultgateway|
         <IPv4 address>|<IPv6 address>|<subnet>|<range>|<list>]
      [action=requireinrequestout|requestinrequestout|
         requireinrequireout|requireinclearout|noauthentication]
      [enable=yes|no]
      [type=dynamic|static]
      [localtunnelendpoint=any|<IPv4 address>|<IPv6 address>]
      [remotetunnelendpoint=any|<IPv4 address>|<IPv6 address>]
      [port1=0-65535|<port range>[,...]|any]
      [port2=0-65535|<port range>[,...]|any]
      [protocol=0-255|tcp|udp|icmpv4|icmpv6|any]
      [interfacetype=wiresless|lan|ras|any]
      [auth1=computerkerb|computercert|computercertecdsap256|
         computercertecdsap384|computerpsk|computerntlm|anonymous[,...]]
      [auth1psk=<string>]
      [auth1kerbproxyfqdn=<fully-qualified dns name>]
      [auth1ca="<CA Name> [certmapping:yes|no] [excludecaname:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth1healthcert=yes|no]
      [auth1ecdsap256ca="<CA Name> [certmapping:yes|no]
         [excludecaname:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth1ecdsap256healthcert=yes|no (default=no)]
      [auth1ecdsap384ca="<CA Name> [certmapping:yes|no]
         [excludecaname:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth1ecdsap384healthcert=yes|no (default=no)]
      [auth2=computercert|computercertecdsap256|computercertecdsap384|
         userkerb|usercert|usercertecdsap256|usercertecdsap384|userntlm|
         anonymous[,...]]
      [auth2kerbproxyfqdn=<fully-qualified dns name>]
      [auth2ca="<CA Name> [certmapping:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth2ecdsap256ca="<CA Name> [certmapping:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [auth2ecdsap384ca="<CA Name> [certmapping:yes|no]
         [catype:root|intermediate (default=root)]
         [certhash:<Hex hash string, with no spaces or leading 0x>]
         [followrenewal:yes|no (default=no)] [certeku:<EKU, EKU, ...>]
         [certname:<CertName>] [certnametype:<SubjectAltDNS|
         SubjectAltEmail|SubjectCN|SubjectOU|SubjectO|SubjectDC>]
         [certcriteriatype:<Selection|Validation|Both (default=both)>]
         | ..."]
      [qmpfs=dhgroup1|dhgroup2|dhgroup14|dhgroup24|ecdhp256|ecdhp384|
         mainmode|none]
      [qmsecmethods=authnoencap:<integrity>+[valuemin]+[valuekb]|
         ah:<integrity>+esp:<integrity>-<encryption>+[valuemin]+[valuekb]
         |default]
      [exemptipsecprotectedconnections=yes|no (default=no)]
      [applyauthz=yes|no (default=no)]

备注:

      - 为已标识的规则设置新的参数值。如果规则不存在, 则该命令失败。若要创建规则,请使用添加命令。
      - 会更新规则中 new 关键字后的值。如果没有值, 或缺少关键字 new,则不进行任何更改。
      - 只能启用或禁用一组规则。
      - 如果多个规则与条件匹配,将更新所有 匹配的规则。
      - 规则名称应该唯一且不能为 "all"。
      - Auth1 和 auth2 可以是用逗号分隔的选项列表。
      - 无法为 auth1 同时指定 Computerpsk 和 computerntlm 方法。
      - 无法将 Computercert 与 auth2 的用户凭据一起指定。
      - 仅在 Windows Vista SP1 及更高版本上支持 Certsigning 选项 ecdsap256 和 ecdsap384。
      - Qmsecmethods 可以是由 "," 分隔的建议列表。
      - 对于 qmsecmethods,integrity=md5|sha1|sha256|aesgmac128|aesgmac192| aesgmac256|aesgcm128|aesgcm192|aesgcm256 和 encryption=3des|des|aes128|aes192|aes256|aesgcm128|aesgcm192| aesgcm256。
      - 如果指定 aesgcm128、aesgcm192 或 aesgcm256,则它必须 用于 ESP 完整性和加密。
      - 仅 Windows Vista SP1 及更高版本上支持 Aesgmac128、aesgmac192、 aesgmac256、aesgcm128、aesgcm192、aesgcm256、sha256。
      - 如果 qmsemethods 设置为默认值,则 qmpfs 也将设置为默认值。
      - Qmpfs=mainmode 使用 PFS 的主模式密钥交换设置。
      - 不建议使用 DES、MD5 和 DHGroup1。提供 这些加密算法仅用于向下兼容。
      - 必须将 CA 名称中的 " 字符替换为 \'
      - 对于 auth1ca 和 auth2ca,CA 名称必须以 "CN=" 为前缀。
      - 可以使用 catype 来指定证书颁发机构类型 - catype=root/intermediate
      - 在 Windows 7 及更高版本上支持 authnoencap。
      - authnoencap 表示计算机将仅使用身份验证, 将不会使用任何每个数据包封装或加密算法 来保护随后作为此连接的一部分交换的网络数据包。
      - 无法在同一规则上同时使用 QMPFS 和 authnoencap。
      - AuthNoEncap 必须至少和一个 AH 或 ESP 完整性套件一起使用。
      - 当 mode=tunnel 时,操作必须是 requireinrequireout、 requireinclearout 或 noauthentication。
      - 当 mode=Transport 时,requireinclearout 无效。
      - 只能为隧道模式规则指定 applyauthz。
      - 只能为隧道模式规则指定 exemptipsecprotectedconnections。 通过将此标志设置为“是”,将从隧道中免除 ESP 流量。仅 AH 流量将不会从隧道中免除。
      - 当 mode=transport 时,只能指定 Port1、Port2 和 Protocol。
      - qmsecmethod 的 Valuemin(指定时)应介于 5-2880 分钟之间。qmsecmethod 的 Valuekb(指定时)应介于 20480-2147483647 KB 之间。
      - Certhash 指定证书的指纹或哈希。
      - Followrenewal 指定是否自动跟随证书中的可续订链接。仅适用于证书部分(需要 certhash)。
      - Certeku 指定要在证书中匹配的用逗号分隔的 EKU OID 列表。
      - Certname 指定要匹配证书名称的字符串(需要 certnametype)。
      - Certnametype 指定要匹配的 certname 的证书字段(需要 certname)。
      - Certcriteriatype 指定当选择本地证书、验证对等证书或两者都执行时是否对证书采取操作。

示例:

      将 rule1 重命名为 rule2:
      netsh advfirewall consec set rule name="rule1" new name="rule2"

      更改规则的操作:
      netsh advfirewall consec set rule name="rule1"  endpoint1=1.2.3.4 endpoint2=4.3.2.1 new action=requestinrequestout

      使用自定义快速模式建议添加规则:
      netsh advfirewall consec set rule name="Custom QM" new  endpoint1=any endpoint2=any  qmsecmethods=authnoencap:aesgmac256,ah:aesgmac256+esp:aesgmac256-none

 

PS C:\WINDOWS\system32> netsh advfirewall consec show rule help

用法: netsh advfirewall consec show rule  name=<string>
      [profile=public|private|domain|any[,...]]
      [type=dynamic|static (default=static)]
      [verbose]

注释:

      - 显示按名称识别的所有规则实例,也可按配置文件和类型识别。

示例:

      显示所有规则:
      netsh advfirewall consec show rule name=all

      显示所有动态规则:
      netsh advfirewall consec show rule name=all type=dynamic

 

 

PS C:\WINDOWS\system32> netsh advfirewall firewall help

下列指令有效:

此上下文中的命令:
`netsh advfirewall firewall ?`            显示 `advfirewall firewall` 子上下文帮助,列出该层级全部可用子命令,等价 help。

`netsh advfirewall firewall add`        **新增防火墙规则**;可创建入站 / 出站规则,支持按程序、端口、协议、远程 IP、接口、用户等条件配置允许 / 阻止动作。

`netsh advfirewall firewall delete`        **删除指定防火墙规则**;按规则名称匹配删除入站、出站规则,支持通配符批量删除。

`netsh advfirewall firewall dump`        输出本上下文所有防火墙规则可读文本,可重定向输出保存为文本,用于查阅规则配置,不能用于 import 导入恢复。

`netsh advfirewall firewall help`            显示上下文帮助,功能与 `?` 完全一致。

`netsh advfirewall firewall set`            **修改已有防火墙规则属性**;更新已存在规则的启用状态、动作、端口、程序、配置文件等参数。

`netsh advfirewall firewall show`            **查询、展示防火墙规则**;可以按名称、规则类型(入站 / 出站)、配置文件筛选,支持 verbose 显示完整详细规则参数。        

若需要命令的更多帮助信息,请键入命令,接着是空格,后面跟 ?。

 

PS C:\WINDOWS\system32> netsh advfirewall firewall add rule help

用法:  netsh advfirewall firewall 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)]

备注:

      - 将新的入站或出站规则添加到防火墙策略。
      - 规则名称应该是唯一的,且不能为 "all"。
      - 如果已指定远程计算机或用户组,则 security 必须为authenticate、authenc、authdynenc 或 authnoencap。
      - 为 authdynenc 设置安全性可允许系统动态协商为匹配给定 Windows Defender 防火墙规则的通信使用加密。根据现有连接安全规则属性协商加密。选择此选项后,只要入站 IPsec 连接已设置安全保护,但未使用 IPsec 进行加密,计算机就能够接收该入站连接的第一个 TCP 或UDP 包。一旦处理了第一个数据包,服务器将重新协商连接并对其进行升级,以便所有后续通信都完全加密。
      - 如果 action=bypass,则 dir=in 时必须指定远程计算机组。
      - 如果 service=any,则规则仅应用到服务。
      - ICMP 类型或代码可以为 "any"。
      - Edge 只能为入站规则指定。
      - AuthEnc 和 authnoencap 不能同时使用。
      - Authdynenc 仅当 dir=in 时有效。
      - 设置 authnoencap 后,security=authenticate 选项就变成可选参数。

示例:

      为不具有封装的 messenger.exe 添加入站规则:
      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 添加出站允许规则
      Add rule name="Allow port range" dir=out protocol=udp localport=5000-5010 action=allow
      

      

 

PS C:\WINDOWS\system32> netsh advfirewall firewall delete rule help

用法: netsh advfirewall firewall 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"

 

PS C:\WINDOWS\system32> netsh advfirewall firewall set rule help

用法:  netsh advfirewall firewall set rule 
      group=<string> | 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|IPHTTPS|any]
      [remoteport=0-65535|<port range>[,...]|any]
      [protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|
         tcp|udp|any]
      new
      [name=<string>]
      [dir=in|out]
      [program=<program path>
      [service=<service short name>|any]
      [action=allow|block|bypass]
      [description=<string>]
      [enable=yes|no]
      [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|RPC|RPC-EPMap|any[,...]]
      [remoteport=0-65535|any[,...]]
      [protocol=0-255|icmpv4|icmpv6|icmpv4:type,code|icmpv6:type,code|
         tcp|udp|any]
      [interfacetype=wireless|lan|ras|any]
      [rmtcomputergrp=<SDDL string>]
      [rmtusrgrp=<SDDL string>]
      [edge=yes|deferapp|deferuser|no (default=no)]
      [security=authenticate|authenc|authdynenc|notrequired]


备注:

      - 为已识别的规则设置新的参数值。如果规则不存在,则该命令失败。若要创建规则,请使用添加命令。
      - 会更新规则中 new 关键字后的值。如果没有值,或缺少关键字 new,则没有任何更改。
      - 一组规则只能被启用或禁用。
      - 如果多个规则与条件匹配,则会更新所有匹配规则。
      - 规则名称应该是唯一的,并且不能是 "all"。
      - 如果指定远程计算机或用户组,security 必须为 authenticate、authenc 或 authdynenc。
      - 为 authdynenc 设置安全性可允许系统动态协商为匹配给定 Windows Defender 防火墙规则的通信使用加密。根据现有连接安全规则属性协商加密。选择此选项后,只要入站 IPsec 连接已设置安全保护,但未使用 IPsec 进行加密,计算机就能够接收该入站连接的第一个 TCP或 UDP 包。 一旦处理了第一个数据包,服务器将重新协商连接并对其进行升级,以便所有后续通信都完全加密。
      - Authdynenc 仅当 dir=in 时有效。
      - 如果 action=bypass,则当 dir=in 时必须指定远程计算机组。
      - 如果 service=any,则规则只适用于服务。
      - ICMP 类型或代码可以是 "any"。
      - 只能为入站规则指定边缘。

示例:

      根据名称为 "allow80" 的规则更改远程 IP 地址:
      netsh advfirewall firewall set rule name="allow80" new remoteip=192.168.0.2

      启用带有分组字符串 "Remote Desktop" 的组:
      netsh advfirewall firewall set rule group="remote desktop" new enable=yes
      
      为 udp- 更改规则 "Allow port range" 上的本地端口
      Set rule name="Allow port range" dir=out protocol=udp localport=5000-5020 action=allow

 

PS C:\WINDOWS\system32> netsh advfirewall firewall show rule help

用法: netsh advfirewall firewall show rule  name=<string>
      [profile=public|private|domain|any[,...]]
      [type=static|dynamic]
      [verbose]

备注:

      - 显示所有按名称指定的匹配规则,也可按配置文件和类型指定规则。如果指定 verbose,则显示所有匹配规则。

示例:

      显示所有动态入站规则:
      netsh advfirewall firewall show rule name=all dir=in type=dynamic

      显示名为 "allow browser" 的所有入站规则的所有设置:
      netsh advfirewall firewall show rule name="allow browser" verbose

 

# netsh firewall(已弃用)命令对照表

> 
> ⚠️备注:`netsh firewall` 为**旧版兼容上下文,微软已弃用**,Win10/Win11 可执行,建议使用 `netsh advfirewall firewall` 替代,必须管理员权限运行Microsoft ...。.

PS C:\WINDOWS\system32> netsh firewall help

下列指令有效:

此上下文中的命令:
`netsh firewall ?`        显示 netsh firewall 上下文帮助信息,列出全部可用一级子命令,等价 help。

`netsh firewall add`        添加防火墙例外规则;支持 `allowedprogram`(放行程序)、`portopening`(放行端口),用于新增入站程序 / 端口放行规则Microsoft ...。

`netsh firewall delete`        删除防火墙已存在例外规则;可删除放行程序 allowedprogram、开放端口 portopening 例外条目Microsoft ...。

`netsh firewall dump`        输出防火墙完整配置可读文本,可重定向输出保存为文本文件,用于查看 / 备份旧版防火墙配置。

`netsh firewall help`        输出上下文帮助文档,功能和 `?` 完全一致。

`netsh firewall set`        修改防火墙各项配置;可设置防火墙开关 opmode、程序例外、端口例外、ICMP 回显设置、系统服务防火墙例外等Microsoft ...。

`netsh firewall show`        查看防火墙状态与配置;可查看 config 总配置、state 运行状态、放行程序、开放端口、ICMP 设置、系统服务例外列表Microsoft ...。        

若需要命令的更多帮助信息,请键入命令,接着是空格,后面跟 ?。

 

 

PS C:\WINDOWS\system32> netsh advfirewall set help

下列指令有效:

此上下文中的命令:
`netsh advfirewall set allprofiles`        一次性对**域、专用、公用**三套网络配置文件批量设置防火墙属性。可修改防火墙启停状态、默认入 / 出站兜底策略、附属开关(本地规则合并、远程管理、多播应答)、防火墙日志相关参数。

`netsh advfirewall set currentprofile`        仅对**当前网卡正在生效**的那一套配置文件做设置。机器当前是公用网络就修改 publicprofile,切换成专用网络则自动对应 privateprofile,无需手动指定 profile 类型。

`netsh advfirewall set domainprofile`        单独配置**域配置文件**。计算机接入企业域网络时启用此套策略,包含防火墙状态、默认流量策略、通知、日志等,仅 domainprofile 生效。

`netsh advfirewall set global`            修改**整机全局 IPsec 配置**,不属于域 / 专用 / 公用任一配置文件,三套配置共用这一组全局参数。可设置 IPsec SA 空闲超时、默认豁免流量、IKE 主模式密钥生命周期、加密算法套件、IPsec 隧道授权、NAT-T 开关、CRL 证书校验等。

`netsh advfirewall set privateprofile`    单独配置**专用(私有)配置文件**,适用于内网办公、家庭局域网场景,可修改防火墙开关、默认流量策略、弹窗通知、网络发现相关设置与日志。

`netsh advfirewall set publicprofile`    单独配置**公用配置文件**,用于公共 WiFi、外网等不受信任网络,默认安全策略最严格,独立修改防火墙状态、流量策略、日志等参数。

PS C:\WINDOWS\system32> netsh advfirewall set allprofiles help

用法:  netsh advfirewall set allprofiles (parameter) (value)

参数:

      state             - 配置防火墙状态。
              用法: state on|off|notconfigured

      firewallpolicy    - 配置默认入站行为和出站行为。
      用法: firewallpolicy (入站行为),(出站行为)
         入站行为:
            blockinbound        - 阻止与入站规则不匹配的入站连接。
            blockinboundalways  - 阻止所有入站连接,即使连接与规则匹配。
            allowinbound        - 允许与规则不匹配的入站连接。
            notconfigured       - 将值返回到未配置状态。
         出站行为:
            allowoutbound       - 允许与规则不匹配的出站连接。
            blockoutbound       - 阻止与规则不匹配的出站连接。
            notconfigured       - 将值返回到未配置状态。

      settings          - 配置防火墙设置。
      用法: settings (参数) enable|disable|notconfigured
      参数:
         localfirewallrules         - 将本地防火墙规则与组策略规则合并。
                                      配置组策略存储时有效。
         localconsecrules           - 将本地连接安全规则与组策略规则合并。
                                      配置组策略存储时有效。
         inboundusernotification    - 在程序侦听入站连接时通知用户。
         remotemanagement           - 允许远程管理 Windows 防火墙。
         unicastresponsetomulticast - 控制对多播的状态单播响应。

      logging           - 配置日志记录设置。
      用法: logging (参数) (值)
      参数:
         allowedconnections  - 记录允许连接的日志。
                               值: enable|disable|notconfigured
         droppedconnections  - 记录放弃连接的日志。
                               值: enable|disable|notconfigured
         filename            - 防火墙日志的名称和位置。
                               值: <string>|notconfigured
         maxfilesize         - 最大日志文件大小(KB)。
                               值: 1 - 32767|notconfigured

注释:

      - 配置所有配置文件的配置文件设置。
      - "notconfigured" 值仅对于组策略存储有效。

示例:

      所有配置文件关闭防火墙:
      netsh advfirewall set allprofiles state off

      设置默认行为,以在所有配置文件上 阻止入站连接和允许出站连接:
      netsh advfirewall set allprofiles firewallpolicy blockinbound,allowoutbound

      在所有配置文件上启用远程管理:
      netsh advfirewall set allprofiles settings remotemanagement enable

      在所有配置文件上,记录放弃连接的日志:
      netsh advfirewall set allprofiles logging droppedconnections enable

PS C:\WINDOWS\system32> netsh advfirewall set currentprofile help

用法: netsh advfirewall set currentprofile (parameter) (value)

参数:

      state             - 配置防火墙状态。
              用法: state on|off|notconfigured

      firewallpolicy    - 配置默认入站行为和出站行为。
      用法: firewallpolicy (入站行为),(出站行为)
         入站行为:
            blockinbound        - 阻止与入站规则不匹配的入站连接。
            blockinboundalways  - 阻止所有入站连接,即使连接与规则匹配。
            allowinbound        - 允许与规则不匹配的入站连接。
            notconfigured       - 将值返回到未配置状态。
         出站行为:
            allowoutbound       - 允许与规则不匹配的出站连接。
            blockoutbound       - 阻止与规则不匹配的出站连接。
            notconfigured       - 将值返回到未配置状态。

      settings          - 配置防火墙设置。
      用法: settings (parameter) enable|disable|notconfigured
      参数:
         localfirewallrules         - 将本地防火墙规则与组策略规则合并。
                                      配置组策略存储时有效。
         localconsecrules           - 将本地连接安全规则与组策略规则合并。
                                      配置组策略存储时有效。
         inboundusernotification    - 在程序侦听入站连接时通知用户。
         remotemanagement           - 允许远程管理 Windows 防火墙。
         unicastresponsetomulticast - 控制对多播的状态单播响应。

      logging           - 配置日志记录设置。
      用法: logging (参数) (值)
      参数:
         allowedconnections  - 记录允许连接的日志。
                               值: enable|disable|notconfigured
         droppedconnections  - 记录放弃连接的日志。
                               值: enable|disable|notconfigured
         filename            - 防火墙日志的名称和位置。
                               值: <string>|notconfigured
         maxfilesize         - 最大日志文件大小(KB)。
                               值: 1 - 32767|notconfigured

注释:

      - 配置当前活动的配置文件的配置文件设置。
      - "notconfigured" 值仅对于组策略存储有效。

示例:

      在当前活动的配置文件上关闭防火墙:
      netsh advfirewall set currentprofile state off

      设置默认行为,以在当前活动的配置文件上阻止入站连接和允许出站连接:
      netsh advfirewall set currentprofile firewallpolicy  blockinbound,allowoutbound

      在当前活动的配置文件上启用远程管理:
      netsh advfirewall set currentprofile settings remotemanagement enable

      在当前活动的配置文件上,记录放弃连接的日志:
      netsh advfirewall set currentprofile logging droppedconnections enable

PS C:\WINDOWS\system32> netsh advfirewall set domainprofile help

用法:  netsh advfirewall set domainprofile (parameter) (value)

参数:

      state             - 配置防火墙状态。
              用法: state on|off|notconfigured

      firewallpolicy    - 配置默认入站和出站行为。
      用法: firewallpolicy (入站行为),(出站行为)
         入站行为:
            blockinbound        - 阻止与入站规则不匹配的入站连接。
            blockinboundalways  - 阻止所有入站连接,即使连接与规则匹配。
            allowinbound        - 允许与规则不匹配的入站连接。
            notconfigured       - 将值返回到未配置的状态。
         出站行为:
            allowoutbound       - 允许与规则不匹配的出站连接。
            blockoutbound       - 阻止与规则不匹配的出站连接。
            notconfigured       - 将值返回到未配置的状态。

      settings          - 配置防火墙设置。
      用法: settings (参数) enable|disable|notconfigured
      参数:
         localfirewallrules         - 将本地防火墙规则与组策略规则合并。
                                      配置组策略存储时有效。
         localconsecrules           - 将本地连接安全规则与组策略规则合并。
                                      配置组策略存储时有效。
         inboundusernotification    - 在程序侦听入站连接时通知用户。
         remotemanagement           - 允许远程管理 Windows 防火墙。
         unicastresponsetomulticast - 控制对多播的状态单播响应。

      logging           - 配置日志记录设置。
      用法: logging (参数) (值)
      参数:
         allowedconnections  - 记录允许连接的日志。
                               值: enable|disable|notconfigured
         droppedconnections  - 记录放弃连接的日志。
                               值: enable|disable|notconfigured
         filename            - 防火墙日志的名称和位置。
                               值: <string>|notconfigured
         maxfilesize         - 最大日志文件大小(以 KB 计)。
                               值: 1 - 32767|notconfigured

注释:

      - 配置域配置文件设置。
      - "notconfigured" 值仅对于组策略存储有效。

示例:

      在域配置文件活动时关闭防火墙:
      netsh advfirewall set domainprofile state off

      设置默认行为,以在域配置文件活动时阻止入站连接和允许出站连接:
      netsh advfirewall set domainprofile firewallpolicy  blockinbound,allowoutbound

      在域配置文件活动时启用远程管理:
      netsh advfirewall set domainprofile settings remotemanagement enable

      在域配置文件活动时,记录放弃连接的日志:
      netsh advfirewall set domainprofile logging droppedconnections enable

PS C:\WINDOWS\system32> netsh advfirewall set global help

用法:  netsh advfirewall set global  statefulftp|statefulpptp  enable|disable|notconfigured
       netsh advfirewall set global  ipsec (parameter) (value)
       netsh advfirewall set global  mainmode (parameter) (value) | notconfigured

IPsec 参数:

      strongcrlcheck    - 配置如何强制 CRL 检查。
                          0: 禁用 CRL 检查(默认值)
                          1: 如果证书被吊销,则失败
                          2: 出现任何错误,都失败
                          notconfigured: 将该值返回到未
                          配置状态。
      saidletimemin     - 配置安全关联空闲时间(以分钟为单位)。
                        - 用法: 5-60|notconfigured (default=5)
      defaultexemptions - 配置默认的 IPSec 免除。默认值为
                          将 IPv6 neighbordiscovery 协议和 DHCP
                          从 IPSec 免除。
                        - 用法: none|neighbordiscovery|icmp|dhcp|notconfigured
      ipsecthroughnat   - 可以与网络地址转换器后面的
                          计算机建立安全关联时配置。
                        - 用法:  never|serverbehindnat|
                                 serverandclientbehindnat|
                                 notconfigured(default=never)
      authzcomputergrp  - 配置授权建立隧道模式连接的计算机。
                        - 用法: none|<SDDL string>|notconfigured
      authzusergrp      - 配置授权建立隧道模式连接的用户。
                          tunnel mode connections.
                        - 用法: none|<SDDL string>|notconfigured

主模式参数:

      mmkeylifetime     - 设置主模式密钥生存时间(以分钟为单位)
                          或会话,或两者。
                        - 用法: <num>min,<num>sess
                          minlifetime: <1> 分钟,
                          maxlifetime: <2880> 分钟,
                          minsessions: <0> 个会话,
                          maxsessions: <2,147,483,647> 个会话
      mmsecmethods      - 配置主模式建议列表
                        - 用法:
                          keyexch:enc-integrity,keyexch:
                          enc-integrity[,...]|default
                        - keyexch=dhgroup1|dhgroup2|dhgroup14|dhgroup24|
                          ecdhp256|ecdhp384
                        - enc=3des|des|aes128|aes192|aes256
                        - integrity=md5|sha1|sha256|sha384
      mmforcedh         - 配置使用 DH 确保密钥交换安全的选项。
                        - 用法:
                          yes|no (default=no)


注释:

      - 配置全局设置,包括高级 IPsec 选项。
      - 不建议使用 DES、MD5 和 DHGroup1。提供这些 加密算法只用于向下兼容。
      - mmsecmethods 关键字 default 将策略设置为: dhgroup2-aes128-sha1,dhgroup2-3des-sha1

示例:

      禁用 CRL 检查:
      netsh advfirewall set global ipsec strongcrlcheck 0

      启用防火墙对状态 FTP 的支持:
      netsh advfirewall set global statefulftp enable

      将全局主模式建议设置为默认值:
      netsh advfirewall set global mainmode mmsecmethods default

      将全局主模式建议设置为客户列表:
      netsh advfirewall set global mainmode mmsecmethods dhgroup1:des-md5,3des-sha1
      
PS C:\WINDOWS\system32> netsh advfirewall set privateprofile help

用法:  netsh advfirewall set privateprofile  (parameter) (value)

参数:

      state             - 配置防火墙状态。
              用法: state on|off|notconfigured

      firewallpolicy    - 配置默认入站行为和出站行为。
      用法: firewallpolicy (入站行为),(出站行为)
         入站行为:
            blockinbound        - 阻止与入站规则不匹配的入站连接。
            blockinboundalways  - 阻止所有入站连接,即使连接与规则匹配。
            allowinbound        - 允许与规则不匹配的入站连接。
            notconfigured       - 将值返回到未配置的状态。
         出站行为:
            allowoutbound       - 允许与规则不匹配的出站连接。
            blockoutbound       - 阻止与规则不匹配的出站连接。
            notconfigured       - 将值返回到未配置的状态。

      settings          - 配置防火墙设置。
      用法: settings (参数) enable|disable|notconfigured
      参数:
         localfirewallrules         - 将本地防火墙规则与组策略规则合并。
                                      配置组策略存储时有效。
         localconsecrules           - 将本地连接安全规则与组策略规则合并。
                                      配置组策略存储时有效。
         inboundusernotification    - 在程序侦听入站连接时通知用户。
         remotemanagement           - 允许远程管理 Windows 防火墙。
         unicastresponsetomulticast - 控制对多播的状态单播响应。

      logging           - 配置日志记录设置。
      用法: logging (参数) (值)
      参数:
         allowedconnections  - 记录允许连接的日志。
                               值: enable|disable|notconfigured
         droppedconnections  - 记录放弃连接的日志。
                               值: enable|disable|notconfigured
         filename            - 防火墙日志的名称和位置。
                               值: <string>|notconfigured
         maxfilesize         - 最大日志文件大小(KB)。
                               值: 1 - 32767|notconfigured

注释:

      - 配置专用配置文件设置。
      - "notconfigured" 值仅对于组策略存储有效。

示例:

      在专用配置文件活动时关闭防火墙:
      netsh advfirewall set privateprofile state off

      设置默认行为,以在专用配置文件活动时
      阻止入站连接和允许出站连接:
      netsh advfirewall set privateprofile firewallpolicy blockinbound,allowoutbound

      在专用配置文件活动时启用远程管理:
      netsh advfirewall set privateprofile settings remotemanagement enable

      在专用配置文件活动时,记录放弃连接的日志:
      netsh advfirewall set privateprofile logging droppedconnections enable

PS C:\WINDOWS\system32> netsh advfirewall set publicprofile help

用法:  netsh advfirewall set publicprofile (参数) (值)

参数:

      state             - 配置防火墙状态。
              用法: state on|off|notconfigured

      firewallpolicy    - 配置默认入站行为和出站行为。
      用法: firewallpolicy (入站行为),(出站行为)
         入站行为:
            blockinbound        - 阻止与入站规则不匹配的入站连接。
            blockinboundalways  - 阻止所有入站连接,即使连接与规则匹配。
            allowinbound        - 允许与规则不匹配的入站连接。
            notconfigured       - 将值返回到未配置状态。
         出站行为:
            allowoutbound       - 允许与规则不匹配的出站连接。
            blockoutbound       - 阻止与规则不匹配的出站连接。
            notconfigured       - 将值返回到未配置状态。

      settings          - 配置防火墙设置。
      用法: settings (参数) enable|disable|notconfigured
      参数:
         localfirewallrules         - 将本地防火墙规则与组策略规则合并。
                                      配置组策略存储时有效。
         localconsecrules           - 将本地连接安全规则与组策略规则合并。
                                      配置组策略存储时有效。
         inboundusernotification    - 在程序侦听入站连接时通知用户。
         remotemanagement           - 允许远程管理 Windows 防火墙。
         unicastresponsetomulticast - 控制对多播的状态单播响应。

      logging           - 配置日志文件设置。
      用法: logging (参数) (值)
      参数:
         allowedconnections  - 记录允许连接的日志。
                               值: enable|disable|notconfigured
         droppedconnections  - 记录放弃连接的日志。
                               值: enable|disable|notconfigured
         filename            - 防火墙日志的名称和位置。
                               值: <string>|notconfigured
         maxfilesize         - 最大日志文件大小(KB)。
                               用法: 1 - 32767|notconfigured

注释:

      - 配置公用配置文件设置。
      - "notconfigured" 值仅对于组策略存储有效。

示例:

      在公用配置文件活动时关闭防火墙:
      netsh advfirewall set publicprofile state off

      设置默认行为,以在公用配置文件活动时阻止入站连接和允许出站连接:
      netsh advfirewall set publicprofile firewallpolicy
      blockinbound,allowoutbound

      在公用配置文件活动时打开远程管理:
      netsh advfirewall set publicprofile settings remotemanagement enable

      在公用配置文件活动时,记录放弃连接的日志:
      netsh advfirewall set publicprofile logging droppedconnections enable

PS C:\WINDOWS\system32>

 

 

 

PS C:\WINDOWS\system32> netsh advfirewall show help

下列指令有效:

此上下文中的命令:
`netsh advfirewall show allprofiles`        一次性查看**域、专用、公用**三套网络配置文件的防火墙完整状态,包含防火墙开关、入站出站默认策略、日志设置。

`netsh advfirewall show currentprofile`        查看当前网卡正在使用的网络配置文件,实时显示当前生效防火墙策略。

`netsh advfirewall show domainprofile`        单独查看**域配置文件**防火墙参数,仅域网络环境下生效。

`netsh advfirewall show global`            查看防火墙全局 IPsec 相关参数,不属于单独网络配置文件。

`netsh advfirewall show privateprofile`    单独查看**专用(私有)配置文件**防火墙参数,内网家庭 / 办公私有网络生效。

`netsh advfirewall show publicprofile`    单独查看**公用配置文件**防火墙参数,公共 Wi-Fi、外网环境生效,默认策略最严格。

`netsh advfirewall show store`            查看防火墙策略存储位置,区分本地存储或组策略存储,判断策略来源。 
 
PS C:\WINDOWS\system32>

netsh advfirewall show 系列输出完整解析

执行命令:show allprofiles / currentprofile / domainprofile / privateprofile / publicprofile / global / store

核心特征:GPO 组策略接管防火墙规则(LocalFirewallRules N/A (仅 GPO 存储)),本机本地自定义防火墙规则失效,所有规则由域组策略下发。

当前生效网络配置文件:公用配置文件 publicprofile

一、三套网络配置文件通用字段解读(域 / 专用 / 公用)

状态                                  启用
防火墙策略                          BlockInbound,AllowOutbound
LocalFirewallRules                    N/A (仅 GPO 存储)
LocalConSecRules                      N/A (仅 GPO 存储)
InboundUserNotification               禁用
RemoteManagement                      禁用
UnicastResponseToMulticast            启用
日志:
LogAllowedConnections                 禁用
LogDroppedConnections                 禁用
FileName                              %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize                           4096

表格

字段 值 详细说明
状态 启用 三套配置文件防火墙全部打开。
防火墙策略 BlockInbound,AllowOutbound 默认入站阻止,出站允许。外部主动访问本机默认全部拦截;本机对外发起的连接默认放行(标准安全基线)。
LocalFirewallRules N/A (仅 GPO 存储) ✅关键标记:本地防火墙规则被禁用,不能在本机 wf.msc 或 netsh 添加本地规则;规则由域 GPO 统一推送。
LocalConSecRules N/A (仅 GPO 存储) IPsec 连接安全规则同样由 GPO 管控,本机本地 IPsec 规则不生效。
InboundUserNotification 禁用 阻止弹窗提示被拦截的入站连接。
RemoteManagement 禁用 不开放防火墙远程管理功能。
UnicastResponseToMulticast 启用 允许单播响应多播请求(网络发现相关,如局域网设备探测)。
LogAllowedConnections 禁用 不记录成功放行的数据包。
LogDroppedConnections 禁用 不记录丢弃拦截的数据包;排查网络阻断时,可临时开启日志。
FileName pfirewall.log 防火墙日志路径:C:\Windows\System32\LogFiles\Firewall\pfirewall.log
MaxFileSize 4096 日志文件最大 4MB,达到上限自动循环覆盖。

currentprofile 输出 = 当前网卡使用【公用配置文件】,也就是现在正在生效的这套策略。

二、netsh advfirewall show global(全局 IPsec 参数)

全局 设置:
----------------------------------------------------------------------
IPsec:
StrongCRLCheck                        0:已禁用
SAIdleTimeMin                         5min
DefaultExemptions                     NeighborDiscovery,DHCP
IPsecThroughNAT                       从未
AuthzUserGrp                          无
AuthzComputerGrp                      无
StatefulFTP                           禁用
StatefulPPTP                          禁用
主模式:
KeyLifetime                           480min,0sess
SecMethods                            DHGroup2-AES128-SHA1,DHGroup2-3DES-SHA1
ForceDH                               No
类别:
BootTimeRuleCategory                  Windows Defender 防火墙
FirewallRuleCategory                  Windows Defender 防火墙
StealthRuleCategory                   Windows Defender 防火墙
ConSecRuleRuleCategory                Windows Defender 防火墙

表格

字段 值 说明
StrongCRLCheck 0: 已禁用 IPsec 证书吊销强校验关闭。
SAIdleTimeMin 5min IPsec 安全关联空闲 5 分钟自动销毁会话。
DefaultExemptions NeighborDiscovery,DHCP IPv6 邻居发现、DHCP 流量豁免 IPsec 加密。
IPsecThroughNAT 从未 默认不启用 IPsec NAT-T,IPsec 隧道无法穿越 NAT 设备。
StatefulFTP 禁用 IPsec 不处理 FTP 动态端口。
StatefulPPTP 禁用 IPsec 关闭 PPTP 状态处理。
KeyLifetime 480min IKE 主模式密钥有效期 8 小时。
SecMethods DHGroup2-AES128-SHA1,DHGroup2-3DES-SHA1 IKE 第一阶段协商算法套件,包含老旧弱算法 SHA1、DHGroup2、3DES,现代安全评估不推荐。
ForceDH No 不强制每次协商重新生成 DH 密钥。

三、netsh advfirewall show store

Microsoft Store 设置:
----------------------------------------------------------------------
策略存储                              本地
确定。

策略存储位置为本地;虽然存储位置是本地,但是上面LocalFirewallRules=N/A代表组策略接管规则。 区分概念:

  • store = 本地:策略数据库存在本机;
  • LocalFirewallRules N/A:规则编辑权限被 GPO 锁定,本地不能新增 / 修改自定义规则,只能读取。

四、重点风险与结论

  1. 🔥最核心信息:GPO 接管防火墙 LocalFirewallRules N/A (仅 GPO 存储)

本机 wf.msc 图形界面、netsh advfirewall firewall add/set 新增本地防火墙规则不会生效。所有入站出站、IPsec 连接安全规则,必须在域控制器组策略编辑器修改,下发策略到客户端。 本机仅能查看策略,无法本地修改规则。

  1. 当前网络位置:公用配置文件

本机网卡识别为公用网络,执行的是公用配置文件策略:BlockInbound,外部主动访问全部拦截。

  1. 日志全部关闭

防火墙放行日志、丢弃日志都未开启。如果出现网络访问被防火墙拦截,需要在 GPO 中开启日志,查看 pfirewall.log 定位丢包。

  1. IPsec 算法套件存在老旧弱密码(SHA1 / DHGroup2 / 3DES)

企业安全基线审计会标记漏洞;如需加固,需要在 GPO 的 IPsec 连接安全规则修改协商算法。

五、运维排错关键提示

  1. 本地执行 netsh advfirewall firewall add rule 添加放行端口 / 程序,命令虽然执行无报错,但规则不会生效,GPO 覆盖本地规则。
  2. 想修改防火墙策略,需要到 AD 域控制器编辑对应 GPO,执行gpupdate /force在客户端刷新组策略。
  3. 查看组策略是否成功应用:gpresult /r,确认防火墙 GPO 策略已应用到本机。

科普卡片文本素材(可直接生成图片)

标题:netsh advfirewall show allprofiles 结果解析 核心标记:LocalFirewallRules N/A(仅 GPO 存储)→ 本机防火墙被域组策略接管,本地规则不生效 当前生效配置文件:公用配置文件

三套配置文件统一策略:BlockInbound,AllowOutbound(阻止入站,允许出站) 日志状态:丢弃日志、允许日志全部关闭;日志路径:pfirewall.log,上限 4MB IPsec 全局:SA 空闲超时 5 分钟;IKE 主模式 8 小时重协商;算法含 SHA1/DHGroup2/3DES 老旧套件,IPsec NAT-T 禁用 策略存储:本地存储,规则管理权由 GPO 锁定

关键运维提醒:本机 netsh/wf.msc 添加本地防火墙规则不会生效,需要修改域 GPO 并 gpupdate 刷新

配套排查命令

# 查看组策略应用状态
gpresult /r
# 强制刷新组策略
gpupdate /force
# 查看防火墙日志
Get-Content C:\Windows\System32\LogFiles\Firewall\pfirewall.log

 

netsh firewall 子命令完整表格

⚠️重要提示:netsh firewall 为旧版已弃用上下文,Win10/Win11 仍然可以兼容运行,但微软官方推荐使用 netsh advfirewall firewall 高级防火墙命令集,旧命令不支持现代配置文件(域 / 专用 / 公用)精细区分、出站规则、IPsec 高级规则。 运行要求:管理员 CMD/PowerShell。

表格

子命令 完整语法示例 详细描述 可用子动作
netsh firewall ? netsh firewall ? 显示 netsh‑firewall 上下文帮助、列出全部可用子命令,等价 help 无
netsh firewall add netsh firewall add allowedprogram netsh firewall add portopening 添加防火墙例外规则:添加允许程序、添加开放端口例外 allowedprogram(添加放行程序)portopening(添加放行端口)
netsh firewall delete netsh firewall delete allowedprogram netsh firewall delete portopening 删除已存在的例外规则:删除放行程序、删除端口开放例外 allowedprogramportopening
netsh firewall dump netsh firewall dump 导出当前防火墙全部配置为脚本文本;输出内容可以直接重定向保存为文件,可用于恢复配置 无参数,直接执行
netsh firewall help netsh firewall help 上下文帮助,和?完全等效,输出所有子命令说明 无
netsh firewall set netsh firewall set opmode netsh firewall set allowedprogramnetsh firewall set portopeningnetsh firewall set icmpsettingnetsh firewall set service 修改 / 设置防火墙各项配置:防火墙开关模式、修改程序例外、修改端口开放、ICMP 回显设置、系统服务防火墙配置 opmode:防火墙开启关闭allowedprogram:修改程序放行portopening:修改端口放行icmpsetting:ICMP/Ping 设置service:系统服务例外
netsh firewall show netsh firewall show config netsh firewall show statenetsh firewall show allowedprogramnetsh firewall show portopeningnetsh firewall show servicenetsh firewall show icmpsetting 查看防火墙状态与配置信息,查看当前例外列表、运行状态、ICMP、服务规则 config:完整配置总览state:防火墙运行状态allowedprogram:放行程序列表portopening:开放端口列表service:系统服务例外icmpsetting:ICMP 设置

常用实操示例(旧版 netsh firewall)

:: 查看完整防火墙配置
netsh firewall show config

:: 添加放行程序
netsh firewall add allowedprogram "C:\test.exe" "测试程序" ENABLE

:: 开放TCP 80端口
netsh firewall add portopening TCP 80 "Web服务" ENABLE

:: 导出配置脚本
netsh firewall dump > firewall_backup.txt

:: 关闭防火墙(旧版,不推荐)
netsh firewall set opmode mode=DISABLE

弃用说明 & 现代替代(netsh advfirewall)

Windows10/11 执行 netsh firewall,控制台会输出警告:IMPORTANT: "netsh firewall" is deprecated; use "netsh advfirewall firewall" instead。

表格

旧弃用命令 现代等效 advfirewall 命令
netsh firewall add allowedprogram netsh advfirewall firewall add rule dir=in action=allow program="xxx.exe"
netsh firewall add portopening TCP 80 netsh advfirewall firewall add rule name="tcp‑80" dir=in action=allow protocol=TCP localport=80
netsh firewall show config netsh advfirewall show allprofilesnetsh advfirewall firewall show rule name=all verbose
netsh firewall dump netsh advfirewall export "c:\temp\fw.wfw"

关键缺陷(netsh firewall 旧上下文)

  1. ❗只主要管理入站规则,原生不支持出站规则配置;
  2. ❗对域 / 专用 / 公用三套网络配置文件支持不完善,很多参数只能作用于当前配置文件;
  3. ❗无法精细配置远程 IP、接口类型、安全认证、边缘遍历等现代防火墙能力;
  4. ❗新功能如 App 容器规则、Windows Defender 防火墙高级安全策略完全无法操作。

建议:排错学习可以了解netsh firewall语法;生产环境全部使用netsh advfirewall firewall。

PixPin_2026-06-25_20-51-45

.wfw 文件是 Windows 防火墙配置文件的扩展名,它代表 "Windows Firewall" 的配置文件。这些文件通常包含了防火墙的设置和规则,能够将防火墙的配置导出或导入,以便在不同的计算机或不同的时间还原或复制防火墙设置。

为什么使用 .wfw 文件:

  1. 备份与恢复:

    • .wfw 文件可以作为防火墙配置的备份。当你配置好了防火墙规则后,你可以通过导出到 .wfw 文件,方便以后恢复或在其他设备上应用相同的配置。
  2. 迁移配置:

    • 如果你需要在多个计算机之间应用相同的防火墙设置,使用 .wfw 文件可以轻松迁移防火墙配置,而不必手动在每台机器上重新设置规则。
  3. 批量管理:

    • .wfw 文件使得防火墙设置的批量管理变得更加简便。你可以通过导出当前防火墙的配置文件,然后在多个系统中导入相同的配置,确保它们拥有一致的防火墙规则。

怎么使用 .wfw 文件:

  1. 导出防火墙配置:

    • 使用 netsh advfirewall export 命令将防火墙配置导出到 .wfw 文件。例如:
      shellCopy Code
      netsh advfirewall export "C:\path\to\your\firewall_config.wfw"
      这将会把当前防火墙配置导出到指定路径的 .wfw 文件中。
  2. 导入防火墙配置:

    • 如果你想恢复或在其他计算机上应用已导出的防火墙配置,可以使用 netsh advfirewall import 命令。例如:
      shellCopy Code
      netsh advfirewall import "C:\path\to\your\firewall_config.wfw"
      这会将指定的 .wfw 文件中的配置导入到当前系统的防火墙。

 

.wfw 文件是 Windows 防火墙的配置文件,可以帮助用户导出、备份、防火墙规则,并在需要时导入或迁移这些配置。这样,可以确保防火墙的设置得到保存,并且在不同设备或不同时间能够快速恢复相同的配置。


.wfw 文件通常是通过 Windows 的 netsh advfirewall export 命令导出防火墙配置时生成的文件。文件本质上是一个 XML 格式的文件,包含了防火墙的规则、配置文件、状态等信息。文件的头部部分通常是 XML 的标准声明,以及根元素和一些基本结构定义。

以下是一个 .wfw 文件头的典型结构:

xmlCopy Code
<?xml version="1.0" encoding="utf-8"?>
<netsh>
  <firewall>
    <!-- 文件的具体内容将在这里 -->
  </firewall>
</netsh>

解释:

  1. <?xml version="1.0" encoding="utf-8"?>:这是 XML 文件的声明,指定文件使用的 XML 版本和字符编码。utf-8 是常见的字符编码方式,确保文件能够支持多种语言和字符集。

  2. <netsh>:这是文件的根节点,表示该文件包含的是 netsh 防火墙配置。netsh 是 Windows 中的一个命令行工具,用于配置网络和防火墙。

  3. <firewall>:这是包含防火墙规则和配置的节点,所有具体的防火墙规则都会嵌套在该节点内。

后续内容:

在文件的后续部分,通常会有防火墙配置文件(<profiles>)和规则(<rules>)的定义,例如:

  • 防火墙配置文件(Domain、Private、Public)的状态和规则。
  • 每个规则的名称、启用状态、协议、端口等信息。

不过,文件头部分通常只是包含了 XML 声明和根元素(<netsh> 和 <firewall>)。


.wfw 文件是 Windows 防火墙配置的导出文件,它包含了防火墙的规则和设置。虽然微软没有公开 .wfw 文件的详细结构或格式规范,但我们可以从导出的 .wfw 文件内容和防火墙配置的相关信息推测它的一些特点。

.wfw 文件结构:

.wfw 文件本质上是一个文本文件,通常包含 XML 格式的配置数据。这些数据描述了防火墙规则、策略和状态等设置。它以 XML 结构存储,且被 netsh advfirewall export 命令生成。可以使用文本编辑器(例如记事本)打开并查看 .wfw 文件,但修改时需要格外小心,因为格式必须保持一致,否则文件可能无法被正确导入。

文件内容示例:

以下是 .wfw 文件可能包含的内容的一个简化示例(具体内容会因系统配置和防火墙规则不同而有所变化):

xmlCopy Code
<netsh>
  <firewall>
    <profiles>
      <profile name="Domain">
        <state>ON</state>
        <inboundpolicy>ALLOW</inboundpolicy>
        <outboundpolicy>ALLOW</outboundpolicy>
        <rules>
          <rule>
            <name>Allow HTTP Inbound</name>
            <enabled>true</enabled>
            <protocol>TCP</protocol>
            <localport>80</localport>
            <remoteport>any</remoteport>
            <action>ALLOW</action>
            <direction>IN</direction>
          </rule>
          <rule>
            <name>Allow HTTPS Inbound</name>
            <enabled>true</enabled>
            <protocol>TCP</protocol>
            <localport>443</localport>
            <remoteport>any</remoteport>
            <action>ALLOW</action>
            <direction>IN</direction>
          </rule>
        </rules>
      </profile>
      <profile name="Private">
        <state>ON</state>
        <inboundpolicy>BLOCK</inboundpolicy>
        <outboundpolicy>ALLOW</outboundpolicy>
        <rules>
          <rule>
            <name>Allow File Sharing</name>
            <enabled>true</enabled>
            <protocol>TCP</protocol>
            <localport>445</localport>
            <remoteport>any</remoteport>
            <action>ALLOW</action>
            <direction>IN</direction>
          </rule>
        </rules>
      </profile>
    </profiles>
  </firewall>
</netsh>

解释:

  • <netsh>:这是根节点,表示这是一个 netsh 配置文件。
  • <firewall>:包含所有防火墙设置的节点。
  • <profiles>:定义了不同的防火墙配置文件,通常包括 "Domain"、"Private" 和 "Public" 配置文件。
  • <profile>:每个 <profile> 节点代表一个防火墙配置文件。
    • name:配置文件的名称(例如,Domain、Private)。
    • <state>:指示防火墙是否启用(ON 或 OFF)。
    • <inboundpolicy> 和 <outboundpolicy>:定义了入站和出站流量的默认行为(例如,ALLOW 或 BLOCK)。
  • <rules>:每个配置文件下定义的防火墙规则。
    • <rule>:每条防火墙规则。
      • name:规则的名称。
      • enabled:规则是否启用(true 或 false)。
      • protocol:规则适用的协议(例如,TCP、UDP)。
      • localport 和 remoteport:指定规则所针对的本地和远程端口。
      • action:规则的操作(ALLOW 或 BLOCK)。
      • direction:流量方向(IN 或 OUT)。

主要部分:

  1. Profiles(配置文件):

    • 防火墙可以配置多个配置文件(如 "Domain"、"Private"、"Public"),每个配置文件有不同的规则和策略。
  2. Rules(规则):

    • 每个配置文件下可以有多个规则,定义了允许或阻止的特定端口、协议和流量方向。
  3. State(状态):

    • 每个配置文件都有一个启用状态,控制防火墙是否启用。

使用 .wfw 文件:

  • 导出:通过 netsh advfirewall export 命令导出当前防火墙配置并保存为 .wfw 文件。
  • 导入:通过 netsh advfirewall import 命令将 .wfw 文件导入到另一台计算机或相同计算机,恢复或应用相同的防火墙配置。

注意:

虽然 .wfw 文件是 XML 格式,但它的结构相对简单,主要包括防火墙规则和策略。手动编辑 .wfw 文件时需要确保保持其正确的 XML 结构和格式,否则导入时可能会出错。

总之,.wfw 文件是一个用于存储和传输 Windows 防火墙配置的文件,可以帮助用户备份、恢复和迁移防火墙规则和策略。


Windows Defender 防火墙 .wfw 策略文件完整技术解析

参考来源:微软官方 Netsh 文档、wf.msc 高级防火墙控制台、博客园技术文章《.wfw 文件是 Windows 防火墙配置文件的扩展名》。 适用系统:Windows10 / Windows11 / Windows Server 2016‑2022;基于netsh advfirewall高级安全防火墙体系,区别于已经弃用的旧版netsh firewall。

一、什么是 .wfw 文件

.wfw(Windows Firewall Policy File)是 Windows 高级安全防火墙原生策略备份文件,由netsh advfirewall export或者wf.msc图形控制台导出生成Microsoft ...。 文件内部封装完整防火墙配置:

  1. 域、专用、公用三套网络配置文件的全局开关、默认入站 / 出站策略;
  2. 全部入站、出站防火墙规则(端口、程序、服务、远程 IP、用户、接口条件);
  3. IPsec 连接安全规则(consec 上下文)、主模式 IKE 协商策略;
  4. 防火墙日志路径、日志大小、丢弃 / 成功数据包日志开关等高级参数博客园。

⚠️误区澄清:虽然记事本打开能看到 XML 文本,但不建议手工编辑 wfw;微软没有公开完整文档规范,修改 XML 标签极易导致文件损坏无法导入恢复。

二、.wfw 的典型应用场景

1、本地备份与故障回滚

修改大量防火墙规则前,导出.wfw备份;配置出错时直接导入恢复整套策略,避免逐条手动重建规则。

2、多台机器配置迁移、基线标准化

内网多终端需要统一防火墙安全基线,在一台模板机器调试好全部入站出站、IPsec 规则,导出.wfw,批量导入其他 Windows 终端,保证全网策略一致。

3、系统重置前保存策略

执行netsh advfirewall reset恢复防火墙出厂默认时,可以同步导出备份,防止自定义规则全部丢失。

注意:.wfw导入是全量覆盖模式,导入后本机原有全部防火墙自定义规则会被清除,生产环境操作前务必先备份本机配置。

三、两种操作方式:图形界面 wf.msc 和 netsh 命令行

方式 1:图形控制台 wf.msc

  1. Win+R 输入 wf.msc 回车,打开「高级安全 Windows Defender 防火墙」;
  2. 选中最顶层节点:本地计算机上的高级安全 Windows Defender 防火墙;
  3. 右侧操作栏:导出策略 → 保存,后缀自动为.wfw;
  4. 恢复:点击导入策略,选择 wfw 文件,确认覆盖本机现有策略,立即生效,无需重启计算机。

方式 2:netsh advfirewall 命令行(管理员 CMD / PowerShell)

导出整套防火墙策略

::导出到桌面
netsh advfirewall export "%USERPROFILE%\Desktop\fw_backup.wfw"

如果路径包含空格、中文,必须使用英文双引号包裹路径字符串。

导入恢复整套防火墙策略

netsh advfirewall import "%USERPROFILE%\Desktop\fw_backup.wfw"

重置防火墙并且自动先备份策略

netsh advfirewall reset export="%USERPROFILE%\Desktop\fw_before_reset.wfw"

✔ PowerShell 等效命令:

Export‑NetFirewallPolicy ‑Path "$env:USERPROFILE\Desktop\fw_backup.wfw"
Import‑NetFirewallPolicy ‑Path "$env:USERPROFILE\Desktop\fw_backup.wfw"

四、重要区分:export(.wfw) VS dump 文本转储

很多管理员混淆export与dump,二者用途完全不同,不能互相替代Microsoft ...。

表格

项目 netsh advfirewall export (输出.wfw) netsh advfirewall dump(输出 txt 文本)
输出格式 内部 XML 封装的.wfw策略文件 可读 netsh 命令脚本文本
是否可以 import 导入恢复 ✅支持官方导入恢复整套配置 ❌仅用于阅读审计,不能 import 导入
包含内容 防火墙规则 + IPsec consec+mainmode + 日志 + 三套配置文件全部参数 输出一批 netsh 执行语句,用于人工查看配置
使用场景 备份、迁移、故障回滚 排错审计、查阅配置,不能做备份恢复
子上下文区分 顶层 export 输出全部配置 netsh advfirewall firewall dump仅输出普通端口 / 程序规则;netsh advfirewall consec dump仅输出 IPsec 连接安全规则

示例 dump 文本导出(仅用于查看,不可用来恢复)

::输出全部可读脚本文本
netsh advfirewall dump > C:\temp\fw_dump.txt
::仅导出普通防火墙规则文本
netsh advfirewall firewall dump > C:\temp\fw_rules.txt
::仅导出IPsec连接安全规则文本
netsh advfirewall consec dump > C:\temp\ipsec_consec.txt

关键结论:真正备份防火墙必须用export生成.wfw;dump 只是可读审计脚本,不能用来做灾难恢复。

五、wfw 文件内容简要结构说明

打开 wfw 文件,头部为标准 XML 声明,简化示例如下(实际文件字段更多)博客园:

<?xml version="1.0" encoding="utf‑8"?>
<netsh>
  <firewall>
    <profiles>
      <!-- domain域配置文件 -->
      <profile name="Domain">
        <state>ON</state>
        <inboundpolicy>BLOCK</inboundpolicy>
        <outboundpolicy>ALLOW</outboundpolicy>
        <rules>
          <rule>
            <name>HTTP‑入站‑TCP‑80</name>
            <enabled>true</enabled>
            <direction>IN</direction>
            <action>ALLOW</action>
            <protocol>TCP</protocol>
            <localport>80</localport>
          </rule>
        </rules>
      </profile>
      <!-- private专用、public公用配置文件 -->
    </profiles>
    <!-- IPsec consec连接安全规则、mainmode主模式策略 -->
  </firewall>
</netsh>

节点说明:

  1. <profiles>:域、专用、公用三套防火墙配置文件,包含防火墙开关、入站出站默认策略;
  2. <rules>:每一条入站 / 出站防火墙规则,包含名称、启用状态、方向、动作、协议、端口、程序路径;
  3. 还包含 IPsec 连接安全、IKE 主模式的完整策略节点。

⚠️警告:不要手动修改 XML 标签,标签写错、少闭合标签,该 wfw 文件直接导入失败。如果需要修改防火墙策略,使用netsh advfirewall firewall add/set命令或者 wf.msc 图形界面。

六、生产环境风险点与最佳实践

1、导入是全量覆盖,会清空本机规则

执行import导入.wfw,会删除本机全部现有的自定义入站出站、IPsec 规则,替换为 wfw 内部策略。

操作标准流程:导入之前,务必先导出本机当前配置做备份。

::第一步:备份本机现有策略
netsh advfirewall export "%USERPROFILE%\Desktop\本机当前备份.wfw"
::第二步:导入目标基线策略
netsh advfirewall import "%USERPROFILE%\Desktop\基线模板.wfw"

2、跨机器导入存在兼容性陷阱

即使 wfw 导入没有报错,规则也有可能不生效,常见坑点:

  1. 程序路径问题:基于程序路径的防火墙规则,目标机器软件安装路径不一致,规则不会匹配流量;
  2. 网络配置文件 (profile) 不匹配:规则绑定「域」配置文件,但是目标机器没有加入域,这条规则永远不触发;
  3. 依赖特定服务:基于 Windows 服务的防火墙规则,如果目标机器没有安装对应服务,规则无效;
  4. Windows 版本差异:高版本 Windows 新增的防火墙属性,导入低版本系统会被忽略。

3、禁止手工修改 wfw 文件

微软没有对外公开完整 wfw schema 规范,记事本编辑后极易损坏,导入直接报错。

如果需要批量修改规则:优先使用netsh advfirewall firewall set rule命令或者 PowerShell Set‑NetFirewallRule。

4、版本管理建议

  • 修改防火墙策略之后,另存一份新的 wfw,不要覆盖原始备份文件,方便回滚旧基线;
  • 重要服务器,把.wfw备份保存到非系统盘,避免系统盘故障丢失备份;
  • 不要把 dump 生成 txt 文本当做备份,txt 只能看,不能做灾难恢复。

七、常见故障排查

  1. import导入 wfw 提示文件无效:
    • 是否使用正确.wfw后缀,不要改成.txt/.xml;
    • 文件是否被记事本手工编辑破坏;
    • 确认执行命令身份是管理员权限。
  2. wfw 导入成功,但是网络不通:
    • 导入是覆盖模式,检查导入后的默认入站策略是否为阻止;
    • 核对规则绑定的网络配置文件(域 / 专用 / 公用)和本机网卡当前网络位置是否匹配;
    • 检查程序路径、服务名称在目标机器是否存在。
  3. netsh advfirewall export命令不生成文件:
    • 路径含有空格中文,没有使用英文双引号包裹;
    • 目标目录没有写入权限,不要直接保存到C:\Windows\System32,优先桌面、C:\temp 目录。

八、补充:新旧 netsh 防火墙命令区分

表格

项目 旧版废弃 netsh firewall 现代 netsh advfirewall
备份文件 没有 wfw,只能 dump 输出文本,不能导入恢复 export 生成.wfw,支持 import 完整恢复
能力限制 仅入站规则,不支持出站,不支持 IPsec 完整入站、出站、IPsec 连接安全、主模式策略
推荐状态 弃用,仅兼容旧脚本 Windows10/11/Server 官方标准接口

执行旧版netsh firewall会输出警告提示:IMPORTANT: "netsh firewall" is deprecated; use "netsh advfirewall firewall" instead。

九、快速运维脚本参考

1、一键备份防火墙到桌面(保存为 bat,管理员运行)

@echo off
chcp 65001 >nul
set "bak=%USERPROFILE%\Desktop\fw_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%.wfw"
netsh advfirewall export "%bak%"
echo 备份文件:%bak%
pause

2、审计导出三份文本用于排错(只查看,不能恢复)

netsh advfirewall dump > "%USERPROFILE%\Desktop\fw_all_dump.txt"
netsh advfirewall firewall dump > "%USERPROFILE%\Desktop\fw_rule_dump.txt"
netsh advfirewall consec dump > "%USERPROFILE%\Desktop\ipsec_consec_dump.txt"

3、重置防火墙,自动先备份

netsh advfirewall reset export="%USERPROFILE%\Desktop\fw_before_reset.wfw"

总结:.wfw是 Windows 防火墙唯一官方可导入的完整策略备份格式;dump仅作为排错审计文本,二者各司其职,不可混淆。运维中备份灾难恢复务必使用netsh advfirewall export生成.wfw文件。


 

posted @ 2025-01-29 03:58  suv789  阅读(386)  评论(0)    收藏  举报