Lab 06 β Firewall Rules with iptables¶
Course: SCIA-120 Β· Introduction to Secure Computing
Topic: Network Security β Packet Filtering & Firewalls
Difficulty: ββ BeginnerβIntermediate
Estimated Time: 45β60 minutes
Related Reading: Chapter 7 β Network Security Fundamentals
Overview¶
A firewall is the first line of network defense β it controls which traffic is allowed in and out of a system based on rules. In this lab you will configure iptables firewall rules inside Docker containers, observe how rules block or allow traffic, and understand the logic behind packet filtering: default-deny vs. default-allow policies.
Learning Objectives¶
- Understand what iptables is and how it processes network packets.
- List, add, and delete firewall rules.
- Apply a default-deny (allowlist) policy vs. default-allow (blocklist) policy.
- Block specific ports, IP addresses, and protocols.
- Understand how Docker itself uses iptables internally.
Prerequisites¶
- Docker Desktop installed and running.
- Lab 04 or 05 recommended.
Part 1 β Understanding iptables Chains¶
iptables organizes rules into chains:
| Chain | When it runs |
|---|---|
INPUT | Packets destined for the local machine |
OUTPUT | Packets originating from the local machine |
FORWARD | Packets routed through the machine |
Each chain has a default policy (ACCEPT or DROP) that applies if no rule matches.
Step 1.1 β Start a Privileged Container (iptables requires privileges)¶
docker run --rm -it \
--name firewall-lab \
--cap-add NET_ADMIN \
--cap-add NET_RAW \
ubuntu:22.04 bash
Step 1.2 β Install iptables and Utilities¶
Step 1.3 β View Current Rules¶
Expected output:
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
...
Chain OUTPUT (policy ACCEPT)
...
By default, everything is ACCEPT β all traffic is allowed.
πΈ Screenshot checkpoint: Take a screenshot of the default (empty) iptables rules.
Part 2 β Adding Basic Rules¶
Step 2.1 β Block All ICMP (Ping) Traffic¶
Add the block rule:
πΈ Screenshot checkpoint: Take a screenshot showing ping working before and blocked after the rule.
Step 2.2 β Allow ICMP Again (Delete the Rule)¶
Part 3 β Block a Specific Port¶
Step 3.1 β Block Inbound Connections on Port 8080¶
πΈ Screenshot checkpoint: Take a screenshot of the rules list showing the port block.
Step 3.2 β Test the Block¶
In one container window start a listener:
Try connecting to each from within the container:
echo "test" | nc -w 1 127.0.0.1 9090 # Should succeed
echo "test" | nc -w 1 127.0.0.1 8080 # Should be blocked/timeout
πΈ Screenshot checkpoint: Take a screenshot showing the difference between the blocked and allowed port.
Part 4 β Block a Specific IP Address¶
Step 4.1 β Block Traffic from a Specific Source IP¶
This blocks all traffic from IP 1.2.3.4 β useful for blocking known malicious IPs.
Step 4.2 β See the Rule Count Increment¶
After network activity, the "pkts" and "bytes" columns increment for matched rules β you can see which rules are being hit.
πΈ Screenshot checkpoint: Take a screenshot of the rules list showing the IP block rule.
Part 5 β Default-Deny Policy¶
The most secure firewall posture is default-deny: block everything, then explicitly allow only what is needed. This is the opposite of default-allow.
Step 5.1 β Set a Default-Deny Policy¶
Important
This will block all incoming traffic. Only run this inside the container β not on your real system.
Step 5.2 β Verify Everything Is Now Blocked¶
Step 5.3 β Selectively Allow SSH (Port 22) and HTTP (Port 80)¶
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -L -v -n
The last rule is critical β it allows responses to connections you initiated (established connections) to come back in.
πΈ Screenshot checkpoint: Take a screenshot of the full iptables ruleset with default DROP and specific ACCEPT rules.
Part 6 β Allowlist vs. Blocklist Comparison¶
Fill in this table as part of your submission:
| Policy | How it works | Security level | Operational risk |
|---|---|---|---|
| Default-Allow (Blocklist) | Allow all; block known bad | Lower | Low β easy to maintain |
| Default-Deny (Allowlist) | Block all; allow known good | Higher | Higher β may block legitimate traffic |
Part 7 β Flush All Rules¶
To reset to default (remove all rules):
πΈ Screenshot checkpoint: Take a screenshot of the clean (empty) state after flushing.
Type exit to leave the container.
Cleanup¶
Lab Assessment¶
Screenshot Submission Checklist¶
- [ ]
screenshot-06aβ Default empty iptables rules - [ ]
screenshot-06bβ Ping before and after ICMP block rule - [ ]
screenshot-06cβ Port 8080 block rule in iptables list - [ ]
screenshot-06dβ Difference between blocked (8080) and allowed (9090) ports - [ ]
screenshot-06eβ IP address block rule - [ ]
screenshot-06fβ Default-deny policy with selective ACCEPT rules - [ ]
screenshot-06gβ Flushed (clean) iptables state
Reflection Questions¶
- What is the difference between a "default-allow" and "default-deny" firewall policy? Which is more secure, and what are the tradeoffs?
- Why is the
ESTABLISHED,RELATEDiptables rule necessary? What would break without it? - A company's IT policy says: "The firewall should block all inbound traffic except ports 80, 443, and 22." Write out the iptables commands that would implement this policy.
- A firewall blocks all traffic from a known-bad IP address. An attacker switches to a different IP. What does this tell you about the limitations of IP-based blocking?
Grading Rubric
- Screenshots complete and clearly labeled: 40 points
- Allowlist vs. Blocklist table completed: 20 points
- Reflection questions answered thoughtfully: 40 points
- Total: 100 points