Prerequisite
To proceed with this guide, ensure you have:
- A functioning fail2ban instance (refer to 03-fail2ban-in-docker)
- A Caddy instance (check out 01-caddy-in-docker)
- Plugin
access-formatted
for Caddy (look 04-install-caddy-plugins to know how to add the plugin).
Return an Unauthorized Error
Referencing the earlier post 02-caddy-hardening, update the configuration to permit only the local network, resulting in an unauthorized error (401) response instead of an abort.
Format Caddy Logs
Add a global configuration on top of your Caddyfile
that add a log
directive:
Configured in this manner, all access-log
logs will be written to the /data/access.log
file, serving as the reference for Fail2Ban to monitor and identify potentially suspicious access.
The Caddy logfile is rich in information. For improved readability and parsing, leverage the transform
logging encoder available in the access-formatted
plugin.
This will standardize the format of each log entry to look like:
Create Fail2Ban Filter
On the data/filter.d/
folder, create a file caddy-custom.conf
with the content:
Create Fail2Ban Jail
On the data/jail.d/
folder, create a file caddy.conf
containing:
Configure Fail2Ban Access Log
You can now restart Fail2Ban using the command:
Conclusion
Fail2Ban is now set to automatically ban clients attempting to connect to a subdomain configured exclusively for the local network. After three unsuccessful attempts, the client will be restricted from making further requests to the Caddy server.
Ressources
- Fail2Ban Github
- Photo by Baron Gimenez on Unsplash