SkillPilotAI

PowerShell NetSecurity module (2/7)

Part 2 of the NetSecurity module: Enable-/Disable- for firewall, Hyper-V and IPsec rules, Find-NetIPsecRule to see which IPsec rule a connection hits, Get-DAPolicyChange for DirectAccess, and the Get-NetFirewall*Filter cmdlets that show a rule's address, interface and program conditions.

218.1 Why is it used?

  • Turn a firewall or IPsec rule on/off temporarily without deleting it.
  • Find which IPsec rule applies to a given connection (debug policy mismatch).
  • Read the conditions of a rule (address, interface, application) stored in separate filter objects.
  • Manage the separate Hyper-V firewall (per-port rules, VM creators, profiles).

218.2 Where it runs

  • Built into Windows 10/11 and Server, PowerShell 5.1 and 7. Enable-/Disable- need admin and change the firewall policy directly.
  • On the office PC only Get-Command and Get-Help ran; the rest is from Microsoft Learn; the practical is nftables in Docker.
PowerShell
Get-Command -Module NetSecurity | Measure-ObjectGet-Help Enable-NetFirewallRule
Output
$ Get-Command -Module NetSecurity | Measure-ObjectCount    : 106$ Get-Help Enable-NetFirewallRuleNAME    Enable-NetFirewallRuleSYNTAX    Enable-NetFirewallRule [-Description <string[]>] [-DisplayGroup <string[]>] [-Group <string[]>] [-Enabled {True |    False}] [-Direction {Inbound | Outbound}] [-Action {NotConfigured | Allow | Block}] [-EdgeTraversalPolicy {Block |...REMARKS    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.

218.3 Enable-NetFirewallRule

Disabled rule blocks nothing, enabled rule drops
  1. 1.Server has a Block rule for port 8080, currently disabled

  2. 2.Client request passes the firewall and reaches the server

  3. 3.Admin runs Enable-NetFirewallRule on that rule

  4. 4.Rule becomes active

  5. 5.Next request is dropped right at the firewall

PowerShell
# Example (docs), admin only, NOT run here:Enable-NetFirewallRule -Group "@FirewallAPI.dll,-28502"Enable-NetFirewallRule -DisplayName "Network Discovery"Enable-NetFirewallRule -Direction Outbound `  -PolicyStore contoso.com\gpo_nameGet-NetFirewallRule -PolicyStore ActiveStore `  -PolicyStoreSourceType Dynamic |  Enable-NetFirewallRule
  • Docs: running it without parameters enables all Windows Firewall rules.
  • Select by -Name / -DisplayName / -Group, query by -Direction / -Action / -Enabled, or via -Associated*Filter objects; -PassThru returns the rule.

218.4 Hyper-V firewall cmdlets

PowerShell
Enable-NetFirewallHyperVRule -PolicyStore ActiveStoreEnable-NetFirewallHyperVRule -DisplayName 'MyServerIPBlock'Get-NetFirewallHyperVRule -EnforcementStatus OKGet-NetFirewallHyperVPortGet-NetFirewallHyperVVMCreatorGet-NetFirewallHyperVProfile -PolicyStore ActiveStoreGet-NetFirewallHyperVVMSetting -Name '{9E288F02-CE00-4D9E-BE2B-14CE463B0298}'
CmdletShows / does
Enable-/Get-NetFirewallHyperVRuleHyper-V rules, applied per Hyper-V firewall port; EnforcementStatus OK = being enforced
Get-NetFirewallHyperVPortActive Hyper-V firewall ports
Get-NetFirewallHyperVVMCreatorVM creators on the system (a rule can target one not present yet)
Get-NetFirewallHyperVProfilePer-profile settings (Domain, Private, Public)
Get-NetFirewallHyperVVMSettingPer-VM-creator settings; -Name takes the GUID in braces

218.5 IPsec: Disable/Enable main mode and IPsec rules

IPsec: main mode + quick mode, Disable = inactive
  1. 1.Main mode: client handshake via the router to the server, both authenticate

  2. 2.Quick mode: data encrypted with ESP travels to the server

  3. 3.Admin runs Disable-NetIPsecRule

  4. 4.Rule becomes inactive but is not deleted

  5. 5.Enable-NetIPsecRule makes it active again

PowerShell
Disable-NetIPsecMainModeRule -DisplayName "Main Mode Rule" `  -PolicyStore domain.contoso.com\gpoEnable-NetIPsecMainModeRule -Group "DA Client" -PolicyStore ActiveStore$phase1AuthSet = Get-NetIPsecPhase1AuthSet `  -DisplayName "Computer Kerb, CA Auth"Disable-NetIPsecMainModeRule -InputObject $phase1AuthSetDisable-NetIPsecRule -DisplayName "Require Outbound Authentication" `  -PolicyStore domain.contoso.com\gpo_nameEnable-NetIPsecRule -Group "Ipsec-DirectAccess-Traffic" `  -Mode Transport -PolicyStore ActiveStore
  • Main mode rules decide how two computers authenticate and which crypto set is used.
  • Select IPsec rules by settings: -Mode None|Transport|Tunnel, -InboundSecurity / -OutboundSecurity None|Request|Require, -KeyModule, -QuickModeCryptoSet, -Phase2AuthSet.
  • Docs: useful for debugging an IPsec policy mismatch.

218.6 Find-NetIPsecRule and Get-DAPolicyChange

PowerShell
Find-NetIPsecRule -RemoteAddress "131.107.33.200" `  -Protocol "TCP" -RemotePort 80
Output
IPsecRuleName         : {c1988b9c-9546-4eea-bf64-fbe85ddbef8c}DisplayName           : External Traffic - Attempt IPsecEnabled               : TrueProfile               : DomainMode                  : TransportInboundSecurity       : RequireOutboundSecurity      : RequestKeyModule             : DefaultPrimaryStatus         : OKPolicyStoreSourceType : GroupPolicy
  • Find-NetIPsecRule takes connection details (-RemoteAddress, -LocalAddress, ports, -Protocol) and returns the rule the system will use; GroupPolicy = it came from a GPO.
  • Get-DAPolicyChange (DirectAccess, double tunnel model) resolves server/domain names, compares them with the IPsec rule's addresses, lists IPs to Add/Delete and writes an Update-NetIPsecRule script (-PSLocation).
PowerShell
Get-DAPolicyChange -DisplayName "TunnelPolicy1" `  -EndpointType Endpoint1 -PSLocation "C:\Update.ps1" `  -Servers "server1.corp.contoso.com", `  "server2.corp.contoso.com", "server3.corp.contoso.com"
Output
IPsec Rule name  : TunnelPolicy1Action           : AddIPv6addresses    : 2001:4829:3243::100:1                 : 2001:4829:3243::100:1GPO              : contoso\DAClientPolicyIPsec Rule name  : TunnelPolicy1Action           : DeleteIPv6addresses    : 2001:4829:3243::100:3                 : 2001:4829:3243::100:4GPO              : contoso\DAClientPolicyFQDN's that did not resolve into IP address:server1.corp.contoso.comserver3.corp.contoso.com

218.7 Firewall filter cmdlets

  • A firewall rule holds only name, direction and action; conditions (address, port, interface, program) live in separate filter objects linked 1:1.
PowerShell
Get-NetFirewallRule -DisplayGroup "Core Networking" |  Get-NetFirewallAddressFilter |  Where-Object -FilterScript { $_.RemoteAddress -Eq "LocalSubnet6" }Get-NetFirewallRule -DisplayName "Contoso Messenger" |  Get-NetFirewallInterfaceFilterGet-NetFirewallRule -DisplayName "Contoso Messenger" |  Get-NetFirewallApplicationFilterGet-NetFirewallDynamicKeywordAddress `  -PolicyStore "ContosoPolicyStore" -AllAutoResolve
Filter cmdletCondition
Get-NetFirewallAddressFilterLocal / remote addresses (e.g. LocalSubnet6)
Get-NetFirewallInterfaceFilterInterface alias
Get-NetFirewallApplicationFilter-Program (exe path) or -Package (Store app SID)
Get-NetFirewallDynamicKeywordAddressNamed address group usable by rules; -Id "{GUID}" needs braces

218.8 Linux equivalent: nftables (Docker lab)

Terminal
nft add table inet n218nft add chain inet n218 input \  "{ type filter hook input priority 0; }"nft add rule inet n218 input \  iifname eth0 ip saddr 192.168.238.20 \  tcp dport 8080 counter drop comment webnft add rule inet n218 input \  iifname eth0 ip saddr 192.168.238.20 \  icmp type echo-request counter drop comment pingnft -a list chain inet n218 input
Output
$ [n218-server] nft -a list chain inet n218 inputtable inet n218 {  chain input { # handle 1    type filter hook input priority filter; policy accept;    iifname "eth0" ip saddr 192.168.238.20 tcp dport 8080 counter packets 0 bytes 0 drop comment "web" # handle 2    iifname "eth0" ip saddr 192.168.238.20 icmp type echo-request counter packets 0 bytes 0 drop comment "ping" # handle 3  }}$ [n218-client] ping -c 2 -W 1 192.168.238.102 packets transmitted, 0 received, 100% packet loss, time 1013ms$ [n218-client] nc -v -w 2 192.168.238.10 8080nc: connect to 192.168.238.10 port 8080 (tcp) timed out: Operation in progress
  • One rule combines interface (iifname), address (ip saddr) and port (tcp dport): the Linux version of interface, address and port filters.
  • nftables has no disabled flag: "disable" = save the ruleset, then delete by handle; "enable" = add the rule back (it gets a new handle).
Terminal
nft list ruleset > /tmp/n218.nftnft delete rule inet n218 input handle 2nft add rule inet n218 input \  iifname eth0 ip saddr 192.168.238.20 \  tcp dport 8080 counter drop comment web
Output
$ [n218-server] nft delete rule inet n218 input handle 2$ [n218-client] nc -v -w 2 192.168.238.10 8080Connection to 192.168.238.10 8080 port [tcp/http-alt] succeeded!hello from n218-server$ [n218-client] ping -c 2 -W 1 192.168.238.102 packets transmitted, 0 received, 100% packet loss, time 1022ms$ [n218-server] nft -a list chain inet n218 input    iifname "eth0" ip saddr 192.168.238.20 icmp type echo-request counter packets 4 bytes 336 drop comment "ping" # handle 3    iifname "eth0" ip saddr 192.168.238.20 tcp dport 8080 counter packets 0 bytes 0 drop comment "web" # handle 4

218.9 Common mistakes

  • Running Enable-NetFirewallRule without parameters: every rule gets enabled.
  • Thinking Disable deletes a rule: it still exists and Enable brings it back; Remove- deletes permanently.
  • Disabling a rule in a GPO and expecting the client to change immediately: it applies at the next GPO apply.
  • Looking for address/port inside Get-NetFirewallRule output: they are in the filter objects.

218.10 Interview tips

  • Temporarily turn off a rule: Disable-/Enable-NetFirewallRule; for IPsec Disable-/Enable-NetIPsecRule and NetIPsecMainModeRule.
  • Always target by -Name / -DisplayName / -Group; no parameter = all rules.
  • Which IPsec rule does this connection use? Find-NetIPsecRule -RemoteAddress -Protocol -RemotePort.
  • Rule disabled in GPO but still active on the client: wait for the client's GPO refresh.