How to secure your Ubuntu server from brute-force attacks?.
Nowadays more and more people and organizations are going on the cloud for their applications, and its very common practice to have a server on the cloud. As more people are using cloud solutions so do more attackers are after them.
According to Ubuntu data, there are around at least 20 million users are using Ubuntu and that’s a very big number. It would be a gold mine for attackers who keep trying to get access to your server using a brute-force attack, for those who don’t know what the hack is brute-force attack?. its an attempt to crack username and password, using trial and error approach with the hope to get it correct for multiple time, if you keep your password or username very generic or easy to guess they might get into your system.
It’s a very old attack method, however, it’s still very effective and popular within the hacker community. You can read more about brute-force attack here.https://en.wikipedia.org/wiki/Brute-force_attack
It’s always a better idea to secure your system/service/server from such wicked people. Let’s get it done.
I will be using my Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-88-generic x86_64) machine.
Update your system first running below commands in the terminal, my assumption is you are already logged in with Root access using SSH.
apt-get update apt-get upgrade
After a system update add Fail2ban, which is an invasion blocking software framework that protects servers from brute-force attacks, it’s written in the Python.
Fail2Ban scans log files like /var/log/auth.log and bans IP addresses conducting too many failed login attempts. It does this by updating system firewall rules to reject new connections from those IP addresses, for a configurable amount of time.
Fail2Ban comes out-of-the-box ready to read many standard log files, such as those for sshd and Apache, and is easily configured to read any log file of your choosing, for any error you wish.
Though Fail2Ban is able to reduce the rate of incorrect authentication attempts, it cannot eliminate the risk presented by weak authentication. Set up services to use only two factor, or public/private authentication mechanisms if you really want to protect services.
apt-get install fail2ban
Copy the configuration file and do changes
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
Change the ban time from 600 (10 minutes) to 604800 (7 days) is guess this would be more than enough to have peaceful nights 🙂
Nest step is to install logwatch is the tool the aggregate all system logs and make meaningful decisions.
apt-get install logwatch
Logwatch can be configurated to trigger a daily email of your logs.
Add below line at the bottom of the cron job file and replace the example email with your own address.
/usr/sbin/logwatch --output mail --mailto firstname.lastname@example.org --detail high