Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🛡️ SENTINEL: Autonomous Security Agent

GitHub Stars Tests Type Coverage License: MIT Python 3.10+ CI Status

Production-ready behavioral threat detection that catches APT campaigns unfolding over weeks—not hours—while controlling LLM costs with 80-90% savings through response caching.

Why Sentinel?

Traditional SIEMs analyze events in isolation. APT campaigns unfold over weeks.

Real-world example: Emotet botnet progression

Week 1-3: Process creation 5→8→12/day (normal variation)
Week 4:   Process creation jumps to 45/day + new exfiltration techniques (🚨 DRIFT DETECTED)

Sentinel's 4-week behavioral window catches this. Traditional SIEMs don't.

But Sentinel doesn't require expensive SaaS fees:

  • Without LLM: Free (Python + scikit-learn only)
  • With LLM: $0.001-0.01 per event (80-90% cheaper than alternatives through caching)
  • Open Source: Deploy locally, no vendor lock-in

Quick Start

Running Tests

PowerShell:

python -m pytest tests/ -v

Command Prompt (cmd):

python -m pytest tests/ -v

Test Summary: 56 tests passing across incidents, pipeline, new features, and more.

Running Demo

PowerShell:

python -m sentinel.demo

Command Prompt (cmd):

python -m sentinel.demo

Analyze a JSON or JSONL log file:

python -m sentinel.demo --input sample_sysmon.json
python -m sentinel.demo --input sample_cloudtrail.jsonl --parser cloudtrail
python -m sentinel.demo --input sample_sysmon.json --report incident.md
python -m sentinel.demo --input sample_sysmon.json --response-plan response.json
python -m sentinel.demo --input sample_sysmon.json --incident-db sentinel_data\incidents.sqlite3

Supported parser names are auto, sysmon, windows, auditd, zeek, and cloudtrail. JSON files can contain a single object, a list of objects, or an object with events, Records, or records.

Production-Grade Code Quality

Sentinel now includes comprehensive type hints, centralized logging, and extensive docstrings:

Type Hints

  • 100% coverage on core modules (llm.py, storage.py, drift.py, network.py)
  • PEP 604 union syntax for Python 3.10+
  • TypedDicts for structured data validation
  • mypy strict mode enabled in CI/CD

Logging Configuration

  • Centralized setup via sentinel/logging_config.py
  • Environment-based configuration (SENTINEL_ENV, LOG_LEVEL)
  • Strategic log points in all core modules
  • Auto-configured on import; no manual setup needed

Example:

import logging
from sentinel.logging_config import setup_logging

setup_logging(level=logging.DEBUG, log_file="sentinel.log")
logger = logging.getLogger("sentinel")

Docstrings

  • Google-style docstrings for all public methods
  • Complete Args/Returns/Raises documentation
  • Example usage in method descriptions

New Features (Production-Hardened)

1. Token Budgeting & Response Caching

Cost Impact:

  • Without caching: 10K events/day × 500 tokens avg × $0.003/1K tokens = $15/day
  • With caching: 10K events × 50 tokens avg (90% cache hit) = $0.15/day

Why it works: Security events are repeating patterns. A Sysmon cmd.exe→powershell execution analyzed once should reuse that result when seen again, not re-query the LLM.

Configuration:

from sentinel.llm import SafetyEnvelopeReasoner
reasoner = SafetyEnvelopeReasoner(
    model="gpt-4o-mini",              # Fixed from gpt-4.1-mini
    token_budget=10000,                # Stop LLM calls if exceeded
    cache_threshold=0.85               # Consider 85%+ similarity as cache hit
)

2. Claude Integration (Anthropic)

Use Claude 3.5 Sonnet as an alternative to OpenAI:

  • Auto-detection via ANTHROPIC_API_KEY environment variable
  • Structured JSON output support
  • Full compatibility with SafetyEnvelopeReasoner
# Automatically uses Claude when ANTHROPIC_API_KEY is set
reasoner = SafetyEnvelopeReasoner.from_environment()

3. JA3 Expansion

Enhanced network threat detection with 7 known-bad TLS fingerprints:

  • Detects Mirai, Emotet, and other malware C2 signatures
  • Configurable scoring weights for custom detection

4. Behavioral Drift Detection: Long-Term Campaign Detection

Traditional Problem: Most anomaly detection analyzes events in isolation. A spike on Thursday and a spike on Friday look unrelated. APT campaigns don't work that way.

Real Example — Emotet Botnet:

Week 1: Process creation = 5/day (clean baseline)
Week 2: Process creation = 7/day (still normal variation)
Week 3: Process creation = 12/day (trending upward...)
Week 4: Process creation = 45/day + T1041 (Exfiltration) + known-bad JA3 = 🚨 CAMPAIGN DETECTED

What Sentinel detects:

  • Frequency drift: 400% spike in incident rate
  • Technique drift: Attacker progression (T1059 Execution → T1041 Exfiltration)
  • Confidence escalation: Low-confidence events become high-confidence (attack ramping up)

Result: Alerts you to campaigns weeks before a traditional SIEM would.

from sentinel.drift import BehavioralDriftAnalyzer

analyzer = BehavioralDriftAnalyzer(anomaly_threshold=0.3)
drifts = analyzer.detect_all_drifting_entities(profiles)

for entity, analysis in drifts.items():
    print(f"{entity}: Drift Score = {analysis['score']:.2f}")
    # Output: host-db-prod: 2.45 (frequency 4.0 + technique 1.5)

5. Advanced Incident Storage Queries

Powerful SQLite query interface:

  • get_incidents_by_entity(entity) - All incidents for an entity
  • get_incidents_by_stage(stage) - Filter by attack stage
  • get_incidents_by_confidence(min_conf, max_conf) - Confidence ranges
  • get_top_entities(limit) - Top entities by incident count
  • get_entity_statistics(entity) - Detailed entity stats
  • find_similar_incidents(tech, stage, limit) - Similarity search
  • compute_entity_reputation() - Multi-factor reputation scoring

6. Multi-Factor Entity Reputation

Reputation scoring combines:

  • Incident frequency (high count = higher risk)
  • Technique diversity (many different techniques = higher risk)
  • Confidence levels (consistent high confidence = higher risk)
  • Stage progression (advanced stages = higher risk)
from sentinel.storage import IncidentStore
store = IncidentStore.connect("incidents.db")
reputation = store.compute_entity_reputation()

Optional LLM Reasoning

Sentinel can route uncertain cases to an optional LLM safety envelope using either OpenAI or Anthropic Claude:

OpenAI (GPT-4.1-mini)

PowerShell:

$env:OPENAI_API_KEY="your-api-key"
python -m sentinel.demo --input sample_sysmon.json --use-llm

Command Prompt (cmd):

set OPENAI_API_KEY=your-api-key
python -m sentinel.demo --input sample_sysmon.json --use-llm

Anthropic Claude (3.5 Sonnet)

PowerShell:

$env:ANTHROPIC_API_KEY="your-api-key"
python -m sentinel.demo --input sample_sysmon.json --use-llm

Command Prompt (cmd):

set ANTHROPIC_API_KEY=your-api-key
python -m sentinel.demo --input sample_sysmon.json --use-llm

LLM Safety Envelope

The deterministic reasoner remains the safety envelope. The LLM only receives sanitized context, must return structured JSON, and its output is rejected if it diverges too far from the baseline or fails validation. Obvious low/high confidence cases stay local for cost control.

Token Budgeting: Configure token limits to control costs:

PowerShell:

$env:SENTINEL_LLM_TOKEN_BUDGET="10000"
python -m sentinel.demo --use-llm

Command Prompt (cmd):

set SENTINEL_LLM_TOKEN_BUDGET=10000
python -m sentinel.demo --use-llm

Persistent Incidents

Use SQLite to persist incidents and review outcomes:

python -m sentinel.demo --response-plan response.json --incident-db sentinel_data\incidents.sqlite3
python -m sentinel.review response.json --approve-all --non-interactive --incident-db sentinel_data\incidents.sqlite3

The store records decision IDs, timestamps, involved entities, confidence, human-review status, serialized audit records, and approval outcomes.

Local Response Planning

The response layer is dry-run by default. It converts recommendations into auditable local response steps such as preserving forensics, alerting an analyst, or preparing host isolation. It prints command previews instead of executing system changes.

python -m sentinel.demo --input tests\fixtures\sample_sysmon.json
python -m sentinel.demo --input tests\fixtures\sample_sysmon.json --response-plan response.json

--execute-response still stays in dry-run unless paired with --allow-execute. Disruptive actions also require human approval.

Human approval workflow:

python -m sentinel.demo --response-plan response.json
python -m sentinel.review response.json
python -m sentinel.review response.json --approve-all --non-interactive --execute

The safest executable response is forensics preservation. When execution is explicitly allowed, Sentinel writes JSON snapshots under sentinel_data/forensics or a directory passed with --forensics-dir.

python -m sentinel.review response.json --approve-all --non-interactive --execute --allow-execute --forensics-dir sentinel_data\forensics

The first disruptive response adapter is Windows Firewall IP blocking. It generates a reversible New-NetFirewallRule step with a matching Remove-NetFirewallRule rollback preview. Actual firewall execution requires all of these:

  • a generated block_ip_windows_firewall step
  • human approval
  • --execute
  • --allow-execute
  • an elevated terminal with permission to change firewall rules

Without --allow-execute, reviewed firewall actions report dry_run.

The second response adapter is a high-risk Windows process stop step. It prepares Stop-Process -Id <pid> -Force only when a process ID is present and the reasoning layer recommends kill_suspicious_process. It is irreversible and human-gated.

The third disruptive response adapter is file quarantine. It prepares a reversible Move-Item into sentinel_data/quarantine when a suspicious file path is present. It is also human-gated.

Live Windows Monitoring

Sentinel can now read recent local Windows Event Logs through PowerShell Get-WinEvent, normalize the events, run the reasoning pipeline, and print a dry-run response plan.

Run one polling cycle:

PowerShell:

python -m sentinel.monitor --once --logs System "Windows PowerShell" --since-minutes 10 --max-events 20

Command Prompt (cmd):

python -m sentinel.monitor --once --logs System "Windows PowerShell" --since-minutes 10 --max-events 20

Run continuously:

PowerShell:

python -m sentinel.monitor --logs System "Windows PowerShell" --interval 30

Command Prompt (cmd):

python -m sentinel.monitor --logs System "Windows PowerShell" --interval 30

Some logs, especially Security, may require an elevated terminal. Missing or restricted logs are reported as read errors and skipped.

Sysmon enrichment is supported for Microsoft-Windows-Sysmon/Operational. Sentinel expands Sysmon event messages and scores common suspicious behaviors:

  • PowerShell encoded commands
  • cmd.exe spawning PowerShell
  • certutil, curl, wget, or bitsadmin downloads
  • LSASS process access
  • autorun registry writes
  • archive staging
  • public outbound connections
  • suspicious DNS patterns

Network metadata scoring is available for Zeek-style records. Sentinel can flag:

  • DNS tunneling-like long or encoded labels
  • large outbound transfer imbalance
  • long-lived sessions
  • known suspicious JA3 TLS fingerprints

The demo uses a small in-memory MITRE-like knowledge graph and deterministic reasoning rules, so it works without Neo4j, Kafka, LangGraph, or an LLM API key. Production integrations can be added behind the existing adapter classes.

Architecture: Multi-Layer Safety Envelope

EVENT INGESTION
├─ Parsers: Sysmon, Windows Event Log, CloudTrail, Zeek, auditd
└─ Normalization: sentinel.events (50+ field schema)
           ↓
PERCEPTION LAYER
├─ Feature Extraction: sentinel.perception
├─ Anomaly Scoring: Baseline + statistical analysis
└─ Network Fingerprinting: sentinel.network (JA3 hashes)
           ↓
REASONING LAYER (Safety Envelope)
├─ Deterministic: Attack-stage classification (primary - 95%)
├─ LLM Optional: Claude/OpenAI for uncertain cases (5%)
├─ Defense: Prompt-injection hardening + output validation
└─ Cost Control: Token budgeting + response caching
           ↓
ANALYTICS LAYER
├─ Drift Detection: sentinel.drift (4-week behavioral windows)
├─ Memory: sentinel.memory (entity graph + context window)
└─ Reputation: Multi-factor threat scoring
           ↓
OUTPUT
├─ Incidents: SQLite persistence (queryable)
├─ Reports: Markdown with reasoning traces
└─ Response: Dry-run action plans (firewall, process, quarantine)

Key Design Principle: LLM is optional safety-net, not core engine. Deterministic rules are primary.

CI/CD Pipelines

Sentinel includes enterprise-grade GitHub Actions workflows for automated testing, security scanning, and deployment:

Main CI Pipeline (.github/workflows/ci.yml)

Runs on every push to main branch:

  • Lint: ruff linting + mypy strict type checking (fail-fast)
  • Test: pytest matrix across Python 3.10, 3.11, 3.12
  • Security Scan: pip-audit, bandit, safety (parallel execution)
  • Type Coverage: mypy report generation

All dependencies mocked to prevent real API calls in CI.

Release Workflow (.github/workflows/release.yml)

Triggered on version tags (e.g., v0.1.0):

  1. Validate: Version matches pyproject.toml, CHANGELOG entry exists
  2. Full Test: Extended test suite with >80% coverage requirement
  3. Build: Python distribution (wheel + sdist)
  4. Publish: PyPI publishing via OIDC trusted credentials (no hardcoded keys)

Security Audit (.github/workflows/security-audit.yml)

Runs weekly and on manual trigger:

  • Dependency Audit: CVE scanning with pip-audit and safety
  • Secret Scanning: TruffleHog git history scanning
  • SAST: CodeQL + Semgrep with Python security rules
  • Findings uploaded to GitHub Security tab

PR Validation (.github/workflows/pr-checks.yml)

Runs on PR creation and updates:

  • Conventional Commits: Validates commit message format
  • PR Quality: Description length and issue linking checks
  • Diff Analysis: Test coverage on changed code
  • Auto-labeling: Type and component labels based on file changes

All Actions Security

SHA-pinned: All GitHub Actions pinned to specific commit SHAs
No Floating Versions: Prevents supply chain attacks
OIDC Trusted Publishing: PyPI credentials via OIDC, not hardcoded keys
Mock APIs: All LLM calls use test credentials in CI

Package Map

  • sentinel.events: unified event schema and parsers for common log formats.
  • sentinel.llm: optional LLM safety-envelope reasoning.
  • sentinel.perception: feature extraction and anomaly scoring interfaces.
  • sentinel.network: Zeek-style network metadata analysis.
  • sentinel.memory: sliding context window, entity graph, and hypothesis store.
  • sentinel.reasoning: attack-stage classification and next-technique prediction.
  • sentinel.defense: prompt-injection input sanitization and output validation.
  • sentinel.audit: decision records, markdown reports, and analyst review queue.
  • sentinel.response: dry-run local response planning.
  • sentinel.review: human approval and guarded response execution CLI.
  • sentinel.storage: SQLite incident and approval persistence.
  • sentinel.sysmon: Sysmon event enrichment and suspicion scoring.
  • sentinel.windows_events: live Windows Event Log ingestion.
  • sentinel.monitor: live monitoring CLI.

Example: Drift Detection Report

Command:

sentinel analyze --input week_of_sysmon.json --drift-window 7 --report incident.md

Output (incident.md):

# SENTINEL Incident Report
Generated: 2024-01-15 10:45 UTC
Events Analyzed: 2,847
Incidents Detected: 1 HIGH confidence

## Incident #1: Behavioral Drift (host-db-prod)

**Entity:** host-db-prod
**Attack Stage:** Execution → Exfiltration  
**Confidence:** 0.89 (HIGH)
**Risk Score:** 0.74

### Evidence
- Process creation: 5→45/day (800% spike) 🚨
- New techniques: T1041 (Exfiltration), T1005 (Data Staging)
- TLS fingerprints: Matched Emotet C2 signature (JA3: e7d705a3...)

### Recommended Actions
1. Preserve forensics: `sentinel_data/forensics/host-db-prod.json`
2. Alert SOC: "Possible Emotet infection on host-db-prod"
3. Escalate to incident response for isolation decision

### Audit Trail
- Decision ID: dec_8f2a9b1c
- Method: Deterministic + Claude-3.5 verification (2 tokens)
- Status: Pending human review

Current Scope

This is a research prototype, not a production security appliance. Response actions are intentionally conservative: forensics can write local snapshots, while firewall, process termination, and file quarantine actions require explicit approval and execution flags.

Development

Installation

Clone and install in development mode:

git clone https://github.com/yourusername/sentinel.git
cd sentinel
pip install -e ".[dev]"

Running Tests

Run full test suite:

pytest tests/ -v

Run with coverage:

pytest tests/ --cov=sentinel --cov-report=html

Code Quality

Type Checking (mypy strict mode):

mypy sentinel/ --strict

Linting (ruff):

ruff check sentinel/
ruff format sentinel/

Security Scanning (bandit):

bandit -r sentinel/

Configuration Files

  • pyproject.toml - Project metadata, dependencies, tool configs
  • conftest.py - pytest fixtures (mocked LLM providers, test data)
  • .ruff.toml - Ruff linter configuration
  • mypy.ini - Type checker strict mode
  • CHANGELOG.md - Release notes (Keep a Changelog format)

Dependencies

Core Dependencies (Required)

  • anthropic>=0.7.0 - Claude API (Optional LLM)
  • openai>=1.0.0 - OpenAI API (Optional LLM)
  • scikit-learn>=1.3.0 - ML algorithms

Optional Dependencies (Advanced Integrations)

  • kafka-python>=2.0.2 - Kafka streaming
  • py2neo>=2021.2.3 - Neo4j graph database
  • networkx>=3.0 - Graph algorithms
  • pyod>=1.0.0 - Outlier detection

Development Dependencies

  • pytest>=7.4.0 - Test framework
  • pytest-cov>=4.1.0 - Coverage plugin
  • mypy>=1.5.0 - Static type checker
  • ruff>=0.1.0 - Linter and formatter
  • pip-audit>=2.6.0 - Dependency vulnerability scanner
  • bandit>=1.7.5 - Security issue scanner
  • safety>=2.3.5 - CVE checker

❓ FAQ

Q: Is this production-ready for enterprise deployment?
A: Sentinel is a research prototype suitable for:

  • Security team training and evaluation
  • Proof-of-concept within larger SOC workflows
  • Integration with SIEM systems as a behavioral analytics layer

NOT recommended as a drop-in replacement for enterprise EDR/SIEM products without hardening (add authentication, RBAC, centralized logging, SOC 2 compliance measures).

Q: Do I need an LLM API key?
A: No. All detection works with deterministic rules only. LLM activates only when OPENAI_API_KEY or ANTHROPIC_API_KEY is set—and is fully optional.

Q: How much does it cost to run?
A:

  • Without LLM: Free (only uses Python standard library + scikit-learn)
  • With LLM + caching: $0.001-0.01 per event (80-90% cheaper than non-cached approaches)

Q: What data sources are supported?
A: Windows (Sysmon, Event Logs, PowerShell), Linux (auditd), Cloud (CloudTrail), Network (Zeek).

Q: How is incident data stored?
A: SQLite by default (portable, file-based, no external DB required). Swap for PostgreSQL/Elasticsearch for production.

About

Sentinel: An autonomous behavioral analytics engine. Beyond simple alerts, it detects multi-week attack patterns and provides auditable, dry-run response plans for automated defense. Open-source, local-first, and AI-augmented.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages