This document provides exhaustive documentation on the architecture, command-line interfaces, security subsystems, and maintenance procedures for the PhishShield-Engine platform.
# Initialize Virtual Environment
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # macOS/Linux
# Install All Dependencies
pip install -r requirements.txt
# Run Database Migrations
alembic upgrade headCopy .env.example to .env and set your configuration variables:
# Generate a cryptographically strong secret for JWT signing
python -c "import secrets; print(secrets.token_hex(32))"In .env:
JWT_SECRET=<generated_secret>
JWT_EXPIRY_HOURS=1
LOGIN_MAX_ATTEMPTS=5
LOGIN_LOCKOUT_MINUTES=15
RESET_TOKEN_EXPIRY_HOURS=1
ENV=prodYou must initialize your models before the first run. For a full deep-dive into the training lifecycle, see the ML Training Guide.
# Recommendation: Quick-start on 50,000 samples (fast mode)
python scripts/train_pipeline.py --generate --fastUsed for day-to-day operations and threat intelligence management.
| Command | Arguments | Description |
|---|---|---|
serve |
--port <val> |
Launches the cinematic security dashboard. |
block |
<domain> [--reason <str>] |
Manually injects a domain into the threat intelligence blocklist. |
metrics |
(None) | Aggregates local threat DB statistics and model health. |
Example Usage:
python cli/manage.py block evil-hacker.com --reason "Active credential harvesting detected"Handles the entire ML lifecycle from raw data to registered production models.
| Flag | Default | Description |
|---|---|---|
--dataset_path |
data/raw/emails.csv |
Path to the training dataset. |
--vectorizer |
tfidf |
Algorithm: tfidf, bow, or tfidf_char. |
--sample_size |
(None) | Sub-sample N rows (prevents OOM on large datasets). |
--fast |
(False) | Shortcut: 50k samples + Skip Ensemble. |
--tune |
(False) | Triggers RandomizedSearchCV tuning. |
--ensemble |
(False) | Trains model ensembles (voting or stacking). |
--ensemble_kind |
voting |
Choose voting (fast) or stacking (accurate). |
--generate |
(False) | Generates synthetic phishing samples. |
python scripts/retrain_scheduler.py: Background daemon that monitorsdata/feedback.db. Triggers retraining once new feedback exceeds the threshold.python scripts/chaos_monkey.py: Injects faults (ML model corruption, load spikes) to verify graceful degradation.python scripts/backup.py backup: Creates a timestamped snapshot of models, threat DBs, and configs.
- Password Hashing: Utilizes
bcryptwith salt generation (gensalt()). Enforces password complexity:- Minimum 8 characters
- At least 1 numeric digit
- At least 1 special character (
!@#$%^&*...)
- Session & JWT Management: Issues short-lived JWTs (default 1 hour expiry) with
sub,iat,exp, andjticlaims. Secrets are managed viaSecretsVaultwith environment-variable precedence. - Brute-Force Lockout: Tracks
failed_login_attemptsper user. Reaching 5 failures locks the account for 15 minutes (locked_until). - Timing Attack Mitigation: Employs constant-time string comparison (
hmac.compare_digest) for API key verification (pse_prefix) and executes dummy password hashes for non-existent users. - Password Reset Flow:
request_password_resetissues a single-use token (secrets.token_urlsafe(32)). Only SHA-256 digests (password_reset_token_hash) and expirations (password_reset_expires) are stored in the database. - Insecure Direct Object Reference (IDOR) Defense: All user resource queries (
GET /auth/me,GET /auth/logs,GET /auth/logs/{log_id},GET /auth/feedback/{feedback_id},DELETE /auth/feedback/{feedback_id}) filter strictly byresource.user_id == current_user.id. Accessing unowned resources returns404 Not Foundwithout disclosing resource existence.
Auth Endpoints (per IP):
/auth/login: 5 requests/min/auth/register: 3 requests/min/auth/password-reset-request: 3 requests/min/auth/password-reset: 5 requests/min
Prediction & AI Endpoints (per IP):
POST /predict: 15 requests/minPOST /analyze-security: 15 requests/minPOST /predict/batch: 5 requests/minPOST /export-report: 10 requests/minPOST /feedback: 10 requests/min
Global default: 60 requests/min per IP
All rate limit violations are logged as RATE_LIMIT_EXCEEDED events to logs/security_audit.log.
The users table includes security governance fields:
username,password_hash,api_keyemail(optional, unique)is_email_verified(boolean flag)failed_login_attempts(integer counter)locked_until(datetime)password_reset_token_hash(string)password_reset_expires(datetime)
Schema revisions are maintained via Alembic (alembic/versions/8eb220da558a8768_add_auth_security_fields.py).
PhishShield enforces three bot-detection layers via anti_bot_middleware applied before every API handler:
- Honeypot Header Trap: Requests containing
X-Honeypot-TraporX-Bot-Checkheaders are blocked (HTTP 403). Legitimate clients never send these headers. - User-Agent Enforcement: All API paths require a non-empty
User-Agentheader. Missing headers indicate scripted or raw socket clients (HTTP 403). - Scanner Blacklist: Requests from known exploit and scraping tools (
sqlmap,nikto,nmap,masscan,bytespider,zgrab,libwww-perl,python-urllib,dirbuster,w3af,acunetix,nessus,openvas) are blocked (HTTP 403). - Honeypot Payload Fields:
PredictRequestandRegisterRequestinclude a hiddenbot_trapfield. Any non-empty value triggers Pydantic validation failure (HTTP 422). - Batch Size Cap:
POST /predict/batchrejects arrays exceeding 50 emails to prevent bulk data harvesting.
All blocked events are logged to logs/security_audit.log with event types BOT_ATTACK_BLOCKED, BOT_BLOCKED_EMPTY_UA, and BOT_HONEYPOT_TRIGGERED.
The calculate_security_risk function (in src/security/risk_scoring.py) executes 10 independent forensic scans:
- Obfuscation Scan: Detects zero-width icons/markers via
src/security/obfuscation_detector.py. - Homograph Scan: Identifies "lookalike" Unicode domains (punycode detection).
- Mixed-Script Detection: Identifying "lookalike" characters from Latin, Greek, and Cyrillic scripts.
- Brand Protection: Fuzzy-matches against 15+ high-value target brands.
- Header Forensics: SPF/DKIM/DMARC validation and
Return-Pathanalysis. - Domain Intel: Local SQLite lookup of known malicious domains.
- IP-Based URLs: Detecting raw IP addresses in message links.
- Suspicious URLs: Heuristic analysis of URL entropy and path patterns.
- Behavioral Threat: Statistical mapping of text-based threat indicators.
- Cyrillic URL Spoofing: Detects Cyrillic characters embedded in URLs (weight: 50).
Control the engine's behavior without modifying code:
- Weights: Adjust the risk contribution of each forensic flag (including
cyrillic_url: 50). - Thresholds: Define "High Risk" and "Suspicious" cutoffs.
- Compliance: Define auto-retention days (Default: 30 days).
The platform is designed to run as a multi-service architecture using Docker Compose:
# Launch API, Threat DB, and Retraining Scheduler (with model training)
docker-compose up --build -d
# Or build without training (use pre-trained models from models/ directory)
docker build --target base -t phishshield-base .
docker-compose up -d- Service API: Exposes the port
8000. - Service Scheduler: Runs the automated retraining watcher.
- Persistence: Volumes are mapped for
data/andlogs/to prevent loss during restarts. - Training Control: Set
TRAIN_MODELS=falsebuild arg to skip training during image build (use pre-trained models).
- Input Validation & Sanitization Tests:
python -m pytest tests/test_input_validation.py -v(29 tests) - Auth & Security Unit Tests:
python -m pytest tests/test_auth_unit.py -v(21 tests) - Abuse & Bot Protection Tests:
python -m pytest tests/test_abuse_protection.py -v(29 tests) - Secrets Audit Tests:
python -m pytest tests/test_secrets_audit.py -v(16 tests) - Full Security Test Suite:
python -m pytest tests/test_input_validation.py tests/test_secrets_audit.py tests/test_auth_unit.py tests/test_abuse_protection.py -v(95 / 95 tests passing) - Integration Tests:
python scripts/chaos_monkey.py(Simulates failure scenarios) - Compliance Audit: View
logs/compliance.logto audit data retention and forensic overrides. - Security Audit Log: View
logs/security_audit.logfor authentication events, rate limit violations, bot blocking, and client error anomalies. - Warning Suppression: Tests use
tests/conftest.pyto suppress httpx deprecation and LGBM feature name warnings.
VIPHACKER100 (Aryan Ahirwar)
Cybersecurity Researcher | AI Security Lead
Last Updated: 2026-09-02