Cloudflare WAF: bảo vệ web app khỏi SQL injection, XSS và DDoS - setup đúng ngay từ đầu
Cloudflare WAF (Web Application Firewall) filter traffic ở edge trước khi request đến server của bạn. Free plan đã có Managed Rules cơ bản, Custom Rules, Rate Limiting. Bài này hướng dẫn cấu hình đúng - tránh false positive làm block user thật, đồng thời đảm bảo coverage cho OWASP Top 10. ---
Vấn đề
Web app của bạn đang "trần truồng" ngoài internet nếu chưa có WAF. Log server chắc chắn đang đầy những request kiểu:
GET /wp-admin/setup-config.php HTTP/1.1
POST /?username=admin'OR'1'='1 HTTP/1.1
GET /etc/passwd HTTP/1.1
Bot scan lỗ hổng, thử SQL injection, brute force login - đây là background noise của mọi web app public. WAF filter những request này ở edge, trước khi chúng tiêu tốn tài nguyên server của bạn.
Cloudflare WAF hoạt động thế nào
Request → Cloudflare Edge
→ WAF check (Managed Rules + Custom Rules)
→ Rate Limit check
→ Bot Management check
→ Pass hoặc Block / Challenge
→ Origin server (nếu pass)
Mỗi request qua edge đều được inspect theo thứ tự các rule. Rule nào match trước thì action của rule đó được áp dụng.
4 action chính:
- Block - trả về 403, request không đến server
- Challenge - hiện CAPTCHA (Managed Challenge hoặc JS Challenge)
- Log - ghi log, không block (dùng để test rule trước khi deploy)
- Skip - bỏ qua các rule tiếp theo cho request này
Bước 1: Bật Managed Rules
Managed Rules là bộ rule được Cloudflare maintain sẵn, cập nhật liên tục theo CVE mới và attack pattern mới.
Vào Security → WAF → Managed Rules.
Bật 2 ruleset chính:
1. Cloudflare Managed Ruleset - cover các attack phổ biến:
- SQL injection
- XSS (Cross-Site Scripting)
- Remote Code Execution
- Path traversal
2. Cloudflare OWASP Core Ruleset - dựa trên OWASP ModSecurity:
- Paranoia Level: bắt đầu với PL1 (ít false positive nhất)
- Score Threshold: High ban đầu, hạ xuống nếu muốn chặt hơn
Lưu ý quan trọng: KHÔNG bật hết tất cả rule ngay. Mặc định Cloudflare chỉ bật subset rule để giảm false positive. Nếu enable all override có thể block traffic hợp lệ, ví dụ: rich text editor gửi HTML content có thể bị detect là XSS.
Quy trình an toàn khi bật Managed Rules:
- Bật ruleset với action = Log trong 24–48 giờ
- Xem Security → Events để xem rule nào trigger
- Nếu có false positive → override rule đó thành Skip
- Sau khi ổn định → đổi action thành Block
Bước 2: Tạo Custom Rules
Custom Rules cho phép bạn viết logic riêng dựa trên nhiều attribute của request.
Vào Security → WAF → Custom Rules → Create Rule.
Rule 1: Block bad user agents
Expression:
(http.user_agent contains "sqlmap") or
(http.user_agent contains "nikto") or
(http.user_agent contains "masscan") or
(http.user_agent contains "zgrab") or
(http.user_agent eq "")
Action: Block
Rule 2: Bảo vệ admin endpoint
Expression:
(http.request.uri.path contains "/admin") and
not (ip.src in {your.office.ip.here})
Action: Managed Challenge (CAPTCHA)
Thay your.office.ip.here bằng IP tĩnh của văn phòng để team không bị block.
Rule 3: Block request từ high-risk country
Nếu app chỉ phục vụ user Việt Nam:
Expression:
(not ip.geoip.country in {"VN" "SG" "US"})
and (http.request.method eq "POST")
Action: Block
Chỉ block POST từ ngoài - GET requests (static assets) vẫn cho qua.
Rule 4: Chặn path traversal và common exploit
Expression:
(http.request.uri.path contains "..") or
(http.request.uri.path contains "/etc/passwd") or
(http.request.uri.path contains "wp-login.php") or
(http.request.uri.query contains "UNION SELECT")
Action: Block
Bước 3: Rate Limiting
Rate Limiting chặn request quá nhiều từ một IP trong khoảng thời gian ngắn - bảo vệ khỏi brute force và scraping.
Vào Security → WAF → Rate Limiting Rules → Create.
Rule: Bảo vệ login endpoint
When incoming requests match:
URI Path: /api/auth/login
OR
URI Path: /login
Rate: 5 requests per 60 seconds
Action: Block (duration: 10 minutes)
Rule: API rate limit chung
When incoming requests match:
URI Path starts with: /api/
Rate: 100 requests per 60 seconds per IP
Action: Managed Challenge
Best practice Rate Limiting: Bắt đầu với limit cao hơn thực tế cần, monitor, rồi hạ dần. Limit quá thấp ngay từ đầu dễ block user hợp lệ.
Bước 4: IP Access Rules
Cho phép blocklist/allowlist IP hoặc country cụ thể áp dụng cho toàn domain.
Vào Security → WAF → Tools → IP Access Rules.
# Block IP cụ thể (sau khi phát hiện tấn công)
IP: 1.2.3.4 → Block
# Allowlist IP văn phòng (không bao giờ bị WAF block)
IP: 203.x.x.x → Allow
# Block toàn bộ một country (cân nhắc kỹ trước khi làm)
Country: [code] → Challenge
IP Access Rules áp dụng trước tất cả rule khác - IP được Allow sẽ bypass WAF hoàn toàn.
Monitor và điều chỉnh
Security Events là nơi bạn xem mọi action WAF đã thực hiện:
Vào Security → Events:
- Lọc theo Action (Blocked, Challenged, Logged)
- Xem Request URI, IP, User Agent, Rule ID nào trigger
- Nếu thấy false positive → override rule đó cho domain/path cụ thể
Security Analytics cho cái nhìn tổng thể:
- Tổng request blocked trong 24h/7d
- Top attacking countries
- Top attacking IPs
- Rule breakdown
Đặt thói quen xem Security Events mỗi tuần - đây là nguồn dữ liệu để tune WAF cho sát với traffic thực của app.
So sánh: WAF free vs trả phí
| Tính năng | Free | Pro ($20/tháng) | Business ($200/tháng) |
|---|---|---|---|
| DDoS Protection | ✓ Unlimited | ✓ | ✓ |
| Managed Rules (cơ bản) | ✓ | ✓ | ✓ Full |
| Custom Rules | 5 rules | 20 rules | 100 rules |
| Rate Limiting | 1 rule | 2 rules | 10 rules |
| WAF Attack Score (ML) | ✗ | ✗ | ✓ |
| Bot Management (nâng cao) | ✗ | ✗ | ✓ |
Với team nhỏ và app không quá phức tạp, free plan là đủ để cover phần lớn attack surface.
Best practices
1. Log trước, block sau. Mọi rule mới đều nên chạy với action = Log trong 24h để catch false positive trước khi block thật.
2. Không block - hãy Challenge. Với traffic nghi ngờ, dùng Managed Challenge (CAPTCHA) thay vì Block cứng. User thật sẽ pass CAPTCHA, bot không pass được.
3. Segment rule theo endpoint. Admin endpoint cần rule chặt hơn API, API cần chặt hơn static assets. Đừng apply một rule cho toàn site.
4. Kết hợp WAF + Rate Limiting + Bot Management. WAF chặn exploit pattern, Rate Limiting chặn volume attack, Bot Management handle sophisticated bots. Ba lớp bổ sung nhau.
5. Đừng để WAF thay cho secure code. WAF là lớp bảo vệ thêm - không phải lý do để bỏ qua input validation trong code.
Kết
Cloudflare WAF free plan đã đủ để block phần lớn attack tự động nhắm vào web app: scanner bot, SQL injection, path traversal, brute force. Quan trọng là cấu hình đúng - bắt đầu với Log mode, monitor, rồi mới Block - để không ảnh hưởng user thật.
Bài tiếp theo chúng tôi cover Cache Rules - cách tăng cache hit rate từ 30% lên 90% và giảm load server đáng kể.
Tham khảo
- Cloudflare WAF Overview
- Managed Rules reference
- Custom Rules guide
- Rate limiting best practices
- Cloudflare WAF Best Practices
BKGlobal Tech Team