Block a client with an IP address blacklisted
In a previous article, we explained how to pass as a header attribute the blacklists to which an IP belongs thanks to the capabilities of the Cloudflare Workers. In this article, the traffic never was redirected in the event of a malicious IP address, delegating that responsibility to the developers checking the content of the HTTP header. This is valid for those who have full control over the server-side code, but for those who only use WordPress or Drupal tools they could not use the script.
Unlike the other script, this one will return an HTTP 403 error if the IP address is among the ones stored in Apility.io, not allowing that client to progress on our website with the malicious IP address.
Apility API and Cloudflare to the rescue
Cloudflare already offers WAF and Page filter capabilities. In this example, we are going to implement an extra security layer in front of our website to block traffic coming from malicious (and suspicious) IP addresses. Thanks to the quarantine capabilities of Apility.io is also possible to block access to our pages from a specific IP address, one or several continents, one or several countries, and one or several Internet Service Providers.
You can have a look at this articles in our blog to know more about how we do it with other technologies: How to create custom security filters to your pages, block malicious users trying to register using anonymous VPN like TOR, anonymous proxies and so on.
The next example will show how to:
- Intercept all requests
- Extract the remote client IP address from the
- Perform an HTTP request to Apility API badip
- If the request returns an HTTP 200 OK, it means that the IP address of the client is in a blacklist. So, the client is redirected to HTTP 403 FORBIDDEN error page.
- If the request returns an HTTP 404 NOT FOUND, it means that the IP address of the client is not inside any blacklist. So, the client can continue with the previous requests as usual.
Hence, when a request is made to the website where the Worker has a route configured, the Worker will figure out if the IP is malicious and will branch to an error page if the IP address is malicious, or will continue to the website if the IP is clean.!
Enable Cloudflare Workers
Workers is a paid feature, so developers have to enter the billing details first, and enable the Workers feature:
Once enabled, we have to open the Launch Editor where developers can code and test the Workers:
The Editor is a Web Application where developers can code, test and preview the Workers. They can also configure the routes. A route is a URL to intercept. A Worker is like a man-in-the-middle: it sits between the browser of the remote clients and the origin server. Then, the route controls what resources at the origin server will be handled.
Coding the Worker
Note: You can register for free and obtain your API KEY!
But now, I’m going to repeat this test using TorBrowser. TorBrowser is a Firefox-based browser to use the TOR network. This is what happens when I open https://apidocs.apility.io with TorBrowser.
Blocking access by IP address, country, continent or service provider
It’s possible to implement advanced WAF-like features fine-tuning the Apility.io features. Apility.io has more than 100 blacklists and some of them are more sensitive than others. A developer can disable the blacklists that can be too broad and focus on blacklists that are more focused on your interest. For example:
- Bots: https://apility.io/list/BOTSCOUT-1D/type/badip
- Forum spammers: https://apility.io/list/STOPFORUMSPAM-1/type/badip
- TOR: https://apility.io/list/TOR/type/badip
- Anonymous proxies: https://apility.io/list/UDGER-WEB-PROXY/type/badip
To select the right blacklists, open Blacklists in the menu at the right-hand side and choose the lists under the IP address tab.
Thanks to the quarantine features, a developer can ban a group of IP addresses, countries, continents or service providers.
Let’s say that a developer wants to block access to his website from all countries except North American countries. With quarantine features, you can do it with different approaches, but the easiest one would be to quarantine all IP addresses from all continents except North America. To do it, access the QUARANTINE option at the black menu at the left side of the screen. To add a Continent to the Management Dashboard goto Quarantine > Continents and enter the Continent to ban and its Time to Live in this form:
Enter all continents except North America. If you want to ban permanently the continents, then leave the TTL box empty, but if you want to set an expiration time, then enter in the TTL box the number of seconds the ban will live.
Now you can open again the https://apidocs.apility.io and if you are in North America, but if you are in Europe like me, my IP will be banned:
The IP is a perfectly clean address from Telefónica Spain: https://apility.io/search/188.8.131.52
Do you have cool ideas of Apility.io and Workers? Let us know in the comments section!