When the LogWatch report from yesterday (for web.akmai.net) arrived in my Inbox, it had over 20,000 failed SSH login attempts. Today I decided it was finally time to do something about all those attacks.
After looking around a bit, I found several different solutions. Some solutions utilized firewall rules and others monitored your
/var/log/auth.log) log files for multiple failed login attempts and then added those IPs/Hosts to the
I decided to go with the latter method and quickly found a nice tutorial for setting up DenyHosts (be sure to download the latest version (2.6 as of this writing) instead of the older version 2.0). Rather than reinvent the wheel, here is what the DenyHosts website says about itself:
What is DenyHosts?
DenyHosts is a Python script that analyzes the sshd server log messages to determine what hosts are attempting to hack into your system. It also determines what user accounts are being targeted. It keeps track of the frequency of attempts from each host.
Additionally, upon discovering a repeated attack host, the /etc/hosts.deny file is updated to prevent future break-in attempts from that host.
An email report can be sent to a system admin.
Since I was setting up DenyHosts on a RedHat-based machine (CentOS) and not a Debian-based machine, I needed to change this line:
update-rc.d denyhosts defaults
chkconfig denyhosts --add
Other than that, the installation steps were just as the tutorial described. I decided to enable the
ADMIN_EMAIL option so that I would receive an email every time something was added to
hosts.deny, but within minutes of starting DenyHosts I had a dozen attacks with a dozen emails on my BlackBerry. I had to disable
ADMIN_EMAIL to stop the spamming!
To make sure DenyHosts was working properly I tried logging in with the wrong password three times. When I tried to connect again, here is what I received:
ssh_exchange_identification: Connection closed by remote host
DenyHosts also has the ability to report to a central server the hosts that are trying to break in and you can also download a list of hosts that have been reported by others. I choose to opt out of doing this for now. The DenyHosts statistics page is pretty cool. Notice how the majority of the hosts come from China? Hmm.
I quickly discovered that DenyHosts was adding my IP address to the
hosts.deny file. When I watched
/var/log/secure I discovered the problem:
Jun 13 20:18:46 web sshd: reverse mapping checking getaddrinfo for 75-147-49-211-newengland.hfc.comcastbusiness.net failed - POSSIBLE BREAKIN ATTEMPT!
Jun 13 20:18:46 web sshd: Accepted publickey for fooUser from ::ffff:220.127.116.11 port 57926 ssh2
Jun 13 20:18:48 web sshd: Did not receive identification string from ::ffff:18.104.22.168
I'm not entirely sure how to fix this, but for now I added my IP address to
/usr/share/denyhosts/data/allowed-hosts (I had to create this file) which prevents DenyHosts from blocking my IP no matter what (see this FAQ for more info). Also, I had to restart DenyHosts (
/etc/init.d/denyhosts restart) before the change to
allowed-hosts took effect.