Security

Securing SSH Against Bruteforce Attacks

Using the iptables recent module to cap new SSH connections per source, log and drop the excess, and let whitelisted hosts through untouched.

Tony Mattke · 2009.06.07 · 2 min read

This is one of the methods I’ve used in the past to secure a Linux host against brute force ssh attacks. While its not a perfect method, it does a good job of preventing 100s of brute force entries in your syslog.

bash
# Create a new table...
iptables -N SSH_WHITELIST

# On the input chain, mark new packets with the SSH 'tag'
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH

# Push new ssh connections through the SSH_WHITELIST table
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j SSH_WHITELIST

# Limit 4 connections from an ip per 60 seconds, to be more strict, use 300 seconds.
# Log connections that go over this limit and drop the packets.
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update \
          --seconds 60 --hitcount 4 --rttl --name SSH -j ULOG --ulog-prefix SSH_brute_force
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update \
          --seconds 60 --hitcount 4 --rttl --name SSH -j DROP

# Check source IPs, if they match trusted hosts, remove SSH 'tag' and accept the traffic.
iptables -A SSH_WHITELIST -s 10.0.1.1 -m recent --remove --name SSH -j ACCEPT
iptables -A SSH_WHITELIST -s 192.168.88.0/24 -m recent --remove --name SSH -j ACCEPT

Related Posts

Automation & Tools · 1 min read

SSH Wrapper Script

2008.11.30

Ok — this is my first script that I’m posting here. Its a VERY simple ssh wrapper script that you can place in your path, preferably in ~/bin bash #!

Security · 8 min read

Best Practices and Securing Cisco IOS

2011.09.06

Everyone has different views on hardening IOS, and while I do not claim to be an expert, these are the practices that I commonly use when bringing up a new device.

Security · 2 min read

Poor Man's VPN Connection

2010.04.20

Have you ever needed to access a site that had an IP restriction, or one inside your remote network?

Comments

5 archived comments from Disqus 2009–2014
Tony
@Justin Wilson
Well, thats the thing about iptables, there is no way to save the entries, so they’re only blocked for the timer amount… But that’s usually enough to stop the attack and they’ll move on elsewhere to an easier target. I tested another method, but never got it perfected.. I may work on it some more and post a follow up..
Tom
I used this article to block abusive IP addresses via iptables and I successfully saved the firewall rules, so they are available on the next boot.
Tim
It is possible to save the iptable rules so they are available on the next boot. I used this article to block abusive IP addresses via iptables and successfully saved the firewall rules.
Justin Wilson
Do the entries stay in there forever or are they purged at one point?
Dustin
If you keep giving away all your secrets and I won’t have to hire you for consulting work anymore! But seriously, everyone here loves your work. Not only do you help us out, but you educate us as we progress and move forward.