Securing SWAG
SWAG is a reverse proxy that allows you to expose your self-hosted apps to the world, but that comes with risks. We can mitigate some risks by fine-tuning SWAG and how we access it:- Prevent accessing some apps via the internet while exposing others.
- Set up brute-force protection via Crowdsec/Fail2Ban.
- Set up geoblock to whitelist/blacklist countries via DBIP/Maxmind.
- Prevent your apps from appearing in search results.
- Set up SSO via Authelia/Authentik.
- Monitor SWAG via a dashboard.
- Access your apps through Wireguard instead of exposing them.
Requirements
- A working instance of SWAG.
Internal applications
Only expose apps you want to share with others and must expose, keep the rest internal and use WireGuard to access them.Requirements
- Split DNS - the source IP on requests needs to be local for allow/deny to work properly.
nginx/internal.conf with the following configuration:
Brute-force protection
Crowdsec and Fail2Ban can prevent brute-force attacks by monitoring the logs of apps and banning IPs that fail multiple login attempts. SWAG comes with Fail2Ban pre-configured with a few basic protections, you can fine-tune it specifically for your apps, or disable it and set up Crowdsec instead.Crowdsec
Crowdsec is a free, open-source and collaborative IPS; it’s like fail2ban but you share your bans with all of the other users to try and pre-emptively block malicious hosts. Follow this blog post to set it up in SWAG.Fail2Ban
Fail2Ban is an intrusion prevention software that protects external applications from brute-force attacks. Attackers that fail to login to your applications a certain number of times will get blocked from accessing all of your applications. Fail2Ban looks for failed login attempts in log files, counts the failed attempts in a short period, and bans the IP address of the attacker.The following is an example of setting up Nextcloud in Fail2Ban, configure other apps in the same way.
Mount the application logs to SWAG’s container by adding a volume for the log to the compose yaml:nextcloud.local under fail2ban/filter.d:
nextcloud.local by running the following command on the docker host:
fail2ban/jail.local:
Geoblock
Geoblock significantly reduces the attack surface of SWAG by restricting access based on countries. Follow the instructions of one of the following mods to set it up: DBIP doesn’t require an account, but Maxmind might be more accurate in some cases.This was removed by author, but I simply decided to leave it for personal purpose.
$geo-whitelist, $geo-blacklist, $lan-ip.
An example for allowing a single country:
Search results
You can prevent apps from appearing in search engine results and being crawled by web crawlers. Note that not all search engines and web crawlers respect this tag, but it significantly reduces the amount. Add the following tossl.conf to enable it on all apps:
SSO
Setting up SSO will provide an additional layer of security and protect you against login bypass exploits in apps. Note that api endpoints shouldn’t have SSO for them to function properly.Monitor
Use monitoring solutions such as SWAG Dashboard to keep an eye on the traffic going through SWAG and check for suspicious activity such as:- Many hits from a country unrelated to your users.
- Many requests to a specific page or static file.
- Referers that shouldn’t refer to your domain.
- Many hits on status codes that are not 2xx.
VPN
The most effective security you can implement is to stop exposing your apps entirely, and instead access them via WireGuard.Requirements
- A working instance of WireGuard.
- Split DNS - the source IP on requests needs to be local for SWAG to work without being exposed.
- DNS Validation - allows you to get an SSL certificate without port forwarding.