Hello great HestiaCP community. I’ve recently been working on adding a few additional jails to fail2ban to stop some of the common probing attacks I have been seeing hit my websites and EXIM server.
I have managed to successfully add the fail2ban jails and I see that they are working. They are successfully identifying the errors in the apache server logs, they are successfully banning the IP, and it’s successfully adding the IP to the iptables and ip6tables. However, the server is NOT blocking the IPs. I don’t understand how this could be. But I tested it several times by triggering the fail2ban jail rules with my IP Address. I can see that my IP address is banned and it’s also added to the appropriate IPTables jail chain.
In fact, I noticed that even the default Hestia jails, which are also banning IPs, are being added to iptables, but the banned IPs are still allowed to hit the server and continue their probing. But I can clearly see that the IP is listed in iptables and blocked to all destinations on all ports.
Anyway else seeing this. It’s almost as if the iptables firewall is not even be used, but I can see that it’s active, running on both the HestiaCP admin panel as well as directly from checking via terminal.
Could someone help me understand why the IPs are not being blocked? Thank you in advance!
Okay, so I figured out the cause. It was due to Cloudflare. I use Cloudflare’s built in proxy service, so my server IP is not shown to the visitors. You can enable this within Cloudflare in their DNS settings.
However, I still don’t understand why having the proxy setting on, prevents the server and iptables from blocking the IP address. If anyone can help me understand that, I would appreciate it. It seems even though I am using Cloudflare, my server can see the external user’s IP address that is probing my server and it successfully identifies the IP, bans it, and then adds it to iptables to block. However, it doesn’t get blocked due to the Cloudflare’s proxy. This is the part I don’t understand.
Anyway, the way I solved this was by using Cloudflare’s API. This website explains it quite well and it’s super quick and easy.
It just involves updating the Cloudflare action file in fail2ban, adding your global API key and email address and then adding in the cloudflare action into each of your fail2ban jails. Once I did that, my problem was solved. In this case, the IP gets banned and it calls the Cloudflare API to ban the IP at the Cloudflare level.
Anyway, I hope this helps the next person that is scratching their head wondering why the hell their server isn’t blocking the IPs. I went through and checked everything but Cloudflare, which is why it took me so longer to figure this out. My logic was, since the server can see the external user’s IP and successfully ban it, it can’t possibly by something related to Cloudflare. But apparently it is and it has to do with their proxy service. I still don’t understand why, but happy that I got this figured out and my server is back in business blocking these annoying aholes.
The server receives headers sent by Cloudflare that contain the real IP, but those IPs are not directly connecting to your server, only Cloudflare’s IPs are.
The reason you see the real IP in the logs is that Nginx has been compiled with the realip modules.
So you see the real IP in the logs because real_ip_header directive uses the CF-Connecting-IP header, which is sent by Cloudflare, to display the client IP in the log.
Amazing! Thank you for the explanation @sahsanu! Now I fully understand. And thank you and the HestiaCP team for building the HestiaCP in a way that considers this. I wasn’t even aware that such considerations and actions were taken to ensure compatibility with Cloudflare, as I assume if it had not been configured that way, the fail2ban rules I had setup would have been blocking Cloudflare IPs instead and that would have caused a completely different issue.
Ok, so now I understand why iptables wasn’t blocking. And I assume the action I took to use the Cloudflare API and fail2ban’s cloudflare action, is the appropriate way to block these bots and TAs. Thanks again!