Join the group!

Resources

Articles‎ > ‎

Port Knocking with iptables

posted Sep 12, 2011, 11:24 AM by Mike Perry   [ updated Sep 12, 2011, 11:34 AM ]
More info to come soon, but this example should work out of the box. To use it save the script to a file and then, as root, `iptables-restore < script`. This script assumes that your SSH server is running on the default port of 22 and that you are not using iptables for anything else.
# START SCRIPT
*filter

#  Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -i ! lo -d 127.0.0.0/8 -j REJECT

#  Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#  Allows all outbound traffic
#  You can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
#-A INPUT -p tcp --dport 80 -j ACCEPT
#-A INPUT -p tcp --dport 443 -j ACCEPT

#  Allows SSH connections
#  Below is a three port knock which will allow a new ssh session. Client must 'knock' on ports 3820, 5446, and 3015 first. Each knock has a 15 second window before the user has to start over.
#  After the knocking is complete the established connection rule comes into place and this won't be tested.
-A INPUT -p tcp --dport 3820 -m recent --set --rsource --name SSH_AUTH_KNOCK1 -m limit --limit 15/min -j LOG --log-prefix "ssh knock 1 " --log-level 7
-A INPUT -p tcp --dport 5446 -m recent --rcheck --rsource --seconds 15 --name SSH_AUTH_KNOCK1 -m recent --set --rsource --name SSH_AUTH_KNOCK2 -m limit --limit 15/min -j LOG --log-prefix "ssh knock 2 " --log-level 6
-A INPUT -p tcp --dport 3015 -m recent --rcheck --rsource --seconds 15 --name SSH_AUTH_KNOCK2 -m recent --set --rsource --name SSH_AUTH -m limit --limit 15/min -j LOG --log-prefix "ssh knock 3 " --log-level 6
-A INPUT -p tcp --dport 22 -m state --state NEW -m recent --rcheck --rsource --seconds 15 --name SSH_AUTH -j ACCEPT

# Allow ping
#-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# Deny Ping
-A INPUT -p icmp -j DROP

# log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Reject all other inbound - default deny unless explicitly allowed policy
-A INPUT -j DROP
-A FORWARD -j DROP

COMMIT
#END SCRIPT
Comments