The site was put behind a content delivery network, and from that moment visitor addresses stopped appearing in the web server log. In their place are a dozen or so of the same addresses — the network's own nodes, through which all the traffic arrives. As far as the server is concerned, every visitor in the world is now one of ten machines.

While things are quiet this goes unnoticed. The problem surfaces on the day somebody starts guessing passwords. Fail2ban counts attempts per address, sees hundreds of failures from one node and bans it. Along with the attacker, part of your ordinary visitors disappear from the site — everyone the delivery network happened to route through that same node. The protection looks like it is working: the jail is active, the bans are there.

Below is the fix, step by step: get the real addresses back, make the ban actually reach the attacker, and close the way around. The commands are written for Cloudflare as the most common case, but the order is the same for any delivery network that passes the visitor's address in a header.

Step 1. Confirm you have the problem

Look at how many different addresses reached the site over a day:

sudo awk '{print $1}' /var/log/apache2/site_access.log | sort -u | wc -l

A few dozen is low for a live site, and traffic is not the reason. Check who owns those addresses:

sudo awk '{print $1}' /var/log/apache2/site_access.log | sort | uniq -c | sort -rn | head -5
whois 172.69.0.1 | grep -iE 'netname|orgname'

The sign: the whois answer names a delivery network rather than an internet provider. The web server takes the proxy for the client, and everything built on addresses now runs on false data. That is the jails, the visitor counts and the map of requests alike.

The real address is not lost, it arrives in a request header — with Cloudflare that is CF-Connecting-IP, in the general case X-Forwarded-For. The web server simply does not know that this header can be trusted.

Step 2. Get the real addresses back into the log

The header may only be trusted from known senders, otherwise any visitor puts whatever they like in it and hands somebody else a ban. That is why the module comes with a list of networks allowed to state the address.

sudo a2enmod remoteip
curl -s https://www.cloudflare.com/ips-v4 -o /tmp/cf4
curl -s https://www.cloudflare.com/ips-v6 -o /tmp/cf6

The file /etc/apache2/conf-available/remoteip.conf is built from those lists:

RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
# … and so on, one line per network from both files

By default the log records the address of the connection, so you need your own log format, one that takes the client address:

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" " realip

In the site's virtual host, point CustomLog at that format, then:

sudo a2enconf remoteip
sudo apache2ctl configtest
sudo systemctl reload apache2

The check: open the site from your phone over mobile data and look at the last lines of the log.

sudo tail -5 /var/log/apache2/site_access.log

The first field should hold an address from your mobile operator, not a node of the delivery network. If the address is unchanged, the log format has not taken effect. Check that CustomLog in the right virtual host refers to realip, and that the configuration file is enabled.

On nginx the module's role is played by a set_real_ip_from directive per network plus real_ip_header CF-Connecting-IP; the log corrects itself after that, no separate format needed.

Step 3. Keep the list of networks current

The delivery network's address list changes a few times a year. Leave it as it is and one day part of the traffic will arrive from addresses that are not on the list, and for that part the proxy address will be recorded again — silently, with no error. The update belongs on a schedule, with both files folded into one list:

#!/bin/sh
# /usr/local/bin/update-cf-ips.sh
set -e
tmp=$(mktemp)
{ curl -fsS https://www.cloudflare.com/ips-v4; echo; curl -fsS https://www.cloudflare.com/ips-v6; } \
    | awk 'NF {print "RemoteIPTrustedProxy " $0}' > "$tmp"
echo "RemoteIPHeader CF-Connecting-IP" | cat - "$tmp" > /etc/apache2/conf-available/remoteip.conf
apache2ctl configtest && systemctl reload apache2

The check: run it by hand and make sure every network made it into the file and that Apache reloaded without errors:

sudo sh /usr/local/bin/update-cf-ips.sh && grep -c RemoteIPTrustedProxy /etc/apache2/conf-available/remoteip.conf

Then once a week:

0 4 * * 1 root /usr/local/bin/update-cf-ips.sh

Note the set -e and the -fsS on curl. Without them a failed download overwrites a working list with an empty file, and trust in the header disappears altogether.

Step 4. Make the ban actually reach its target

This is the step most often skipped, and without it the previous two only give you a correct log. The log now holds the real address, the jail duly bans it — and nothing changes. The firewall rule drops packets from the attacker's address, but their requests reach you not from it, but from a node of the delivery network. For the server that is a different sender, and the rule does not apply to it.

You can see this on a running jail:

sudo fail2ban-client status web-probes
sudo iptables -L f2b-web-probes -n

The address is on the banned list, the rule exists, and requests from it keep arriving at the site. The way out is to ban on the delivery network's side, and fail2ban ships an action for exactly that:

[web-probes]
action = cloudflare-token[cfuser=you@example.com, cftoken=YOUR_TOKEN, cfzone=YOUR_ZONE_ID]

The token is created in the delivery network's dashboard with the right to edit firewall rules of a single zone, and it needs nothing else. In fail2ban older than 1.0 the action file is called cloudflare.conf and asks for an ordinary API key instead of a token.

The check: ban your own address by hand and try to open the site from your phone.

sudo fail2ban-client set web-probes banip 203.0.113.10

What should come up is the delivery network's block page, not your site. Lift the ban afterwards:

sudo fail2ban-client set web-probes unbanip 203.0.113.10

Step 5. Close the way around

As long as the server answers requests to its own address directly, everything above can be bypassed. The server's address is in certificate history or in old DNS records, and an attacker goes straight to it, past the delivery network. Then the header is beside the point, and the ban on the network's side does nothing.

So the site's ports are opened only to the delivery networks:

sudo ufw delete allow 80,443/tcp
for net in $(cat /tmp/cf4 /tmp/cf6); do sudo ufw allow from "$net" to any port 80,443 proto tcp; done

The check: from any outside machine, a request to the server's address should go nowhere, while the domain name still works:

curl -sv --max-time 5 http://203.0.113.10/ 2>&1 | tail -2
curl -s -o /dev/null -w '%{http_code}\n' https://site.example.com/

The first command should end in a timeout, the second should return 200. Do not remove the rule for SSH while you are at it. The login port stays open for your own addresses, or you will lock yourself out.

What changes

The logs hold real addresses again, which means the statistics and the jails both count what is actually happening. The ban reaches the attacker and leaves alone the visitors the same network happened to route the same way. The direct path to the server is closed, and the delivery network stops being something easy to step around.

Worth keeping in mind that this needs checking more than once. The list of networks is updated, the log format is easy to lose in the next edit of the virtual host, and the token for banning can be revoked along with other keys. One sign gives it away: if the log is back to the same dozen addresses, one of the three steps has come undone. What the logs, the jails and the list of open ports look like on one screen is on the demo pages below.