By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Zero-fluff, hyper-practical guide for real-world deployments and exam prep
Firewalls are the first line of defense in network security—they decide what traffic gets in (or out) of your systems. In CompTIA Security+, you’ll be tested on four firewall types: - Stateless firewalls (packet filters) - Stateful firewalls (connection-aware) - Web Application Firewalls (WAFs) (Layer 7 protection) - Next-Gen Firewalls (NGFWs) (deep packet inspection + extras)
Why this matters in production: - Stateless firewalls are fast but dumb—they block/allow traffic based on static rules (e.g., "Block all TCP port 22"). If you misconfigure them, you either expose services or break legitimate traffic. - Stateful firewalls remember connections (e.g., "Allow return traffic for an outbound SSH session"). If you don’t use them, you’ll either: - Over-permit (allowing malicious traffic to sneak in as "return traffic"), or - Over-restrict (breaking apps that need bidirectional communication). - WAFs stop Layer 7 attacks (SQLi, XSS, CSRF). Without one, your web apps are sitting ducks for automated bots and script kiddies. - NGFWs combine stateful inspection with intrusion prevention (IPS), app awareness, and SSL decryption. If you’re not using one, you’re blind to encrypted threats (e.g., malware hiding in HTTPS).
Real-world scenario: You’re a sysadmin at a healthcare company. Your legacy app runs on an old Linux server with a stateless firewall. A pentest reveals that attackers can spoof return traffic to bypass your rules. You need to upgrade to a stateful firewall (or NGFW) to close this gap—without breaking the app’s database connections.
iptables
Prerequisites: - A Linux VM (Ubuntu 22.04 LTS recommended). - sudo access. - Basic familiarity with iptables (we’ll walk through it).
sudo
Goal: Configure a stateful firewall that:1. Allows SSH (port 22) from your IP only.2. Allows HTTP/HTTPS (ports 80/443) from anywhere.3. Allows return traffic for established connections.4. Drops everything else.
sudo iptables -L -v -n
-L
-v
-n
Expected output:
Chain INPUT (policy ACCEPT) Chain FORWARD (policy ACCEPT) Chain OUTPUT (policy ACCEPT)
Default policy is ACCEPT—this is dangerous. We’ll fix it.
sudo iptables -F # Flush all rules sudo iptables -X # Delete all custom chains sudo iptables -Z # Zero all packet/byte counters
sudo iptables -P INPUT DROP sudo iptables -P FORWARD DROP sudo iptables -P OUTPUT DROP
-P
Warning: If you’re connected via SSH, don’t close your terminal—you’ll lock yourself out!
sudo iptables -A INPUT -i lo -j ACCEPT sudo iptables -A OUTPUT -o lo -j ACCEPT
-i lo
127.0.0.1
-o lo
Why? Many services (e.g., databases, Docker) rely on loopback.
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT sudo iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT
-m conntrack
--ctstate ESTABLISHED,RELATED
Why? Without this, return traffic (e.g., HTTP responses) gets dropped.
sudo iptables -A INPUT -p tcp --dport 22 -s YOUR_IP_HERE -j ACCEPT
-p tcp
--dport 22
-s YOUR_IP_HERE
123.45.67.89
Why? Restricting SSH to your IP blocks brute-force attacks.
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
-s
Why? Web servers need to accept traffic from the internet.
sudo iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT # DNS (TCP) sudo iptables -A OUTPUT -p udp --dport 53 -j ACCEPT # DNS (UDP) sudo iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT # HTTP sudo iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT # HTTPS
Why? Your server needs to resolve DNS, download updates, and fetch dependencies.
sudo apt install iptables-persistent -y sudo netfilter-persistent save
iptables-persistent
service iptables save
Why? Without saving, rules disappear on reboot.
Expected output (simplified):
Chain INPUT (policy DROP) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED ACCEPT all -- 0.0.0.0/0 127.0.0.1 ACCEPT tcp -- YOUR_IP_HERE 0.0.0.0/0 tcp dpt:22 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443 Chain OUTPUT (policy DROP) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate ESTABLISHED ACCEPT all -- 127.0.0.1 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:53 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
Test connectivity: - SSH into the server (should work). - Run curl google.com (should work). - Try nc -zv 127.0.0.1 22 (should fail—SSH is restricted to your IP).
curl google.com
nc -zv 127.0.0.1 22
sudo iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP
sudo iptables -A INPUT -j LOG --log-prefix "IPTABLES-DROP: " --log-level 4
/var/log/syslog
sudo iptables -A INPUT -p tcp --dport 22 -m comment --comment "Allow SSH for admin" -j ACCEPT
iptables -L -v -n | grep DROP
sudo iptables -A INPUT -j LOG --log-prefix "IPTABLES-ALLOW: "
ESTABLISHED,RELATED
-s 0.0.0.0/0
lo
Stateless (no memory), WAF (Layer 7 only), NGFW (does more than just stateful).
"You need to block SQL injection attacks. Which firewall do you use?"
Stateful (only tracks connections), Stateless (no payload inspection).
"What’s the default rule for a secure firewall?"
Implicit allow (dangerous).
"Which firewall performs deep packet inspection (DPI)?"
"Your company’s web app is under DDoS attack. Which firewall should you deploy to mitigate Layer 7 attacks?" --WAF (blocks HTTP floods, SQLi, XSS). --Stateful (won’t stop Layer 7 attacks), Stateless (too basic), NGFW (overkill if you only need WAF).
Challenge: You’re managing a Linux server with iptables. A pentest reveals that attackers can spoof return traffic to bypass your stateless rules. How do you fix this?
Solution:
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
Why it works: - Stateful tracking ensures only legitimate return traffic is allowed. - Without this, attackers can craft packets with spoofed source IPs to bypass rules.
iptables -L -v -n
iptables -F
iptables -P INPUT DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -j LOG --log-prefix "DROP: "
sudo netfilter-persistent save
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.