PowerShell NetSecurity module (3/7)
Part 3 of the NetSecurity module: the Get-NetFirewall* and Get-NetIPsec* cmdlets that read the whole firewall and IPsec policy (rules, profiles, filters, crypto/auth sets, active SAs), plus two New- cmdlets. Read-only, but the output is a full map of a network, so treat it as sensitive.
219.1 Why is it used?
- See which rules exist and which are effective (ActiveStore = sum of all policy stores).
- Find the port, address, service or interface type of a rule through its filter object.
- Read profile (Domain/Private/Public) and global firewall settings.
- Inspect IPsec rules, crypto/auth sets and the SAs active right now.
219.2 Where it runs
- Built into Windows 10/11 and Server, PowerShell 5.1 and 7. 26 Get cmdlets in NetSecurity.
- Get cmdlets change nothing, but they show the whole firewall policy and VPN IPsec SAs; on the office PC none were run, only Get-Command / Get-Help.
- Docs pages give no output for most examples, so only commands are shown; the practical is nftables in Docker.
Get-Command -Module NetSecurity -Verb Get | Measure-ObjectGet-Help Get-NetFirewallRule$ Get-Command -Module NetSecurity -Verb Get | Measure-ObjectCount : 26$ Get-Help Get-NetFirewallRule (trimmed)Synopsis : Get-NetFirewallRule [-All] [-PolicyStore <string>] [-GPOSession <string>] [-TracePolicyStore] [-CimSession <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] Get-NetFirewallRule -DisplayName <string[]> [-PolicyStore <string>] ... Get-NetFirewallRule [-Description <string[]>] [-DisplayGroup <string[]>] [-Group <string[]>] [-Enabled <Enabled[]>] [-Direction <Direction[]>] [-Action <Action[]>] ...ModuleName : NetSecurity219.3 How the firewall checks a packet
- Step 1
1.Client sends a request to TCP 8080
- Step 2
2.Windows Firewall looks at the active profile (Private)
- Step 3
3.It checks the rule Allow Web 8080 (name, direction, action)
- Step 4
4.Conditions live in separate filter objects: port filter TCP 8080 matches
- Step 5
5.Packet goes on to the web app
| Get cmdlet | Reads |
|---|---|
| Get-NetFirewallProfile | Domain/Private/Public profile settings |
| Get-NetFirewallSetting | Global settings: active profile, exemptions, authorization lists |
| Get-NetFirewallRule | Name, direction, action, enabled, group |
| Get-NetFirewallPortFilter | Protocol and ports |
| Get-NetFirewallInterfaceTypeFilter | Any | Wired | Wireless | RemoteAccess |
| Get-NetFirewallSecurityFilter | Authentication, encryption, allowed users/computers, OverrideBlockRules |
| Get-NetFirewallServiceFilter | Windows service the rule is tied to |
219.4 Get-NetFirewallRule, profile and setting
Get-NetFirewallRule -PolicyStore ActiveStoreGet-NetFirewallProfile -Name Public | Get-NetFirewallRuleGet-NetFirewallProfile -PolicyStore ActiveStoreGet-NetFirewallSettingGet-NetFirewallSetting -PolicyStore corp.contoso.com/gpo_name | Set-NetFirewallSetting -Exemptions RouterDiscovery- Without -PolicyStore only the PersistentStore is read; ActiveStore gives the effective policy.
- 29 options: -Enabled, -Direction, -Action, -Group, -EdgeTraversalPolicy, -PrimaryStatus, -PolicyStoreSourceType, -AssociatedNetFirewall*Filter...
- Get-NetFirewallProfile = the Domain/Private/Public tabs of the Windows Firewall properties page.
219.5 Filter cmdlets (pipeline pattern)
Get-NetFirewallRule -DisplayName "Contoso Messenger" | Get-NetFirewallPortFilterGet-NetFirewallPortFilter -DynamicTransport ProximitySharing | Get-NetFirewallRuleGet-NetFirewallInterfaceTypeFilter -InterfaceType WiredGet-NetFirewallSecurityFilter -OverrideBlockRules $True | Get-NetFirewallRuleGet-NetFirewallServiceFilter -Service dnscache | Get-NetFirewallRule | Where-Object -Property { $_.Enabled -Eq "False" }- Rule -> filter: pipe a rule into the filter cmdlet to see its conditions.
- Filter -> rule: query filters first, then pipe into Get-NetFirewallRule to find the rules.
- OverrideBlockRules $True = authenticated bypass rules that allow traffic even against block rules.
219.6 IPsec Get cmdlets
- Step 1
1.PC and server verify identity in main mode
- Step 2
2.Secure channel built: main mode SA (only one per pair)
- Step 3
3.Quick mode runs inside it: keys and protection suite for the data
- Step 4
4.Quick mode SA built (there can be many)
- Step 5
5.Data packets travel encrypted in ESP
| Cmdlet | Shows | Useful filter |
|---|---|---|
| Get-NetIPsecRule | Which traffic is protected | -Mode Tunnel|Transport, -InboundSecurity, -KeyModule |
| Get-NetIPsecMainModeRule | Main mode rules | -MainModeCryptoSet, -Phase1AuthSet |
| Get-NetIPsecMainModeCryptoSet | Key exchange encryption | -ForceDiffieHellman, -MaxMinutes, -MaxSessions |
| Get-NetIPsecPhase1AuthSet | Computer authentication (Kerberos, certificate) | -AssociatedNetIPsecRule |
| Get-NetIPsecQuickModeCryptoSet | Data encryption | -PerfectForwardSecrecyGroup |
| Get-NetIPsecPhase2AuthSet | Phase 2 (user) authentication | -AssociatedNetIPsecRule |
| Get-NetIPsecMainModeSA / QuickModeSA | Active SAs = peers connected right now | -Name, -CimSession |
| Get-NetIPsecDospSetting | IPsec DoS protection settings | -Name |
Get-NetIPsecRule -DisplayName "SecureNet: Exempt SAP Server"Get-NetIPsecRule -DisplayGroup "DirectAccess-Traffic"Get-NetIPsecPhase1AuthSet -DisplayName "P1 Auth Set" | Get-NetIPsecMainModeRuleGet-NetIPsecMainModeCryptoSet -ForceDiffieHellman $falseGet-NetIPsecQuickModeCryptoSet -DisplayGroup "DirectAccess-Traffic"Get-NetIPSecRule -DisplayName "Authenticate Both Computer and User" | Get-NetIPsecPhase2AuthSet$computer1 = "RemoteMachineName"Get-NetIPsecMainModeSA -Name "196511" -CimSession $Computer1 | Remove-NetIPsecQuickModeSA -CimSession $computer1Get-NetIPsecDospSetting -Name CorpNet-PubNet219.7 Two New- cmdlets
$GUID = New-GuidNew-NetFirewallDynamicKeywordAddress ` -Id $('{' + $GUID + '}') ` -Keyword "ContosoSubnet" -AutoResolve $TrueNew-NetFirewallHyperVProfile ` -Name '{9E288F02-CE00-4D9E-BE2B-14CE463B0298}' ` -Profile Public -Enabled True- A dynamic keyword address can be used in a rule's remote address instead of fixed IPs; the GUID must be in braces; -AutoResolve $True = resolvable name, otherwise give -Addresses.
- New-NetFirewallHyperVProfile sets Hyper-V firewall profile settings for one VM creator (-DefaultInboundAction, -AllowLocalFirewallRules, -Enabled).
219.8 Linux equivalent: nft list (Docker lab)
nft --versionnft list rulesetnft list tables$ nft --versionnftables v1.0.9 (Old Doc Yak #3)$ nft list ruleset (trimmed)# Warning: table ip nat is managed by iptables-nft, do not touch!table ip nat { chain DOCKER_OUTPUT { ip daddr 127.0.0.11 tcp dport 53 counter packets 0 bytes 0 xt target "DNAT" ip daddr 127.0.0.11 udp dport 53 counter packets 0 bytes 0 xt target "DNAT" } ...$ nft list tablestable ip nat# n219.nftdefine client = 192.168.239.20table inet n219 { counter client-pings { } chain input { type filter hook input priority 0 policy accept ip saddr $client icmp type echo-request \ counter name "client-pings" accept tcp dport 22 counter accept }}nft -f /root/n219.nftnft -a list table inet n219nft list counter inet n219 client-pings$ nft -a list table inet n219table inet n219 { # handle 2 counter client-pings { # handle 2 packets 0 bytes 0 } chain input { # handle 1 type filter hook input priority filter; policy accept; ip saddr 192.168.239.20 icmp type echo-request counter name "client-pings" accept # handle 3 tcp dport 22 counter packets 0 bytes 0 accept # handle 4 }}[client] $ ping -c 3 -W 1 192.168.239.10 | tail -23 packets transmitted, 3 received, 0% packet loss, time 2035ms$ nft list counter inet n219 client-pings counter client-pings { packets 3 bytes 252[client] $ ping -c 2 -W 1 192.168.239.10 | tail -22 packets transmitted, 2 received, 0% packet loss, time 1022ms$ nft list counter inet n219 client-pings counter client-pings { packets 5 bytes 420- nft list ruleset = Get-NetFirewallRule; the inet family is one table for IPv4 and IPv6; -a shows rule handles.
- The named counter went 0 -> 3 -> 5 packets, proving the rule matches real traffic.
219.9 Common mistakes
- Looking for the port in Get-NetFirewallRule output: it lives in the port filter; pipe into Get-NetFirewallPortFilter.
- Forgetting -PolicyStore ActiveStore and missing GPO rules (only PersistentStore is read).
- Sharing Get output from an office machine: it reveals allowed ports, VPN peers and IPsec policy; get approval first.
- Expecting one quick mode SA per pair: there is one main mode SA but can be many quick mode SAs.
219.10 Interview tips
- Find the rule for port 8080: Get-NetFirewallPortFilter (LocalPort 8080) | Get-NetFirewallRule, with -PolicyStore ActiveStore for the effective policy.
- Rule = name/direction/action; conditions = Port, Address, Security, Service, InterfaceType filters.
- IPsec: Get-NetIPsecRule for policy, Get-NetIPsecMainModeSA / QuickModeSA for live connections.
- Linux: nft list ruleset / tables / chain / counter.