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 Safety: Passing PiWheels License: MIT

The "Security-First" Middleware for NIS2 Compliance.

django-nis2-shield is a plug-and-play library designed to help Django applications meet the technical requirements of the NIS2 Directive (Network and Information Security 2), with a focus on Forensic Logging, Active Defense, and SIEM Integration.

Part of the NIS2 Shield Ecosystem: Use with infrastructure for Demonstrable Compliance (audited via tfsec) and @nis2shield/react-guard for client-side protection.

✨ Key Features

🔒 Forensic Logger

  • Structured logs (JSON or CEF) signed with HMAC-SHA256
  • Automatic PII field encryption (GDPR compliant)
  • Configurable IP anonymization

🛡️ Active Defense

  • Rate Limiting: Protection against application-level DoS attacks (sliding window algorithm)
  • Session Guard: Session hijacking prevention with mobile network tolerance
  • Tor Blocker: Automatic blocking of Tor exit nodes
  • MFA Gatekeeper: 2FA redirect for sensitive paths

📊 Compliance & Reporting

  • check_nis2 command for configuration auditing
  • Incident report generation for CSIRT (24h deadline)
  • SIEM presets for Elasticsearch, Splunk, QRadar, Graylog, Sumo Logic, and Datadog

🔔 Real-time Alerting (v0.3.0+)

  • Webhook notifications for security events
  • Supports Slack, Microsoft Teams, Discord, and generic HTTP

📦 Installation

pip install django-nis2-shield

For development:

pip install django-nis2-shield[dev]

⚙️ Configuration

settings.py

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

MIDDLEWARE = [
    ...,
    # Add after SessionMiddleware and before CommonMiddleware
    'django_nis2_shield.middleware.Nis2GuardMiddleware', 
    ...,
]

# NIS2 Shield Configuration
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 per window
    'RATE_LIMIT_WINDOW': 60,  # seconds
    'RATE_LIMIT_ALGORITHM': 'sliding_window',  # or 'fixed_window'
    '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/',
    
    # Webhooks (v0.3.0+)
    'ENABLE_WEBHOOKS': True,
    'WEBHOOKS': [
        {'url': 'https://hooks.slack.com/...', 'format': 'slack'},
    ]
}

Log Format: CEF (Enterprise SIEM)

For CEF output instead of JSON:

from django_nis2_shield.cef_formatter import get_cef_logging_config

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

🚀 Usage

Configuration Audit

python manage.py check_nis2

Threat Intelligence Update

python manage.py update_threat_list

Incident Report Generation

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

📈 Dashboard Monitoring

The project includes a Docker stack for log visualization:

cd dashboard
docker compose up -d

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

See dashboard/README.md for details.

🧪 Testing

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

📄 License

MIT License - see LICENSE for details.

🤝 Contributing

Contributions are welcome! Open an issue or PR on GitHub.


Documentation · PyPI · Changelog

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.1.tar.gz (26.2 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.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_nis2_shield-0.3.1.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for django_nis2_shield-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5e32f4fa8e093545d963b2044be6d6f3f2ced7b7efe0f5282add29cc225b588e
MD5 cb3c83d21e06dfac9314d25fb19e3216
BLAKE2b-256 ff7152f268ecb2d7620527f0e5d2fb720754a5abc2c7444095f14acd2bf792f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_nis2_shield-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 55c59508c6e778757fc59d6ceca7316eddead8c6f7a342fc5a2adbb2314ef5ae
MD5 daabc131907ef22e07926be032e5ea6f
BLAKE2b-256 a9ade64a2787437edb2fa3c207240d8e061717220fb36eaad4ba4255e4db7a5a

See more details on using hashes here.

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