Skip to content

SurajsinghBayas/SentinelX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

39 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ SentinelX β€” AI-Powered Real-Time Threat Detection Platform

SentinelX FastAPI Python PostgreSQL Redis Docker

Production-grade AI cybersecurity backend detecting phishing, scams, and social engineering in real-time.

API Docs β€’ Architecture β€’ Setup β€’ Docker


Overview

SentinelX is a modular, AI-ready backend platform that continuously monitors communication streams β€” email, SMS, messaging, and phone calls β€” to detect malicious intent before credential theft or system compromise occurs.

Key Capabilities

Feature Technology
NLP Phishing Detection HuggingFace DistilBERT / Zero-Shot Classification
Behavioral Analysis Rule-based social engineering pattern engine
URL Threat Scoring Regex heuristics + optional VirusTotal API
Speech-to-Text OpenAI Whisper (tiny β†’ large models)
Risk Scoring Weighted multi-signal composite engine
Async Processing Celery + Redis task queues
Authentication JWT + bcrypt + RBAC
Database PostgreSQL + SQLAlchemy + Alembic
Containerization Docker + Docker Compose

Architecture

SentinelX Backend
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ routes/          # FastAPI route handlers
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.py      # POST /auth/register, /login, GET /me
β”‚   β”‚   β”‚   β”œβ”€β”€ analyze.py   # POST /analyze/{email,sms,call}, /transcribe/audio
β”‚   β”‚   β”‚   β”œβ”€β”€ alerts.py    # GET /alerts, POST /alerts/{id}/acknowledge
β”‚   β”‚   β”‚   └── dashboard.py # GET /dashboard/{stats,threats,trends}
β”‚   β”‚   β”œβ”€β”€ dependencies/    # Auth dependency injectors
β”‚   β”‚   └── middleware/      # Request logging, tracing
β”‚   β”‚
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ config.py        # Pydantic Settings (env vars)
β”‚   β”‚   β”œβ”€β”€ security.py      # JWT + bcrypt
β”‚   β”‚   └── logging.py       # Structured JSON logging
β”‚   β”‚
β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”œβ”€β”€ base.py          # SQLAlchemy DeclarativeBase
β”‚   β”‚   β”œβ”€β”€ session.py       # Engine + session factory
β”‚   β”‚   └── models/          # User, Threat, Alert, AuditLog
β”‚   β”‚
β”‚   β”œβ”€β”€ schemas/             # Pydantic request/response models
β”‚   β”‚
β”‚   β”œβ”€β”€ services/            # Business logic orchestration
β”‚   β”‚   β”œβ”€β”€ email_service.py
β”‚   β”‚   β”œβ”€β”€ sms_service.py
β”‚   β”‚   β”œβ”€β”€ call_service.py
β”‚   β”‚   β”œβ”€β”€ alert_service.py
β”‚   β”‚   β”œβ”€β”€ risk_service.py
β”‚   β”‚   └── dashboard_service.py
β”‚   β”‚
β”‚   β”œβ”€β”€ ml/                  # AI/ML inference layer
β”‚   β”‚   β”œβ”€β”€ phishing_model.py  # HuggingFace zero-shot classifier
β”‚   β”‚   β”œβ”€β”€ sms_model.py       # SMS-specific scam detector
β”‚   β”‚   β”œβ”€β”€ url_detector.py    # URL threat analysis
β”‚   β”‚   β”œβ”€β”€ behavior_model.py  # Social engineering pattern engine
β”‚   β”‚   β”œβ”€β”€ whisper_service.py # Speech-to-text
β”‚   β”‚   └── risk_engine.py     # Composite risk scorer
β”‚   β”‚
β”‚   β”œβ”€β”€ workers/
β”‚   β”‚   └── celery_worker.py   # Async task definitions
β”‚   β”‚
β”‚   └── main.py              # FastAPI app entry point
β”‚
β”œβ”€β”€ alembic/                 # Database migrations
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ requirements.txt
└── .env.example

Risk Scoring Formula

RiskScore = 0.35 Γ— NLPScore
          + 0.25 Γ— BehaviorScore
          + 0.20 Γ— URLScore
          + 0.20 Γ— ReputationScore
Score Range Threat Level
0 – 30 🟒 LOW
31 – 60 🟑 MEDIUM
61 – 85 🟠 HIGH
86 – 100 πŸ”΄ CRITICAL

Quick Start

Prerequisites

  • Docker β‰₯ 24.0 + Docker Compose β‰₯ 2.0
  • OR: Python 3.11+, PostgreSQL 16, Redis 7

1. Clone and Configure

git clone https://github.com/your-org/SentinelX.git
cd SentinelX

# Create environment file
cp backend/.env.example backend/.env

# IMPORTANT: Generate a secure JWT secret key
python -c "import secrets; print(secrets.token_hex(32))"
# Paste the output as SECRET_KEY in backend/.env

2. Edit backend/.env

SECRET_KEY=your-generated-256-bit-key-here
DATABASE_URL=postgresql://sentinelx:sentinelx_pass@postgres:5432/sentinelx_db
REDIS_URL=redis://redis:6379/0

Docker Deployment

Start All Services

docker-compose up --build

This starts:

  • PostgreSQL on port 5432
  • Redis on port 6379
  • FastAPI backend on port 8000
  • Celery worker (email, sms, call queues)

Access the API

Interface URL
Swagger UI http://localhost:8000/docs
ReDoc http://localhost:8000/redoc
Health Check http://localhost:8000/health

Start with Celery Flower monitoring

docker-compose --profile monitoring up --build
# Flower dashboard at http://localhost:5555

Useful Commands

# View backend logs
docker-compose logs -f backend

# View Celery worker logs
docker-compose logs -f celery_worker

# Run database migrations
docker-compose exec backend alembic upgrade head

# Stop all services
docker-compose down

# Stop and remove volumes (clean slate)
docker-compose down -v

Local Development (Without Docker)

# 1. Create virtual environment
cd backend
python -m venv .venv
source .venv/bin/activate

# 2. Install dependencies
pip install -r requirements.txt

# 3. Set environment variables
cp .env.example .env
# Edit .env with local DATABASE_URL and REDIS_URL

# 4. Run database migrations
alembic upgrade head

# 5. Start the API server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

# 6. In a separate terminal, start Celery worker
celery -A app.workers.celery_worker.celery_app worker --loglevel=info

API Documentation

All endpoints are prefixed with /api/v1.

πŸ” Authentication

Method Endpoint Description Auth Required
POST /api/v1/auth/register Create a new user No
POST /api/v1/auth/login Get JWT access token No
GET /api/v1/auth/me Get current user profile Yes

Register

curl -X POST http://localhost:8000/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "password": "SecurePass123",
    "role": "operator"
  }'

Login

curl -X POST http://localhost:8000/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email": "john@example.com", "password": "SecurePass123"}'

Response:

{
  "access_token": "eyJhbGci...",
  "token_type": "bearer",
  "expires_in": 3600
}

πŸ” Threat Analysis

All analysis endpoints require Authorization: Bearer <token>.

Analyze Email

curl -X POST http://localhost:8000/api/v1/analyze/email \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "sender": "security@paypa1-alert.xyz",
    "subject": "URGENT: Your account has been suspended",
    "body": "Click here immediately to verify your account and avoid permanent suspension."
  }'

Response:

{
  "threat_id": "550e8400-e29b-41d4-a716-446655440000",
  "threat_detected": true,
  "risk_score": 87.4,
  "threat_level": "CRITICAL",
  "confidence": 0.92,
  "classification_label": "phishing",
  "reasons": [
    "NLP classified as 'phishing' (score: 91.0)",
    "Urgency manipulation tactics detected (2 indicators)",
    "Authority or brand impersonation detected (1 indicator)",
    "Suspicious TLD (.xyz)"
  ],
  "extracted_urls": [],
  "nlp_score": 91.0,
  "behavior_score": 78.5,
  "url_score": 25.0,
  "reputation_score": 60.0,
  "processing_mode": "sync"
}

Analyze SMS

curl -X POST http://localhost:8000/api/v1/analyze/sms \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "sender": "+91XXXXXXXXXX",
    "message": "Congratulations! You won a free iPhone. Claim now: https://bit.ly/abc123"
  }'

Analyze Call Transcript

curl -X POST http://localhost:8000/api/v1/analyze/call \
  -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "transcript": "Hello this is IRS. You owe taxes. Pay now or you will be arrested.",
    "caller_id": "+18005551234",
    "duration_seconds": 120
  }'

Transcribe Audio File (Whisper)

curl -X POST http://localhost:8000/api/v1/transcribe/audio \
  -H "Authorization: Bearer <token>" \
  -F "file=@/path/to/call_recording.mp3"

Async Processing

Pass "async_processing": true to any analysis endpoint to queue via Celery:

{
  "sender": "...",
  "subject": "...",
  "body": "...",
  "async_processing": true
}

Response includes task_id for polling via Celery result backend.


🚨 Alerts

Method Endpoint Description
GET /api/v1/alerts List alerts (paginated)
POST /api/v1/alerts/{id}/acknowledge Acknowledge an alert
# Get unacknowledged alerts
curl http://localhost:8000/api/v1/alerts?unacknowledged_only=true \
  -H "Authorization: Bearer <token>"

# Acknowledge an alert
curl -X POST http://localhost:8000/api/v1/alerts/550e8400.../acknowledge \
  -H "Authorization: Bearer <token>"

πŸ“Š Dashboard

Method Endpoint Description
GET /api/v1/dashboard/stats KPI statistics
GET /api/v1/dashboard/threats Recent threats list
GET /api/v1/dashboard/trends Daily trends (past N days)
# Get KPI stats
curl http://localhost:8000/api/v1/dashboard/stats \
  -H "Authorization: Bearer <token>"

# Get 14-day trends
curl "http://localhost:8000/api/v1/dashboard/trends?days=14" \
  -H "Authorization: Bearer <token>"

Database Migrations (Alembic)

# Generate a new migration after model changes
alembic revision --autogenerate -m "add_new_field"

# Apply all pending migrations
alembic upgrade head

# Roll back one migration
alembic downgrade -1

# View current migration status
alembic current

ML Models

NLP Classifier (HuggingFace)

The system uses DistilBERT in zero-shot classification mode by default.

Classification labels:

  • safe β€” No threat detected
  • phishing β€” Phishing attempt
  • scam β€” General scam
  • credential_theft β€” Targeting credentials
  • malicious_link β€” Contains malicious URLs
  • impersonation β€” Identity spoofing

Fallback: If HuggingFace is unavailable, the system automatically falls back to regex keyword heuristics.

Whisper Models

Configure model size in .env:

Model VRAM Speed Accuracy
tiny ~1 GB ~32x Good
base ~1 GB ~16x Better
small ~2 GB ~6x Great
medium ~5 GB ~2x Excellent
large ~10 GB 1x Best

Environment Variables Reference

Variable Default Description
SECRET_KEY β€” Required. 256-bit JWT signing key
DATABASE_URL β€” PostgreSQL connection string
REDIS_URL β€” Redis connection string
NLP_MODEL_NAME distilbert-base-uncased HuggingFace model name
WHISPER_MODEL_SIZE base Whisper model variant
ALERT_TRIGGER_THRESHOLD 61 Min risk score to generate alert
VIRUSTOTAL_API_KEY β€” Optional VirusTotal API key
LOG_LEVEL INFO Logging verbosity

Security Considerations

  • JWT Keys: Always generate a unique SECRET_KEY per environment
  • Password Hashing: bcrypt with 12 rounds
  • Rate Limiting: 100 requests/minute per IP (configurable)
  • CORS: Restricted to configured origins
  • Non-root Docker: Container runs as sentinelx user (UID 1001)
  • Input Sanitization: Pydantic v2 strict validation on all inputs
  • Content Truncation: Email/SMS body stored as 2000-char excerpts

Future Roadmap

  • Kafka integration for real-time stream processing
  • WebSocket push notifications for live alerts
  • Multilingual support (Whisper multi-language + multilingual NLP)
  • Deepfake voice detection pipeline
  • MITRE ATT&CK framework mapping
  • SOC integration (Splunk, Elastic SIEM)
  • AI voice agent detection
  • Live telecom integrations (Twilio, Vonage)

License

MIT License β€” See LICENSE for details.


Built with ❀️ for cybersecurity operators worldwide.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors