Brute-force break-in attempts are quite frequent against the SSH server. However, there is an open source software that can help you deal with this problem automatically, namely fail2ban. Fail2ban provides a way to protect private virtual server( VPS ) from malicious behavior by intruders or hackers automatically. This program works by scanning through log files and respond to unsuccessful login attempts and repeated login attempts. Here are the steps on how to implement fail2ban and steps have been tested on CentOS 6, CentOS 7, RHEL 6 and RHEL 7.
1. Install fail2ban :
# yum install fail2ban -y
2. Make a copy of original config file :
# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
3. Update jail.local configuration file :
# vi /etc/fail2ban/jail.local
Add as below :
[ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] sendmail-whois[name=SSH, dest=receipient@gmail.com, sender=fail2ban@ehowstuff.com, sendername="Fail2Ban"] logpath = /var/log/secure maxretry = 5
4. Configure the prefered “bantime”, “findtime” and “maxretry” before a host get banned :
# vi /etc/fail2ban/jail.local
Update to the following :
.. .. # "bantime" is the number of seconds that a host is banned. bantime = 7200 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 600 # "maxretry" is the number of failures before a host get banned. maxretry = 3 .. ..
5. Verify sshd filter file :
You can verify the default sshd filter file.
# vi /etc/fail2ban/filter.d/sshd.conf
6. Restart fail2ban :
# service fail2ban restart
7. After a few hours of implementation, fail2ban start capturing and banned for such violence and attempts to guess the password for my VPS. Look at the log at path /var/log/secure for monitoring :
# tail -f /var/log/secure
Mar 3 13:37:59 rn sshd[30681]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57 user=root Mar 3 13:38:02 rn sshd[30681]: Failed password for root from 115.231.218.57 port 2919 ssh2 Mar 3 13:38:05 rn sshd[30681]: Failed password for root from 115.231.218.57 port 2919 ssh2 Mar 3 13:38:07 rn sshd[30681]: Failed password for root from 115.231.218.57 port 2919 ssh2 Mar 3 13:38:09 rn sshd[30681]: Failed password for root from 115.231.218.57 port 2919 ssh2 Mar 3 13:38:12 rn sshd[30681]: Failed password for root from 115.231.218.57 port 2919 ssh2 Mar 3 13:38:13 rn sshd[30681]: PAM 4 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57 user=root Mar 3 13:38:48 rn sshd[30702]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57 user=root Mar 3 13:38:50 rn sshd[30702]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:38:52 rn sshd[30702]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:38:54 rn sshd[30702]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:38:56 rn sshd[30702]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:38:58 rn sshd[30702]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:38:58 rn sshd[30702]: PAM 4 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57 user=root Mar 3 13:39:00 rn sshd[30704]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57 user=root Mar 3 13:39:02 rn sshd[30704]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:39:04 rn sshd[30704]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:39:07 rn sshd[30704]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:39:09 rn sshd[30704]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:39:11 rn sshd[30704]: Failed password for root from 115.231.218.57 port 3090 ssh2 Mar 3 13:39:12 rn sshd[30704]: PAM 4 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57 user=root Mar 3 13:39:24 rn sshd[30708]: Invalid user admin from 115.231.218.57 Mar 3 13:39:24 rn sshd[30708]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57 Mar 3 13:39:26 rn sshd[30708]: Failed password for invalid user admin from 115.231.218.57 port 2898 ssh2 Mar 3 13:39:27 rn sshd[30708]: Failed password for invalid user admin from 115.231.218.57 port 2898 ssh2 Mar 3 13:39:30 rn sshd[30708]: Failed password for invalid user admin from 115.231.218.57 port 2898 ssh2 Mar 3 13:39:33 rn sshd[30708]: Failed password for invalid user admin from 115.231.218.57 port 2898 ssh2 Mar 3 13:39:35 rn sshd[30708]: Failed password for invalid user admin from 115.231.218.57 port 2898 ssh2 Mar 3 13:39:35 rn sshd[30708]: PAM 4 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=115.231.218.57
8. Fail2ban start to ban and unban after two hours :
# tail -f /var/log/messages
Mar 3 13:38:13 rn fail2ban.actions[25912]: WARNING [ssh-iptables] Ban 115.231.218.57 Mar 3 13:38:58 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:39:12 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:39:33 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:39:43 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:39:56 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:40:20 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:40:30 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:40:41 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 13:40:51 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:30:32 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:30:46 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:31:35 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:32:34 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:32:51 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:33:02 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:33:32 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:33:43 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:33:54 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:34:06 rn fail2ban.actions[25912]: INFO [ssh-iptables] 115.231.218.57 already banned Mar 3 15:38:14 rn fail2ban.actions[25912]: WARNING [ssh-iptables] Unban 115.231.218.57
9. All the ban action followed by the email trigger as per screenshot :