đĨ Flask Framework
Flask Installation
Complete installation guide for AIWAF Flask. Get advanced AI-powered security protection for your Flask applications with zero dependencies and flexible storage options.
đĻ Installation Options
Basic Installation
Install AIWAF Flask without AI features for essential protection:
# Basic installation (without AI features)
pip install aiwaf-flask
AI-Enhanced Installation (Recommended)
Install with AI anomaly detection capabilities:
# With AI anomaly detection features
pip install aiwaf-flask[ai]
Full Installation
Complete installation with all features and development tools:
# Full installation (AI + development tools)
pip install aiwaf-flask[all]
AI Features: The AI anomaly detection requires NumPy and Scikit-learn. Install with
[ai] or [all] extras for machine learning capabilities.
âī¸ Requirements
- Python 3.7+
- Flask 2.0+
- For AI Features: scikit-learn, numpy
đ Quick Start Examples
1. Basic Flask Integration
Minimal setup with CSV storage (no database required):
from flask import Flask
from aiwaf_flask import register_aiwaf_middlewares
app = Flask(__name__)
# Basic configuration
app.config.update({
'AIWAF_USE_CSV': True, # Enable CSV storage
'AIWAF_ENABLE_LOGGING': True, # Enable access logging
'AIWAF_DATA_DIR': 'aiwaf_data', # Data directory
'AIWAF_LOG_DIR': 'logs', # Log directory
})
# Register AIWAF protection
register_aiwaf_middlewares(app)
@app.route('/')
def home():
return "Protected by AIWAF!"
if __name__ == '__main__':
app.run(debug=True)
2. Database Integration
Using database storage for production environments:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from aiwaf_flask import register_aiwaf_middlewares
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['AIWAF_USE_CSV'] = False # Use database storage
db = SQLAlchemy(app)
register_aiwaf_middlewares(app)
3. Advanced AIWAF Class Usage
Use the AIWAF class for more control over middleware selection:
from flask import Flask
from aiwaf_flask import AIWAF
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'
# Enable only specific middlewares
aiwaf = AIWAF(app, middlewares=[
'rate_limit', # Rate limiting protection
'ip_keyword_block', # IP/keyword blocking
'ai_anomaly', # AI anomaly detection
'logging' # Request/response logging
])
# Or enable all except specific ones
aiwaf = AIWAF(app, disable_middlewares=[
'honeypot', # Disable honeypot timing
'uuid_tamper' # Disable UUID tampering protection
])
đĄī¸ Available Middlewares
| Middleware | Name | Description |
|---|---|---|
| IP & Keyword Block | ip_keyword_block | Blocks malicious IPs and detects attack keywords |
| Rate Limiting | rate_limit | Protects against brute force and DDoS attacks |
| Honeypot Timing | honeypot | Detects automated form submissions |
| Header Validation | header_validation | Validates HTTP headers for security threats |
| AI Anomaly Detection | ai_anomaly | Machine learning-based pattern analysis |
| UUID Tampering | uuid_tamper | Protects against UUID manipulation attacks |
| Request Logging | logging | Comprehensive request/response logging |
đ§ Storage Options
CSV Storage (Recommended for small apps)
app.config['AIWAF_USE_CSV'] = True
app.config['AIWAF_DATA_DIR'] = 'aiwaf_data' # Optional: custom directory
Benefits: No database required, human-readable, easy backup/migration, fast setup
Database Storage (Recommended for production)
app.config['AIWAF_USE_CSV'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
Benefits: Concurrent access, transactional safety, better performance at scale
Note: When using database storage, make sure to initialize your database tables with
db.create_all() in your application context.
â Verification
After installation, verify AIWAF is working:
python -c "
from aiwaf_flask import AIWAF
print('â
AIWAF Flask installed successfully!')
try:
import numpy, sklearn
print('â
AI dependencies available')
except ImportError:
print('âšī¸ AI dependencies not installed (use pip install aiwaf-flask[ai])')
"
đ Next Steps
Once installed, continue with:
- Configure middleware settings
- Learn CLI commands for management
- Start your Flask app and monitor the protection logs
Pro Tip: AIWAF automatically creates necessary directories and files on first run. Check the
aiwaf_data/ and logs/ directories after starting your application.