Web cache poisoning with an unkeyed cookie

2 min read Easy Portswigger
Web cache poisoning
Contents

On this page

The lab

here is how portswigger describes it

This lab is vulnerable to web cache poisoning because cookies aren't included in the cache key. An unsuspecting user regularly visits the site's home page. To solve this lab, poison the cache with a response that executes alert(1) in the visitor's browser.

same family as the unkeyed header lab, but this time the unsafe input comes in through a cookie. the app sets a fehost cookie and then reflects its value back inside the page, and the cache does not include cookies in its key. so we can set a malicious fehost, make the server build a poisoned response, and the cache stores it under the plain home page key with the cookie ignored. the regular visitor then gets our poisoned copy without ever sending the cookie themselves.

the cache key decides which stored response answers a request, and it is built from only the parts the cache chooses to look at, usually the path and a few headers. cookies are very often left out of that key on purpose, because tying cached pages to per-user cookies would make the cache almost useless. a cookie that the cache ignores is an unkeyed cookie, it still reaches the application and can still change the response, but the cache treats two requests that differ only in that cookie as the same entry. that is the hole, if the app reflects an unkeyed cookie into the page unsafely, we send one poisoned cookie value, the server bakes it into the response, and the cache files that response under the normal key and serves it to every later visitor, none of whom need our cookie.

we browse the application a little, a few pages back and forth, then study the requests and responses in the Burp HTTP history. we notice the first response sets a cookie, fehost=prod-cache-01.

a

Step 2 - Confirm where it lands in the response#

we reload the home page and look at where that cookie value ends up. the fehost value is reflected back inside a double-quoted javascript object in the response. so whatever we put in the cookie is dropped straight into a js string, which is exactly the kind of reflection we can break out of.

Step 3 - break out with an XSS payload#

we place an xss payload in the fehost cookie that closes the string, runs our code, and reopens it so the surrounding javascript stays valid:

http
Cookie: fehost=someString"-alert(1)-"someString
aa

the " closes the original string, -alert(1)- runs as part of the expression, and the trailing " keeps the object literal syntactically intact so the script still parses and our alert(1) executes.

Step 4 - Poison the cache#

we replay the request until the response both contains our payload and shows X-Cache: hit in the headers. the hit means our poisoned response is now the stored copy for the home page, and since the cookie is unkeyed it was filed under the ordinary home page key.

aaa

Step 5 - Confirm as the victim#

we load the home page url in the browser and the alert(1) fires, served from the poisoned cache.

aaaa

the regular visitor hits the same cached entry on their next visit and runs our payload without ever sending the cookie, which solves the lab.

with this, the lab is solved!