House of Shield - Defend Your Network Perimeter
# Configure your rule above and click "Generate Rule"
# UFW syntax will appear here
# firewalld syntax will appear here
Packet arrives
NAT decisions
Your rules here
Destination
Origin
Your rules here
NAT decisions
Packet leaves
Packet arrives
NAT decisions
Your rules here
NAT decisions
Packet leaves
Packet is allowed through
Packet is blocked
Packet is logged
Click any template to load it into the rule builder
Permit incoming SSH connections on port 22 (TCP)
Port: 22/TCP Action: ACCEPT
Permit web traffic on ports 80 and 443 (TCP)
Ports: 80,443/TCP Action: ACCEPT
Permit ICMP echo requests for network diagnostics
Protocol: ICMP Action: ACCEPT
Permit DNS queries on port 53 (UDP/TCP)
Port: 53/UDP Action: ACCEPT
Permit responses to outgoing connections
State: ESTABLISHED Action: ACCEPT
Drop all traffic from a malicious IP address
Source: 192.168.1.100 Action: DROP
Record all SSH connection attempts
Port: 22/TCP Action: LOG
Permit MySQL connections from local network
Port: 3306/TCP Source: 192.168.1.0/24
# Secure default - deny all incoming, allow outgoing iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # Allow loopback iptables -A INPUT -i lo -j ACCEPT # Allow established connections iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
Incoming packets destined for this machine
Outgoing packets originating from this machine
Packets passing through this machine to another destination
Controls packets coming INTO your server from the network
# Allow incoming SSH iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow incoming HTTP/HTTPS iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
Controls packets going OUT from your server to the network
# Allow outgoing DNS iptables -A OUTPUT -p udp --dport 53 -j ACCEPT # Allow outgoing HTTP/HTTPS iptables -A OUTPUT -p tcp -m multiport --dports 80,443 -j ACCEPT
Controls packets being routed THROUGH your server to other destinations
# Allow forwarding from internal to external network iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT # Allow forwarding for established connections iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
What happens to packets that don't match any rule
# View current policies iptables -L | grep policy # Set default DROP (secure - deny all, allow specific) iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # Set default ACCEPT (permissive - allow all, block specific) iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT
Uncomplicated Firewall (UFW) - A user-friendly frontend for iptables
# Enable UFW sudo ufw enable # Disable UFW sudo ufw disable # Check status sudo ufw status sudo ufw status verbose sudo ufw status numbered # Reset to defaults sudo ufw reset
# Allow specific port sudo ufw allow 22 sudo ufw allow 80/tcp sudo ufw allow 53/udp # Allow port range sudo ufw allow 6000:6007/tcp # Allow from specific IP sudo ufw allow from 192.168.1.100 # Allow from subnet to specific port sudo ufw allow from 192.168.1.0/24 to any port 22 # Allow specific service sudo ufw allow ssh sudo ufw allow http sudo ufw allow https
# Deny specific port sudo ufw deny 23 # Deny from specific IP sudo ufw deny from 192.168.1.100 # Deny to specific IP sudo ufw deny to 192.168.1.100 # Deny from subnet sudo ufw deny from 10.0.0.0/8
# Delete by rule specification sudo ufw delete allow 80 # Delete by rule number sudo ufw status numbered sudo ufw delete 3 # Delete specific rule sudo ufw delete allow from 192.168.1.100
# Set default policies sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw default deny forward # Rate limiting (prevents brute force) sudo ufw limit ssh sudo ufw limit 22/tcp # Application profiles sudo ufw app list sudo ufw allow 'Nginx Full' sudo ufw allow 'OpenSSH' # Logging sudo ufw logging on sudo ufw logging off sudo ufw logging low sudo ufw logging medium sudo ufw logging high
# Web server setup sudo ufw allow ssh sudo ufw allow http sudo ufw allow https sudo ufw enable # Database server (MySQL) from local network only sudo ufw allow from 192.168.1.0/24 to any port 3306 # Allow ping sudo ufw allow proto icmp # Block specific IP sudo ufw deny from 203.0.113.100 # Allow specific IP to all ports sudo ufw allow from 192.168.1.50
Start with a secure baseline - deny everything by default, then explicitly allow what you need
# iptables approach iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT # UFW approach sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw default deny forward
Every open port is a potential attack vector
netstat -tulpn or ss -tulpnPrevent brute force attacks by limiting connection attempts
# UFW rate limiting (easy) sudo ufw limit ssh sudo ufw limit 22/tcp # iptables rate limiting (more control) iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 4 -j DROP # Explanation: Allow only 3 new SSH connections per 60 seconds per IP
Permit return traffic for connections you initiated
# Allow established and related connections iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT # Or using state module iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Monitor and log potential attacks or unusual activity
# Log dropped packets (before DROP policy) iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables-DROP: " --log-level 7 # Log specific port attempts iptables -A INPUT -p tcp --dport 23 -j LOG --log-prefix "Telnet-attempt: " # View logs sudo tail -f /var/log/syslog | grep iptables sudo journalctl -f | grep iptables
Limit access to sensitive services based on source address
# Allow SSH only from your office network iptables -A INPUT -p tcp --dport 22 -s 203.0.113.0/24 -j ACCEPT # Allow database only from app servers iptables -A INPUT -p tcp --dport 3306 -s 192.168.1.10 -j ACCEPT iptables -A INPUT -p tcp --dport 3306 -s 192.168.1.11 -j ACCEPT # UFW equivalent sudo ufw allow from 203.0.113.0/24 to any port 22
Always allow local communication
# Allow all loopback traffic iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT # UFW automatically allows loopback
Make your rules survive reboots and maintain backups
# Save iptables rules sudo iptables-save > /etc/iptables/rules.v4 sudo ip6tables-save > /etc/iptables/rules.v6 # Install iptables-persistent (Debian/Ubuntu) sudo apt install iptables-persistent # Or use netfilter-persistent sudo netfilter-persistent save # UFW persists automatically when enabled # Backup your rules sudo cp /etc/iptables/rules.v4 /root/iptables-backup-$(date +%Y%m%d).v4
Periodically review your firewall configuration
# List all rules sudo iptables -L -n -v --line-numbers sudo ufw status numbered # Check for open ports sudo netstat -tulpn sudo ss -tulpn # Review logs sudo grep -i iptables /var/log/syslog sudo journalctl -u ufw
Always test firewall changes to avoid lockouts
# Emergency reset cron job (removes itself after 5 minutes) at now + 5 minutes <<< "iptables -F; iptables -X; iptables -P INPUT ACCEPT"
Test your knowledge of Linux firewall concepts and best practices