to begin, i created waf rules to my cloudflare domain. this was found by clicking on the domain, security, security rules. i did not know these existed and when i found out, it was a life saver across my whole domain. actually, at first i created these waf rules on my authentik domain, but after consideration, i decided to put it on my whole domain. tune it to specific needs and configurations.
the four below rules are ‘custom rules’
first rule: allow trusted ip addresses like home and vps
(ip.src in {1.x.x.x 2.x.x.x})
second rule: allow usa only (blocks any country that is not usa)
Allow US Only: ip.src.country ne “US”
third rule: block cloudflare bots
cf.client.bot
fourth rule: block ‘important’ and/or ‘admin’ paths
(http.request.uri.path in {“/admin” “/wp-admin” “/wp-login.php” “/.env” “/phpmyadmin” “/.git” “/config.json” “/wp-config.php” “/xmlrpc.php” “/.env.example” “/.env.backup” “/.env.dev” “/.env.prod” “/.env.local” “/.git/config” “/.git/HEAD” “/.svn/entries” “/config.php” “/web.config” “/docker-compose.yml” “/appsettings.json” “/server.xml” “/database.yml” “/pma” “/myadmin” “/mysqladmin” “/dbadmin” “/adminer.php” “/pgadmin” “/cmd.php” “/shell.php” “/c99.php” “/b374k.php” “/ws.php” “/eval.php” “/test.php” “/up.php” “/server-status” “/phpinfo.php” “/info.php” “/php-info.php” “/actuator/env” “/actuator/health” “/swagger-ui.html” “/api-docs” “/backup.zip” “/backup.sql” “/dump.sql” “/db.sql” “/www.zip” “/site.zip” “/backup.tar.gz” “/setup.php” “/install.php” “/composer.json” “/package.json” “/nginx.conf” “/httpd.conf” “/administrator” “/bitrix/admin” “/magento/admin” “/admin/login.php” “/admin/config.php” “/boaform/admin/formLogin” “/console” “/manager/html” “/xampp” “/webalizer” “/cpanel” “/whm” “/solr” “/api/v1/pod” “/v1/agent/self” “/_cat/indices” “/api/json” “/grafana/login” “/zabbix” “/aws/credentials” “/.aws/credentials” “/.kube/config” “/.ssh/id_rsa” “/.ssh/authorized_keys” “/etc/passwd” “/id_rsa” “/old” “/backup” “/bak” “/temp” “/tmp” “/test” “/api/swagger.json” “/v2/_catalog” “/jenkins/login” “/jira/login.jsp” “/confluence/login.action” “/ghost/api/v3/admin/” “/Autodiscover/Autodiscover.xml” “/ews/exchange.asmx” “/owa/auth/logon.aspx” “/piwik” “/matomo” “/laravel.log” “/storage/logs/laravel.log” “/debugbar/assets/stylesheets” “/.idea/workspace.xml” “/.vscode/sftp.json” “/.DS_Store” “/.htaccess” “/.htpasswd” “/db.sqlite3” “/db.sqlite” “/database.sqlite” “/database.sqlite3” “/settings.py” “/yarn.lock” “/package-lock.json”}) or (http.user_agent eq “”) or (http.user_agent contains “curl”) or (http.user_agent contains “python”) or (http.user_agent contains “Go-http-client”) or (http.user_agent contains “wget”) or (http.user_agent contains “masscan”) or (http.user_agent contains “zgrab”) or (http.user_agent contains “nmap”) or (http.user_agent contains “Netcraft”) or (http.user_agent contains “Nuclei”) or (http.user_agent contains “sqlmap”) or (http.user_agent contains “Censys”) or (http.user_agent contains “shodan”) or (http.user_agent contains “projectdiscovery”) or (http.user_agent contains “fasthttp”) or (http.user_agent contains “scrapy”) or (http.user_agent contains “http-client”) or (http.user_agent contains “java”) or (http.user_agent contains “okhttp”) or (http.user_agent contains “ffuf”) or (http.user_agent contains “gobuster”) or (http.user_agent contains “dirb”) or (http.user_agent contains “nikto”) or (http.user_agent contains “httpx”) or (http.user_agent contains “Arachni”) or (http.user_agent contains “colly”) or (http.user_agent contains “LeakIX”) or (http.user_agent contains “OpenVAS”) or (http.user_agent contains “Acunetix”) or (http.user_agent contains “DirBuster”) or (http.user_agent contains “Havij”) or (http.user_agent contains “Morfeus”) or (http.user_agent contains “WPScan”) or (http.user_agent contains “ZmEu”) or (http.user_agent contains “libwww-perl”) or (http.user_agent contains “Lemon-Duck”)
the final rule is a rate limit rule:
(http.host eq “auth.domain.com” and
(http.request.uri.path contains “/if/flow/” or
http.request.uri.path contains “/api/” or
http.request.uri.path contains “/application/o/” or
http.request.uri.path contains “/if/admin/”))
this rule essentially rate limits requests on these paths on the authentik domain. that way an attacker cannot brute force any type of attacks, and it gets blocked. the reason why this is on the authentik domain specifically is that all my cloudflare access apps are behind authentik, so when a user navigates to a cloud instance, it first pops up with a cloudflare screen, then the user must authenticate. which also means that if a user finds the authentik url, they can brute force it, which is why the rate limit rules exist. the rule is set to x requests in y seconds = block for z seconds.
by integrating these five rules, it has greatly strengthened my security posture, and cloudflare gives some analytics for when the rules are enforced.
after configuring those rules, i went to fix how authentik talks to cloudflare. this was done by creating the oidc claim to ‘groups’. this was not defined anywhere i could find, so i had to create a custom mapping.
the way i found this was by trial and error and looking through how the groups expression was made. i looked at different mappings that included groups, which was how i deciphered how to filter by groups.
how to create a custom mapping in authentik:
customization > property mappings
name the mapping and make the scope groups
the expression: return list(set([group.name for group in request.user.ak_groups.all()]))
i found that this expression worked best and gave a clean output. the other one i used had duplicates, which worked still, but wanted a clean output nonetheless.
once this is created, go to applications > providers > cloudflare provider
advanced protocol settings > add the new mapping
then create the authentik groups and map the users accordingly.
on the cloudflare side, navigate to zero trust > integrations > identity providers
then go to authentik and add ‘groups’ to the oidc claims and scopes.
now, go back to the main zero trust screen then to access controls > policies
create a new policy rule name, create new rule > policy rules > oidc claimns and add the group name.
link the policy rule based on application; crafty policy goes to crafty application
inside the oidc configuration page, you can test if it works based on the users inside of cloudflare.
this essentially is the cloudflare + authentik setup for group based security. before it was kind of a catch all, where i just log in as my admin user. now i have waf rules enforced with group based authorization. basically, it makes it way easier if you were to open access to other people who want access to those resources. it makes it easy since all you have to do now is create the user in authentik and add them to the group. the great thing is that since it reads the claims, that is all it goes off of. no need of syncing groups or anything, just reading the oidc defined claims.
i had also created a location policy rule and attached it to my applications, but it has since been removed since it is considered ‘legacy’ now, but its taken care of at the waf rule level domain wide. helps reduce digital footprints in other countries.