Using the recent
extension
Based on @Tomasz's idea, I created the rule in ufw and inspected the result in iptables. Seems ufw uses the recent
extension:
:LOG_REJECT - [0:0]
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --set
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 30 --hitcount 6 -j LOG_REJECT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A LOG_REJECT -m limit --limit 3/min -j LOG --log-prefix "[LIMITED SSH]"
-A LOG_REJECT -j REJECT --reject-with icmp-port-unreachable
Using the limit
extension
Which is slightly neater/simpler:
:LOG_REJECT - [0:0]
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -m limit --limit 12/minute -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j LOG_REJECT
-A LOG_REJECT -m limit --limit 3/minute -j LOG --log-prefix "[LIMITED SSH]" --log-level 7
-A LOG_REJECT -j REJECT --reject-with icmp-port-unreachable
(Though I'm unsure if icmp-port-unreachable
is a proper rejection message in this case.)
Using the connlimit
extension
...maybe someone else can add that. :)