Web cache poisoning with multiple headers

3 min read Medium Portswigger
Web cache poisoning
Contents

On this page

The lab

here is how portswigger describes it

This lab contains a web cache poisoning vulnerability that is only exploitable when you use multiple headers to craft a malicious request. A user visits the home page roughly once a minute. To solve this lab, poison the cache with a response that executes alert(document.cookie) in the visitor's browser.

same goal as the unkeyed header lab, serve a poisoned script import to every visitor, but here no single header does the job on its own. one unkeyed header controls the host used to build an absolute url, another controls the scheme and only takes effect on a redirect. neither matters alone, the host header does nothing until a redirect is in play, and the redirect only fires when the scheme header is wrong. chained together they produce a 302 to a url on our host, and that redirect gets cached. this is why it needs multiple headers.

why two headers are needed#

the app reflects X-Forwarded-Host into an absolute url, but on a plain request that reflection has no visible effect. the second header, X-Forwarded-Scheme, is the trigger, when its value is anything other than https the app decides the request is insecure and answers with a 302 redirect to the https version of the same url. and crucially it builds that redirect's Location using X-Forwarded-Host. so one header alone is inert, but set the scheme to something wrong to force the redirect and set the host to our exploit server, and the Location points at us. both headers are unkeyed, so the poisoned 302 gets cached under the normal key and served to real visitors, whose browsers follow it and pull our script.

Step 1 - locate the script request#

we browse the app, then in Proxy > HTTP history we study the traffic we generated. we find the GET request for the javascript file /resources/js/tracking.js and send it to Repeater, since that import is what we want to redirect.

b

Step 2 - see that X-Forwarded-Host alone does nothing#

we add a cache-buster query parameter so our tests get their own key, and add X-Forwarded-Host with an arbitrary hostname like example.com. we send it and notice the response is unchanged, the host header on its own has no effect here.

bb

Step 3 - find the redirect trigger and chain the headers#

we remove X-Forwarded-Host and instead add X-Forwarded-Scheme. with any value other than https the server answers with a 302, and the Location header redirects us to the same url but over https://. so this header controls whether a redirect happens.

now we put both headers together, the wrong scheme to force the redirect and the host to steer where it points:

http
X-Forwarded-Host: example.com
X-Forwarded-Scheme: nothttps

the 302's Location now points at https://example.com/, the host we supplied is being used to build the redirect target.

bbb

Step 4 - 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:

javascript
alert(document.cookie)
bbbb

Step 5 - poison the cache and solve#

back in Repeater we point the host at our exploit server and keep the scheme wrong so the redirect still fires:

http
X-Forwarded-Host: YOUR-EXPLOIT-SERVER-ID.exploit-server.net
X-Forwarded-Scheme: nothttps

we send the request until the response reflects our exploit server url and shows X-Cache: hit, meaning the poisoned 302 is now cached for the real script path. we keep replaying to hold the cache poisoned until the victim, who visits about once a minute, loads the home page, follows the cached redirect to our tracking.js, and runs alert(document.cookie).

bbbbb

with this, the lab is solved!