01 Solution Description
I have had an idea. I do not know if it is my own, nor do I care because I am way too excited for it.
The Problem
The problem is well known and simple. As you all know, the Internet is the wildest west. People will do whatever they can to gain leverage over well meaning businesses and nerds like myself. One way that they mostly just annoy well prepared websites is Vulnerability Crawling. There are billions of bots crawling the internet looking for well known vulnerabilities that any website might be exposing themselves with.
Really. Just look at my metrics and logs for this exact website that just got indexed by Google on the day of writing this blog.
# Simplified Logs
API CALL: GET /wp-admin/maint/ | STATUS: 200 | DURATION: 33.845µs
API CALL: GET /wp-admin/user/ | STATUS: 200 | DURATION: 32.904µs
API CALL: GET /wp-content/upgrade/ | STATUS: 200 | DURATION: 32.262µs
API CALL: GET /wp-includes/ | STATUS: 200 | DURATION: 35.76µs
API CALL: GET /wp-includes/Requests/ | STATUS: 200 | DURATION: 27.574µs
API CALL: GET /wp-includes/sodium_compat/src/ | STATUS: 200 | DURATION: 30.509µs |
API CALL: GET /wp-includes/sitemaps/providers/ | STATUS: 200 | DURATION: 21.511µs
API CALL: GET /wp-includes/php-compat/ | STATUS: 200 | DURATION: 31.661µs |
API CALL: GET /wp-includes/theme-compat/ | STATUS: 200 | DURATION: 40.388µs
API CALL: GET /wp-admin/css/colors/ | STATUS: 200
API CALL: GET /.well-known/acme-challenge/ | STATUS: 200
GET /admin/uploads/ | STATUS: 200 | DURATION: 38.504µs
GET /admin/ | STATUS: 200 | DURATION: 41.089µs
GET /local/ | STATUS: 200 | DURATION: 39.948µs
GET /Site/ | STATUS: 200 | DURATION: 35.008µs
GET /admin/editor/ | STATUS: 200 | DURATION: 38.344µs
GET /plugins/ | STATUS: 200 | DURATION: 32.563µs
The red line is the number of SPA (Single Page Applications) routed calls which is good! It means people are receiving my webpage. But then you look at the logs and realize that they are just bots crawling for vulnerabilities. And I for one, hate this. I despise the fact that there are 30 something mu seconds being wasted on my compute for me to send the same repeated 404 webpage.
The first thing to ask is, why do they have status 200? If this is a 404 webpage shouldn't the status be 404 as well? In an ideal world, yes but with a SPA, you can't really differ which pages are frontend's 404s, your built webpage is the one that does. You simply just send the cached index.html to the user.
So there is one problem, the other problem is annoyance on my logs. These tell me nothing about my website rather that bots are getting the same index.html every crawl.
The goal
I want my webpage to efficiently:
- Find bots that are crawling it and ban their IPs every 'x' amount of time to account for DHCP.
- Return a proper HTTP 404 on non-existent 404 web-page loads.
The Solution
Now there are many ways to achieve both of these at different sections of the request. You could remove the SPA and deploy the front-end as its separate entity. Then ban bots based on the IPs observed on the spikes in my Prometheus metrics with something like Fail2ban [1].
Or you can be crazy and waste a lot of time on this idea that will most likely not be the best solution. But it's my solution :)
Bloom Filters
Randomly while rolling around in bed, after a horrible first solution. I remembered GRC's own Steve Gipson talking about Bloom Filters on SN 989 [2]. The reason why this was the topic of that week's podcast was to solve the in-device queries of Certificate Revocation Lists (CRLs). I will not dig further into it as I believe it is better explain in Steve's own words, so go give it a listen.
So let me quickly explain the use case, Bloom Filters are a data structure that will guarantee whether an element is NOT in a set [3]. Meaning, we can create a set from what our nice bot friends have given us above and create a bloom filter out of it. We will let paths through that are guaranteed not in the filter. But if they might be in the filter, they might also be false positives so we will then cascade to a dynamically created and small correct paths bloom filter for which we will send a 404 if it is not in the filter. Then let a proper website load on double false positive or false positive - true positive combinations.
Where App is our SPA Front End and both rectangles are Bloom Filters
Now, this might look a bit chance driven to actually 404, but with some proper adjustments and good ol' Math we can actually limit the # of non-404 false positive routes to ridiculously low percentages.
All these 404 responses will be tracked by IP and then stored in a DB table which will keep the short amount of time they will be banned for. Then I plan on creating a second chance system. Where post ban, if a 404 tolerance is breached, ban will be reinstated, but a 200 tolerance will remove them from the table.
Architecture
Now this is already getting out of hand and I love it. But this will require:
- Log crawling
- Route crawling
- DB modification & access
- Dynamic NGINX reverse proxy modification for banning / Any other banning software somewhere in the DMZ.
This satisfies enough priviledge and duties for me to segregate it from the main API entirely. So the idea is that this will be a middleware API call that will return 404 or not with a very strict timeout of let's hope something like 10ms. This also gives me the chance to further practice Google Remote Procedure Calls (gRPC) [4] which I have always wanted to dig further into.
I might open source this, I might not. I will try and share code snippets where valuable and mathematical Latex like formulas through the process.
Hope you enjoy it, I know damn well that I will.
And yes, this is incredibly over-engineered.
References
- [1] Detect and stop 404 attacks with fail2ban
- [2] Security Now #989
- [3] Wikipedia Bloom Filters
- [4] gRPC