我们为某些程序定义了一个新的Windows防火墙规则,用于在某个端口上接受入站TCP连接。这可以使用netsh.exe实用程序或Powershell NetFirewallRule来完成。例如,下面的示例命令允许notepad.exe接受端口5001上的TCP连接(我知道记事本不能这样做):
New-NetFirewallRule -program "C:\windows\System32\notepad.exe" -direction Inbound -Action Allow -Protocol tcp -LocalPort 5001 -Name "Testing Notepad on port 5001" -DisplayName "Testing Notepad on port 5001"
要检索/查看此规则,可以再次使用netsh.exe或Get-NetFirewallRule。
理想情况下,我们希望使用Powershell Get-NetFirewallRule,但我们无法查看创建规则时使用的实际程序路径。
下面是netsh.exe的输出:
netsh advfirewall firewall show rule name="Testing Notepad on port 5001" verbose
Rule Name: Testing Notepad on port 5001
----------------------------------------------------------------------
Enabled: Yes
Direction: In
Profiles: Domain,Private,Public
Grouping:
LocalIP: Any
RemoteIP: Any
Protocol: TCP
LocalPort: 5001
RemotePort: Any
Edge traversal: No
Program: C:\windows\System32\notepad.exe
InterfaceTypes: Any
Security: NotRequired
Rule source: Local Setting
Action: Allow
Ok.
以下是Get-NetFirewallRule的输出:
Get-NetFirewallRule -Name "Testing Notepad on port 5001" | Format-list *
Name : Testing Notepad on port 5001
ID : Testing Notepad on port 5001
Group :
Platform : {}
LSM : False
DisplayName : Testing Notepad on port 5001
Enabled : True
Profile : Any
Direction : Inbound
Action : Allow
EdgeTraversalPolicy : Block
PrimaryStatus : OK
Status : The rule was parsed successfully from the store.
(65536)
EnforcementStatus : NotApplicable
PolicyStoreSourceType : Local
Caption :
Description :
ElementName : Testing Notepad on port 5001
InstanceID : Testing Notepad on port 5001
CommonName :
PolicyKeywords :
PolicyDecisionStrategy : 2
PolicyRoles :
ConditionListType : 3
CreationClassName : MSFT|FW|FirewallRule|Testing Notepad on port 5001
ExecutionStrategy : 2
Mandatory :
PolicyRuleName :
Priority :
RuleUsage :
SequencedActions : 3
SystemCreationClassName :
SystemName :
DisplayGroup :
LocalOnlyMapping : False
LooseSourceMapping : False
Owner :
Platforms : {}
PolicyStoreSource : PersistentStore
Profiles : 0
RuleGroup :
StatusCode : 65536
PSComputerName :
CimClass : root/standardcimv2:MSFT_NetFirewallRule
CimInstanceProperties : {Caption, Description, ElementName, InstanceID...}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemPropertieses
对于使用Powershell cmdlet检索程序路径、端口、协议等,有什么建议或想法吗?
发布于 2016-02-13 22:37:59
为此,您应该使用Get-NetFirewall*Filter
cmdlet。
PS> Get-Command Get-NetFirewall*Filter
CommandType Name Version Source
----------- ---- ------- ------
Function Get-NetFirewallAddressFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallApplicationFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallInterfaceFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallInterfaceTypeFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallPortFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallSecurityFilter 2.0.0.0 NetSecurity
Function Get-NetFirewallServiceFilter 2.0.0.0 NetSecurity
所有这些cmdlet都有-AssociatedNetFirewallRule
参数,它接受管道输入。
在这种情况下,可以使用以下命令:
Get-NetFirewallRule -Name "Testing Notepad on port 5001" | Get-NetFirewallApplicationFilter
https://stackoverflow.com/questions/35385551
复制相似问题