The lab
here is how portswigger describes it
This lab is vulnerable to web cache poisoning. It uses multiple layers of caching. A user regularly visits this site's home page using Chrome. To solve the lab, poison the internal cache so that the home page executes
alert(document.cookie)in the victim's browser.
the new idea here is two caches, not one. there is an external cache facing us and an internal cache sitting deeper in, and they key requests differently. a header can be keyed by one and unkeyed by the other, which means a value that would normally bust the cache for us at one layer slips through and poisons a stored fragment at the other. the plan is to find a page fragment that the internal cache stores separately, notice that X-Forwarded-Host is unkeyed there, and use it to poison that fragment so the geolocate.js url points at our exploit server.
why two caches change the game#
with one cache, a header is either in the key or not. with two layers stacked, each has its own key, and the interesting case is a header that is keyed externally but unkeyed internally. keyed externally means our normal requests with different header values land on different external entries, so it looks like a cache-buster to us. unkeyed internally means the deeper cache ignores it when storing a fragment, so the fragment it saves carries whatever our header said, under a key that plain visitor requests also match. so we can change the header freely without disturbing the outer cache while still writing our poison into the inner one. the fragment the internal cache stores separately is the geolocate.js import url, and that is what we overwrite.
Step 1 - map the external cache#
we load the home page, it works as a cache oracle, and send GET / to Repeater. we change the query string and the change is always reflected, which means the external cache includes the query string in its key. to experiment without clobbering the real external entry we use Param Miner to add a dynamic cache-buster query parameter, giving each of our requests its own external key. then we add X-Forwarded-Host pointing at our exploit server.

Step 2 - Find the separately cached fragment#
we keep sending the request and eventually the url for the geolocate.js resource is also overwritten with our exploit server url. that tells us this fragment is cached separately by the internal cache. and we notice we are getting a cache hit on that fragment even with our cache-buster query parameter present, which means the query string is unkeyed by the internal cache.

Step 3 - prove X-Forwarded-Host is unkeyed internally#
we remove the X-Forwarded-Host header and resend. the internally cached fragment still reflects our exploit server url, while the other two urls no longer do. that difference is the whole point, the header is unkeyed by the internal cache but keyed by the external one, so we can poison the internally cached fragment with it.

Step 4 - host the malicious script#
we go to the exploit server and create a file at the path the fragment imports:
/js/geolocate.js
with the payload:
alert(document.cookie)
store the exploit.

Step 5 - poison the internal cache and solve#
back in Repeater we disable the dynamic cache buster in the query string and re-add the X-Forwarded-Host header pointing at our exploit server.

we send the request over and over until all three dynamic urls in the response point to our exploit server, meaning the internal fragment is fully poisoned under the real key. we keep replaying to hold it poisoned until the victim, who regularly visits in Chrome, loads the page, imports our geolocate.js, and runs alert(document.cookie).


with this, the lab is solved!
