23 Jan, 2009 in Special by admin

How to Block SSH brute force attempts on Ubuntu

It never takes long for the script kiddies to start once you open up ssh to the interweb. Look in your /var/log/auth.log and I’ll bet you find a load of brute force on ssh:

Failed password for invalid user  from 61.19.237.163 port 50739 ssh2
Failed password for invalid user test from 61.19.237.163 port 50950 ssh2
Failed password for invalid user user from 61.19.237.163 port 51105 ssh2
Failed password for invalid user administrator from 61.19.237.163 port 51254 ssh2

This is a “1337 h4X0r” (read:kiddie) running a dictionary against your ssh . If you have chosen strong passwords, then you are a bit safer, but there are better ways.

Use certificates instead of passwords

Have a look at the passwordless ssh howto here .If you dont want to switch off passwords, there is another way to block brute force :

DenyHosts

This is a script designed to run on your ssh to block brute force , I really like this approach because you can get it to block traffic from the offending ip – so if someone tries ssh and fails, they can’t try anything else from the same ip for a set number of days. This makes it inconvenient, and much easier to try somewhere else. It also logs the names attempted, so you could use those in your own dictionary should you be so inclined…

Download from http://denyhosts.sourceforge.net/index.html

tar zxvf  DenyHosts-2.5.tar.gz # change for whatever version you're using
cd Denyhosts-2.5
 python setup.py install
cd /usr/share/denyhosts
 cp  denyhosts.cfg-dist denyhosts.cfg
 vi denyhosts.cfg
# If you are using Ubuntu, your  LOG should be as below, comment out  the others
SECURE_LOG = /var/log/auth.log
HOSTS_DENY = /etc/hosts.blocked # We use this instead of hosts.deny so we can use TCPWRAPPERS
 later
BLOCK_SERVICE =  # Not essential, but i prefer just locking the buggers out completely.
Be careful of this on web servers etc, you dont want to block an entire internet cafe from
 browsing your site because of a visiting script kiddie.
PURGE_DENY = 7d # Removes ips from the list after 7 days, adjust to taste - default is never
LOCK_FILE = /var/run/denyhosts.pid #Comment out the other LOCK_FILEs
SYNC_SERVER = http://xmlrpc.denyhosts.net:9911 #This is a great idea, it lets users around the
world share info, so misbehaving ips dont even need to try you to get blocked. Disabled by default.
#Edit the other settings to taste, the defaults should be fine for most setups, but you might want
to lower allowed  etc.
 touch /etc/hosts.blocked
 vi /etc/hosts.deny
:/etc/hosts.blocked
sshd::spawn python2.4 /usr/bin/denyhosts.py --purge -c /etc/denyhosts.cfg: allow

Job done.

Bookmark This

One Response so far | Have Your Say!

  1. BrandonG777 - Gravatar

    BrandonG777  |  May 22nd, 2009 at 9:52 pm #

    Outdated.

    sudo apt-get install denyhosts

    * OPTIONAL *
    sudo vim/etc/denyhosts.conf

Leave a Feedback

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>