Security: SSHD Brute Force Attacks On The Rise

| 3 Comments
54policetux.gifYou've read the thread in various blogs where yet another smug Unix/Linux/Mac gearhead refuses to admit they have any security issues.  And then the obligatory blog comments flame war ensues.

If you've run any *ix box with sshd Secure Shell service running that takes full exposure to the Internet, say open with the default port 22, and have looked at your /var/log/messages (or wherever your distro stores sshd messages) file, you know that it has become a 'free-for-all'.

Every day, brute force dictionary-based attacks are run from points far and wide across the globe.  Some areas score a higher frequency than others.  China and South Korea are the leaders and the attacks often come overnight.  Here's what a typical brute-force sshd attack looks like as recorded in the log:


Jul 28 21:32:16 server sshd[10855]: Illegal user office from 213.191.74.219
Jul 28 21:32:16 server sshd[10855]: Failed password for illegal user office from 1.2.3.4 port 53033 ssh2
Jul 28 21:32:16 server sshd[10857]: Illegal user samba from 1.2.3.4
Jul 28 21:32:16 server sshd[10857]: Failed password for illegal user samba from 1.2.3.4 port 53712 ssh2
Jul 28 21:32:16 server sshd[10859]: Illegal user tomcat from 1.2.3.4
Jul 28 21:32:16 server sshd[10859]: Failed password for illegal user tomcat from 1.2.3.4 port 54393 ssh2
Jul 28 21:32:16 server sshd[10861]: Illegal user webadmin from 1.2.3.4
Jul 28 21:32:16 server sshd[10861]: Failed password for illegal user webadmin from 1.2.3.4 port 55099 ssh2


Things you can do to harden your system:

  • Edit your /etc/ssh/sshd_config and set "PermitRootLogin no" "Protocol 2"
  • Use passwordless Public Key Authentication, with "UsePAM no" in sshd_config.
  • Make system login ids and passwords as strong as possible
  • Install brute force detection software, such as DenyHosts or fail2ban
  • Move ssh from port 22 to a port above 1024
  • Set up a Firewall rule to 'rate limit' sshd

You should strongly consider using the passwordless Public Key method.  Setting "UsePAM no" will turn off PAM authentication so that remote connect attempts won't prompt for a login/password.  But, there may be times when remote access makes Public Key access impractical.  So, in that situation you'll want to use a "UsePAM yes" setting.

As I've said, port 22 takes a lot of hacking every day, so I've installed DenyHosts.

When the hack involves a fixed ip source, DenyHosts will add the offending ip to /etc/hosts.deny after the fifth failed login attempt.  Denyhosts works by scanning the /var/log/messages file.

When the hack involves a variable (forged) ip, and the scan is done in such a way that each successive login attempt comes from a 'bogus' source ip, these brute force attempts 'fly below the radar' so to speak of DenyHosts.

Moving the port 22 to another one above 1024 can reduce the brute force attempts but is not a guarantee they won't occur. 

You should be able to filter these sshd attacks and you can by adding two rules to your server's Firewall.  In openSUSE 11.0 you can add custom iptable rules to /etc/sysconfig/scripts/SuSEfirewall2-custom.  Putting the rules there will ensure that they will get applied any time you ifup/ifdown your eth0 interface or restart the Firewall or server.

These two lines have eliminated brute force attacks for the most part:

iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 8 --rttl --name SSH -j DROP


The two directives 'rate limit' ssh connection attempts to a maximum of 8 within a one minute time frame.

For those offenders who should be 'banned' from your system, add the file /etc/badips and this script to the SuSEfirewall-custom as well:

### BEGIN BAD IPS
if [ -f /etc/badips ]
then
  for BAD_IP in `cat /etc/badips`
    do
      iptables -I INPUT -s $BAD_IP -j DROP
      echo 'ip '$BAD_IP' banned'
      echo
    done
else
  echo "Can't read /var/badips"
fi
### END BAD IPS
Should you need to ban an ip, add each to /etc/badips on a separate line--restart the Firewall and they're history.

A directive from the root prompt of "iptables -L | more" will confirm your firewall settings.
A directive of "/sbin/SuSEfirewall2 status | more" will show you in summary form the iptable activity.

There are other security measures you can put into place such as Tarpit, but the above recommendations should put the sshd hackers out of business.

Thanks and Be Safe!
--Dietrich

3 Comments

Thanks for the tip Dietrich.

I've been monitoring 'auth.log' and I've been seeing ssh brute force attempts from many locales. Especially China and Saudi Arabia.

Thanks for the firewall tips. I've been seeing brute force attempts on SSHd ever since I set my server up, but did notice an increase in the last few days. They also like to go after my FTP server (PureFTPd).

You are welcome. If you apply the rules it also works for sftp. (ftp is clear text)

Use this short script to test while tailing the log:

#!/bin/bash

for i in `seq 1 10` ; do
echo 'exit $i' | netcat 192.168.1.101 22 ;
done

Best!
--Dietrich

Share this post:
digg del.icio.us reddit Newsvine Google Bookmark FaceBook Stumble Upon Bookmark and Share

Leave a comment

Recent Entries

We Interrupt This Program
We Interrupt This Program By Virginia Heffernan January 2, 2009My video will resume after this ad, in 26 seconds. The…
Linux in 2009: Recession vs. GNU
Linux in 2009: Recession vs. GNUDecember 24, 2008 By Bruce Byfield Pundits and business executives alike are predicting gloomy…
The way is cleared for Debian 5
2 January 2009, 15:10 The way is cleared for Debian 5 The Debian developers have decided to release the…