Debian Linux Stop SSH User Hacking / Cracking Attacks with DenyHosts Software
Q. I've noticed lots of failed login attempt for my Debian Linux VPS root server account. How do I stop automated bot based SSH attacks on my server?
A. You can use DenyHosts - a Python based script that analyzes the sshd server log messages to determine what hosts are attempting to hack into your system. It is an utility to help sys admins thwart ssh crackers. It also determines what user accounts are being targeted. It keeps track of the frequency of attempts from each host. It will automatically blocks ssh attacks by adding entries to /etc/hosts.deny. DenyHosts will also inform Linux administrators about offending hosts, attacked users and suspicious logins.
Step # 1: Make Sure Python is installed
First, make sure python is installed under Debian / Ubuntu Linux:
# dpkg --list | grep python2
Find out version (DenyHosts requires 2.3 or above version)
$ python -V
Output:
Python 2.5.1
Step # 2: Download DenyHosts
Visit official project home page to grab latest source code or packages. Use apt-get command under Debian / Ubuntu Linux, enter
$ sudo apt-get install denyhost
DenyHosts configuration - /etc/denyhosts.conf
- The default configuration file is /etc/denyhosts.conf.
- You also need to create / update a whitelist in /etc/hosts.allow. For example, if you have static IP assigned by ISP, enter in this file. You can add all the important hosts that you never want blocked.
Step # 1: Setup a whitelist
Open /etc/hosts.allow:
# vi /etc/hosts.allow
Allow sshd from 202.54.1.2 i.e. you never want to block yourself
sshd: 202.54.1.2
Save and close the file. Verify and examines your tcp wrapper configuration file and reports all potential and real problems:
# tcpdchk -v
Step # 1: Configure DenyHosts
Open default configuration file - /etc/denyhosts.conf, enter:
# vi /etc/denyhosts.conf
Setup your email ID so you would receive emails regarding newly restricted hosts and suspicious logins, set this address to match your email address.
ADMIN_EMAIL = vivek@nixcraft.com
Save and close the file. Here is my own sample configuration file for Debian Linux 4.0 server (config file is documented very well, just open and read it):
############ THESE SETTINGS ARE REQUIRED ############ SECURE_LOG = /var/log/auth.log HOSTS_DENY = /etc/hosts.deny PURGE_DENY = BLOCK_SERVICE = sshd DENY_THRESHOLD_INVALID = 5 DENY_THRESHOLD_VALID = 10 DENY_THRESHOLD_ROOT = 1 DENY_THRESHOLD_RESTRICTED = 1 WORK_DIR = /var/lib/denyhosts SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES HOSTNAME_LOOKUP=YES LOCK_FILE = /var/run/denyhosts.pid ############ THESE SETTINGS ARE OPTIONAL ############ ADMIN_EMAIL = vivek@nixcraft.com SMTP_HOST = localhost SMTP_PORT = 25 SMTP_FROM = DenyHostsSMTP_SUBJECT = DenyHosts Report AGE_RESET_VALID=5d AGE_RESET_ROOT=25d AGE_RESET_RESTRICTED=25d AGE_RESET_INVALID=10d ######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE ########## DAEMON_LOG = /var/log/denyhosts DAEMON_SLEEP = 30s DAEMON_PURGE = 1h
Restart the daemon:
# /etc/init.d/denyhosts restart
Further readings:
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Linux / UNIX FAQ:
- How do I block an IP on my Linux server?
- Block ip address of spammers with iptables under Linux
- Linux Set the Block Size for a SCSI Tape Device
- Linux/UNIX: Determine file type
- How do I find out more information about block devices?
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!
Tags: /etc/denyhosts.conf, /etc/hosts.allow, brute force hacking, computer hacking, configuration file, default configuration, DenyHosts, hacking security, log messages, password hacking, server log, ssh brute force attacks, ssh denyhost, ssh denyhosts, ssh dictionary attacks, ssh hacking attacks, tcp wrapper, whitelist ~ Last updated on: February 4, 2008



February 13th, 2008 at 6:10 am
Locking out IP’s after multiple failed sshd login attempts
The following two rules will limit incoming connections to port 22 to no more than 3 attempts in a minute - an more than that will be dropped:
iptables -I INPUT -p tcp –dport 22 -i eth0 -m state –state NEW -m recent –set
iptables -I INPUT -p tcp –dport 22 -i eth0 -m state –state NEW -m recent –update –seconds 60 –hitcount 4 -j DROP
April 13th, 2008 (5 weeks ago) at 5:43 pm
thanks a lot
April 19th, 2008 (4 weeks ago) at 10:08 pm
@kunal
You may need to install fail2ban package.
It’s so simple script that work like your description above.