How to bypass CloudFlare bot protection ?
A Cloudflare Worker can be configured as a reverse proxy that forwards requests to any site, and by adding a firewall rule the proxy can be accessed from Tor without triggering bot protection. This allows an attacker to bypass Cloudflare's bot protection and set an arbitrary X-Forwarded-For header, potentially evading IP-based restrictions.
{{media:0}}
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Cloudflare offers JavaScript workers that execute code server side, on Cloudflare's own infrastructure. The feature is intended for things like static sites and maintenance pages, but the same capability can be turned into a serverless command and control channel or a phishing proxy. This write-up explores using a worker as a reverse proxy to get past Cloudflare's bot protection.
The starting observation is familiar to anyone who has tried to reach a site such as shodan.io from Tor: the captcha challenge is hard to get past. The researcher submitted what looked like a security flaw to Cloudflare's bug bounty programme several months before publishing. Cloudflare's position was that it is not a problem, and the researcher leaves the judgement to the reader.
Setup
The prerequisites are deliberately minimal:
- Register a domain. A free .tk domain is enough.
- Create a Cloudflare account and add the domain.
- Once the domain is validated by Cloudflare, add at least one valid DNS entry that uses proxy mode. The report notes that any IP can be used for this entry.
With the zone in place, the next step is a JavaScript worker acting as a reverse proxy. The full code is published at https://github.com/jychp/cloudflare-bypass. Create a new worker and paste the contents of worker.js into it. The values TOKEN_HEADER, TOKEN_VALUE, HOST_HEADER and IP_HEADER can be customised.
Finally, add a route to the worker:
proxy.domain.com/*
Finding it
Requesting proxy.domain.com returns "Welcome to NGINX.". The worker code is simple: it looks for a specific header acting as a magic value and forwards the request to the given domain. That behaviour is what makes the worker useful as a proxy, and it is also the basis for the bypass described below.
Exploitation
Step 1: drive the proxy with the Python wrapper
A Python wrapper in the same GitHub repository makes the proxy easy to use:
>>> from cfproxy import CFProxy>>> proxy = CFProxy('proxy.domain.com', 'A random User-Agent', '[redacted]')>>> req = proxy.get('https://icanhazip.com')>>> print(req.status_code)200>>> print(req.text)[redacted]
The wrapper is constructed with the proxy host, a User-Agent string and an IP address. The get call fetches https://icanhazip.com through the proxy and returns status 200 with the body [redacted]. A WHOIS lookup on that address shows it is a Cloudflare IP, probably the server running the worker.
Step 2: allow Tor through the Cloudflare firewall
At this point, requesting the proxy through Tor is blocked. The fix is a rule in the Cloudflare firewall, under Firewall / Firewall Rules. The report shows one way to make the rule work and notes that any other trigger can be used instead. With the rule in place, the proxy can be reached over Tor without the reCaptcha challenge.
Step 3: observe the headers the origin receives
With the proxy working, any site behind Cloudflare can be requested, except sites using "Under Attack Mode" such as shodan.io. Requesting a site that displays the request headers shows what the origin sees:
- ACCEPT: /
- ACCEPT-ENCODING: gzip
- CDN-LOOP: cloudflare; subreqs=1
- CF-CONNECTING-IP: 2a06:98c0:3600::103 (could be any Cloudflare IP)
- CF-EW-VIA: 15
- CF-RAY: [REDACTED]
- CF-REQUEST-ID: [REDACTED]
- CF-VISITOR: {"scheme":"https"}
- CF-WORKER: yourdomain.com (OPSEC Warning !)
- CONNECTION: Keep-Alive
- HOST: www.whatismybrowser.com
- USER-AGENT: My Random User-Agent
- X-FORWARDED-FOR: [redacted] (yes, we can override this header with whatever we want !)
- X-FORWARDED-PROTO: https
The important line is X-FORWARDED-FOR: it can be set to an arbitrary value. The origin IP is not forwarded to the website, so the only way to block this kind of request on the server is to filter on the CF-WORKER header. That header also leaks the domain running the worker, which the report flags as an OPSEC warning.
Root cause
The worker forwards requests to the target and lets the caller control the X-FORWARDED-FOR value, while the real origin IP is not passed on. Applications that trust X-FORWARDED-FOR for IP-based rate limiting or for IP verification during login therefore see whatever value the caller chose. Once the firewall rule permits it, the proxy can be reached over Tor without the reCaptcha challenge. Cloudflare's assessment was that this is not a bug.
Impact
With the proxy in place, an attacker can reach Cloudflare-fronted sites over Tor without the captcha, subject to the site not using Under Attack Mode. Because X-FORWARDED-FOR is arbitrary, IP-based request limits can be bypassed during scraping, and IP verification during a login procedure can be defeated, which the report describes as more dangerous. The only server-side mitigation identified is filtering on the CF-WORKER header. The report also points out that a free Cloudflare account comes with 100 000 request/day.
Fix
No fix was made: Cloudflare's position was that this is not a bug. The mitigation available to site owners is to filter requests on the CF-WORKER header rather than relying on X-FORWARDED-FOR for IP-based controls.
Variants to test
- Send a request through a worker proxy to an endpoint that echoes headers, and check whether
X-FORWARDED-FORreflects a value you supplied. A positive result is your chosen address appearing in the response. - Target an application that rate-limits by client IP and vary
X-FORWARDED-FORon each request. A positive result is the limit not triggering. - Target a login flow that verifies the client IP and supply a different address. A positive result is the verification passing with an address you control.
- Check whether the origin exposes
CF-WORKERin its responses or logs, and whether it rejects requests carrying it. A positive result for the defender is a rejection; absence of filtering means the proxy path is open.
What happened, in plain words
A security researcher found a way to use a free feature of Cloudflare, a company that sits in front of millions of websites and filters their traffic, to get past Cloudflare's own robot protection. He reported it to Cloudflare's bug bounty programme, which pays people for finding security problems. Cloudflare replied that it is not a bug. The researcher published the details anyway and left it to readers to decide.
The trick works like this. Cloudflare lets customers run small programs, called workers, on Cloudflare's own computers. A worker can be told to fetch a page from another website and hand the result back to whoever asked. The request reaches the target site through Cloudflare's network rather than from the visitor's own computer. The researcher also showed that the visitor can choose the address the target site believes the request came from, which can bypass IP-based request limits.
Before going further, a few everyday comparisons help.
- A proxy is a middleman. Instead of you walking to a shop, you phone an assistant who walks there for you and describes what they see. Websites see the assistant, not you.
- A server is a computer that answers requests from other computers. When you open a web page, your browser asks a server for it.
- Cloudflare is a company whose servers stand in front of a customer's website. Visitors talk to Cloudflare, and Cloudflare passes the request on to the real website. This lets Cloudflare block attacks and robots before they reach the site.
- A worker is a small program that Cloudflare runs on its own servers on your behalf. You write the program, Cloudflare runs it, and it can make requests to other sites.
- A header is a line of extra information attached to a web request or response, a bit like the label on a parcel. It says things such as which browser is asking, or which address the request came from.
- Tor is a free network that hides who you are by bouncing your traffic through several computers. Many sites treat Tor traffic with suspicion because it is often used to hide abuse.
- A captcha is the puzzle that asks you to tick a box or pick out traffic lights, meant to prove you are a human and not a robot.
- A bug bounty programme is a scheme where a company invites outsiders to look for security flaws and pays for good reports.
Context
Cloudflare offers JavaScript workers that run code on Cloudflare's own infrastructure. JavaScript is the programming language that web pages use in your browser; here the same language is used to run a program on a server instead. The feature is meant for things like static sites (pages that never change) and maintenance pages (the "we will be back soon" screen shown while a site is being fixed). But the same ability can be turned into a serverless command and control channel, meaning a way for an attacker to send instructions to and collect results from machines without owning any server, or into a phishing proxy, a middleman that shows a fake login page while passing your entries to the real site.
This write-up explores using a worker as a reverse proxy to get past Cloudflare's bot protection. A reverse proxy is a middleman that sits in front of a website and forwards requests to it; here the worker plays that role for other people's websites.
The starting observation is familiar to anyone who has tried to reach a site such as shodan.io from Tor: the captcha challenge is hard to get past. Shodan is a site that uses Under Attack Mode. The researcher submitted what looked like a security flaw to Cloudflare's bug bounty programme several months before publishing. Cloudflare's position was that it is not a problem, and the researcher leaves the judgement to the reader.
Setup
The prerequisites are deliberately minimal:
- Register a domain. A domain is a name such as example.com that you rent and point at a server. A free .tk domain is enough.
- Create a Cloudflare account and add the domain. This tells Cloudflare to handle traffic for that name.
- Once the domain is validated by Cloudflare, add at least one valid DNS entry that uses proxy mode. DNS is the internet's address book: it translates a name into the numeric address of a server. Proxy mode means Cloudflare hides the real address and answers visitors itself. The report notes that any IP can be used for this entry. An IP is the numeric address of a computer on the internet.
With the zone in place, the next step is a JavaScript worker acting as a reverse proxy. The full code is published at https://github.com/jychp/cloudflare-bypass. Create a new worker and paste the contents of worker.js into it. The values TOKEN_HEADER, TOKEN_VALUE, HOST_HEADER and IP_HEADER can be customised. These are settings inside the program: the name of the header that acts as a password, the password value itself, the header that says which site to fetch, and the header that says which address to claim the request came from.
Finally, add a route to the worker. A route is a rule that says which web addresses the worker should handle:
proxy.domain.com/*
This line means: whenever anyone asks for any page under proxy.domain.com, run the worker. The * stands for "anything after this point".
Finding it
Requesting proxy.domain.com returns "Welcome to NGINX.". NGINX is a common web server program, and that welcome page is its default. The worker code is simple: it looks for a specific header acting as a magic value and forwards the request to the given domain. A magic value is a secret word that proves the caller knows the trick, like a password at a door. That behaviour is what makes the worker useful as a proxy, and it is also the basis for the bypass described below.
Exploitation
Step 1: drive the proxy with the Python wrapper
A Python wrapper in the same GitHub repository makes the proxy easy to use. Python is a popular programming language, and a wrapper is a small helper program that hides the fiddly details of talking to something else:
>>> from cfproxy import CFProxy>>> proxy = CFProxy('proxy.domain.com', 'A random User-Agent', '[redacted]')>>> req = proxy.get('https://icanhazip.com')>>> print(req.status_code)200>>> print(req.text)[redacted]
Line by line:
from cfproxy import CFProxyloads the helper code from a file called cfproxy so it can be used.proxy = CFProxy('proxy.domain.com', 'A random User-Agent', '[redacted]')creates the helper. It is told the proxy's address, a User-Agent string (the text a browser normally sends to say which browser it is; here it is deliberately made up), and an IP address to claim as the visitor's address.req = proxy.get('https://icanhazip.com')asks the proxy to fetch a page. icanhazip.com is a simple site whose only job is to reply with the address it saw the request coming from.print(req.status_code)prints the reply code.200means success.print(req.text)prints the page body, which is[redacted].
The wrapper is constructed with the proxy host, a User-Agent string and an IP address. The get call fetches https://icanhazip.com through the proxy and returns status 200 with the body [redacted]. A WHOIS lookup on that address shows it is a Cloudflare IP, probably the server running the worker. WHOIS is a public directory that says who owns an address or a domain.
Step 2: allow Tor through the Cloudflare firewall
At this point, requesting the proxy through Tor is blocked. The fix is a rule in the Cloudflare firewall, under Firewall / Firewall Rules. A firewall is a filter that decides which traffic is allowed in and which is turned away. The report shows one way to make the rule work and notes that any other trigger can be used instead. With the rule in place, the proxy can be reached over Tor without the reCaptcha challenge. reCaptcha is Google's brand of captcha puzzle.
Step 3: observe the headers the origin receives
With the proxy working, any site behind Cloudflare can be requested, except sites using "Under Attack Mode" such as shodan.io. Under Attack Mode is a Cloudflare setting that makes every visitor pass a challenge, so it cannot be slipped past this way. Requesting a site that displays the request headers shows what the origin sees. The origin is the real web server behind Cloudflare, the one that actually holds the site's content:
- ACCEPT: /
- ACCEPT-ENCODING: gzip
- CDN-LOOP: cloudflare; subreqs=1
- CF-CONNECTING-IP: 2a06:98c0:3600::103 (could be any Cloudflare IP)
- CF-EW-VIA: 15
- CF-RAY: [REDACTED]
- CF-REQUEST-ID: [REDACTED]
- CF-VISITOR: {"scheme":"https"}
- CF-WORKER: yourdomain.com (OPSEC Warning !)
- CONNECTION: Keep-Alive
- HOST: www.whatismybrowser.com
- USER-AGENT: My Random User-Agent
- X-FORWARDED-FOR: [redacted] (yes, we can override this header with whatever we want !)
- X-FORWARDED-PROTO: https
What these lines mean:
ACCEPT: */*says the caller will take any kind of content.ACCEPT-ENCODING: gzipsays the caller can handle compressed replies, which saves bandwidth.CDN-LOOP: cloudflare; subreqs=1is a marker Cloudflare adds to spot requests that loop back through its own network.CF-CONNECTING-IPis the address Cloudflare connected from. The report notes it could be any Cloudflare IP.CF-EW-VIAandCF-RAYandCF-REQUEST-IDare Cloudflare's own tracking labels, used to trace a request through its systems.CF-VISITOR: {"scheme":"https"}records that the visitor used https, the encrypted form of the web.CF-WORKER: yourdomain.comreveals which domain's worker made the request. The report flags this as an OPSEC warning. OPSEC means operational security, the practice of not leaving clues about who you are.CONNECTION: Keep-Aliveasks to keep the network connection open for further requests.HOST: www.whatismybrowser.comnames the site being fetched. whatismybrowser.com is a site that reports back what your browser told it.USER-AGENT: My Random User-Agentis the made-up browser name from the wrapper.X-FORWARDED-FOR: [redacted]is the claimed visitor address. The report's comment notes it can be overridden with whatever value you want.X-FORWARDED-PROTO: httpssays the original visitor used https.
The important line is X-FORWARDED-FOR: it can be set to an arbitrary value. The origin IP is not forwarded to the website, so the only way to block this kind of request on the server is to filter on the CF-WORKER header. That header also leaks the domain running the worker, which the report flags as an OPSEC warning.
Root cause
The worker forwards requests to the target and lets the caller control the X-FORWARDED-FOR value, while the real origin IP is not passed on. Applications that trust X-FORWARDED-FOR for IP-based rate limiting or for IP verification during login therefore see whatever value the caller chose. Rate limiting means capping how many requests one visitor may make in a period, like a shop limiting each customer to two items. IP verification during login means checking that a login comes from the address that usually uses the account, an extra lock on the door. Once the firewall rule permits it, the proxy can be reached over Tor without the reCaptcha challenge. Cloudflare's assessment was that this is not a bug.
Impact
With the proxy in place, an attacker can reach Cloudflare-fronted sites over Tor without the captcha, subject to the site not using Under Attack Mode. Because X-FORWARDED-FOR is arbitrary, IP-based request limits can be bypassed during scraping, and IP verification during a login procedure can be defeated, which the report describes as more dangerous. Scraping means automatically collecting data from websites, often in bulk. The only server-side mitigation identified is filtering on the CF-WORKER header. A mitigation is a partial defence that reduces harm without removing the cause. The report also points out that a free Cloudflare account comes with 100 000 request/day.
Fix
No fix was made: Cloudflare's position was that this is not a bug. The mitigation available to site owners is to filter requests on the CF-WORKER header rather than relying on X-FORWARDED-FOR for IP-based controls.
Variants to test
- Send a request through a worker proxy to an endpoint that echoes headers, and check whether
X-FORWARDED-FORreflects a value you supplied. An endpoint is a specific address on a server that does one job, like a counter in a shop. A positive result is your chosen address appearing in the response. - Target an application that rate-limits by client IP and vary
X-FORWARDED-FORon each request. A positive result is the limit not triggering. - Target a login flow that verifies the client IP and supply a different address. A positive result is the verification passing with an address you control.
- Check whether the origin exposes
CF-WORKERin its responses or logs, and whether it rejects requests carrying it. A positive result for the defender is a rejection; absence of filtering means the proxy path is open.
Glossary
- Bug bounty programme: a scheme where a company invites outsiders to find security flaws and pays for good reports.
- Captcha: a puzzle meant to prove a visitor is human and not a robot.
- CDN: a content delivery network, a set of servers spread around the world that serve a site's traffic on its behalf; Cloudflare is one.
- Cloudflare: a company whose servers stand in front of customer websites and filter traffic before it reaches them.
- DNS: the internet's address book, translating names into numeric addresses.
- Domain: a rented name such as example.com that points at a server.
- Endpoint: a specific address on a server that performs one job.
- Firewall: a filter that decides which network traffic is allowed and which is turned away.
- Header: a line of extra information attached to a web request or response.
- IP address: the numeric address of a computer on the internet.
- JavaScript: the programming language web pages use in the browser; also used here to run programs on servers.
- Magic value: a secret word in a request that proves the caller knows the trick, like a password at a door.
- Mitigation: a partial defence that reduces harm without removing the cause.
- NGINX: a common web server program.
- OPSEC: operational security, the practice of not leaving clues about who you are.
- Origin: the real web server behind Cloudflare that holds the site's content.
- Phishing proxy: a middleman that shows a fake login page while passing entries to the real site.
- Proxy: a middleman that makes requests on someone else's behalf.
- Python: a popular programming language.
- Rate limiting: capping how many requests one visitor may make in a period.
- reCaptcha: Google's brand of captcha puzzle.
- Reverse proxy: a middleman that sits in front of a website and forwards requests to it.
- Route: a rule saying which web addresses a worker should handle.
- Scraping: automatically collecting data from websites, often in bulk.
- Server: a computer that answers requests from other computers.
- Serverless: running code on someone else's infrastructure without managing a server yourself.
- Tor: a free network that hides who you are by bouncing traffic through several computers.
- Under Attack Mode: a Cloudflare setting that makes every visitor pass a challenge.
- User-Agent: the text a browser sends to say which browser it is.
- WHOIS: a public directory that says who owns an address or a domain.
- Worker: a small program Cloudflare runs on its own servers on your behalf.
- Wrapper: a small helper program that hides the fiddly details of talking to something else.
- X-FORWARDED-FOR: a header claiming the address of the original visitor behind a proxy.