Logo

dev-resources.site

for different kinds of informations.

How to install and configure Fail2ban for protecting SSH and Nginx

Published at
12/5/2024
Categories
linux
security
fail2ban
vps
Author
xinitd
Categories
4 categories in total
linux
open
security
open
fail2ban
open
vps
open
Author
6 person written this
xinitd
open
How to install and configure Fail2ban for protecting SSH and Nginx

Your virtual private servers (VPS) is under brute-force attacks by SSH protocol, or bad bots crawling your site and searching locations like admin panels, index.php files, etc? I tried to find solution for protecting projects in WEB. His name - Fail2ban.

Note: this is not completely 100 percent protection, but is better than nothing.

Here some examples of bad bots' headers:

Go-http-client/1.1
python-requests/2.32.3
Python/3.11 aiohttp/3.9.3
Python-urllib/3.8
python-httpx/0.27.0
Ruby
curl/7.61.1
libwww-perl/5.820
lychee/0.11.1
Enter fullscreen mode Exit fullscreen mode

What is Fail2ban? This is software for protecting services, connected to network, like Apache, Nginx, OpenSSH, Postfix, Asterisk, and so on. Fail2ban protect from brute-force attacks, incorrect authentication attempts, bad-bots crawling, etc...

First you need to install Fail2ban. Before installation please see official installation guide on GitHub. Maybe something has been changed after this article published.

How to install in Debian/Ubuntu:

sudo apt update && sudo apt upgrade -y
sudo apt install fail2ban -y
Enter fullscreen mode Exit fullscreen mode

How to install in CentOS/CentOS Stream:

sudo yum update -y
sudo yum install epel-release -y && sudo yum install fail2ban -y
Enter fullscreen mode Exit fullscreen mode

Start and enable in autorun Fail2ban service:

sudo systemctl start fail2ban
sudo systemctl enable fail2ban
Enter fullscreen mode Exit fullscreen mode

Create new /etc/fail2ban/jail.local file and put next configurations:

[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.0.0/24 your_external_address
findtime = 10m
maxretry = 3
bantime = 3600m
Enter fullscreen mode Exit fullscreen mode

Here you may change values as you need.

Configuration for protect OpenSSH service:

[sshd]
enabled = true
port    = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
Enter fullscreen mode Exit fullscreen mode

Configuration for Nginx authentication protection:

[nginx-http-auth]
enabled  = true
port    = http,https
logpath = %(nginx_error_log)s
Enter fullscreen mode Exit fullscreen mode

For limitation HTTP requests:

[nginx-limit-req]
enabled  = true
port    = http,https
logpath = %(nginx_error_log)s
Enter fullscreen mode Exit fullscreen mode

For protection from badbots copy apache-badbots file's example:

sudo cp /etc/fail2ban/filter.d/apache-badbots.conf /etc/fail2ban/filter.d/nginx-badbots.conf
Enter fullscreen mode Exit fullscreen mode

In file /etc/fail2ban/filter.d/nginx-badbots.conf add to the end of badbotscustom variable heeded user-agents:

badbotscustom = |Custom-AsyncHttpClient|^\-$|Go-http-client\/.*|python-requests\/.*|Python\/.*\ aiohttp\/.*|python-httpx\/.*|python-requests\/.*|Scrapy\/.*|Python-urllib\/.*|curl\/.*|lychee\/.*|\*|
Enter fullscreen mode Exit fullscreen mode

And put to end of the file /etc/fail2ban/jail.local following config lines for activating jail:

[nginx-badbots]
enabled  = true
port    = http,https
logpath = %(nginx_access_log)s
findtime = 10m
maxretry = 1
bantime = 3600m
Enter fullscreen mode Exit fullscreen mode

Save and exit from text editor, restart Fail2ban service for apply changes:

sudo systemctl restart fail2ban
Enter fullscreen mode Exit fullscreen mode

Check how your jails working:

sudo fail2ban-client status sshd
sudo fail2ban-client status nginx-http-auth
sudo fail2ban-client status nginx-limit-req
sudo fail2ban-client status nginx-bad-request
sudo fail2ban-client status nginx-badbots
Enter fullscreen mode Exit fullscreen mode

Are my posts is helpful? You may support me on Patreon.

vps Article's
30 articles in total
Favicon
Como realizar o Deploy de Projetos Web em uma VPS
Favicon
Why a Web Hosting Provider Needs SafeLine WAF
Favicon
Segurança de servidores remotos
Favicon
How to Install Ubuntu Desktop GUI on a VPS with RDP Access and get 5 Gbps internet speed
Favicon
Secure Your Ubuntu VPS: Restrict SSH Access to a Specific IP
Favicon
Understanding VPS Servers: A Comprehensive Guide
Favicon
How to setup Ghost in a VPS using Docker, Mailgun and SQLite
Favicon
Choosing a VPS/VDS Server in 2024: My TOP-4 Picks
Favicon
VPS Servers for Linux - Everything You Need to Know
Favicon
How to install and configure Fail2ban for protecting SSH and Nginx
Favicon
Terabix: Affordable Malaysian Cloud VMs Quick Review
Favicon
Increase Debian based Linux VPS server’s security
Favicon
A $5 to $10 VPS can do a lot more than you think
Favicon
Why VPS Hosting is the Future of Small Business Websites in 2024–2025
Favicon
Free VPS Hosting and Windows VPS to Understand How They Work on Websites
Favicon
Cheapest VPS Offers for 2024: Reliable & Budget-Friendly Solutions for Cloud Hosting
Favicon
Dubai VPS Hosting: How It Offers the Best Solution for Your Business
Favicon
How can I upgrade my VPS hosting as my website grows?
Favicon
Path-Based Reverse Proxying with Caddy
Favicon
Switch from NGINX to Caddy
Favicon
Access Instagram Freely from Turkey: Wireguard VPN Setup on Ubuntu 22.04
Favicon
Shared vs. VPS Server: Which Option Is Best for You?
Favicon
Transforme seus HTMLs em PDFs com facilidade usando wkhtmltopdf em sua VPS
Favicon
Alibaba Cloud Promo Center
Favicon
Web Hosting Services: Ultimate Guide to Choosing the Best Provider
Favicon
Deploy docker containers in VPS with GitHub Actions
Favicon
GDPR Compliance for Businesses Using VPS Hosting
Favicon
How to Deploy your NextJS App on a VPS
Favicon
Deploying Keycloak to a VPS Using Docker-compose, Nginx, Certbot, and SSL
Favicon
How to Install Focalboard on Ubuntu 22.04

Featured ones: