Last Updated: May 1, 2025
iptables Basics
iptables -L -v -nList all rules with packet counts (numeric, verbose)
iptables -A INPUT -p tcp --dport 22 -j ACCEPTAllow SSH on INPUT chain
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTAllow return traffic
iptables -P INPUT DROPSet default policy to drop all INPUT
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPTAllow entire subnet
iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/min -j ACCEPTRate-limit HTTP
iptables -D INPUT 3Delete rule #3 from INPUT chain
iptables-save > /etc/iptables/rules.v4Save rules persistently
iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPTInsert rule at position 1
Chains & Tables
| Item | Description |
|---|---|
INPUT | Packets destined for this host (local delivery) |
OUTPUT | Packets originating from this host |
FORWARD | Packets routed through this host (gateway/NAT) |
PREROUTING | Packets before routing decision (DNAT, redirect) |
POSTROUTING | Packets after routing decision (SNAT, masquerade) |
filter table | Default table — packet filtering (ACCEPT/DROP/REJECT) |
nat table | Network Address Translation (SNAT, DNAT, MASQUERADE) |
nftables (modern replacement)
nft add table inet filterCreate a new table (inet = IPv4+IPv6)
nft add chain inet filter input { type filter hook input priority 0; }Create input chain
nft add rule inet filter input tcp dport 22 acceptAllow SSH
nft add rule inet filter input ct state established,related acceptAllow return traffic
nft list rulesetShow all rules (like iptables -L)
nft -f /etc/nftables.confLoad rules from config file
WAF Concepts
| Item | Description |
|---|---|
WAF | Web Application Firewall — filters HTTP traffic (layer 7) |
OWASP Top 10 | Standard threat model: SQLi, XSS, CSRF, etc. |
ModSecurity | Open-source WAF module for Apache/Nginx |
Cloud WAF | Cloudflare, AWS WAF, Azure WAF — managed at edge |
Rule types | Allow-list (only known good) vs block-list (block known bad) |
False positives | WAF blocking legitimate traffic — tune rules carefully |
Pro Tip: iptables rules process in order — first match wins. Always put your default DROP policy LAST and add specific ALLOW rules above it. Test rules on a non-production server first.