Skip to main content

A Security-First Middleware for Django to ensure NIS2 compliance with forensic logging, active defense, and SIEM integration.

Project description

Django NIS2 Shield

PyPI version Python Django License: MIT

Il Middleware "Security-First" per la conformità NIS2.

django-nis2-shield è una libreria plug-and-play progettata per aiutare le applicazioni Django a soddisfare i requisiti tecnici della Direttiva NIS2 (Network and Information Security 2), con un focus su Logging Forense, Active Defense e SIEM Integration.

✨ Caratteristiche Principali

🔒 Forensic Logger

  • Log strutturati (JSON o CEF) firmati con HMAC-SHA256
  • Cifratura automatica dei campi PII (GDPR compliant)
  • IP Anonymization configurabile

🛡️ Active Defense

  • Rate Limiting: Protezione contro attacchi DoS applicativi
  • Session Guard: Prevenzione Session Hijacking con tolleranza mobile
  • Tor Blocker: Blocco automatico dei nodi di uscita Tor
  • MFA Gatekeeper: Reindirizzamento 2FA per path sensibili

📊 Compliance & Reporting

  • Comando check_nis2 per audit della configurazione
  • Generazione report incidenti per CSIRT (deadline 24h)
  • Preset SIEM per Elasticsearch, Splunk, e altri

📦 Installazione

pip install django-nis2-shield

Per lo sviluppo:

pip install django-nis2-shield[dev]

⚙️ Configurazione

settings.py

INSTALLED_APPS = [
    ...,
    'django_nis2_shield',
]

MIDDLEWARE = [
    ...,
    # Inserire dopo SessionMiddleware e prima di CommonMiddleware
    'django_nis2_shield.middleware.Nis2GuardMiddleware', 
    ...,
]

# Configurazione NIS2
NIS2_SHIELD = {
    # Security Keys
    'INTEGRITY_KEY': 'change-me-to-a-secure-secret',
    'ENCRYPTION_KEY': b'your-32-byte-fernet-key-here=',  # Fernet.generate_key()
    
    # Privacy (GDPR)
    'ANONYMIZE_IPS': True,
    'ENCRYPT_PII': True,
    'PII_FIELDS': ['user_id', 'email', 'ip', 'user_agent'],
    
    # Active Defense
    'ENABLE_RATE_LIMIT': True,
    'RATE_LIMIT_THRESHOLD': 100,  # requests/minute
    'ENABLE_SESSION_GUARD': True,
    'SESSION_IP_TOLERANCE': 'subnet',  # 'exact', 'subnet', 'none'
    'BLOCK_TOR_EXIT_NODES': True,
    
    # MFA
    'ENFORCE_MFA_ROUTES': ['/admin/', '/finance/'],
    'MFA_SESSION_FLAG': 'is_verified_mfa',
    'MFA_REDIRECT_URL': '/accounts/login/mfa/',
}

Formato Log: CEF (Enterprise SIEM)

Per output in formato CEF invece di JSON:

from django_nis2_shield.cef_formatter import get_cef_logging_config

LOGGING = get_cef_logging_config('/var/log/django_nis2.cef')

🚀 Utilizzo

Audit della Configurazione

python manage.py check_nis2

Aggiornamento Threat Intelligence

python manage.py update_threat_list

Generazione Report Incidenti

python manage.py generate_incident_report --hours=24 --output=incident.json

📈 Dashboard Monitoring

Il progetto include uno stack Docker per visualizzare i log:

cd dashboard
docker compose up -d

# Accesso:
# - Kibana: http://localhost:5601
# - Grafana: http://localhost:3000 (admin/admin)

Vedi dashboard/README.md per dettagli.

🧪 Testing

# Con gli script esistenti
PYTHONPATH=. python tests/test_basic.py

# Con pytest
pip install pytest pytest-django
PYTHONPATH=. pytest tests/ -v

📄 Licenza

MIT License - vedi LICENSE per dettagli.

🤝 Contributing

Le contribuzioni sono benvenute! Apri una issue o una PR su GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_nis2_shield-0.3.0.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_nis2_shield-0.3.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file django_nis2_shield-0.3.0.tar.gz.

File metadata

  • Download URL: django_nis2_shield-0.3.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_nis2_shield-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8b4fe9f5c9cd2d66be548c6eae27798570a0e6cb51521eab65b671047d8c7c72
MD5 1c0cfcb0dbc8a82af4c353fbd31da9e2
BLAKE2b-256 256fd5deba9f1e1a115723c48232a9aca6aee97b9a06a7343e99e2cc6d4c52a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nis2_shield-0.3.0.tar.gz:

Publisher: publish.yml on nis2shield/django-nis2-shield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_nis2_shield-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_nis2_shield-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 016169a0c9a53a0dd3a254bcbba87f34915dc6ae41b7cd7cb40545b835bda35f
MD5 3d11fd9bdbba43278d575c7d130c645c
BLAKE2b-256 4e81219eeee56ab79511cc2c6553534f629379ccfd8fe61e950e490c38ed9d82

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nis2_shield-0.3.0-py3-none-any.whl:

Publisher: publish.yml on nis2shield/django-nis2-shield

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page