Table of Contents
-
Understanding iptables Modules
- What Are Modules?
- How Modules Work
-
Key Categories of iptables Modules
- 2.1 Protocol-Specific Modules
- 2.2 Connection Tracking Modules
- 2.3 Logging & Monitoring Modules
- 2.4 Rate Limiting & Traffic Control Modules
- 2.5 Network Address Translation (NAT) Modules
- 2.6 Miscellaneous Modules
1. Understanding iptables Modules
What Are Modules?
Iptables modules are shared libraries (*.ko files) that extend the core functionality of the netfilter framework. They add new match conditions (criteria a packet must meet) and target actions (what to do with matching packets). Without modules, iptables is limited to basic filtering; with modules, you can implement stateful firewalls, rate limiting, application-level inspection, and more.
How Modules Work
Modules are loaded dynamically when referenced in an iptables rule (via the -m or --match flag). Modern Linux systems automatically load required modules using modprobe, but you can also load them manually (e.g., modprobe xt_limit for the rate-limiting module).
To check if a module is available, use:
iptables -m <module_name> --help
For example, iptables -m state --help lists options for the state module.
2. Key Categories of iptables Modules
2.1 Protocol-Specific Modules
These modules add granular control for specific protocols (TCP, UDP, ICMP, etc.) beyond basic port matching.
tcp Module
Enables matching TCP flags (e.g., SYN, ACK) and options.
Syntax:
-m tcp --tcp-flags <mask> <complement> [--tcp-option <option>]
Examples:
- Block TCP packets with only the SYN flag set (common in SYN floods):
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,ACK SYN -j DROP - Allow only packets with SYN-ACK flags (established connections):
iptables -A INPUT -p tcp -m tcp --tcp-flags SYN,ACK SYN,ACK -j ACCEPT
udp Module
Extends UDP filtering with port ranges and checksums.
Syntax:
-m udp --source-port <port|range> --destination-port <port|range>
Example: Allow UDP traffic on ports 1000–2000:
iptables -A INPUT -p udp -m udp --dport 1000:2000 -j ACCEPT
icmp Module
Controls ICMP traffic (e.g., ping) by message type/code.
Syntax:
-m icmp --icmp-type <type|code>
Example: Allow only “Echo Request” (ping) and block “Destination Unreachable” messages:
iptables -A INPUT -p icmp -m icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp -m icmp --icmp-type destination-unreachable -j DROP
2.2 Connection Tracking Modules
These modules enable stateful firewalling by tracking the state of network connections, a critical feature for modern networks.
conntrack Module
The core module for connection tracking. It monitors connections and assigns states (NEW, ESTABLISHED, RELATED, INVALID).
Syntax:
-m conntrack --ctstate <state> [--ctproto <protocol>] [--ctorigsrc <IP>]
Example: Allow established/related connections (most critical for usability):
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
state Module (Legacy)
A wrapper around conntrack for backward compatibility. Uses --state instead of --ctstate.
Example: Same as above, using state:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
Helper Modules (e.g., ftp, sip)
Protocols like FTP or SIP use multiple connections (e.g., data/control channels). Helper modules track these “related” connections.
Example: Allow FTP passive mode by loading the ftp helper:
modprobe nf_conntrack_ftp # Load the helper module
iptables -A INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT # Control channel
iptables -A INPUT -m conntrack --ctstate RELATED -j ACCEPT # Related data channels
2.3 Logging & Monitoring Modules
These modules help debug rules or monitor traffic by logging packets.
log Module
Logs packets to the kernel ring buffer (view with dmesg or journalctl).
Syntax:
-m log --log-prefix "<message>" [--log-level <level>] [--log-tcp-sequence] [--log-ip-options]
Example: Log denied SSH attempts with a custom prefix:
iptables -A INPUT -p tcp --dport 22 -j LOG --log-prefix "SSH DENIED: " --log-level 4
iptables -A INPUT -p tcp --dport 22 -j DROP
nflog Module (Modern Alternative)
Sends logs to userspace via netlink (more efficient than log). Use with tools like ulogd2.
Example: Log to nflog group 10:
iptables -A INPUT -p icmp -m nflog --nflog-group 10 --nflog-prefix "ICMP: " -j ACCEPT
2.4 Rate Limiting & Traffic Control Modules
Prevent abuse by limiting traffic volume or connection counts.
limit Module
Limits packets per time unit (e.g., 100 packets/minute).
Syntax:
-m limit --limit <rate> [--limit-burst <burst>]
--limit: Rate (e.g.,10/min,1/sec).--limit-burst: Initial “burst” of packets allowed before limiting.
Example: Limit ping requests to 10 per minute (prevents ping floods):
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 10/min --limit-burst 5 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP # Block excess
recent Module
Tracks recent IP addresses to block brute-force attacks (e.g., SSH).
Syntax:
-m recent --name <list> [--set|--rcheck|--update|--remove] [--seconds <time>] [--hitcount <n>]
Example: Block IPs with 5+ SSH attempts in 60 seconds:
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --rcheck --seconds 60 --hitcount 5 -j DROP
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --set -j ACCEPT # Add to list on first attempt
connlimit Module
Limits the number of concurrent connections per IP.
Syntax:
-m connlimit --connlimit-above <n> [--connlimit-mask <cidr>]
Example: Allow max 3 SSH connections per IP:
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP
2.5 Network Address Translation (NAT) Modules
NAT modules modify source/destination IPs/ports, critical for routing (e.g., home routers).
masquerade Module
Dynamic SNAT for systems with changing public IPs (e.g., DHCP).
Example: Masquerade local traffic (192.168.1.0/24) via the WAN interface (eth0):
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
dnat Module
Redirects incoming traffic to a local IP/port (port forwarding).
Example: Forward external port 8080 to internal server 192.168.1.100:80:
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.100:80
redirect Module
Redirects traffic to the localhost (e.g., for proxies).
Example: Redirect all port 80 traffic to a local proxy on port 3128:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3128
2.6 Miscellaneous Modules
owner Module
Matches packets by process owner (UID/GID).
Example: Block traffic from user “malicious_user” (UID 1001):
iptables -A OUTPUT -m owner --uid-owner 1001 -j DROP
time Module
Matches packets by time (e.g., block traffic outside business hours).
Syntax:
-m time --timestart <HH:MM> --timestop <HH:MM> [--days <Mon,Tue,...>]
Example: Allow SSH only on weekdays (Mon-Fri) from 9 AM to 5 PM:
iptables -A INPUT -p tcp --dport 22 -m time --timestart 09:00 --timestop 17:00 --days Mon,Tue,Wed,Thu,Fri -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP
3. Practical Examples: Combining Modules
Modules shine when combined to solve complex problems. Here’s a real-world scenario:
Secure Home Server Rule Set
- Allow established/related connections.
- Block brute-force SSH (5 attempts/60s).
- Limit ping to 10/min.
- Log denied traffic.
# Default policy: deny all
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# Allow loopback
iptables -A INPUT -i lo -j ACCEPT
# Allow established/related
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# SSH: block brute-force, limit connections
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --rcheck --seconds 60 --hitcount 5 -j DROP
iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j DROP
iptables -A INPUT -p tcp --dport 22 -m recent --name SSH --set -j ACCEPT
# Ping: limit to 10/min
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 10/min --limit-burst 5 -j ACCEPT
# Log denied traffic
iptables -A INPUT -j LOG --log-prefix "DENIED: " --log-level 4
4. Conclusion
Iptables modules transform a basic firewall into a versatile tool for securing networks, controlling traffic, and debugging issues. From stateful tracking with conntrack to brute-force protection with recent, modules enable granular control tailored to your needs.
To master iptables, experiment with modules in a test environment (e.g., a VM) and refer to the iptables-extensions man page for deep dives. With practice, you’ll build robust, efficient firewall rules that balance security and usability.