Skip to main content

WAF Middleware cho Flask (Bảo vệ OWASP Top 10)

Đây là thư viện tường lửa ứng dụng Web (WAF) mạnh mẽ dành cho Flask, được xây dựng như một đồ án chuyên ngành, bảo vệ người dùng trước các lỗi phổ biến bảo mật năm 2025.

🌟 Tính năng bảo vệ OWASP Top 10

Phiên bản WAF 2.0 chặn hiệu quả các lỗ hổng dựa trên OWASP Top 10 - 2021/2025:

  1. A01:2021 - Broken Access Control:
    • Bảo vệ chống CSRF nâng cao bằng cách đối chiếu header Origin và Host.
    • Giới hạn Request (Rate Limiting) và theo dõi quét thư mục bằng cách đếm số lỗi 404 (chống Dir-Scan).
  2. A03:2021 - Injection (SQLi, XSS, CMD, NoSQL...):
    • XSS (Cross-Site Scripting): Decode payload (URL/HTML entities) chống bypass, quét HTML5 vector, DOM methods, expression CSS.
    • SQL Injection: Quét cú pháp Tautology (OR 1=1), thủ thuật Union Select, Time-based blind injection và cả comment bypass.
    • Command & LDAP & NoSQL Injection: Chặn các toán tử shell (|, ;), regex dò tìm MongoDB ($where, $ne) và truy cập LDAP payload (*, \).
  3. A04:2021 - Insecure Design:
    • Tạo ra 16 bẫy Honeypot (ví dụ /.env, /wp-admin, /phpmyadmin). Khi hacker quét tới đây, IP sẽ bị cấm ngay lập tức (Ban IP vĩnh viễn).
  4. A05:2021 - Security Misconfiguration:
    • Tự động gắn các Security Headers: CSP, X-Content-Type-Options, X-Frame-Options, Referrer-Policy.
  5. A07:2021 - Identification and Authentication Failures:
    • Phát hiện Brute-Force Attack: Giới hạn tối đa 10 lần sai mật khẩu tại các URL /login trong 5 phút.
  6. A08:2021 - Software and Data Integrity Failures:
    • Chống Insecure Deserialization: Quét RCE thông qua các framework serialize (Python Pickle, Java, PHP object stream).
  7. A10:2021 - Server-Side Request Forgery (SSRF):
    • Ngăn chặn hệ thống request đến các Internal IP (127.0.0.1, 10.x, 192.168.x) và Cloud Metadata (169.254.x.x của AWS/GCP).
  8. Malicious File Upload:
    • Kiểm tra Extension, đối chiếu Magic Bytes, hỗ trợ SHA-256 hash checking mã độc đã biết, quét mã độc ẩn bên trong thân file ảnh và chặn request body quá cỡ (chống DoS).

🚀 Cài đặt

Bạn có thể tải hoặc cài đặt thư viện này thông qua pip.

pip install new-waf-middleware-name

🛠 Hướng dẫn Sử dụng

Tích hợp WAF vào ứng dụng Flask của bạn vô cùng nhanh gọn:

import os
from flask import Flask
from waf_middleware.firewall import Firewall

app = Flask(__name__)

# Cấu hình Secret Key (Bắt buộc cho Session)
app.config['SECRET_KEY'] = 'your-secret-key'

# Cấu hình Bot Telegram (Load từ Environment Variables)
os.environ['WAF_TELEGRAM_TOKEN'] = 'your-telegram-bot-token'
os.environ['WAF_TELEGRAM_CHAT_ID'] = 'your-telegram-chat-id'

# Khởi chạy WAF 
waf = Firewall(app)

@app.route('/')
def index():
    return "Web này đã được bảo vệ bởi WAF Middleware!"

if __name__ == "__main__":
    app.run(debug=True)

🧪 Cách Test nhanh (Sử dụng cURL hoặc Postman)

Chạy ứng dụng bằng python app.py và sử dụng các lệnh Terminal (hoặc CMD) sau:

  1. Test SQL Injection (Chặn Injection):
    curl "http://localhost:5000/search?query=admin' OR 1=1--"
    
  2. Test XSS payload qua URL bypass (Đã Encode):
    curl "http://localhost:5000/search?query=%3Csvg%20onload=alert(1)%3E"
    
  3. Test Honeypot (Quét thư mục nhạy cảm):
    curl "http://localhost:5000/.env"
    
    (Chú ý: Nếu chạy lệnh này, IP Localhost của bạn có thể bị Ban, phải truy cập vào file blacklist.txt lưu trên code để tự gỡ IP ra).
  4. Test Header Injection (Tấn công bằng Request Headers):
    curl -H "User-Agent: union select 1,2,3" "http://localhost:5000/"
    

Tất cả các lệnh trên sẽ bị trả về HTTP Status Code 403 Forbidden và một cảnh báo sẽ đẩy về Bot Telegram của bạn (nếu đã cài Token).


📦 Hướng dẫn Build và đẩy thư viện mới lên PyPI

Làm sao để đổi tên thư viện này và đẩy ngược lại lên pypi.org?

  1. Đổi tên: Vào file setup.py, đổi name="tên-thư-viện-của-bạn". Đổi version = "1.0.0". (Lưu ý tên không được trùng với thư viện đã có trên PyPI).
  2. Cài đặt Twine & Build tool:
    pip install setuptools wheel twine
    
  3. Đóng gói thư viện:
    python setup.py sdist bdist_wheel
    
    Lệnh này tạo ra thư mục dist/.
  4. Upload lên PyPI:
    twine upload dist/*
    
    Bạn sẽ cần nhập Username (thường là __token__) và Password (là mã API Token bạn tạo trên trang PyPI).

🔄 Hướng dẫn Liên kết Git Repository (Github/Gitlab)

Project này hiện đang liên kết với https://github.com/HoaiThoai/waf-middleware.git. Nếu bạn muốn gỡ ra và push lên repo rỗng mới:

# Gỡ liên kết cũ
git remote remove origin

# Gắn liên kết repo mới của bạn
git remote add origin https://github.com/YourName/new-repo.git

# Push code mới lên
git add .
git commit -m "Upgrade WAF to v2.0 with OWASP Protection"
git push -u origin main

Tác giả: Nguyễn Hoài Thoại (Phiên bản nâng cấp 2025 - 2026)

Metadata

Release files for waf-middleware-hthoai35 5.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for waf-middleware-hthoai35 5.0.0
File Size Uploaded Provenance Actions
waf_middleware_hthoai35-5.0.0.tar.gz 17.1 kB Without provenance Details for waf_middleware_hthoai35-5.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for waf-middleware-hthoai35 5.0.0
File Interpreter ABI Platform Actions
waf_middleware_hthoai35-5.0.0-py3-none-any.whl Python 3 none any Details for waf_middleware_hthoai35-5.0.0-py3-none-any.whl

Total release size: 34.4 kB

Release files / waf_middleware_hthoai35-5.0.0.tar.gz

Download URL waf_middleware_hthoai35-5.0.0.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
44eabc5be63bb1287bbe41a9055f5524c9758bf1604d13cb3e9f8b1e36c45b22
BLAKE2b-256 checksum
How to use checksums
23acf21701c3db2ac375b3bd1998411042d5af3fc804d2ab3e9862e5ad0ca264
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / waf_middleware_hthoai35-5.0.0-py3-none-any.whl

Download URL waf_middleware_hthoai35-5.0.0-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66ca0f3d101ffb547a08d00488338542136a0ee9816b56f50c1b7ed945db66c4
BLAKE2b-256 checksum
How to use checksums
fe3398ce2c9eacffc234cf32b4a3c0b2f219ec4aab104b6d59b2fccac881feca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for waf-middleware-hthoai35 5.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

5.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page