I currently have these iptables rules on a Linux router, to block repeated SSH connections:
-A wan_ingress -p tcp --dport 22 -m state --state NEW -m recent --name sshthrottle --set -m comment --comment "Tag incoming SSH requests" -A wan_ingress -m state --state NEW -m recent --name sshthrottle --rcheck --seconds 300 --reap --hitcount 3 -j logdrop -m comment --comment "Log and drop packets" -A logdrop -j LOG --log-prefix "sshthrottle:drop " -A logdrop -j DROP However the problem is that it also blocks repeated successful connections, in that if I connect and disconnect successfully three times within five minutes, I also get blocked. Only for five minutes though, so until now I've just put up with it.
Since there are SSH brute forcers that automatically throttle themselves, I'd now like to extend the timeout from five minutes to a day, but this would mean I can only SSH in a limited number of times a day before I get blocked too.
So I'm wondering what the best way would be to add an additional iptables rule that says if the SSH connection was successful, remove the IP from the list used for counting the number of connection attempts.
I am thinking it would be using something like --state ESTABLISHED and a second packet counter to confirm the SSH session has been actually established (to avoid counting a lengthy unsuccessful login attempt as an established connection) however I'm not quite sure how to get that rule to trigger a removal of the IP from the original list.