Skip to content

Bot & Web Scanner Protection with Fail2ban

Reference Hardening Guide

This guide provides an optional reference configuration for host-level Nginx + Fail2ban hardening. Because log formats, firewall backends, and upstream proxies vary across environments, always verify that /var/log/nginx/access.log records real client IPs and dry-run each filter with fail2ban-regex against your existing logs before enabling enforcement.

Internet-facing Coneshare instances are regularly targeted by automated bots and vulnerability scanners probing for unrelated CMS or config paths (e.g., .env, wp-config.php, wp-login.php, .git, .htaccess).

While Coneshare safely ignores or returns 404 Not Found for nonexistent routes, automated scans can:

  • Consume network bandwidth and reverse proxy worker connections.
  • Clutter access logs with thousands of probe attempts.
  • Trigger unnecessary noise in error monitoring systems.

Using Fail2ban alongside your Nginx reverse proxy allows you to automatically detect clear scanner patterns and block abusive IP addresses at the firewall level before they reach your services.


How It Works

Client (Scanner / Bot)
       │
       ▼
[ Nginx Reverse Proxy ] ──( logs requests, status codes & User-Agents )──► /var/log/nginx/access.log
       │                                                                            │
       │                                                                            ▼
       │                                                                   [ Fail2ban Daemon ]
       │                                                                 ( evaluates 3 filters )
       │                                                                            │
       ▼                                                                            ▼
[ Drops connection via Firewall ] ◄───( adds ban rule to firewall )─────────────────┘
  1. Nginx logs all incoming requests, status codes, and User-Agents to /var/log/nginx/access.log.
  2. The Fail2ban daemon monitors the access log against three conservative filters designed to avoid false bans on legitimate traffic:
  3. Vulnerability Scanner Filter (nginx-scanner): Catches direct probes for well-known sensitive/CMS paths (/.env, /wp-config.php, /wp-login.php, /xmlrpc.php, /.git, /.htaccess, /phpinfo.php) and raw binary protocol probes (hex-escaped \x... requests returning 400). Legitimate document downloads, searches, and API calls under /api/, /files/, and /view/ are never matched.
  4. Aggressive 404 Filter (nginx-404): Catches high-rate directory fuzzing while exempting /api/, /mcp/, /.well-known/, and standard browser assets (favicon.ico, robots.txt, apple-touch-icon).
  5. Known Attack Tool Filter (nginx-badbots): Blocks unmistakable vulnerability scanner User-Agents (nikto, sqlmap, nuclei, zgrab, masscan, gobuster, dirbuster, wpscan) without penalizing legitimate scripting tools (curl, wget, python) used by API, webhook, or MCP clients.
  6. If an IP exceeds the configured threshold, Fail2ban temporarily or persistently bans it at the host firewall.

Prerequisites

  • Coneshare deployed directly behind a host Nginx reverse proxy (see Nginx Reverse Proxy Setup).
  • Root or sudo access on the host server.

Do Not Use Host Fail2ban Behind Cloudflare / CDN Without Real-IP Rules

If your server sits behind Cloudflare, another CDN, or a cloud load balancer (10.x / 172.16.x):

  • Without Nginx ngx_http_realip_module (set_real_ip_from), Fail2ban will either see the load balancer's private IP (which is in ignoreip and never banned) or ban the CDN edge IP and block legitimate users.
  • Even with Real-IP enabled, host firewall bans cannot drop traffic already proxied through a CDN—use your CDN's WAF / rate-limiting rules instead.

Step 1: Install Fail2ban

Install Fail2ban on your host machine:

sudo apt update
sudo apt install fail2ban -y
sudo dnf install epel-release -y
sudo dnf install fail2ban fail2ban-systemd -y

Enable and start the Fail2ban service:

sudo systemctl enable --now fail2ban

Step 2: Create the Filter Definitions

Create the three simple filter definitions in /etc/fail2ban/filter.d/:

1. Vulnerability Scanner Filter (nginx-scanner.conf)

Catches requests probing root paths that never exist in Coneshare (/.env, /wp-config.php, /wp-login.php, /xmlrpc.php, /.git, /.htaccess, /phpinfo.php)—including cases where the SPA fallback returns 200—as well as raw binary probes (TLS/SMB payloads logged with \x escapes returning 400):

Create /etc/fail2ban/filter.d/nginx-scanner.conf:

[Definition]
failregex = ^<HOST> -.*"(?:GET|POST|HEAD) \/(?:[^\s"]*\/)?(?:\.env[^\s"?]*|wp-config\.php|wp-login\.php|xmlrpc\.php|\.git|\.htaccess|phpinfo\.php)(?:[?\/ ][^"]*)?" (?:200|403|404) \d+
            ^<HOST> -.*"\\x[0-9a-fA-F]{2}[^"]*" 400 \d+
ignoreregex =

2. Aggressive 404 Scanner Filter (nginx-404.conf)

Catches high-frequency path brute-forcing while ignoring /api/, /mcp/, /.well-known/ (used by OAuth/MCP discovery), and common browser asset lookups:

Create /etc/fail2ban/filter.d/nginx-404.conf:

[Definition]
failregex = ^<HOST> -.*"(?:GET|POST|HEAD) [^"]*" 404 \d+
ignoreregex = "(?:GET|POST|HEAD) (?:\/api\/|\/mcp\/|\/\.well-known\/|\/favicon\.ico|\/robots\.txt|\/apple-touch-icon)

3. Known Attack Tool Filter (nginx-badbots.conf)

Detects dedicated vulnerability scanners case-insensitively ((?i)), while leaving legitimate HTTP clients (curl, wget, python-httpx, MCP clients) untouched:

Create /etc/fail2ban/filter.d/nginx-badbots.conf:

[Definition]
failregex = (?i)^<HOST> -.*"(?:GET|POST|HEAD) [^"]*" \d+ \d+ "[^"]*" "[^"]*(?:nikto|sqlmap|nuclei|zgrab|masscan|gobuster|dirbuster|wpscan)[^"]*"
ignoreregex =

Step 3: Configure Jail Settings

Always define local configuration in /etc/fail2ban/jail.local rather than editing jail.conf directly.

Create or edit /etc/fail2ban/jail.local:

[DEFAULT]
# Default ban time: 1 day (86400 seconds)
bantime  = 86400

# Evaluation window: 10 minutes (600 seconds)
findtime = 600

# Default max retry count
maxretry = 3

# Whitelist trusted IP addresses (your office/home IP, LAN subnets, and localhost)
ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16

# Firewall backend: use nftables-multiport on modern Ubuntu (22.04+), Debian (12+), and RHEL (9+)
# Switch to iptables-multiport if your host uses legacy iptables
banaction = nftables-multiport

# -------------------------------------------------------------
# 1. Vulnerability & Sensitive Path Scanners
# -------------------------------------------------------------
[nginx-scanner]
enabled  = true
port     = http,https
filter   = nginx-scanner
logpath  = /var/log/nginx/access.log
maxretry = 3
findtime = 600
bantime  = 86400

# -------------------------------------------------------------
# 2. Repeated 404 Not Found Scans (High-Rate Fuzzing)
# -------------------------------------------------------------
[nginx-404]
enabled  = true
port     = http,https
filter   = nginx-404
logpath  = /var/log/nginx/access.log
maxretry = 30
findtime = 60
bantime  = 3600

# -------------------------------------------------------------
# 3. Known Attack Tool Probes
# -------------------------------------------------------------
[nginx-badbots]
enabled  = true
port     = http,https
filter   = nginx-badbots
logpath  = /var/log/nginx/access.log
maxretry = 3
findtime = 600
bantime  = 86400

Conservative Defaults

nginx-404 uses a generous threshold (30 non-exempt 404s within 60 seconds) and a short 1-hour ban (bantime = 3600) so normal users clicking broken share links or refreshing pages are never locked out.


Step 4: Verify and Apply

1. Test Filter Rules Against Logs

Test each filter against your existing Nginx access logs using fail2ban-regex:

sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-scanner.conf
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-404.conf
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-badbots.conf

2. Restart Fail2ban Service

Apply the new configuration:

sudo systemctl restart fail2ban

Verify that all three jails are running:

sudo fail2ban-client status

Expected output:

Status
|- Number of jail:      3
`- Jail list:   nginx-404, nginx-badbots, nginx-scanner

Step 5: Managing and Monitoring Bans

Check Jail Activity

View status and currently banned IPs for a specific jail:

sudo fail2ban-client status nginx-scanner
sudo fail2ban-client status nginx-404
sudo fail2ban-client status nginx-badbots

Unban an IP Address

If an IP is blocked accidentally, unban it with:

sudo fail2ban-client set nginx-scanner unbanip <IP_ADDRESS>
sudo fail2ban-client set nginx-404 unbanip <IP_ADDRESS>
sudo fail2ban-client set nginx-badbots unbanip <IP_ADDRESS>

Manually Ban a Malicious IP

sudo fail2ban-client set nginx-scanner banip <IP_ADDRESS>

Inspect Active Firewall Drop Rules

sudo nft list table inet f2b-table
sudo iptables -L -n -v | grep f2b