Educational Python application demonstrating API security hardening, threat detection, custom security middleware, brute force protection, API abuse prevention, security headers, and vulnerability scanning.
- Input Validation - Sanitize and validate user input
- Authentication - API key validation
- Authorization - Role-based access control
- Security Middleware - Layered security approach
- HTTPS Enforcement - Secure connections
- SQL Injection Detection - Pattern-based detection
- XSS Detection - Cross-site scripting prevention
- Path Traversal Detection - Directory traversal prevention
- Malicious Pattern Recognition - Common attack patterns
- Threat Logging - Security event tracking
- Global Rate Limiting - Overall request limits
- Endpoint-Specific Limits - Different limits per endpoint
- IP-Based Limiting - Per-IP rate limits
- Sliding Window - Time-based request tracking
- Rate Limit Headers - X-RateLimit-* headers
- Login Attempt Tracking - Count failed logins
- Account Lockout - Lock after N failures
- IP-Based Blocking - Block suspicious IPs
- Lockout Duration - Configurable timeout
- Automatic Unlock - Time-based unlock
- Blacklist Management - Block malicious IPs
- Whitelist Management - Allow trusted IPs
- Temporary Blocks - Time-limited blocks
- Block History - Track blocking events
- Auto-Expiry - Temporary blocks expire
- X-Content-Type-Options - Prevent MIME sniffing
- X-Frame-Options - Prevent clickjacking
- X-XSS-Protection - XSS filter
- Strict-Transport-Security - Force HTTPS
- Content-Security-Policy - CSP rules
- Referrer-Policy - Control referrer info
- Header Scanner - Check security headers
- Injection Scanner - Test for SQL injection
- XSS Scanner - Test for XSS vulnerabilities
- Authentication Scanner - Check auth requirements
- Security Report - Generate findings
git clone https://github.com/Amruth22/Python-API-Security-Hardening.git
cd Python-API-Security-Hardeningpython -m venv venv
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activatepip install -r requirements.txtpython main.pypython api/app.pyThe API will be available at http://localhost:5000
python tests.pyPython-API-Security-Hardening/
│
├── security/
│   ├── rate_limiter.py          # Rate limiting
│   ├── brute_force_protection.py # Login protection
│   ├── security_headers.py      # HTTP headers
│   └── middleware.py            # Security middleware
│
├── threat_detection/
│   ├── threat_detector.py       # Threat detection
│   └── ip_blocker.py            # IP blocking
│
├── validation/
│   └── input_validator.py       # Input validation
│
├── scanner/
│   └── vulnerability_scanner.py # Security scanning
│
├── api/
│   └── app.py                   # Flask API
│
├── main.py                      # Demonstration script
├── tests.py                     # 10 unit tests
├── requirements.txt             # Dependencies
├── .env                         # Configuration
└── README.md                    # This file
from security.rate_limiter import RateLimiter
# Create rate limiter
limiter = RateLimiter(max_requests=100, window=60)
# Check if request is allowed
if limiter.is_allowed(client_ip):
    # Process request
    pass
else:
    # Return 429 Too Many Requests
    return "Rate limit exceeded", 429from security.brute_force_protection import BruteForceProtection
# Create protection
protection = BruteForceProtection(max_attempts=5, lockout_duration=300)
# Check if account is locked
if protection.is_locked(username):
    return "Account locked", 423
# Record failed login
locked = protection.record_failed_attempt(username, client_ip)
if locked:
    return "Account locked due to too many failures", 423from threat_detection.threat_detector import ThreatDetector
# Create detector
detector = ThreatDetector()
# Check for SQL injection
if detector.detect_sql_injection(user_input):
    return "SQL injection detected", 400
# Check for XSS
if detector.detect_xss(user_input):
    return "XSS detected", 400
# Detect any threat
threat = detector.detect_threats(request_data)
if threat:
    return f"{threat} detected", 400from security.security_headers import SecurityHeaders
# Add to Flask app
app = Flask(__name__)
security_headers = SecurityHeaders(app)
# All responses will include security headersfrom threat_detection.ip_blocker import IPBlocker
# Create blocker
blocker = IPBlocker()
# Block an IP
blocker.block_ip("192.168.1.100", reason="Malicious activity")
# Check if blocked
if blocker.is_blocked(client_ip):
    return "IP blocked", 403
# Whitelist an IP
blocker.whitelist_ip("192.168.1.1")from validation.input_validator import InputValidator
# Create validator
validator = InputValidator()
# Validate email
if not validator.validate_email(email):
    return "Invalid email", 400
# Validate password
result = validator.validate_password(password)
if not result['valid']:
    return {"errors": result['errors']}, 400
# Sanitize input
sanitized = validator.sanitize_dict(request_data)GET /GET /healthPOST /auth/login
Content-Type: application/json
{
  "username": "admin",
  "password": "Admin123!"
}Brute Force Protection:
- Max 5 attempts per account
- 5-minute lockout after failures
- IP tracking for suspicious activity
GET /api/data
X-API-Key: secure-api-key-12345POST /api/user
X-API-Key: secure-api-key-12345
Content-Type: application/json
{
  "username": "john_doe",
  "email": "john@example.com",
  "password": "SecurePass123"
}GET /security/infoGET /security/headersPOST /security/scan
X-API-Key: secure-api-key-12345
Content-Type: application/json
{
  "url": "http://localhost:5000/api/data"
}Purpose: Prevent API abuse and DDoS attacks
How it works:
- Tracks requests per IP address
- Sliding time window
- Returns 429 when limit exceeded
- Includes retry-after header
Configuration:
RATE_LIMIT_REQUESTS=100
RATE_LIMIT_WINDOW=60Purpose: Prevent password guessing attacks
How it works:
- Tracks failed login attempts
- Locks account after threshold
- Blocks suspicious IPs
- Auto-unlocks after timeout
Configuration:
MAX_LOGIN_ATTEMPTS=5
LOCKOUT_DURATION=300Headers Added:
- X-Content-Type-Options: nosniff
- X-Frame-Options: DENY
- X-XSS-Protection: 1; mode=block
- Strict-Transport-Security: max-age=31536000
- Content-Security-Policy: default-src 'self'
Detects:
- SQL injection attempts
- XSS attacks
- Path traversal
- Malicious patterns
Action:
- Blocks request
- Logs security event
- Returns 400 Bad Request
Validates:
- Email format
- Username format
- Password strength
- JSON structure
- Data types
Sanitizes:
- HTML escaping
- Special character removal
- Whitespace trimming
Run the comprehensive test suite:
python tests.py- ✅ Rate Limiting - Test request limits
- ✅ Brute Force Protection - Test login lockout
- ✅ Security Headers - Test header presence
- ✅ SQL Injection Detection - Test SQL patterns
- ✅ XSS Detection - Test XSS patterns
- ✅ IP Blocking - Test blacklist/whitelist
- ✅ Input Validation - Test validation rules
- ✅ Threat Detection Dict - Test nested data
- ✅ Endpoint Rate Limiting - Test per-endpoint limits
- ✅ Security Integration - Test all components together
Malicious: admin' OR '1'='1
Detection: Pattern matching for SQL keywords
Prevention: Input validation, parameterized queries
Malicious: <script>alert('xss')</script>
Detection: Pattern matching for script tags
Prevention: HTML escaping, CSP headers
Attack: Multiple login attempts
Detection: Failed attempt counting
Prevention: Account lockout, rate limiting
Attack: Excessive requests
Detection: Request rate tracking
Prevention: Rate limiting, throttling
For production use:
- 
Use HTTPS: - Always use SSL/TLS
- Enforce HTTPS
- Use valid certificates
 
- 
External WAF: - Use cloud WAF (Cloudflare, AWS WAF)
- DDoS protection
- Advanced threat detection
 
- 
Database Security: - Use parameterized queries
- Encrypt sensitive data
- Implement proper access control
 
- 
Monitoring: - Log security events
- Set up alerts
- Monitor attack patterns
 
- 
Regular Updates: - Keep dependencies updated
- Patch vulnerabilities
- Security audits
 
- Flask 3.0.0 - Web framework
- requests 2.31.0 - HTTP client
- python-dotenv 1.0.0 - Environment variables
- pytest 7.4.3 - Testing framework
- passlib 1.7.4 - Password hashing
This project is for educational purposes. Feel free to use and modify as needed.
Happy Securing! 🔒