You have probably seen very simple iptables rules to do this. This is a little bit better.
-A INPUT -i eth0 -p tcp -m tcp --dport 22 -m state --state NEW -m recent --set --name SSH --rsource -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m recent --rcheck --seconds 30 --hitcount 4 --rttl --name SSH --rsource -j REJECT --reject-with tcp-reset -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m recent --rcheck --seconds 30 --hitcount 3 --rttl --name SSH --rsource -j LOG --log-prefix "SSH brute force " -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m recent --update --seconds 30 --hitcount 3 --rttl --name SSH --rsource -j REJECT --reject-with tcp-reset -A INPUT -i eth0 -p tcp -m tcp --dport 22 -j ACCEPT
That’s it. Now what is it? What does it do? How does it work?
OK. Let’s explain in English. You put these five rules in your iptables table filter (the default table).
- The first rule tells the system:
TCP packets are going to come in, that will attempt to establish an SSH connection. Mark them as SSH. Pay attention to the source of the packet.
- The second rule says:
If a packet attempting to establish an SSH connection comes, and it’s the fourth packet to come from the same source in thirty seconds, just reject it with prejudice and stop thinking about it.
- The third and fourth rules mean:
If an SSH connection packet comes in, and it’s the third attempt from the same guy in thirty seconds, log it to the system log once, then immediately reject it and forget about it.
- The fifth rule says:
Any SSH packet not stopped so far, just accept it.
And that’s all. You may want to change your network interface (-i eth0) to the public-facing network interface in your computer / server. You may also want to adjust the number of seconds and hitcount to your tastes. Remember that the second rule has a hitcount that is one higher than the ones following it — this is a precaution to stop the packet from continuing down the chain of rules, so brute forcing won’t spam your logs. Spamming your logs may not be a problem to you, but it is a problem in my environment because the server literally speaks attacks out loud.
Related Posts: On this day...
- Kanye and Jay-Z destroy a Maybach - 2011
- Unwritten 2: A pulse-pounding graphic novel that shows the grim and selfish ways that people use stories - 2010
- Criterion Collection reissues R. Crumb documentary with special features - 2010
- Microsoft Rolling out Retail stores to compete directly with the Apple stores - 2009
- Showdown at the 4chan corral - 2009
- "Open Source Twitter" proposed as antidote to Twitter's DDOS vulnerability - 2009
- Not your normal road on Google Street View - 2009
- VMWARE Users: All your ESX 3.5 are belong to us - 2008