Over the last few years a large share of the defacements I have seen ended the same way: the attacker did not replace the home page with a message, they quietly added online gambling content to it. The pages exist to build backlinks and lift gambling sites in search results, so the attacker wants them to stay up for as long as possible, unnoticed.
This guide shows a stopgap I use for that situation. A small Cloudflare Worker sits between the site and its visitors, reads each page on the way out, and refuses to serve it if it contains gambling keywords.
I. The Problem
Injected gambling content is hard to catch. It can land on any page, in a template, in a database row or in a static file the CMS never touches. Some injections only show up when the visitor looks like a search engine crawler, so the owner browsing their own site sees nothing wrong. The usual first sign is a search result for the domain with a gambling title, by which point the domain's reputation has already taken the hit.
Cleaning it up properly means finding how the attacker got in, and that takes time. In the meantime the pages keep serving. I wanted something that stops them from reaching anyone while the investigation runs, so I wrote a Cloudflare Worker script for it (ChatGPT helped with the first draft).
II. How It Works
Cloudflare Workers run JavaScript on Cloudflare's network, at the edge between your visitors and your server. It is a serverless platform, so there is nothing to host. A Worker can rewrite requests and responses, route traffic or call other APIs.
Here the Worker is used as an outbound content filter:
- A request comes in. The Worker forwards it to the origin server as normal.
- When the origin answers, the Worker reads the response body and looks for any of the configured gambling keywords.
- If one is found, the visitor gets a
403 Forbiddenpage instead of the original content. - Paths on a whitelist, for example
/aboutor/contact, skip the check entirely. That saves work on pages you trust, and gives you a way out when a legitimate page happens to contain one of the words.
What decides which requests the Worker sees is a Worker Route. A route is a URL pattern: it can cover a single path such as /login, a subdomain, or every request to example.com. In this setup the route is how you choose which parts of the site get filtered.
III. Implementation
You need a Cloudflare account and a domain whose DNS is already on Cloudflare. Workers and Worker Routes are both on the Free Plan.
Creating the Worker
In the Dashboard, open Compute (Workers) > Workers and Pages and click Create.
Pick Workers > Create Worker.
Give the Worker a name and click Deploy. Cloudflare deploys a placeholder "Hello World" script first; you replace it in a moment.
Once you see Success! Your project is deployed to Region: Earth, click Edit Code.
Copy the script from the gacor-cf-worker repository, replace everything in the editor with it, and click Deploy.
Two lists at the top of the script control its behaviour. whitelistPaths holds the paths that are never checked, and forbiddenKeywords holds the words that trigger a block:
const whitelistPaths = ['/path1', '/path2', '/path3'];const forbiddenKeywords = ['gacor', 'slot', 'judi'];Edit both for your site, then click Deploy again. The Worker is live, but nothing reaches it yet.
Connecting a Worker Route
Back in the Dashboard, select your domain and open Worker Routes.
Under Route, enter the domain and path pattern the Worker should cover. Under Worker, pick the Worker you just created. Click Save.
From now on, every request that matches the route passes through the Worker before the response goes back to the visitor.
IV. Testing
To check the Worker against more than plain HTML, I built a small test application that serves the same gambling text as HTML, JSON and plain text, and put it on two domains:
- https://gcr-site-real.mastomi.id has no Worker Route.
- https://gcr-site-routed.mastomi.id is routed through the Worker.
On the first domain every page shows the keyword gacor. On the second, the same pages come back as 403 Forbidden.
Then I checked the whitelist by adding /teks.txt, one of the test pages, to whitelistPaths:
const whitelistPaths = ['/teks.txt'];After deploying, /teks.txt is served normally on the routed domain even though it still contains gacor, while the other pages stay blocked.
V. Takeaways
- A Worker on a Worker Route can keep injected gambling pages away from visitors and crawlers within minutes, without touching the compromised server.
- Keyword matching is blunt. Pick keywords that will not appear on your legitimate pages, and use the whitelist for the pages where they must.
- The script is a starting point. One useful addition is an alert whenever a keyword matches, which tells you exactly which URL was injected and speeds up the cleanup.
- The attacker still has whatever access they used. Treat the Worker as time bought for a root-cause investigation, and remove it only after the hole is found and closed.











