Skip to content

Instantly share code, notes, and snippets.

@jriguera
Created March 9, 2015 00:57
Show Gist options
  • Save jriguera/31d8040e5c91464c91fd to your computer and use it in GitHub Desktop.
Save jriguera/31d8040e5c91464c91fd to your computer and use it in GitHub Desktop.
Limit ssh connections
#!/bin/sh
# requires hashlimit
iptables -I INPUT -p tcp -m tcp --dport 22 -m state --state NEW \
-m hashlimit --hashlimit 1/min \
--hashlimit-burst 5 --hashlimit-mode srcip \
--hashlimit-name SSH --hashlimit-htable-expire 120000 \
-j ACCEPT
iptables -I INPUT -p tcp -m tcp --dport 22 \
--tcp-flags SYN,RST,ACK SYN -j DROP
iptables -I INPUT -p tcp -m state --state NEW \
-m tcp --dport 22 -j ACCEPT
# The 1st rule allows up to 5 connections per min. After the limit of 5
# connections per min is reached, the second rule becomes active and
# the hashlimit module starts to countdown from 2 minutes (2 x 60 000 milliseconds).
# If you connect within 1 minute, the hashlimit counter is reset to 120000.
# If you connect after 1 minute, you drop to the 3rd rule and are allowed access.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment