[Day 6] 3. iptables LAB
Goal
Set up a Stateful Packet Inspection (SPI) Firewall that:
- Allows all outgoing connections.
- Blocks all unwanted incoming connections.
Scenario Overview
-
Two machines:
- VM1: 192.168.80.130
- VM2: 192.168.80.131
-
Both machines have ports 22 (SSH) and 80 (HTTP) open.
-
Tasks:
- Connect to port 22 and port 80 of the same machine.
- Connect between VM1 and VM2 for both ports.
- After firewall configuration:
- VM1 should allow all outgoing traffic.
- VM2 should be blocked from accessing ports 22 of VM1.
HTTP Setup
-
Install required packages on both machines:
bashsudo apt-get install apache2 links- : HTTP client for testing.
linksis similar tolinkscurl
-
Test HTTP connection:
bashlinks http://<IP-address>- Displays the file hosted by Apache.
index.html - Press to exit.
Q
- Displays the
iptables Basics
-
Check if
is installed:iptablesbashsudo which iptables -
Install
if needed:iptablesbashsudo apt-get install iptables -
View current rules:
bashsudo iptables -L -
Change default policies:
bashsudo iptables -P INPUT DROP sudo iptables -P FORWARD DROP sudo iptables -P OUTPUT ACCEPT- : Blocks all incoming traffic by default.
INPUT DROP - : Blocks packets not originating from or destined for the machine.
FORWARD DROP - : Allows all outgoing traffic.
OUTPUT ACCEPT
Allowing Loopback Traffic
Enable loopback (local traffic) to avoid self-blocking:
bashsudo iptables -A INPUT -i lo -j ACCEPT
- : stands for appending the rule.
-A - : Specifies the loopback interface.
-i lo - : Allows the traffic.
-j ACCEPT
Stateful Packet Inspection (SPI)
To establish a TCP connection, a 3-way handshake is performed:
Client → SYN → Server → SYN-ACK → Client → ACK → Server
- SYN packets: Have a state NEW.
- SYN-ACK and ACK packets: Have a state ESTABLISHED.
When trying SSH from Machine 1 to Machine 2, the command
sudo iptables -P INPUT DROPTo allow responses to outgoing connections (e.g., SYN-ACK):
bashsudo iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
- : Loads the state module.
-m state - : Allows incoming packets part of an already established connection.
--state ESTABLISHED
Allowing SSH Traffic
-
To allow SSH (port 22) from all machines:
bashsudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT- : Specifies destination port 22.
--dport 22
-
To allow SSH access to a specific machine:
bashsudo iptables -A INPUT -s <source-IP> -p tcp --dport 22 -j ACCEPT-
Example for the base machine (192.168.80.1):
bashsudo iptables -A INPUT -s 192.168.80.1 -p tcp --dport 22 -j ACCEPT
-
Automating iptables Rules
Manually writing rules is tedious. Use a script to automate it:
-
Create a script:
bashvim fw.sh -
Script content:
bash#!/bin/bash # Flush current rules iptables -F # Default policies iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD DROP # Allow loopback traffic iptables -A INPUT -i lo -j ACCEPT # Allow responses to established connections iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT # Allow SSH traffic only from base machine iptables -A INPUT -s 192.168.80.1 -p tcp --dport 22 -j ACCEPT # Display current rules iptables -L -v -
Save and run the script:
bashsudo chmod u+x fw.sh sudo ./fw.sh
Persistent Rules
Rules vanish after a reboot unless made persistent.
-
Save current rules:
bashsudo mkdir -p /etc/iptables sudo sh -c "iptables-save > /etc/iptables/rules.v4" -
Install
:iptables-persistentbashsudo apt-get install iptables-persistent- Choose Yes for both IPv4 and IPv6.
!3. iptables-save-configuration.png
Additional Commands
-
View rules with line numbers:
bashsudo iptables -L --line-numbers -
Delete a specific rule:
bashsudo iptables -D INPUT <line-number>
Read Next
[Day 6] 4. Installing and Configuring pfSense with Debian Machines
#### **Setup Requirements** - **3 Machines:** - Debian 1 (IP: 192.168.40.10) - Debian 2 (IP: 192.168.50.10) - pfSense (IP: 192.168.4...
[Day 6] 5. Extra
1. **Quantum Computers or Quantum Computing** Quantum computing uses quantum-mechanical phenomena (like superposition and entanglement) to perf...