Exploiting origin server normalization for web cache deception

5 min read Medium Portswigger
Web cache deception
Contents

On this page

The lab

here is how portswigger describes it

To solve the lab, find the API key for the user carlos. You can log in to your own account using the following credentials: wiener:peter. We have provided a list of possible delimiter characters to help you solve the lab: Web cache deception lab delimiter list.

the new pieces in this lab#

same goal again, cache carlos's account page and read his API key, but here neither the path-abstraction trick nor a useful delimiter is available. the only delimiter the origin honours is ?, which the cache honours too (useless, as in the last lab). so we exploit a different disagreement: path normalization.

  • normalization is the process of resolving a URL path into its canonical form, in particular resolving dot-segments. .. means "go up one directory", so /aaa/../my-account normalizes to /my-account. a server that normalizes will resolve that and route to /my-account.
  • an encoded dot-segment is ..%2f, the %2f is a URL-encoded /. so /aaa/..%2fmy-account is /aaa/../my-account once the %2f is decoded. whether a component resolves this depends on whether it decodes %2f and then resolves the ...
  • the discrepancy this time: the origin server decodes and resolves the dot-segment (so /resources/..%2fmy-account becomes /my-account and returns the account page), but the cache does not, it treats the literal string /resources/..%2fmy-account as a path that starts with /resources, and the cache has a static-directory cache rule that caches everything under /resources. so the cache stores the response thinking it's a static resource, while the origin served the private account page.
  • a static-directory cache rule caches by path prefix (everything under /resources, /static, /assets, …) rather than by file extension. that's the rule we abuse here, we don't need a .js extension at all, we just need the URL to appear to live under /resources to the cache while resolving to /my-account at the origin. the plan: confirm the origin resolves ..%2f (so /aaa/..%2fmy-account returns the account page), find that /resources is a cached static directory, then combine the two, /resources/..%2fmy-account looks like a /resources static file to the cache but resolves to /my-account at the origin, so carlos's account page gets cached and we read it.

why the normalization discrepancy leaks the API key#

the flaw is that the cache and the origin normalize the path differently. the origin decodes %2f and resolves the .., so /resources/..%2fmy-account collapses to /my-account and it returns the logged-in user's private page. the cache does not decode and resolve, it sees the raw path still beginning with /resources, matches its "cache everything under /resources" rule, and stores the response as a static asset. so one request is simultaneously "the account page" (origin) and "a file under /resources" (cache). the cache ends up holding carlos's authenticated, per-user response keyed under a path anyone can request. the API key leaks because the component that decides what to return fully normalizes the path while the component that decides whether to cache does not, and the gap between those two views is exactly the account page filed under a static directory.

Step 1 - confirm no path abstraction#

we log in as wiener:peter; the response contains our API key. in Proxy > HTTP history we send GET /my-account to Repeater and change the path to /my-account/abc. this returns 404 Not Found, so the origin does not abstract the path to /my-account. the segment trick is out.

111

Step 2 - build a reference response#

we change the path to /my-accountabc and send. this returns 404 Not Found with no caching, our reference for "not a delimiter".

222

Step 3 - brute-force delimiters (and find none useful)#

we send the request to Intruder, Sniper, with a payload position after /my-account:

/my-account§§abc

we load the delimiter candidate list and deselect "URL-encode these characters", then start the attack. sorting by status, only ? returns 200 with our API key. since ? is universally treated as a delimiter (the cache honours it too, as we saw last lab), it's useless for deception. so we pivot to normalization discrepancies.

333

Step 4 - confirm the origin resolves encoded dot-segments#

in Repeater we remove abc and prepend an arbitrary directory plus an encoded dot-segment:

/aaa/..%2fmy-account

this returns 200 with our API key. so the origin decodes %2f and resolves .., interpreting the path as /my-account. normalization discrepancy is in play.

444

Step 5 - find the cached static directory#

in Proxy > HTTP history we notice static resources all live under the /resources prefix, and responses under /resources show evidence of caching. so /resources is a cached directory, our candidate prefix.

555

Step 6 - confirm the cache doesn't resolve the dot-segment#

we add an encoded dot-segment after /resources:

/resources/..%2fYOUR-RESOURCE

the 404 response carries X-Cache: miss; resending it flips X-Cache to hit. so the cache caches this despite the ..%2f, meaning it did not resolve the dot-segment, it still saw /resources. to be sure it's the prefix rule (not something else), we also test /resources/aaa: X-Cache: miss then hit on resend, confirming a static-directory cache rule on /resources.

666

Step 7 - combine into the exploit path#

in the Repeater tab with /aaa/..%2fmy-account, we build the exploit path:

/resources/..%2fmy-account

this returns 200 with our API key and X-Cache: miss; resending flips X-Cache to hit. so to the cache it's a /resources static file, and to the origin it resolves to /my-account, exactly the deception we need.

777

Step 8 - craft the exploit#

we go to the exploit server and, in the Body, redirect carlos to the malicious URL, adding an arbitrary query parameter as a cache buster so the victim's response is cached under a fresh key rather than serving our earlier entry:

html
<script>document.location="https://YOUR-LAB-ID.web-security-academy.net/resources/..%2fmy-account?wcd"</script>
888

Step 9 - retrieve carlos's cached response#

we deliver the exploit, then visit the same URL:

https://YOUR-LAB-ID.web-security-academy.net/resources/..%2fmy-account?wcd

the cached response contains carlos's API key.

999

Step 10 - submit the key#

we copy and submit carlos's API key.

xxx

with this, the lab is solved!