Firewall
Table of Contents
A network security control that monitors and filters traffic based on rules. Acts as the enforcement point for a network access policy — permitting or blocking traffic based on source, destination, port, protocol, and connection state. A foundational control in any layered security architecture.
Related: Network Security Network Segmentation pfSense VLANs
Types
| Type | How it works | When to use |
|---|---|---|
| Packet filter | Inspects header fields only — IP, port, protocol. Stateless. | Simple ACLs, routers |
| Stateful | Tracks connection state — allows replies to established sessions automatically | Most firewalls |
| Application layer (L7) | Inspects payload content — understands HTTP, DNS, TLS | WAFs, next-gen firewalls |
| Next-generation (NGFW) | Stateful + application awareness + IDS/IPS + user identity | Enterprise perimeter |
| Web application firewall (WAF) | Specifically filters HTTP/S — protects web applications | In front of web servers |
Linux — iptables
# View current rules iptables -L -n -v iptables -L -n -v --line-numbers # with line numbers for deletion # Allow established/related connections (required for stateful behaviour) iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow incoming SSH iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow incoming HTTP and HTTPS iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT # Block a specific IP iptables -A INPUT -s 192.168.1.50 -j DROP # Allow traffic from a specific subnet iptables -A INPUT -s 10.0.0.0/8 -j ACCEPT # Set default policy — drop everything not explicitly allowed iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # Delete a rule by line number iptables -D INPUT 3 # Save rules (Debian/Ubuntu) iptables-save > /etc/iptables/rules.v4 # Flush all rules (careful — removes all rules) iptables -F
Linux — ufw (Uncomplicated Firewall)
Higher-level wrapper over iptables — simpler syntax for common tasks.
# Enable / disable ufw enable ufw disable ufw status verbose # Allow / deny by port ufw allow 22 ufw allow 80/tcp ufw deny 3306 # Allow / deny by service name ufw allow ssh ufw allow http ufw allow https # Allow from specific IP ufw allow from 192.168.1.0/24 to any port 22 # Delete a rule ufw delete allow 80 # Reset to defaults ufw reset
Linux — nftables (Modern Replacement for iptables)
# View ruleset nft list ruleset # Basic stateful firewall nft add table inet filter nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; } nft add rule inet filter input ct state established,related accept nft add rule inet filter input tcp dport 22 accept nft add rule inet filter input tcp dport { 80, 443 } accept nft add rule inet filter input drop # Save and restore nft list ruleset > /etc/nftables.conf nft -f /etc/nftables.conf
Windows Firewall
# View firewall status
Get-NetFirewallProfile
# Enable / disable
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
# List rules
Get-NetFirewallRule | Where-Object {$_.Enabled -eq "True"} | Select-Object DisplayName, Direction, Action
# Add inbound rule (allow SSH)
New-NetFirewallRule -DisplayName "Allow SSH" -Direction Inbound `
-Protocol TCP -LocalPort 22 -Action Allow
# Add outbound block rule
New-NetFirewallRule -DisplayName "Block outbound 4444" -Direction Outbound `
-Protocol TCP -RemotePort 4444 -Action Block
# Remove a rule
Remove-NetFirewallRule -DisplayName "Allow SSH"
# netsh equivalent (older syntax)
netsh advfirewall firewall add rule name="Allow SSH" `
protocol=TCP dir=in localport=22 action=allow
Security Relevance
Egress Filtering
Outbound filtering is frequently neglected — most firewalls restrict inbound by default but allow all outbound. This is why reverse shells (attacker → victim) are used instead of bind shells (victim listens) — the victim's firewall typically blocks inbound connections but not outbound.
Attacker (listener) ← ← ← Victim (connects out on permitted port) Victim firewall: outbound 443 allowed → reverse shell works Victim firewall: inbound 4444 blocked → bind shell fails
Firewall Evasion
Common techniques encountered in pentests:
# Use permitted ports (80, 443, 53) for reverse shells nc -lvnp 443 # listener on HTTPS port — often not egress-filtered # DNS tunnelling — exfiltrate over port 53 (almost never blocked) # Tools: iodine, dnscat2 # Port knocking — send specific sequence to open a port # Requires knowledge of the knock sequence # Nmap firewall evasion nmap -f $IP # fragment packets nmap --mtu 24 $IP # custom MTU fragmentation nmap -D RND:10 $IP # decoy scan nmap -sA $IP # ACK scan — maps firewall rules
Checking Firewall Rules Post-Compromise
# Linux iptables -L -n -v nft list ruleset ufw status verbose cat /etc/iptables/rules.v4 # Windows Get-NetFirewallRule | Where-Object {$_.Enabled -eq "True"} netsh advfirewall firewall show rule name=all
Tips
- Default deny (allowlist) is the correct base policy — deny all then explicitly permit what is needed, rather than permit all and deny specific threats
- Egress filtering is as important as ingress — restrict outbound to known-good destinations and ports to limit attacker post-exploitation options
- Stateful firewalls handle return traffic automatically — you only need to write rules for the initiating direction
- In CTF and pentest contexts, a reverse shell on port 443 or 80 bypasses most egress filtering since these ports are routinely permitted outbound
- Logging dropped packets is essential — without logs you cannot see what the firewall is blocking or detect scanning activity