netsh advfirewall

netsh advfirewall 命令使用高级安全实用工具配置和管理 Windows 防火墙。 此工具允许管理员配置防火墙规则、安全策略,以及监视防火墙行为的方方面面。

Syntax

netsh advfirewall 命令根据要执行的特定作提供各种子命令和上下文。

netsh advfirewall consec | dump | export | firewall | help | import | mainmode | monitor | reset | set | show | ?
netsh advfirewall [consec | firewall | mainmode] <add> <delete> <dump> <help> <set> <show> <?>
netsh advfirewall [monitor] <delete> <dump> <help> <show> <?>
netsh advfirewall export <path\filename>
netsh advfirewall import <path\filename>
netsh advfirewall reset [export <path\filename>]
netsh advfirewall set [allprofiles | currentprofile | domainprofile | privateprofile | publicprofile] state <on> <off> <notconfigured>
netsh advfirewall set firewallpolicy <blockinbound> <blockinboundalways> <allowinbound> <allowoutbound> <blockoutbound> <notconfigured>
netsh advfirewall set settings <localfirewallrules> <localconsecrules> <inboundusernotification> <remotemanagement> <unicastresponsetomulticast>
netsh advfirewall set logging <allowedconnections> <droppedconnections> <filename> <maxfilesize>
netsh advfirewall set global [statefulftp | statefulpptp] <enable> <disable> <notconfigured>
netsh advfirewall set global ipsec [strongcrlcheck <value>] [saidletimemin <value>] [defaultexemptions <value>] [ipsecthroughnat <value>] [authzcomputergrp <value>] [authzusergrp <value>]
netsh advfirewall set global mainmode [mmkeylifetime <value>] [mmsecmethods <value>] [mmforcedh <value>]
netsh advfirewall show [allprofiles | currentprofile | domainprofile | privateprofile | publicprofile] <state> <firewallpolicy> <settings> <logging>
netsh advfirewall show global <ipsec> <statefulftp> <statefulpptp> <mainmode> <categories>
netsh advfirewall show <store>

Parameters

Command Description
consec <add><delete><help><set><show> 规则名称=<string>

consec <dump>
切换到 netsh advfirewall consec 上下文以配置连接安全规则。

  • add - 添加新的连接安全规则。
  • delete - 删除所有匹配的连接安全规则。
  • 转储 - 显示配置脚本。
  • help - 显示命令列表。
  • set - 设置现有规则的属性的新值。
  • show - 显示指定的连接安全规则。
  • 转储 显示可用于还原当前配置的配置脚本。
    出口 <path\filename> 将当前的防火墙策略设置导出到指定的文件。
    防火墙<add><delete><help><set><show>规则名称=<string>

    防火墙 <dump>
    切换到 netsh advfirewall firewall 上下文以配置防火墙规则。

  • add - 添加新的入站或出站防火墙规则。
  • delete - 删除所有匹配的防火墙规则。
  • 转储 - 显示配置脚本。
  • help - 显示命令列表。
  • set - 设置现有规则的属性的新值。
  • show - 显示指定的防火墙规则。
  • 帮助 还是 在当前上下文中显示命令及其说明的列表。
    进口 <path\filename> 将防火墙策略设置从指定文件导入到当前策略存储中。
    mainmode <add><delete><help><set><show> 规则名称=<string>

    主模式 <dump>
    切换到 netsh advfirewall mainmode 上下文以配置主模式设置。

  • add - 添加新的 mainmode 规则。
  • delete - 删除所有匹配的主模式规则。
  • 转储 - 显示配置脚本。
  • help - 显示命令列表。
  • set - 设置现有规则的属性的新值。
  • show - 显示指定的 mainmode 规则。
  • 监视删除 mmsa \| qmsa<source destination \| all>

    监控 <help>

    监视器显示 <consec><currentprofile><firewall><mainmode><mmsa><qmsa>

    监控 <dump>
    netsh advfirewall monitor切换到上下文以显示监视信息。

  • delete - 删除所有匹配的安全关联。 源目标为一个或多个 IP 地址。 All 指定该作应应用于所有条目。
  • 转储 - 显示配置脚本。
  • help - 显示命令列表。
  • show - 显示运行时防火墙策略设置:
    - consec 显示当前 consec 状态信息。
    - currentprofile 显示当前活动配置文件。
    - firewall 显示当前防火墙状态信息。
    - mainmode 显示当前主模式状态信息。
    - mmsa 显示主模式 CA。
    - qmsa 显示快速模式 CA。
  • 重置 <export path\filename> 将具有高级安全策略的 Windows Defender 防火墙重置为默认设置。 可以选择将当前活动策略导出到指定文件。

    在组策略对象中,此命令返回所有设置,并 notconfigured 删除所有连接安全和防火墙规则。
    设置 allprofiles |currentprofile |domainprofile |privateprofile |publicprofile | parameter<value> 为防火墙配置每个配置文件或全局设置。

  • allprofiles - 设置所有配置文件中的属性。
  • currentprofile - 设置活动配置文件中的属性。
  • domainprofile - 设置域配置文件中的属性。
  • global - 设置全局属性。
  • privateprofile - 设置专用配置文件中的属性。
  • publicprofile - 设置公共配置文件中的属性。

  • 该值 notconfigured 仅对组策略存储有效。

    可用 parameter<value> 如下所示:

    state - 配置防火墙状态。
  • <on> - 启用防火墙。 防火墙打开时,会根据配置的规则主动筛选网络流量。
  • <off> - 禁用防火墙。 不会应用筛选或保护,并且允许所有网络流量。
  • <notconfigured> - 根据系统策略或管理模板设置,将防火墙状态设置为其默认配置。

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

  • settings - 配置防火墙设置。
  • localfirewallrules - 将本地防火墙规则与组策略规则合并。 配置组策略存储时有效。
  • localconsecrules - 将本地连接安全规则与组策略规则合并。 配置组策略存储时有效。
  • inboundusernotification - 当程序侦听入站连接时通知用户。
  • remotemanagement - 允许远程管理 Windows 防火墙。
  • unicastresponsetomulticast - 控制对多播的有状态单播响应。
  • <enable> - 打开指定的设置。
  • <disable> - 关闭指定的设置。
  • <notconfigured> - 将设置还原到系统默认配置。

  • logging - 配置日志记录设置。
  • allowedconnections - 确定是否在日志中记录成功的入站和出站连接。
    <on> - 记录成功的连接。
    <off> - 不记录连接。
    <notconfigured> - 还原到系统用于记录成功连接的默认行为。

  • droppedconnections - 指定是否记录防火墙阻止的连接。
    <on> - 记录所有阻止的连接。
    <off> - 不要记录阻止的连接。
    <notconfigured> - 还原到系统记录阻止连接的默认行为。

  • filename - 设置用于存储日志文件的名称和位置。
    <string> - 为日志文件指定的文件路径和文件名,例如 C:\Logs\firewall.log
    <notconfigured> - 使用系统提供的默认文件路径和名称。

  • maxfilesize - 设置日志文件的最大大小(KB)。 如果文件超过此大小,则覆盖最早的条目。 可接受的文件大小介于 1 KB 和 32,767 KB 之间。
    <notconfigured> - 采用系统的默认最大文件大小。
  • 设置全局 [statefulftp | statefulpptp] <enable><disable><notconfigured> 配置全局设置,包括高级 IPsec 选项。 不建议使用 DES、MD5 和 DHGroup1。 这些加密算法仅用于向后兼容性。

    mmsecmethods 关键字默认将策略设置为 dhgroup2-aes128-sha1,dhgroup2-3des-sha1

    statefulftp - 此选项控制有关 FTP(文件传输协议)流量的防火墙行为。
    statefulpptp - 此选项管理 PPTP(点到点隧道协议)流量的处理。

    <enable> - 激活所选协议的有状态检查。 这可确保防火墙可以动态调整规则以正确处理相应的流量。
    <disable> - 停用所选协议的有状态检查。 这可能会导致正确处理特定于协议的流量的问题,因为防火墙不会动态调整其规则。
    <notconfigured> - 将设置重置为由系统策略或配置确定的默认行为。 这样,系统就可以应用其默认规则,而无需用户指定的更改。
    设置全局 ipsec parameter<value> strongcrlcheck - 配置证书吊销列表(CRL)检查的强制级别。
    <0> - 禁用 CRL 检查(默认设置)。
    <1> - 如果证书被吊销,连接失败。
    <2> - 在与 CRL 相关的任何错误上连接失败。
    <notconfigured> - 将值重置为未配置的状态。

    saidletimemin - 设置安全关联(SA)的空闲时间(以分钟为单位),然后再将其视为非活动状态。 可接受的值范围从分钟到560分钟。
    <notconfigured> - 默认值为 5 分钟。

    defaultexemptions - 管理免除 IPsec 保护的默认协议。 默认值是免除 IPv6 邻居发现协议和 DHCP 的 IPsec。 可接受的值包括:
    <none>
    <neighbordiscovery>
    <icmp>
    <dhcp>
    <notconfigured>

    ipsecthroughnat - 配置 IPsec 与网络地址翻译器(NAT)背后的设备建立安全关联的条件。 可接受的值包括:
    <never> - 不允许关联(默认值)。
    <serverbehindnat> - 仅当服务器位于 NAT 后面时。
    <serverandclientbehindnat> - 如果服务器和客户端都位于 NAT 后面。
    <notconfigured> - 默认值为 <never>.

    authzcomputergrp - 指定有权建立隧道模式连接的计算机组。
    <none> - 没有特定的计算机组有权建立隧道模式连接。
    <SDDL string> - 指定一个安全描述符定义语言(SDDL)字符串,该字符串定义有权建立连接的计算机组。
    <notconfigured> - 指定默认设置。

    authzusergrp - 指定有权建立隧道模式连接的用户组。
    <none> - 没有特定的用户组有权建立隧道模式连接。
    <SDDL string> - 定义有权建立连接的特定用户组。
    <notconfigured> - 指定默认设置。
    设置全局 mainmode parameter<value> | <notconfigured> mmkeylifetime - 设置主模式下的键有效持续时间。 密钥生存期按分钟数 [<num min]、会话数 [>num<> sess] 或两者定义。
    密钥的最短生存期(以分钟为单位)范围从分钟到最长2880分钟不等1
    密钥有效的最小会话数范围从会话到最大会话数2,147,483,6470

    mmsecmethods - 为主模式 IPsec 协商配置安全建议列表,其中包括密钥交换、加密和完整性算法。 可以使用以逗号分隔的建议列表的格式 keyexch:enc-integrity。 每个建议都包含一组来自每个类别的算法。
  • keyexch:指定用于密钥交换的 Diffie-Hellman (DH) 或椭圆曲线 Diffie-Hellman 组。 可接受的值为:
    <dhgroup1>
    <dhgroup2>
    <dhgroup14>
    <dhgroup24>
    <ecdhp256>
    <ecdhp384>
  • enc:指定用于保护数据的加密算法。 可接受的值为:
    <3des>
    <des>
    <aes128>
    <aes192>
    <aes256>
  • 完整性:指定用于确保数据完整性的算法。 可接受的值为:
    <md5>
    <sha1>
    <sha256>
    <sha384>

    mmforcedh - 在 IPsec 主模式协商期间强制使用 DH 进行密钥交换。
    <yes> - 强制使用 DH 进行密钥交换。
    <no> - 不强制使用 DH 进行密钥交换(默认设置)。
  • 显示 allprofiles |currentprofile |domainprofile |privateprofile |publicprofile | parameter 显示防火墙的当前配置文件或全局属性。

  • allprofiles - 显示所有配置文件的属性。
  • currentprofile - 显示活动配置文件的属性。
  • domainprofile - 显示域配置文件中的属性。
  • global - 显示全局属性。
  • privateprofile - 显示专用配置文件的属性。
  • publicprofile - 显示公共配置文件的属性。
  • store - 显示当前策略存储。

  • 如果未为配置文件指定参数,将显示所有属性。

    可用参数如下所示:

    state:显示每个配置文件当前是启用还是禁用具有高级安全性的 Windows Defender 防火墙。
    firewallpolicy:显示入站和出站连接的默认作,指示每个配置文件默认是否允许或阻止此类连接。
    settings:提供防火墙的详细属性,包括每个配置文件的配置设置。
    logging:显示当前日志记录配置,包括已删除数据包和成功连接的日志文件位置、大小和日志记录选项。
    显示全局 parameter 显示具有高级安全性的 Windows Defender 防火墙的全局设置。 如果未指定任何参数,则会显示所有属性。

    可用参数如下所示:

    ipsec:显示特定的 IPsec 设置。
    statefulftp:显示有状态 FTP 支持的配置。
    statefulpptp:显示有状态 PPTP 支持的配置。
    mainmode:显示主模式的配置,这是 IPsec 协商过程中的一个阶段,双方相互进行身份验证,并建立安全通道供进一步通信。
    categories:显示与防火墙类别相关的配置。

    Remarks

    Consec 参数备注

    - The rule name should be unique and can't be "all".
    - When mode=tunnel, tunnel endpoints must be specified unless the action is "noauthentication". If specific IP addresses are used, they must belong to the same IP version.
      - For dynamic tunnels, tunnel endpoints can be set to "any".
      - Local tunnel endpoints aren't required for Client policies, for example, "any".
      - Remote tunnel endpoints aren't required for Gateway policies, for example, "any". Additionally, the action must be one of the following: requireinrequireout, requireinclearout, or noauthentication.
    - requireinclearout isn't valid when mode=Transport.
    - At least one authentication must be specified.
    - Auth1 and auth2 can be comma-separated lists of options.
    - Computerpsk and computerntlm methods can't be specified together for auth1.
    - Computercert can't be specified with user credentials for auth2.
    - Certsigning options ecdsap256 and ecdsap384 are only supported on Windows Vista SP1 and later.
    - Qmsecmethods can be a list of proposals separated by a ",".
    - For qmsecmethods, integrity=md5|sha1|sha256|aesgmac128|aesgmac192|aesgmac256|aesgcm128|aesgcm192|aesgcm256 and encryption=3des|des|aes128|aes192|aes256|aesgcm128|aesgcm192|aesgcm256.
    - If aesgcm128, aesgcm192, or aesgcm256 is specified, it must be used for both ESP integrity and encryption.
    - Aesgmac128, aesgmac192, aesgmac256, aesgcm128, aesgcm192, aesgcm256, sha256 are only supported on Windows Vista SP1 and later.
    - Qmpfs=mainmode uses the main mode key exchange setting for PFS.
    - The use of DES, MD5, and DHGroup1 isn't recommended. These cryptographic algorithms are provided for backward compatibility only.
    - The default value for certmapping and excludecaname is "no".
    - The " characters within CA name must be replaced with \'
    - For auth1ca and auth2ca, the CA name must be prefixed by "CN=".
    - catype can be used to specify the Certification authority type "catype=root/intermediate".
    - authnoencap is supported on Windows 7 and later.
    - authnoencap means that the computers will only use authentication, and won't use any per packet encapsulation or encryption algorithms to protect subsequent network packets exchanged as part of this connection.
    - QMPFS and authnoencap can't be used together on the same rule.
    - AuthNoEncap must be accompanied by at least one AH or ESP integrity suite.
    - applyauthz can only be specified for tunnel mode rules.
    - exemptipsecprotectedconnections can only be specified for tunnel mode rules. By setting this flag to "Yes", ESP traffic is exempted from the tunnel. AH only traffic won't be exempted from the tunnel.
    - Valuemin, when specified, for a qmsecmethod should be between 5 to 2880 minutes. Valuekb, when specified, for a qmsecmethod should be between 20480 KB to 2147483647 KB.
    - Certhash specifies the thumbprint, or hash of the certificate.
    - Followrenewal specifies whether to automatically follow renewal links in certificates. Only applicable for certificate section (requires certhash).
    - Certeku specifies the comma separated list of EKU OIDs to match in the certificate.
    - Certname specifies the string to match for certificate name (requires certnametype).
    - Certnametype specifies the certificate field for the certname to be matched against (requires certname).
    - Certcriteriatype specifies whether to take the action with the certificate when selecting the local certificate, validating the peer certificate, or both.
    - Within a computercert authentication mapping, multiple certificates can be referenced by separating each entry by using the "|" character.
    

    防火墙参数备注

    - Add a new inbound or outbound rule to the firewall policy.
    - Rule name should be unique and can't be "all".
    - If a remote computer or user group is specified, security must be: authenticate, authenc, authdynenc, or authnoencap.
    - Setting security to authdynenc allows systems to dynamically negotiate the use of encryption for traffic that matches a given Windows Defender Firewall rule. Encryption is negotiated based on existing connection security rule properties. This option enables the ability of a machine to accept the first TCP or UDP packet of an inbound IPsec connection as long as it's secured, but not encrypted, using IPsec. Once the first packet is processed, the server renegotiates the connection and upgrade it so that all subsequent communications are fully encrypted.
    - If action=bypass, the remote computer group must be specified when dir=in.
    - If service=any, the rule applies only to services.
    - ICMP type or code can be "any".
    - Edge can only be specified for inbound rules.
    - AuthEnc and authnoencap can't be used together.
    - Authdynenc is valid only when dir=in.
    - When authnoencap is set, the security=authenticate option becomes an optional parameter.
    

    Mainmode 参数备注

    - Add a new mainmode rule to the firewall policy.
    - Rule name should be unique and can't be "all".
    - Computerpsk and computerntlm methods can't be specified together for auth1.
    - The use of DES, MD5, and DHGroup1 isn't recommended. These cryptographic algorithms are provided for backward compatibility only.
    - The minimum main mode keylifetime is mmkeylifetime=1min. The maximum main mode mmkeylifetime=2880min. The minimum number of sessions=0 sessions. The maximum=2,147,483,647 sessions.
    - The mmsecmethods keyword default sets the policy to: dhgroup2-aes128-sha1,dhgroup2-3des-sha1
    - Certhash specifies the thumbprint, or hash of the certificate.
    - Followrenewal specifies whether to automatically follow renewal links in certificates. Only applicable for certificate section (requires certhash).
    - Certeku specifies the comma separated list of EKU OIDs to match in the certificate.
    - Certname specifies the string to match for certificate name (requires certnametype).
    - Certnametype specifies the certificate field for the certname to be matched against (requires certname).
    - Certcriteriatype specifies whether to take the action with the certificate when selecting the local certificate, validating the peer certificate, or both.
    

    监视参数注释

    - This command deletes the matching security association as specified by the source destination pair.
    - Source and destination are each a single IPv4 or IPv6 address.
    

    Examples

    Note

    有关所有参数的详细用法说明和其他示例,请使用 help? 命令。 这些命令提供特定作的语法详细信息和指南。

    consec

    若要添加需要身份验证的连接安全规则,请运行以下命令:

    netsh advfirewall consec add rule name="RequireAuth" endpoint1=any endpoint2=any action=requireinrequestout
    

    若要添加使用 Kerberos 进行身份验证的新连接安全规则,请运行以下命令:

    netsh advfirewall consec add rule name="KerberosAuthRule" endpoint1=any endpoint2=any action=requireinrequireout auth1=computerkerb
    

    若要按名称删除连接安全规则,请运行以下命令:

    netsh advfirewall consec delete rule name="RuleName"
    

    dump

    若要将当前高级防火墙配置保存到文本文件,请运行以下命令:

    netsh advfirewall dump > "C:\path\to\firewall_dump.txt"
    

    若要在 CMD 中显示当前防火墙配置,请运行以下命令:

    netsh advfirewall dump
    

    若要在诊断的主转储中包含高级防火墙状态,请运行以下命令:

    netsh advfirewall dump | findstr "State"
    

    export

    若要导出用于备份的当前防火墙策略,请运行以下命令:

    netsh advfirewall export "C:\folder\firewall_backup.wfw"
    

    firewall

    若要添加允许端口 8080 上的入站流量的规则,请运行以下命令:

    netsh advfirewall firewall add rule name="Allow8080" protocol=TCP dir=in localport=8080 action=allow
    

    若要阻止传出到特定 IP 的流量,请运行以下命令:

    netsh advfirewall firewall add rule name="BlockOutIP" protocol=TCP dir=out remoteip=192.168.1.100 action=block
    

    若要按名称删除特定防火墙规则,请运行以下命令:

    netsh advfirewall firewall delete rule name="MyRule"
    

    help

    若要获取所有防火墙命令的帮助,请运行以下命令:

    netsh advfirewall firewall ?
    

    若要了解添加防火墙规则的语法详细信息,请运行以下命令:

    netsh advfirewall firewall add rule ?
    

    import

    若要从文件导入已保存的防火墙配置,请运行以下命令:

    netsh advfirewall import "C:\folder\firewall_config.wfw"
    

    mainmode

    若要添加用于安全协商的 mainmode 规则,请运行以下命令:

    netsh advfirewall mainmode add rule name="MyMainmodeRule" auth1=computerkerb
    

    若要显示 mainmode 规则的详细设置,请运行以下命令:

    netsh advfirewall mainmode show rule name=all
    

    若要通过指定其名称删除 mainmode 规则,请运行以下命令:

    netsh advfirewall mainmode delete rule name="MyMainmodeRule"
    

    monitor

    若要监视活动连接安全关联,请运行以下命令:

    netsh advfirewall monitor show mmsa
    

    若要显示统计信息和活动连接,请运行以下命令:

    netsh advfirewall monitor show firewall
    

    reset

    若要在不进行其他确认的情况下重置防火墙设置,请运行以下命令:

    netsh advfirewall reset
    

    set

    若要禁用已删除数据包的日志记录,请运行以下命令:

    netsh advfirewall set currentprofile logging droppedconnections disable
    

    若要为阻止的应用程序启用通知,请运行以下命令:

    netsh advfirewall set currentprofile settings allowunicastresponsetomulticast enable
    

    show

    若要显示所有活动防火墙规则,请运行以下命令:

    netsh advfirewall firewall show rule name=all
    

    若要按名称显示有关特定规则的详细信息,请运行以下命令:

    netsh advfirewall firewall show rule name="MyRuleName" verbose