SkillPilotAI

netsh advfirewall (1/2)

netsh advfirewall is the command line of Windows Defender Firewall with Advanced Security: the same job as the wf.msc GUI, but scriptable. Part 1 covers profiles, global settings, show commands and firewall rules (add, set, delete, show).

113.1 What it does and where it runs

  • Manages the state and default policy of every profile, logging, inbound/outbound rules and IPsec rules.
  • Built into every Windows since Vista / Server 2008; not on Linux or macOS.
  • set / add / delete need an elevated prompt (Run as administrator). The video ran only read-only commands from a normal prompt on an office PC.

113.2 How an inbound packet is decided

Windows Firewall: inbound decision
  1. 1.LAN laptop 192.168.1.50 opens TCP 22 to Windows PC 192.168.1.10

  2. 2.Firewall finds the network's profile: Domain, Private or Public

  3. 3.Block rules are checked first

  4. 4.Then allow rules: a match lets TCP 22 reach the PC

  5. 5.TCP 445 matches no rule

  6. 6.Default inbound policy: Block

Block rule > allow rule > default policy

Rule anatomy: SSH from LAN
  1. 1.Rule: dir=in action=allow protocol=TCP localport=22

  2. 2.Scope: remoteip=192.168.1.0/24

  3. 3.Packet from LAN laptop 192.168.1.50 matches: allowed

  4. 4.Same packet from 203.0.113.5 (internet) is outside scope

  5. 5.Firewall drops it

113.3 Context-level commands (12)

Terminal
netsh advfirewall ?netsh advfirewall dump
Output
consec         - Changes to the `netsh advfirewall consec' context.dump           - Displays a configuration script.export         - Exports the current policy to a file.firewall       - Changes to the `netsh advfirewall firewall' context.import         - Imports a policy file into the current policy store.mainmode       - Changes to the `netsh advfirewall mainmode' context.monitor        - Changes to the `netsh advfirewall monitor' context.reset          - Resets the policy to the default out-of-box policy.set            - Sets the per-profile or global settings.show           - Displays profile or global properties. $ netsh advfirewall dumpThe 'netsh advfirewall dump' command is not implemented in this versionof Windows. Instead, use the 'netsh advfirewall export' command to writethe current Windows Defender Firewall with Advanced Security configuration fromthe current policy store to a file on disk.
  • export saves the policy to a .wfw file, import loads it back, reset restores out-of-box defaults.
  • dump is not implemented: use export for backups.

113.4 Profiles: state and firewallpolicy

Output
set allprofiles | currentprofile | domainprofile | privateprofile | publicprofilestate on|off|notconfigured        (notconfigured: GPO store only)firewallpolicy IN,OUT blockinbound        no match: block blockinboundalways  block even on a match allowinbound        no match: allow allowoutbound       out, no match: allow blockoutbound       out, no match: block
  • Normal default: blockinbound,allowoutbound.
  • blockinboundalways is an emergency mode that ignores even allow rules.
  • notconfigured is valid only in a Group Policy store.

113.5 Profile settings and logging

Output
settings <name> enable|disable|notconfigured localfirewallrules          local + GPO merge localconsecrules            IPsec rules merge inboundusernotification     listen popup remotemanagement            remote management unicastresponsetomulticast  mcast replylogging allowedconnections enable|disablelogging droppedconnections enable|disablelogging filename <path>|notconfigured   (default %systemroot%\system32\LogFiles\Firewall\pfirewall.log)logging maxfilesize 1-32767   (KB)
  • logging droppedconnections enable is the most useful switch for troubleshooting.

113.6 Global settings

Terminal
netsh advfirewall set global ?netsh advfirewall show global statefulftpnetsh advfirewall show global mainmode
Output
Usage: set global statefulftp|statefulpptp enable|disable|notconfigured      set global ipsec (parameter) (value)      set global mainmode (parameter) (value) | notconfigured $ netsh advfirewall show global statefulftpGlobal Settings:StatefulFTP                           EnableOk. $ netsh advfirewall show global mainmodeGlobal Settings:KeyLifetime                           Access DeniedSecMethods                            Access DeniedForceDH                               Access DeniedOk.
  • Global settings belong to the whole firewall, not one profile.
  • StatefulFTP Enable: the firewall tracks the FTP data connection itself.
  • Even reading IPsec / main mode settings needs elevation (Access Denied).
Output
ipsec strongcrlcheck 0|1|2      CRL checkipsec saidletimemin 5-60        SA idleipsec defaultexemptions none|icmp|dhcp|neighbordiscoveryipsec ipsecthroughnat never|serverbehindnat|serverandclientbehindnatipsec authzcomputergrp none|<SDDL>ipsec authzusergrp none|<SDDL>mainmode mmkeylifetime 480min,0sessmainmode mmsecmethods dhgroup14:aes256-sha256 | defaultmainmode mmforcedh yes|no
  • Avoid weak main mode proposals: DES, MD5 and DH group 1.

113.7 show: profile and global properties

Terminal
netsh advfirewall show ?netsh advfirewall show storenetsh advfirewall show allprofiles ?
Output
show allprofiles - Displays properties for all profiles.show currentprofile - Displays properties for the active profile.show domainprofile - Displays properties for the domain properties.show global    - Displays the global properties.show privateprofile - Displays properties for the private profile.show publicprofile - Displays properties for the public profile.show store     - Displays the policy store for the current interactive session. $ netsh advfirewall show storeStore Settings:Policy Store                          LocalOk. $ netsh advfirewall show allprofiles ?Usage: show allprofiles [parameter]      state             - Displays whether Windows Defender Firewall with Advanced                          Security is on or off.      firewallpolicy    - Displays default inbound and outbound                          firewall behavior.      settings          - Displays firewall properties.      logging           - Displays logging settings.
  • Policy Store Local = this session edits the local store, not Group Policy.
  • On a personal PC, netsh advfirewall show allprofiles state shows State ON/OFF per profile.

113.8 firewall context: show rule

Terminal
netsh advfirewall firewall show rule ^ name="Remote Desktop - User Mode (TCP-In)"
Output
Rule Name:                            Remote Desktop - User Mode (TCP-In)Enabled:                              NoDirection:                            InProfiles:                             Domain,Private,PublicGrouping:                             Remote DesktopProtocol:                             TCPLocalPort:                            3389Action:                               AllowOk.
  • ^ at the end of a line continues the command on the next line (cmd).
  • Enabled: No means the built-in RDP rule is currently off.
Terminal
netsh advfirewall firewall show rule ^ name="Core Networking - Teredo (UDP-In)" ^ verbosenetsh advfirewall firewall show rule ^ name=n113-no-such-rule
Output
Enabled:                              YesDirection:                            InProtocol:                             UDPLocalPort:                            TeredoProgram:                              C:\WINDOWS\system32\svchost.exeService:                              iphlpsvcSecurity:                             NotRequiredRule source:                          Local SettingAction:                               AllowOk. No rules match the specified criteria.
  • show rule parameters: name (or name=all), profile, type (static/dynamic), dir, verbose.
  • No rules match is handy to test for in scripts.

113.9 firewall add rule (43 parameters)

Output
name=<text>        unique, not "all"dir=in|out         inbound or outboundaction=allow|block|bypassprogram=<exe path> service=<svc>|anyprofile=public|private|domain|anylocalip= / remoteip=any|IP|subnet|range|listremoteip=localsubnet|dns|dhcp|wins|defaultgatewaylocalport=any|N|N-M|RPC|RPC-EPMap|IPHTTPSremoteport=any|N|N-Mprotocol=tcp|udp|icmpv4|icmpv6|0-255|anyprotocol=icmpv4:8,any   ICMP type,codeinterfacetype=wireless|lan|ras|anyedge=no|yes|deferapp|deferusersecurity=notrequired|authenticate|authenc|authdynenc|authnoencap
Terminal
netsh advfirewall firewall add rule name="SSH from LAN" dir=in action=allow ^ protocol=TCP localport=22 remoteip=192.168.1.0/24
  • name, dir and action are required; name must be unique.
  • edge=yes allows traffic that arrives through NAT/Teredo.
  • action=bypass skips even block rules; inbound it needs rmtcomputergrp (authenticated IPsec).

113.10 firewall delete rule and set rule

Output
delete rule name=<text>|all  dir= profile= program= service=  localip= remoteip= localport= remoteport= protocol=    (filters)set rule name=<text> | group=<text>  (same filters)new                      new values from here  new name= dir= action= enable= profile= localip= remoteip=  new localport= remoteport= protocol= interfacetype= edge= security=
  • delete removes every rule that matches; name=all with filters can wipe many rules, so be careful.
  • set rule: filters come before new, new values after new. Without new nothing changes.
  • With group=, set rule can only enable/disable (e.g. turn on the Remote Desktop group).
CommandWhat it doesKey point
add ruleCreate a rulename + dir + action required
set ruleChange matching rulesValues after the new keyword
delete ruleDelete all matching rulesname=all is dangerous
show ruleDisplay rulesverbose for all details

113.11 consec context (intro)

Terminal
netsh advfirewall consec ?netsh advfirewall consec show rule ^ name=n113-no-such-rule
Output
add            - Adds a new connection security rule.delete         - Deletes all matching connection security rules.dump           - Displays a configuration script.set            - Sets new values for properties of an existing rule.show           - Displays a specified connection security rule. $ netsh advfirewall consec show rule name=n113-no-such-ruleThe requested operation requires elevation (Run as administrator).
  • consec = connection security (IPsec) rules; same add/delete/set/show commands. Covered in detail in part 2.

113.12 Common mistakes

  • set rule name=allow80 enable=no without new: nothing changes.
  • Thinking an allow rule always lets traffic in: a block rule wins; only an authenticated bypass rule skips a block.
  • Running set/add from a normal prompt: needs elevation.
  • Using dump for backups: not implemented, use export.

113.13 Interview tips

  • Open a port safely: elevated prompt, export a backup, add rule (name, dir=in, action=allow, protocol, localport), restrict remoteip and profile, verify with show rule.
  • Order: block rules > allow rules > default policy.
  • Default policy is blockinbound,allowoutbound per profile (Domain/Private/Public).
  • For new automation, PowerShell New-NetFirewallRule does the same job.