前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PS命令之网络防火墙策略配置

PS命令之网络防火墙策略配置

作者头像
全栈工程师修炼指南
发布2022-09-29 15:21:08
2.1K0
发布2022-09-29 15:21:08
举报

[TOC]

0x00 Windows 防火墙规则(NetFirewall)查看配置

描述: 在PowerShell中针对windows防火墙的规则的操作和查看常用命令,我们可以利用Get-Command *-NetFirewallRule命令进行查看到Function类型的命令。

  • Get-NetFirewallRule
  • Set-NetFirewallRule
  • New-NetFirewallRule
  • Show-NetFirewallRule
  • Get-NetFirewallAddressFilter
  • Get-NetFirewallPortFilter
  • Get-NetFirewallApplicationFilter
  • Get-NetFirewallInterfaceFilter
  • Get-NetFirewallInterfaceTypeFilter
  • Get-NetFirewallProfile
  • Get-NetFirewallSecurityFilter
  • Get-NetFirewallServiceFilter
  • Get-NetFirewallSetting
  • Set-NetFirewallAddressFilter
  • Set-NetFirewallPortFilter
  • Set-NetFirewallApplicationFilter
  • Set-NetFirewallInterfaceFilter
  • Set-NetFirewallInterfaceTypeFilter
  • Set-NetFirewallProfile
  • Set-NetFirewallRule
  • Set-NetFirewallSecurityFilter
  • Set-NetFirewallServiceFilter
  • Set-NetFirewallSetting
  • Enable-NetFirewallRule
  • Disable-NetFirewallRule
  • Copy-NetFirewallRule
  • Rename-NetFirewallRule
  • Remove-NetFirewallRule

New-NetFirewallRule 命令 - 添加一个新的防火墙规则

描述: 该命令创建一个新的入站或出站防火墙规则,并将该规则添加到目标计算机。

语法参数:

代码语言:javascript
复制
New-NetFirewallRule
   [-PolicyStore <String>]
   [-GPOSession <String>]
   [-Name <String>]
   -DisplayName <String>
   [-Description <String>]
   [-Group <String>]
   [-Enabled <Enabled>]
   [-Profile <Profile>]
   [-Platform <String[]>]
   [-Direction <Direction>]
   [-Action <Action>]
   [-EdgeTraversalPolicy <EdgeTraversal>]
   [-LooseSourceMapping <Boolean>]
   [-LocalOnlyMapping <Boolean>]
   [-Owner <String>]
   [-LocalAddress <String[]>]
   [-RemoteAddress <String[]>]
   [-Protocol <String>]
   [-LocalPort <String[]>]
   [-RemotePort <String[]>]
   [-IcmpType <String[]>]
   [-DynamicTarget <DynamicTransport>]
   [-Program <String>]
   [-Package <String>]
   [-Service <String>]
   [-InterfaceAlias <WildcardPattern[]>]
   [-InterfaceType <InterfaceType>]
   [-LocalUser <String>]
   [-RemoteUser <String>]
   [-RemoteMachine <String>]
   [-Authentication <Authentication>]
   [-Encryption <Encryption>]
   [-OverrideBlockRules <Boolean>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

# 关键参数:
-Action :指定创建指定操作的匹配防火墙规则。# 值: NotConfigured, Allow, Block
-Authentication : 指定防火墙规则需要身份验证。#值:NotRequired、Required或NoEncap。
-Direction :指定创建指定方向的匹配防火墙规则。#值:Inbound, Outbound
-DynamicTarget : 指定动态传输。#值: Any, ProximityApps, ProximitySharing, WifiDirectPrinting, WifiDirectDisplay, WifiDirectDevices
-EdgeTraversalPolicy : 指定创建所指示的边缘遍历(NAT)策略的匹配防火墙规则,仅当Direction参数设置为Inbound时有效。#值:Block、Allow、DeferToUser或DeferToApp。
-Encryption : 指定防火墙规则上需要身份验证中的加密。# 值:NotRequired、Required或Dynamic。
-IcmpType : 指定ICMP类型代码. #值: 0-255,3:4,Any
-InterfaceType : 指定只有通过指定接口类型进行的网络连接才受此规则要求的约束。 # 值: Any, Wired, Wireless, RemoteAccess
-LocalAddress :指定具有匹配IP地址的网络数据包与此规则匹配(本地); # 值: IPv4 or IPv6 address, hostname, subnet, range, or the following keyword: Any
  # * Single IPv4 Address: 1.2.3.4
  # * Single IPv6 Address: fe80::1
  # * IPv4 Subnet (by network bit count): 1.2.3.4/24
  # * IPv6 Subnet (by network bit count): fe80::1/48
  # * IPv4 Subnet (by network mask): 1.2.3.4/255.255.255.0
  # * IPv4 Range: 1.2.3.4-1.2.3.7
  # * IPv6 Range: fe80::1-fe80::9 Querying for rules with this parameter can only be performed using filter objects. See the Get-NetFirewallAddressFilter cmdlet for more information.
-LocalPort : 指定具有匹配IP本地端口号的网络数据包与此规则匹配。# 值: port, range, or keyword and depends on the protocol. 如果未指定协议参数则此参数的可接受值为:RPC、RPCEPMap、Teredo、IPHTTPSIn、IPHTTPSOut或Any。
-LocalUser : 指定此防火墙规则应用于的网络通信的主体。# 此防火墙规则应应用于的网络流量的主体。主体(由安全描述符定义语言(SDDL)字符串中的安全标识符(SID)表示)是服务、用户、应用程序容器或与网络流量相关联的任何SID
-LooseSourceMapping : 指示已创建与指定值匹配的防火墙规则,此参数指定松散源映射的防火墙规则,该规则描述数据包在转发到目标时是否可以具有非本地源地址。# 如果此参数设置为True则规则接受从主机传入的数据包,而不是数据包发送到的主机。此参数仅适用于UDP协议通信。
-OverrideBlockRules :指示允许匹配的网络流量否则将被阻止。
-Owner :指定防火墙规则的所有者表示为SDDL字符串或者SID
-Package :指定应用防火墙规则的Windows应用商店应用程序。 # 此参数指定为安全标识符SID,例如 S-1-15-2-1310292540-1029022339-4008023048-2190398717-53961996-4257829345-603366646
-Program :指定规则允许通信的程序的路径和文件名。
-Platform : 指定应用关联规则的Windows版本。# 值: Major.Minor 例如 6.1 对应windows 7
-PolicyStore : 规则的策略存储; # 值: PersistentStore,ActiveStore,RSOP,SystemDefaults,StaticServiceStore,ConfigurableServiceStore
-Profile : 指定将规则分配给的一个或多个配置文件, 仅当指定的配置文件当前处于活动状态时,规则才在本地计算机上处于活动状态。这种关系是多对多的,用户可以通过更改防火墙规则实例上的Profiles字段来间接修改。一次只能应用一个配置文件。# 值:Any(default)、Domain、Private、Public或NotApplicable。
-Protocol : 指定具有匹配IP地址的网络数据包与此规则匹配。 # 值: TCP, UDP, ICMPv4, or ICMPv6. 
-ThrottleLimit : 指定可以建立以运行cmdlet的最大并发操作数。如果省略此参数或输入值0则Windows PowerShell® 根据计算机上运行的CIM cmdlet的数量计算cmdlet的最佳限制。限制仅适用于当前cmdlet而不适用于会话或计算机。

基础示例:

代码语言:javascript
复制
# 1.创建一个出站防火墙规则以阻止来自本地计算机的所有通信通信源自TCP端口80,8080(本地80不对外反馈数据)。
New-NetFirewallRule -DisplayName "Block Outbound Port 80" -Direction Outbound -LocalPort 80,8080 -Protocol TCP -Action Block

# 2.阻止来自所有WINS服务器的所有入站流量。
New-NetFirewallRule -DisplayName "Block WINS" -Direction Inbound -Action Block -RemoteAddress WINS

# 3.创建入站防火墙规则该规则仅允许Windows Messenger程序的流量来自与本地计算机位于同一子网中的计算机进行访问。
New-NetFirewallRule -DisplayName "Allow Messenger" -Direction Inbound -Program "C:\app\msmsgs.exe" -RemoteAddress LocalSubnet -Action Allow

# 4.创建防火墙规则,该规则仅在使用单独的IPsec规则对来自远程计算机的连接进行身份验证的情况下才允许入Windows Messenger网络通信。
New-NetFirewallRule -DisplayName "Allow Authenticated Messenger" -Direction Inbound -Program "C:\app\msmsgs.exe" -Authentication Required -Action Allow

# 5.创建防火墙规则,该规则允许来自特定计算机组成员的计算机的所有网络流量,并且仅允许来自特定用户组成员的用户的所有网络流量。必须使用单独的连接安全规则通过身份验证来确认这两个成员身份。
New-NetFirewallRule -DisplayName "Allow Only Specific Computers and Users" -Direction Inbound -RemoteMachine "D:(A;;CC;;;SIDforMachineGroupAccount)"  -RemoteUser "D:(A;;CC;;;SIDforUserGroupAccount)"  -Action Allow -Authentication Required

# 6.使用两个cmdlet创建阻止所有无线网络流量的防火墙规则。
New-NetFirewallRule -Name "Block Wireless In" -Direction Inbound -InterfaceType Wireless -Action Block
New-NetFirewallRule -Name "Block Wireless Out" -Direction Outbound -InterfaceType Wireless -Action Block

# 7.创建规则允许使用Teredo IPv6接口`从边缘(NAT)设备`的远程计算机上发送到端口12345的TCP流量和端口5000-5020的范围到特定应用程序。
New-NetFirewallRule -DisplayName "Allow TCP 12345 and 5000-5020 over Teredo" -Direction Inbound -Action Allow -EdgeTraversalPolicy Allow -Protocol TCP -LocalPort 12345,5000-5020 -Program "C:\Program Files (x86)\TestIPv6App.exe"

# 8.创建规则允许外部入站访问本机80端口(http服务)以及该端口的出站规则
New-NetFirewallRule -DisplayName "Http Server" -Direction Inbound -LocalPort 80 -RemotePort Any -Protocol TCP -Enabled True
New-NetFirewallRule -DisplayName "Http Server" -Direction Outbound -LocalPort 80 -RemotePort Any -Protocol TCP -Enabled True

# 9.通过脚本设置当前机器防火墙允许ICMPv4协议入站规则。
New-NetFirewallRule -DisplayName "Allow Ping" -Description "Allow ping" -Protocol ICMPv4 -IcmpType 8 -Enabled True -Profile Any -Action Allow

Get-NetFirewallRule 命令 - 从目标计算机检索防火墙规则。

描述: 该cmdlet返回与用户的搜索参数匹配的防火墙规则实例,此cmdlet通过指定Name参数(默认)、DisplayName参数、规则属性或关联的筛选器或对象返回一个或多个防火墙规则。查询的规则可以放入变量中,并通过管道传输到其他cmdlet以进行进一步修改或监视。

Tips : 如果执行基于这些参数(端口,地址,安全性,接口和服务)的规则查询,则应检索带有带有Get动词的对应cmdlet的过滤器对象。

基础语法:

代码语言:javascript
复制
Get-NetFirewallRule [-All] [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] [-TracePolicyStore] [<CommonParameters>]

Get-NetFirewallRule [-Name] <String[]> -DisplayName <String[]>  [-Action <Action[]>] [-AsJob] [-CimSession <CimSession[]>] [-Description <String[]>] [-Direction <Direction[]>] [-DisplayGroup <String[]>] [-EdgeTraversalPolicy <EdgeTraversal[]>] [-Enabled <Enabled[]>] [-GPOSession <String>] [-Group <String[]>] [-LocalOnlyMapping <Boolean[]>] [-LooseSourceMapping <Boolean[]>] [-Owner <String[]>] [-PolicyStore <String>] [-PolicyStoreSource <String[]>] [-PolicyStoreSourceType <PolicyStoreType[]>] [-PrimaryStatus <PrimaryStatus[]>] [-Status <String[]>] [-ThrottleLimit <Int32>] [-TracePolicyStore]
[<CommonParameters>]
# 附带的参数
{
  -AssociatedNetFirewallInterfaceFilter <CimInstance> [<CommonParameters>]
  -AssociatedNetFirewallInterfaceTypeFilter <CimInstance> [<CommonParameters>]
  -AssociatedNetFirewallAddressFilter <CimInstance> [<CommonParameters>]
  -AssociatedNetFirewallApplicationFilter <CimInstance> [<CommonParameters>]
  -AssociatedNetFirewallProfile <CimInstance> [<CommonParameters>]
  -AssociatedNetFirewallServiceFilter <CimInstance> [<CommonParameters>]
  -AssociatedNetFirewallSecurityFilter <CimInstance> [<CommonParameters>]
  -AssociatedNetFirewallPortFilter <CimInstance> [<CommonParameters>]
}

# 重要参数
-PolicyStore # 规则的策略存储是防火墙和IPsec策略的容器,可接受值为 PersistentStore 、ActiveStore、RSOP、SystemDefaults、StaticServiceStore、ConfigurableServiceStore等参数值
  * PersistentStore:有时称为静态规则,此存储包含本地计算机的持久策略。此策略不是来自GPO的,而是在计算机上手动或以编程方式(在安装应用程序期间)创建的。在此存储中创建的规则将附加到ActiveStore并在计算机上立即被激活。
  * ActiveStore:此存储包含当前活动的策略,这是适用于计算机的所有策略存储的总和。这是本地计算机(适用于该计算机的所有GPO的总和)和本地存储(PersistentStore,静态Windows服务强化(WSH)和可配置的WSH)的结果策略集(RSOP)。---- GPO也是策略存储。可以按以下方式指定计算机GPO。------ -PolicyStore hostname。---- Active Directory GPO可以指定如下。------ -PolicyStore domain.fqdn.com\GPO_Friendly_Namedomain.fqdn.comGPO_Friendly_Name。------如下。-------- -PolicyStore localhost -------- -PolicyStore corp.contoso.com\FirewallPolicy ---- Active Directory中的GPO可以通过创建新的GPO cmdlet或组策略管理控制台。
  * RSOP:此只读存储包含应用于本地计算机的所有GPO的总和。
  * SystemDefaults:此只读存储包含WindowsServer®2012附带的防火墙规则的默认状态。
  * StaticServiceStore:此只读存储包含Windows Server 2012附带的所有服务限制。出于WFAS的目的,可选功能和与产品相关的功能被视为Windows Server 2012的一部分。
  * ConfigurableServiceStore:此读写存储包含为第三方服务添加的所有服务限制。此外,为Windows Store应用程序容器创建的网络隔离规则将显示在此策略存储中。默认值为PersistentStore

基础示例:

代码语言:javascript
复制
# 示例1.检索活动存储中的所有防火墙规则,该活动存储是适用于计算机的所有策略存储的集合。
PS C:\> Get-NetFirewallRule -PolicyStore ActiveStore
  # Name                  : WiFiDirect-KM-Driver-In-TCP
  # DisplayName           : 仅 WFD 驱动程序(TCP-In)
  # Description           : 驱动程序用于通过 WFD 进行通信的入站规则(TCP-In)
  # DisplayGroup          : WLAN 服务 - WFD 服务内核模式驱动程序规则
  # Group                 : @wlansvc.dll,-36865
  # Enabled               : True
  # Profile               : Any
  # Platform              : {}
  # Direction             : Inbound
  # Action                : Allow
  # EdgeTraversalPolicy   : Block
  # LooseSourceMapping    : False
  # LocalOnlyMapping      : False
  # Owner                 :
  # PrimaryStatus         : Inactive
  # Status                : 已从存储区成功分析规则。 (65536)
  # EnforcementStatus     : {DisabledInProfile, ProfileInactive}
  # PolicyStoreSource     : PersistentStore
  # PolicyStoreSourceType : Local
# - 此示例检索与活动存储中的所有规则关联的接口别名。
PS C:\> Get-NetFirewallRule -PolicyStore ActiveStore | Format-Table
  # Name                                                          DisplayName                                           DisplayGroup                                       Enabled Profile                 Direction Action
  # ----                                                          -----------                                           ------------                                       ------- -------                 --------- ------
  # SNMPTRAP-In-UDP                                               SNMP 陷阱服务(UDP In)                                 SNMP 陷阱                                          False   Private, Public         Inbound   Allow
  # SNMPTRAP-In-UDP-NoScope                                       SNMP 陷阱服务(UDP In)                                 SNMP 陷阱                                          False   Domain                  Inbound   Allow

# 示例2.检索范围scope为public公共概要文件的所有防火墙规则。
PS C:\> Get-NetFirewallProfile -Name Public | Get-NetFirewallRule
  # Name                  : PNRPMNRS-PNRP-In-UDP
  # DisplayName           : Windows 协作计算机名注册服务(PNRP-In)
  # Description           : 用于允许对等名称解析的 Windows 协作计算机名注册服务的入站规则。[UDP 3540]
  # DisplayGroup          : Windows 协作计算机名注册服务
  # Group                 : @FirewallAPI.dll,-34002
  # Enabled               : False
  # Profile               : Any
  # Platform              : {}
  # Direction             : Inbound
  # Action                : Allow
  # EdgeTraversalPolicy   : DeferToApp
  # LooseSourceMapping    : False
  # LocalOnlyMapping      : False
  # Owner                 :
  # PrimaryStatus         : OK
  # Status                : 已从存储区成功分析规则。 (65536)
  # EnforcementStatus     : NotApplicable
  # PolicyStoreSource     : PersistentStore
  # PolicyStoreSourceType : Local

# 示例3.指定显示或修改组中的防火墙规则关联的服务
# 方式1
PS C:\> $nfServiceFilter = Get-NetFirewallRule -Group "@FirewallAPI.dll,-30502" | Get-NetFirewallServiceFilter -Service Any
PS C:\> Set-NetFirewallServiceFilter -Service Ssdpsrv -InputObject $nfServiceFilter
This cmdlet can be run using only the pipeline.
# 方式2
PS C:\> Get-NetFirewallRule -Group "@FirewallAPI.dll,-30502" | Get-NetFirewallServiceFilter -Service Any | Set-NetFirewallServiceFilter -Service Ssdpsrv


# 示例4.防护墙各种规则查询
# (1) 防火墙规则名称进行过滤
Get-NetFirewallRule -Name 'WINRM*' | Select-Object Name 
# (2) 防火墙规则按照是否启用以及行为是允许还是阻塞,还有访问方向等条件进行过滤。
# -Action [NotConfigured, Allow, Block]
# -Direction [Inbound, Outbound]
# -Enabled [False,True]
Get-NetFirewallRule -Enabled False -Action Allow -Direction Inbound  | Select-Object Name 
# (3) 防火墙规则按照策略存储和总体状态进行过滤
# -PrimaryStatus [Unknown, OK, Inactive, Error]
Get-NetFirewallRule -PolicyStore SystemDefaults -PrimaryStatus ok | Select-Object Name 
# (4) 防火墙规则按照描述和显示组进行过滤
Get-NetFirewallRule -Description "*文件和打印机共享*" -DisplayGroup "*共享*" | Select-Object Name
  # Name
  # ----
  # FPS-NB_Session-In-TCP-NoScope
  # FPS-NB_Session-Out-TCP-NoScope
  # FPS-SMB-In-TCP-NoScope
  # FPS-SMB-Out-TCP-NoScope
  # FPS-NB_Name-In-UDP-NoScope
  # FPS-NB_Name-Out-UDP-NoScope
  # FPS-NB_Datagram-In-UDP-NoScope
  # FPS-NB_Datagram-Out-UDP-NoScope
  # FPS-SpoolSvc-In-TCP-NoScope
  # FPS-NB_Session-In-TCP
  # FPS-NB_Session-Out-TCP
  # FPS-SMB-In-TCP
  # FPS-SMB-Out-TCP
  # FPS-NB_Name-In-UDP
  # FPS-NB_Name-Out-UDP
  # FPS-NB_Datagram-In-UDP
  # FPS-NB_Datagram-Out-UDP
  # FPS-SpoolSvc-In-TCP
  # FPS-LLMNR-In-UDP
  # FPS-LLMNR-Out-UDP
  # FPSSMBD-iWARP-In-TCP
  # FPS-LLMNR-Out-UDP_1
  # FPS-LLMNR-In-UDP_1

# (5) 执行基于这些参数(端口,地址,安全性,接口和服务)的规则查询,则应检索带有带有Get动词的对应cmdlet的过滤器对象 (重点-重点-重点)
> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallAddressFilter
  # LocalAddress  : Any
  # RemoteAddress : Any

> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallPortFilter
  # Protocol      : TCP
  # LocalPort     : 445
  # RemotePort    : Any
  # IcmpType      : Any
  # DynamicTarget : Any

> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallInterfaceFilter
  # InterfaceAlias : Any

> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallInterfaceTypeFilter
  # InterfaceType : Any


> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallApplicationFilter # (查询该规则对应的软件包)
  # Program : System
  # Package :

> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallSecurityFilter
  # Authentication     : NotRequired
  # Encryption         : NotRequired
  # OverrideBlockRules : False
  # LocalUser          : Any
  # RemoteUser         : Any
  # RemoteMachine      : Any

> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallServiceFilter
  # Service : Any


> Get-NetFirewallRule -Name FPS-SMB-In-TCP-NoScope | Get-NetFirewallProfile
  # Name                            : Domain
  # Enabled                         : True
  # DefaultInboundAction            : NotConfigured
  # DefaultOutboundAction           : NotConfigured
  # AllowInboundRules               : NotConfigured
  # AllowLocalFirewallRules         : NotConfigured
  # AllowLocalIPsecRules            : NotConfigured
  # AllowUserApps                   : NotConfigured
  # AllowUserPorts                  : NotConfigured
  # AllowUnicastResponseToMulticast : NotConfigured
  # NotifyOnListen                  : True
  # EnableStealthModeForIPsec       : NotConfigured
  # LogFileName                     : %systemroot%\system32\LogFiles\Firewall\pfirewall.log
  # LogMaxSizeKilobytes             : 4096
  # LogAllowed                      : False
  # LogBlocked                      : False
  # LogIgnored                      : NotConfigured
  # DisabledInterfaceAliases        : {NotConfigured}

Set-NetFirewallRule 命令 - 设置目标计算现有防火墙规则。

描述:此cmdlet修改现有的防火墙规则属性,可以通过Name参数(默认),DisplayName参数或使用DisplayGroup或Group参数的组关联来修改一个或多个防火墙规则,如要将规则移至新的GPO,请使用带有NewPolicyStore参数的Copy-NetFirewallRule 复制现有规则然后使用此cmdlet删除旧规则。

Tips : 该 cmdlet 不能用于将对象添加到策略存储,只能在创建时使用Copy-NetFirewallRulecmdlet或New-NetFirewallRule cmdlet将对象添加到策略存储中。

语法参数:

代码语言:javascript
复制
Set-NetFirewallRule
   [ [-Name] <String[]> | -DisplayName <String[]> | -Group <String[]> | -InputObject <CimInstance[]> ]
   [-PolicyStore <String>]
   [-GPOSession <String>]
   [-NewDisplayName <String>]
   [-Description <String>]
   [-Enabled <Enabled>]
   [-Profile <Profile>]
   [-Platform <String[]>]
   [-Direction <Direction>]
   [-Action <Action>]
   [-EdgeTraversalPolicy <EdgeTraversal>]
   [-LooseSourceMapping <Boolean>]
   [-LocalOnlyMapping <Boolean>]
   [-Owner <String>]
   [-LocalAddress <String[]>]
   [-RemoteAddress <String[]>]
   [-Protocol <String>]
   [-LocalPort <String[]>]
   [-RemotePort <String[]>]
   [-IcmpType <String[]>]
   [-DynamicTarget <DynamicTransport>]
   [-Program <String>]
   [-Package <String>]
   [-Service <String>]
   [-InterfaceAlias <WildcardPattern[]>]
   [-InterfaceType <InterfaceType>]
   [-LocalUser <String>]
   [-RemoteUser <String>]
   [-RemoteMachine <String>]
   [-Authentication <Authentication>]
   [-Encryption <Encryption>]
   [-OverrideBlockRules <Boolean>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

# 参数说明
-Authentication # 指定防火墙规则上需要身份验证。此参数的可接受值为:NotRequired,Required或NoEncap。
-Direction      # 指定防火墙规则上进站和出站,可接收参数值为(Inbound, Outbound)

基础示例:

代码语言:javascript
复制
# 1.更改规则以匹配规则允许其通信的Web服务器的其他远程IP地址
Set-NetFirewallRule -DisplayName "AllowWeb80" -RemoteAddress "192.168.0.2"


# 2.启用预定义组中的所有规则。
# 方式1
Enable-NetFirewallRule -DisplayGroup "Windows Firewall Remote Management"
# 方式2
Set-NetFirewallRule -DisplayGroup "Windows Firewall Remote Management" -Enabled True


# 3.将规则更改为要求身份验证,并确定该规则的范围以应用于域配置文件。
# 必须存在单独的IPsec规则才能执行身份验证。
Set-NetFirewallRule -DisplayName "AllowMessenger" -Authentication Required -Profile Domain

# 4.允许子网主机通过ICMP4进行ping操作即允许icmp协议通过;
Enable-NetFirewallRule -Name FPS-ICMP4-ERQ-In # Winserver
Enable-NetFirewallRule -Name CoreNet-Diag-ICMP4-EchoRequest-In  # Win10
Set-NetFirewallRule -Name CoreNet-Diag-ICMP4-EchoRequest-In -RemoteAddress LocalSubnet -Enable True

Get-NetFirewallAddressFilter 命令 - 从目标计算机修改地址筛选器对象。

描述: 该 cmdlet 返回地址过滤器对象与输入规则相关联,地址过滤器对象表示与输入规则关联的本地和远程地址,单个规则的LocalAddressRemoteAddress参数在单独的NetFirewallAddressFilter对象中表示

Tips: 修改规则地址的两种方式即过管道传递到Set-NetFirewallRule,Set-NetIPsecRule或Set-NetIPsecMainModeRule cmdlet中便可以配置地址属性。

语法参数:

代码语言:javascript
复制
Get-NetFirewallAddressFilter [-All] [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] [<CommonParameters>]

Get-NetFirewallAddressFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] -AssociatedNetFirewallRule <CimInstance> [<CommonParameters>]

Get-NetFirewallAddressFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] -AssociatedNetIPsecMainModeRule <CimInstance> [<CommonParameters>]

Get-NetFirewallAddressFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] -AssociatedNetIPsecRule <CimInstance> [<CommonParameters>]

Set-NetFirewallAddressFilter 命令 - 从目标计算机检索地址筛选器对象。

描述: 该命令修改地址筛选器对象,从而修改防火墙、IPsec和主模式规则的本地和远程地址条件。

语法参数:

代码语言:javascript
复制
Set-NetFirewallAddressFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-LocalAddress <String[]>] [-PassThru] [-PolicyStore <String>] [-RemoteAddress <String[]>] [-ThrottleLimit <Int32>] [-Confirm] [-WhatIf] [<CommonParameters>]

Set-NetFirewallAddressFilter [-AsJob] [-CimSession <CimSession[]>] [-LocalAddress <String[]>] [-PassThru] [-RemoteAddress <String[]>] [-ThrottleLimit <Int32>] -InputObject <CimInstance[]> [-Confirm] [-WhatIf] [<CommonParameters>]

# 参数:
-CimSession : # 在远程会话或远程计算机上运行cmdlet,输入计算机名称或会话对象默认值为本地计算机上的当前会话。例如 New-CimSession 或 Get-CimSession cmdlet 的输出。
-GPOSession : # 从中检索要修改的规则的网络GPO。该参数的使用方式与PolicyStore参数相同。在WindowsPowerShell®中修改GPO时,对GPO的每次更改都需要加载,修改和保存整个GPO。

基础示例:

代码语言:javascript
复制
# 1.获取与特定IPsec规则关联的地址配置。
Get-NetFirewallRule -DisplayName "*ICMP*" | Get-NetFirewallAddressFilter | Where-Object -FilterScript { $_.RemoteAddress -Eq "LocalSubnet6" }
  # LocalAddress  : fe80::/64
  # RemoteAddress : {LocalSubnet6, ff02::1, fe80::/64}

  # LocalAddress  : Any
  # RemoteAddress : {LocalSubnet6, ff02::2, fe80::/64}

# 2.改特定IPsec规则的第一个端点本地地址。
Set-NetIPsecRule -DisplayName "Tunnel Rule" -LocalAddress Any
# - Alternatively(或者), this task can be done with the following cmdlets.
$nfwAddressFilter = ( Get-NetIPsecRule -DisplayName "Tunnel Rule" | Get-NetFirewallAddressFilter )
Set-NetFirewallAddressFilter -InputObject $nfwAddressFilter -LocalAddress Any
# - Alternatively(或者), this task can be done with the following cmdlet.
Get-NetIPsecRule -DisplayName "Tunnel Rule" | Get-NetFirewallAddressFilter | Set-NetFirewallAddressFilter -LocalAddress Any


# 3.使用防火墙规则关联的筛选器对象并修改这些规则的远程地址为LocalSubnet4。
Get-NetFirewallRule -Name "CoreNet-Diag-ICMP4-EchoRequest-In" | Get-NetFirewallAddressFilter | Where-Object -FilterScript { $_.RemoteAddress -Eq "LocalSubnet6" } | Set-NetFirewallAddressFilter -RemoteAddress LocalSubnet4
# 这是此cmdlet的替代方法。
Get-NetFirewallRule -Name "CoreNet-Diag-ICMP4-EchoRequest-In" | Get-NetFirewallAddressFilter | Where-Object -FilterScript { $_.RemoteAddress -Eq "LocalSubnet6" } | Get-NetFirewallRule | Set-NetFirewallRule -RemoteAddress LocalSubnet4

Get-NetFirewallPortFilter 命令 - 从目标计算机检索端口筛选器对象。

描述: 该cmdlet返回端口过滤器对象与输入规则相关联,端口筛选器对象表示与防火墙和IPsec规则关联的端口和协议条件。单个规则中表示的单个规则的Protocol,LocalPort,RemotePort,IcmpType和DynamicTransport参数在单独的NetFirewallPortFilter对象中表示;

语法参数:

代码语言:javascript
复制
Get-NetFirewallPortFilter [-All] [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] [<CommonParameters>]

Get-NetFirewallPortFilter [-AsJob] [-CimSession <CimSession[]>] [-DynamicTarget <DynamicTransport[]>] [-GPOSession <String>] [-PolicyStore <String>] [-Protocol <String[]>] [-ThrottleLimit <Int32>] [<CommonParameters>]

Get-NetFirewallPortFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] -AssociatedNetFirewallRule <CimInstance> [<CommonParameters>]

Get-NetFirewallPortFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] -AssociatedNetIPsecRule <CimInstance> [<CommonParameters>]

# 参数:
-DynamicTarget : 指定动态传输的数组可接受值 `[ other,ProximityApps,ProximitySharing,WifiDirectPrinting,WifiDirectDisplay,WifiDirectDevices]`
-Protocol : 指定具有匹配IP地址的网络数据包与此规则匹配。值`[ TCP, UDP, ICMPv4, or ICMPv6. ]`

Set-NetFirewallPortFilter 命令 - 从目标计算机修改端口筛选器对象。

描述: 修改端口筛选器对象从而使用防火墙或IPsec规则的Protocol,LocalPort,RemotePort,IcmpType和DynamicTransport参数修改协议和端口条件

语法参数:

代码语言:javascript
复制
Set-NetFirewallPortFilter [-AsJob] [-CimSession <CimSession[]>] [-DynamicTarget <DynamicTransport>] [-GPOSession <String>] [-IcmpType <String[]>] [-LocalPort <String[]>] [-PassThru] [-PolicyStore <String>] [-Protocol <String>] [-RemotePort <String[]>] [-ThrottleLimit <Int32>] [-Confirm] [-WhatIf] [<CommonParameters>]

Set-NetFirewallPortFilter [-AsJob] [-CimSession <CimSession[]>] [-DynamicTarget <DynamicTransport>] [-IcmpType <String[]>] [-LocalPort <String[]>] [-PassThru] [-Protocol <String>] [-RemotePort <String[]>] [-ThrottleLimit <Int32>] -InputObject <CimInstance[]> [-Confirm] [-WhatIf] [<CommonParameters>]

基础示例:

代码语言:javascript
复制
# 1.获取特定防火墙规则的端口属性
Get-NetFirewallPortFilter -PolicyStore ActiveStore | Format-Table -Property * # 检索与活动存储中的所有规则关联的端口条件
Get-NetFirewallPortFilter | more
  # Protocol      : UDP
  # LocalPort     : 162
  # RemotePort    : Any
  # IcmpType      : Any
  # DynamicTarget : Any
Get-NetFirewallRule -Name "CoreNet-Diag-ICMP4-EchoRequest-In" | Get-NetFirewallPortFilter       
  # Protocol      : ICMPv4
  # LocalPort     : RPC
  # RemotePort    : Any
  # IcmpType      : 8
  # DynamicTarget : Any


# 2.修改指定防火墙规则的LocalPort参数值。
# 方式1.此cmdlet可以在没有管道的情况下运行。
Set-NetFirewallRule -DisplayName "Play To streaming server" -LocalPort 10246
# 方式2.此cmdlet只能使用管道运行。
Get-FirewallRule -DisplayName "Play To streaming server" | Get-NetFirewallPortFilter | Set-NetFirewallPortFilter -LocalPort 10246
# 筛选防火墙规则关联的端口并进行设置本地端口
Get-NetFirewallPortFilter | Where-Object -Property LocalPort -EQ 10246 | Set-NetFirewallPortFilter -LocalPort Any
Get-NetFirewallRule -DisplayGroup "File and Printer Sharing" | Get-NetFirewallPortFilter | Where-Object -Property { $_.RemotePort -Eq "137" } | Set-NetFirewallPortFilter -LocalPort Any
# - 方式3.
$nfPortFilter = Get-FirewallRule -DisplayName "Play To streaming server" | Get-NetFirewallPortFilter
Set-NetFirewallPortFilter -LocalPort 10246 -InputObject $nfPortFilter


# 3.找到允许ProximitySharing的内置网络隔离规则并对其进行阻止从而禁止邻近配对。
Get-NetFirewallPortFilter -DynamicTransport ProximitySharing | Get-NetFirewallRule | Set-NetFirewall -Action Block
  # Name                  : ProximityUxHost-Sharing-In-TCP-NoScope
  # DisplayName           : 通过 TCP 的邻近感应共享(TCP sharing-In)
  # Description           : 通过 TCP 的邻近感应共享的入站规则
  # DisplayGroup          : 邻近感应共享
  # Group                 : @FirewallAPI.dll,-36251
  # Enabled               : True
  # Profile               : Any
  # Platform              : {}
  # Direction             : Inbound
  # Action                : Allow
  # EdgeTraversalPolicy   : Block
  # LooseSourceMapping    : False
  # LocalOnlyMapping      : False
  # Owner                 :
  # PrimaryStatus         : OK
  # Status                : 已从存储区成功分析规则。 (65536)
  # EnforcementStatus     : NotApplicable
  # PolicyStoreSource     : PersistentStore
  # PolicyStoreSourceType : Local

Get-NetFirewallInterfaceFilter 命令 - 从目标计算机检索接口筛选器对象

Set-NetFirewallInterfaceFilter 命令 - 从目标计算机设置接口筛选器对象

Get-NetFirewallInterfaceTypeFilter 命令- 从目标计算机检索接口类型筛选器对象

Set-NetFirewallInterfaceTypeFilter 命令- 修改接口类型筛选器对象从而修改防火墙或IPsec规则的接口类型条件

描述: 接口筛选器对象表示与防火墙和IPsec规则关联的接口。

基础语法:

代码语言:javascript
复制
#  - 从目标计算机检索接口筛选器对象。
Get-NetFirewallInterfaceFilter [-All] [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] [<CommonParameters>] -AssociatedNetIPsecRule <CimInstance> [<CommonParameters>]

# - 从目标计算机检索接口类型筛选器对象
Get-NetFirewallInterfaceTypeFilter [-All] [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] [<CommonParameters>]  -AssociatedNetIPsecRule <CimInstance>  -AssociatedNetFirewallRule <CimInstance>  [-InterfaceType <InterfaceType[]>]

# - 从目标计算机设置接口筛选器对象。
Set-NetFirewallInterfaceFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-InterfaceAlias <String[]>] [-PassThru] [-PolicyStore <String>] -InputObject <CimInstance[]>  [-ThrottleLimit <Int32>] [-Confirm] [-WhatIf] [<CommonParameters>] 

# - 修改接口类型筛选器对象从而修改防火墙或IPsec规则的接口类型条件
Set-NetFirewallInterfaceTypeFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-InterfaceType <InterfaceType>] [-PassThru] [-PolicyStore <String>] [-ThrottleLimit <Int32>] [-Confirm] [-WhatIf]  [<CommonParameters>] -InputObject <CimInstance[]>


# 参数说明:
- InterfaceAlias : String[] 类型可选值: 
- InterfaceType : 指定只有通过指定接口类型进行的网络连接才受此规则要求的约束,此参数的可接受值为:Any(默认值)、Wired、Wireless或RemoteAccess。

基础示例:

代码语言:javascript
复制
# 0.检索与活动存储中的所有规则关联的接口别名和接口类型。
Get-NetFirewallInterfaceFilter -PolicyStore ActiveStore
  # InterfaceAlias : Any
Get-NetFirewallInterfaceTypeFilter -PolicyStore ActiveStore
  # InterfaceType : Any

# 1.查看`CoreNet-Diag-ICMP4-EchoRequest-In`规则的接口范围
Get-NetFirewallRule -Name "CoreNet-Diag-ICMP4-EchoRequest-In" | Get-NetFirewallInterfaceFilter
  # InterfaceAlias : Any

# 2.查看`CoreNet-Diag-ICMP4-EchoRequest-In`规则的接口类型
Get-NetFirewallRule -Name "CoreNet-Diag-ICMP4-EchoRequest-In" | Get-NetFirewallInterfaceTypeFilter
  # InterfaceType : Any
Get-NetFirewallInterfaceTypeFilter -InterfaceType Wired | Set-NetFirewallInterfaceTypeFilter -InterfaceType Any

# 3.设置修改特定防火墙规则的接口别名字段
Set-NetFirewallRule -DisplayName "Contoso Messenger" -InterfaceAlias Any

Get-NetFirewallRule -DisplayName "Contoso Messenger" | Get-NetFirewallInterfaceFilter | Set-NetFirewallInterfaceFilter -InterfaceAlias Any

Get-NetIPsecRule -Group "Wired Rules" | Get-NetFirewallInterfaceFilter | Where-Object -FilterScript { $_.InterfaceAlias -Eq "Wired2" } | Set-NetFirewallInterfaceFilter -InterfaceAlias Wired3

$nfwInterfaceFilter = (Get-FirewallRule -DisplayName "Contoso Messenger" | Get-NetFirewallInterfaceFilter)
$nfwInterfaceFilterWired2 = (Where-Object -Property { $_.InterfaceAlias -Eq "Wired2" } -InputObject $nfwInterfaceFilter)
Set-NetFirewallInterfaceFilter -InterfaceAlias Any -InputObject $nfwInterfaceFilter
Set-NetFirewallInterfaceFilter -InterfaceAlias Wired3 -InputObject $nfwInterfaceFilterWired2

# 4.设置修改特定防火墙规则的接口类型字段
# 方式1
Set-NetFirewallRule -DisplayName "Contoso Messenger" -InterfaceType Any
# 方式2
Get-FirewallRule -DisplayName "Contoso Messenger" | Get-NetFirewallInterfaceTypeFilter | Set-NetFirewallInterfaceTypeFilter -InterfaceType Any
# 方式3
$nfwInterfaceTypeFilter = ( Get-NetIPsecRule -Group DirectAccess | Get-NetFirewallInterfaceTypeFilter )
$nfwInterfaceTypeFilterWired = Where-Object -Property { $_.InterfaceType -Eq "Wired" } -InputObject $nfwInterfaceTypeFilter
Set-NetFirewallInterfaceTypeFilter -InterfaceType RemoteAccess -InputObject $nfwInterfaceTypeFilterWired
# 方式4
Get-NetIPsecRule -Group DirectAccess | Get-NetFirewallInterfaceTypeFilter | Where-Object -Property { $_.InterfaceType -Eq "Wired" } | Set-NetFirewallInterfaceTypeFilter -InterfaceType RemoteAccess

Get-NetFirewallSecurityFilte 命令 - 从目标计算机检索安全筛选器对象

Set-NetFirewallSecurityFilte 命令 - 从目标计算机设置安全筛选器对象

描述: 该 cmdlet 返回的安全过滤器对象与输入防火墙规则有关,安全筛选器对象表示与防火墙规则关联的安全条件。 Tips: 单个规则的Authentication,Encryption,OverrideBlockRules,LocalUser,RemoteUser和RemoteMachine参数在单独的NetFirewallSecurityFilter对象中表示,并且过滤器与规则的关系始终是一对一的并且会自动进行管理。

语法参数:

代码语言:javascript
复制
# - 从目标计算机检索安全筛选器对象
Get-NetFirewallSecurityFilter [-All] [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] [<CommonParameters>]
Get-NetFirewallSecurityFilter [-AsJob] [-Authentication <Authentication[]>] [-CimSession <CimSession[]>] [-Encryption <Encryption[]>] [-GPOSession <String>] [-LocalUser <String[]>] [-OverrideBlockRules <Boolean[]>] [-PolicyStore <String>] [-RemoteMachine <String[]>]
Get-NetFirewallSecurityFilter [-AsJob] [-CimSession <CimSession[]>] [-GPOSession <String>] [-PolicyStore <String>] [-ThrottleLimit <Int32>] -AssociatedNetFirewallRule <CimInstance> [<CommonParameters>]

# - 从目标计算机设置安全筛选器对象
Set-NetFirewallSecurityFilter [-AsJob] [-Authentication <Authentication>] [-CimSession <CimSession[]>] [-Encryption <Encryption>] [-GPOSession <String>] [-LocalUser <String>] [-OverrideBlockRules <Boolean>] [-PassThru] [-PolicyStore <String>] [-RemoteMachine <String>] [-RemoteUser <String>] [-ThrottleLimit <Int32>] [-Confirm] [-WhatIf] [<CommonParameters>]

Set-NetFirewallSecurityFilter [-AsJob] [-Authentication <Authentication>] [-CimSession <CimSession[]>] [-Encryption <Encryption>] [-LocalUser <String>] [-OverrideBlockRules <Boolean>] [-PassThru] [-RemoteMachine <String>] [-RemoteUser <String>] [-ThrottleLimit <Int32>] -InputObject <CimInstance[]> [-Confirm] [-WhatIf] [<CommonParameters>]

# 参数说明
-Authentication  # 指定防火墙规则需要身份验证。此参数的可接受值为:NotRequired、Required或NoEncap。
-Encryption  # 指定防火墙规则上需要身份验证中的加密,身份验证通过单独的IPsec或主模式规则完成。此参数的可接受值为:NotRequired、Required或Dynamic。

基础示例:

代码语言:javascript
复制
# 1.索与活动存储中的所有防火墙规则关联的安全性条件
$ Get-NetFirewallSecurityFilter -PolicyStore ActiveStore | Format-Table -Property *
Authentication  Encryption LocalUser    RemoteUser RemoteMachine Caption Description ElementName InstanceID          CommunicationStatus Detailed  Status
--------------  ---------- ---------     ---------- ------------- ------- ----------- ----------- ----------         ------------------- --------
NotRequired NotRequired Any                                                                    Any        Any        SNMPTRAP-In-UDP
NotRequired NotRequired Any                                                                    Any        Any        {CC0329A5-245B-405C-80F3-0D4001663753}
NotRequired NotRequired Any                                                                    Any        Any        HNS Container Networking - DNS (UDP-In) - 9BBB3B53-0C52-47CB-9BC5-5E7AC5A9EE8F - 0
NotRequired NotRequired O:LSD:(A;;CC;;;S-1-5-92-3339056971-1291069075-3798698925-2882100687-0) Any        Any        WFDPRINT-DAFWSD-In-Active

# 2.获取特定防火墙规则的 安全属性 和所有经过身份验证的 旁路规则 
Get-NetFirewallRule -DisplayName "Contoso Messenger" | Get-NetFirewallSecurityFilter
Get-NetFirewallSecurityFilter -OverrideBlockRules $True | Get-NetFirewallRule
# - 获取要求指定用户组进行身份验证的防火墙规则。
Get-NetFirewallSecurityFilter -Authentication Required | Where-Object -Property { $_.RemoteUser -Eq "$secureUserGroupSDDL" } | Get-NetFirewallRule

# 3.通过请求经过身份验证的通信量来修改与打印机防火墙规则关联的安全条件。
$nfSecurityFilter = Get-NetFirewallRule -DisplayGroup "*Printer*" | Get-NetFirewallSecurityFilter
Set-NetFirewallSecurityFilter -Authentication Request -InputObject $nfSecurityFilter

Get-NetFirewallRule -DisplayGroup "*Printer*" | Get-NetFirewallSecurityFilter | Set-NetFirewallSecurityFilter -Authentication Request

# 4.修改特定防火墙规则的用户字段
$users = New-Object -TypeName System.Security.Principal.NTAccount ("corp.contoso.com\Administrators")
$SIDofSecureUserGroup = $users.Translate([System.Security.Principal.SecurityIdentifier]).Value
$SecureMachineGroupSDDL = "D:(A;;CC;;; $SIDofSecureUserGroup)"
$nfSecurityFilter = Get-FirewallRule -DisplayName "Authorize Secure Computer" | Get-NetFirewallSecurityFilter
Set-NetFirewallSecurityFilter -RemoteMachine $SecureMachineGroupSDDL -InputObject $nfSecurityFilter

# - This cmdlet can be run using only the pipeline.
Get-FirewallRule -DisplayName "Authorize Secure Computer" | Get-NetFirewallSecurityFilter | Set-NetFirewallSecurityFilter -RemoteMachine $SecureMachineGroupSDDL

# - This cmdlet can be run without the pipeline.
Set-NetFirewallRule -DisplayName "Authorize Secure Computer" -RemoteMachine $SecureMachineGroupSDDL

Get-NetFirewallServiceFilter 命令 - 从目标计算机检索服务筛选器对象。

Set-NetFirewallServiceFilter 命令 - 从目标计算机设置服务筛选器对象

描述: 服务筛选器对象代表与防火墙规则关联的Windows服务,单个规则的Service参数在单独的NetFirewallServiceFilter 对象中表示,过滤器与规则之间的关系始终是一对一的并且会自动进行管理。

Tips: 只能使用过滤器对象查询与过滤器关联的规则参数。

基础示例:

代码语言:javascript
复制
# 1.检索与活动存储中的所有防火墙规则关联的服务条件(默认是检索持久性存储)
Get-NetFirewallServiceFilter -PolicyStore ActiveStore
  # Service : SNMPTRAP
  # Service : SNMPTRAP
  # Service : Any
# - 从持久性存储中获取与dnscache服务关联的禁用防火墙规则。
Get-NetFirewallServiceFilter -Service dnscache | Get-NetFirewallRule | Where-Object -Property { $_.Enabled -Eq "False" }

# 2.获取与使用本地化名称指定的防火墙规则关联的服务
Get-NetFirewallRule -DisplayName "Wireless Portable Devices" | Get-NetFirewallServiceFilter

Get-NetFirewallApplicationFilter 命令 - 从目标计算机检索应用程序筛选器对象。

Set-NetFirewallApplicationFilter 命令 - 从目标计算机修改防火墙规则的程序和程序包条件。

描述: 应用程序过滤器对象代表与防火墙规则关联的应用程序,单个规则的“程序”和“程序包”参数在单独的Get-NetFirewallApplicationFilter对象中表示,过滤器与规则的关系始终是一对一的并且会自动进行管理。

基础语法:

代码语言:javascript
复制
# - 从目标计算机检索应用程序筛选器对象。
Get-NetFirewallApplicationFilter
  [-All] | -AssociatedNetFirewallRule <CimInstance>
  [-Program <String[]>]
  [-Package <String[]>]
  [-PolicyStore <String>]
  [-GPOSession <String>]
  [-CimSession <CimSession[]>]
  [-ThrottleLimit <Int32>]
  [-AsJob]
  [<CommonParameters>]

# - 从目标计算机修改防火墙规则的程序和程序包条件。
Set-NetFirewallApplicationFilter
   [-PolicyStore <String>]
   [-GPOSession <String>]
   [-Program <String>]
   [-Package <String>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [-PassThru]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

基础示例:

代码语言:javascript
复制
# 1.检索与活动存储中的所有规则关联的应用程序。
Get-NetFirewallApplicationFilter -PolicyStore ActiveStore | Where-Object { $_.Package -ne '' }
  # Program : Any
  # Package :
  # Program : C:\WINDOWS\system32\svchost.exe
  # Package :
# - 根据 Program 条件过滤防火墙规则;
Get-NetFirewallApplicationFilter | Where-Object { $_.Program -like 'C:\windows\system32\ftp.exe' }
  # Program : C:\windows\system32\ftp.exe
  # Package :


# 2.获取与特定防火墙规则关联的应用程序配置。
Get-NetFirewallRule -DisplayName "Contoso Messenger" | Get-NetFirewallApplicationFilter


# 3.更改与特定防火墙规则关联的应用程序路径。
# 方式1
Get-NetFirewallRule -DisplayName "Contoso Messenger" | Get-NetFirewallApplicationFilter | Set-NetFirewallApplicationFilter -Program %SystemRoot%\System32\messenger.exe
# 方式2
Set-NetFirewallRule -DisplayName "Contoso Messenger" -Program %SystemRoot%\System32\messenger.exe


# 4.修改与Windows Store应用程序的所有相关防火墙规则关联的程序包
$NewPackageSDDL = "S-1-15-2-4292807980-2381230043-3108820062-1451069988-2614848061-670482394-695399705"

# 方式1
Get-NetFirewallRule -Group Socialite | Get-NetFirewallApplicationFilter | Set-NetFirewallAddressFilter -Package $NewPackageSDDL

# 方式2
$nfwApplicationFilter = (Get-NetFirewallRule -Group Socialite | Get-NetFirewallApplicationFilter )
Set-NetFirewallAddressFilter - InputObject $nfwApplicationFilter -Package $NewPackageSDDL

# 方式3
Get-NetFirewallRule -Group Socialite | Get-NetFirewallApplicationFilter | Set-NetFirewallAddressFilter -Package $NewPackageSDDL

Enable-NetFirewallRule 命令 - 启用以前禁用的防火墙规则

描述: Enable-NetFirewallRule cmdlet使以前禁用的防火墙规则在计算机或组策略组织单位中处于活动状态。 此cmdlet使用Name参数(默认)、DisplayName参数、规则属性或关联的筛选器或对象获取要启用的一个或多个防火墙规则。

Disable-NetFirewallRule 命令 - 禁用当前已启用的防火墙规则

描述: 重要说明不带参数运行此cmdlet将禁用目标计算机上的所有Windows防火墙规则,将禁用先前启用的防火墙规则在计算机或组策略组织单位内无效。禁用规则不会主动修改系统行为,但是该规则仍存在于计算机或组策略对象(GPO)中因此可以重新启用它。

基础语法:

代码语言:javascript
复制
# - 启用以前禁用的防火墙规则
Enable-NetFirewallRule [-Name] <String[]>  -DisplayName <String[]>  [-Action <Action[]>] [-AsJob] [-CimSession <CimSession[]>] [-Description <String[]>] [-Direction <Direction[]>] [-DisplayGroup <String[]>] [-EdgeTraversalPolicy <EdgeTraversal[]>] [-Enabled <Enabled[]>] [-GPOSession <String>] [-Group <String[]>] [-LocalOnlyMapping <Boolean[]>] [-LooseSourceMapping <Boolean[]>] [-Owner <String[]>] [-PassThru] [-PolicyStore <String>] [-PolicyStoreSource <String[]>] [-PolicyStoreSourceType <PolicyStoreType[]>] [-PrimaryStatus <PrimaryStatus[]>] [-Status <String[]>] [-ThrottleLimit <Int32>] [-TracePolicyStore] [-Confirm] [-WhatIf] [<CommonParameters>] -InputObject <CimInstance[]>


# - 禁用当前已启用的防火墙规则
Disable-NetFirewallRule
  [-Name] <String[]>  | -DisplayName <String[]>
  [-Description <String[]>]
  [-DisplayGroup <String[]>]
  [-Group <String[]>]
  [-Enabled <Enabled[]>]
  [-Direction <Direction[]>]
  [-Action <Action[]>]
  [-EdgeTraversalPolicy <EdgeTraversal[]>]
  [-LooseSourceMapping <Boolean[]>]
  [-LocalOnlyMapping <Boolean[]>]
  [-Owner <String[]>]
  [-PrimaryStatus <PrimaryStatus[]>]
  [-Status <String[]>]
  [-PolicyStoreSource <String[]>]
  [-PolicyStoreSourceType <PolicyStoreType[]>]
  [-PolicyStore <String>]
  [-GPOSession <String>]
  [-TracePolicyStore]
  [-CimSession <CimSession[]>]
  [-ThrottleLimit <Int32>]
  [-AsJob]
  [-PassThru]
  [-WhatIf]
  [-Confirm]
  [<CommonParameters>]

{
  -AssociatedNetFirewallAddressFilter <CimInstance>       # 获取与要启用的给定地址过滤器关联的防火墙规则
  -AssociatedNetFirewallApplicationFilter <CimInstance>   # 获取与要启用的给定应用程序筛选器关联的防火墙规则
  -AssociatedNetFirewallInterfaceFilter <CimInstance>     # 获取与要启用的给定接口筛选器关联的防火墙规则
  -AssociatedNetFirewallInterfaceTypeFilter <CimInstance> # 获取与要启用的给定接口类型过滤器关联的防火墙规则
  -AssociatedNetFirewallPortFilter <CimInstance>        # 获取与要启用的给定端口筛选器关联的防火墙规则
  -AssociatedNetFirewallSecurityFilter <CimInstance>    # 获取与要启用的给定安全筛选器关联的防火墙规则
  -AssociatedNetFirewallServiceFilter <CimInstance>     # 获取与要启用的给定服务筛选器关联的防火墙规则
  -AssociatedNetFirewallProfile <CimInstance>           # 获取与要启用的给定防火墙配置文件类型关联的防火墙规则
}

# 参数解析:
-EdgeTraversalPolicy : 指定启用指示的边缘遍历策略的匹配防火墙规则; # Block,Allow,DeferToUser或DeferToApp。

基础示例:

代码语言:javascript
复制
# 1.启用|禁用所有文件和打印机共享规则。使用通用的世界范围内的间接字符串@FirewallAPI来指定组。
Enable-NetFirewallRule -Group "@FirewallAPI.dll,-28502"
Disable-NetFirewallRule -Group "@FirewallAPI.dll,-28502"
Enable-NetFirewallRule -DisplayName "Network Discovery"
Disable-NetFirewallRule -DisplayName "Network Discovery"

# 2.在指定的GPO中启用所有以前启用的出站防火墙规则。
Enable-NetFirewallRule -Direction Outbound -PolicyStore contoso.com\gpo_name

# 3.在计算机上启用动态防火墙规则。(禁用一样将Enable改成disable即可)
# 方式1
$nfwRule = Get-NetFirewallRule -PolicyStore ActiveStore -PolicyStoreSourceType Dynamic
Enable-NetFirewallRule -InputObject $nfwRule
# 方式2
Get-NetFirewallRule -PolicyStore ActiveStore -PolicyStoreSourceType Dynamic | Enable-NetFirewallRule
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-12-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0x00 Windows 防火墙规则(NetFirewall)查看配置
    • New-NetFirewallRule 命令 - 添加一个新的防火墙规则
      • Get-NetFirewallRule 命令 - 从目标计算机检索防火墙规则。
        • Set-NetFirewallRule 命令 - 设置目标计算现有防火墙规则。
          • Get-NetFirewallAddressFilter 命令 - 从目标计算机修改地址筛选器对象。
            • Set-NetFirewallAddressFilter 命令 - 从目标计算机检索地址筛选器对象。
              • Get-NetFirewallPortFilter 命令 - 从目标计算机检索端口筛选器对象。
                • Set-NetFirewallPortFilter 命令 - 从目标计算机修改端口筛选器对象。
                  • Get-NetFirewallInterfaceFilter 命令 - 从目标计算机检索接口筛选器对象
                    • Set-NetFirewallInterfaceFilter 命令 - 从目标计算机设置接口筛选器对象
                      • Get-NetFirewallInterfaceTypeFilter 命令- 从目标计算机检索接口类型筛选器对象
                        • Set-NetFirewallInterfaceTypeFilter 命令- 修改接口类型筛选器对象从而修改防火墙或IPsec规则的接口类型条件
                          • Get-NetFirewallSecurityFilte 命令 - 从目标计算机检索安全筛选器对象
                            • Set-NetFirewallSecurityFilte 命令 - 从目标计算机设置安全筛选器对象
                              • Get-NetFirewallServiceFilter 命令 - 从目标计算机检索服务筛选器对象。
                                • Set-NetFirewallServiceFilter 命令 - 从目标计算机设置服务筛选器对象
                                  • Get-NetFirewallApplicationFilter 命令 - 从目标计算机检索应用程序筛选器对象。
                                    • Set-NetFirewallApplicationFilter 命令 - 从目标计算机修改防火墙规则的程序和程序包条件。
                                      • Enable-NetFirewallRule 命令 - 启用以前禁用的防火墙规则
                                        • Disable-NetFirewallRule 命令 - 禁用当前已启用的防火墙规则
                                        相关产品与服务
                                        多因子身份认证
                                        多因子身份认证(Multi-factor Authentication Service,MFAS)的目的是建立一个多层次的防御体系,通过结合两种或三种认证因子(基于记忆的/基于持有物的/基于生物特征的认证因子)验证访问者的身份,使系统或资源更加安全。攻击者即使破解单一因子(如口令、人脸),应用的安全依然可以得到保障。
                                        领券
                                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档