Defensecybersecuritydefacementcloudflare workers

Stop Your Website from Serving Online Gambling Content with Cloudflare Workers

by Tomi5 min read12 figures · 2 snippets

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:

  1. A request comes in. The Worker forwards it to the origin server as normal.
  2. When the origin answers, the Worker reads the response body and looks for any of the configured gambling keywords.
  3. If one is found, the visitor gets a 403 Forbidden page instead of the original content.
  4. Paths on a whitelist, for example /about or /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.

Workers and Pages menu
Fig. 1 — Compute (Workers) > Workers and Pages

Pick Workers > Create Worker.

Create Worker
Fig. 2 — Create Worker

Give the Worker a name and click Deploy. Cloudflare deploys a placeholder "Hello World" script first; you replace it in a moment.

Naming the Worker
Fig. 3 — Naming and deploying the Worker

Once you see Success! Your project is deployed to Region: Earth, click Edit Code.

Worker deployed
Fig. 4 — The Worker is deployed

Copy the script from the gacor-cf-worker repository, replace everything in the editor with it, and click Deploy.

Worker editor
Fig. 5 — Pasting the script into the Worker editor

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:

javascript · 2 lines
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.

Worker Routes menu
Fig. 6 — The Worker Routes menu

Under Route, enter the domain and path pattern the Worker should cover. Under Worker, pick the Worker you just created. Click Save.

Adding a route
Fig. 7 — Adding a route to the Worker

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:

On the first domain every page shows the keyword gacor. On the second, the same pages come back as 403 Forbidden.

Test result 1
Fig. 8 — Test result: with and without the Worker
Test result 2
Fig. 9 — Test result: with and without the Worker
Test result 3
Fig. 10 — Test result: with and without the Worker
Test result 4
Fig. 11 — Test result: with and without the Worker

Then I checked the whitelist by adding /teks.txt, one of the test pages, to whitelistPaths:

javascript · 1 line
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.

Whitelist test
Fig. 12 — A whitelisted path is still served

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.
Updated July 5, 2025EOF · 5 min