Companies subject to NIS2 Directive need demonstrable compliance. This middleware provides:
- Forensic logging with HMAC-SHA256 integrity and PII encryption (Art. 21.2.h)
- Rate limiting to prevent DoS/Brute Force attacks (Art. 21.2.e)
- Session Guard to detect hijacking via IP/User-Agent validation (Art. 21.2.a)
- Multi-SIEM Presets: Native connectors for Splunk, Datadog, Elasticsearch
Part of the NIS2 Shield Ecosystem: Use with
@nis2shield/react-guard,@nis2shield/angular-guard, or@nis2shield/vue-guardfor client-side protection andnis2shield/infrastructurefor a full-stack implementation.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Frontend β
β @nis2shield/{react,angular,vue}-guard β
β βββ SessionWatchdog (idle detection) β
β βββ AuditBoundary (crash reports) β
β βββ β POST /api/nis2/telemetry/ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Backend (NIS2 Adapter) β
β **Nis2Shield.AspNetCore** β
β βββ ForensicLogger (HMAC signed logs) β
β βββ RateLimiter, SessionGuard, TorBlocker β
β βββ β SIEM (Elasticsearch, Splunk, Datadog) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Infrastructure β
β nis2shield/infrastructure β
β βββ Centralized Logging (ELK/Splunk) β
β βββ Audited Deployment (Terraform/Helm) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
dotnet add package Nis2Shield.AspNetCorevar builder = WebApplication.CreateBuilder(args);
// 1. Register NIS2 Shield
builder.Services.AddNis2Shield(builder.Configuration);
var app = builder.Build();
// 2. Activate Middleware (before Auth)
app.UseNis2Shield();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();{
"Nis2": {
"Enabled": true,
"IntegrityKey": "your-hmac-secret-key",
"Logging": {
"Enabled": true,
"AnonymizeIp": true,
"EncryptPii": true
},
"ActiveDefense": {
"RateLimitEnabled": true,
"RateLimitThreshold": 100,
"RateLimitWindowSeconds": 60,
"BlockTorExitNodes": true
},
"SessionGuard": {
"Enabled": true,
"SubnetTolerance": 24,
"AllowUserAgentChange": false
}
}
}- Standardized logs (
NIS2-JSON-SCHEMA v1.0) with HMAC-SHA256 integrity signature - Automatic PII field encryption (GDPR compliant)
- IP anonymization for privacy compliance
- Rate Limiting: Sliding window protection against application-level DoS attacks
- Session Guard: Session hijacking prevention via IP/User-Agent fingerprinting
- Tor Blocker: Automatic blocking of Tor exit nodes
- Elasticsearch: Bulk API with daily index rotation
- Splunk: HTTP Event Collector (HEC) support
- Datadog: Logs API v2 integration
- Real-time alerts to Slack, Microsoft Teams, Discord
- Configurable event filtering
builder.Services.AddNis2Shield(options =>
{
options.IntegrityKey = Environment.GetEnvironmentVariable("NIS2_HMAC_KEY")!;
// Rate Limiting
options.ActiveDefense.RateLimitEnabled = true;
options.ActiveDefense.RateLimitThreshold = 50;
options.ActiveDefense.RateLimitWindowSeconds = 60;
// Session Guard - strict mode
options.SessionGuard.Enabled = true;
options.SessionGuard.SubnetTolerance = 32; // exact IP match
options.SessionGuard.AllowUserAgentChange = false;
});builder.Services.AddNis2Shield(options =>
{
options.IntegrityKey = Environment.GetEnvironmentVariable("NIS2_HMAC_KEY")!;
options.Logging.EncryptPii = true;
options.Logging.AnonymizeIp = true;
options.Logging.PiiFields = new List<string> { "email", "patient_id", "ssn" };
});{
"Nis2": {
"IntegrityKey": "your-hmac-key",
"Siem": {
"Enabled": true,
"Provider": "Splunk",
"Endpoint": "https://splunk.company.com:8088/services/collector",
"ApiKey": "your-hec-token",
"IndexName": "nis2-security"
}
}
}{
"Nis2": {
"Webhooks": {
"Enabled": true,
"Targets": [
{
"Name": "Security Alerts",
"Url": "https://hooks.slack.com/services/xxx/yyy/zzz",
"Provider": "Slack",
"Events": ["rate_limit_exceeded", "tor_node_blocked", "session_hijack_detected"]
}
]
}
}
}dotnet testSubscribe to our Security Mailing List to receive immediate alerts about:
- Critical vulnerabilities (CVEs)
- NIS2/DORA regulatory logic updates
- Major breaking changes
For reporting vulnerabilities, see SECURITY.md.
MIT License - see LICENSE for details.
| Project | Technology | Package |
|---|---|---|
| django-nis2-shield | Django | pip install django-nis2-shield |
| nis2-spring-shield | Spring Boot | Maven Central |
| @nis2shield/express-middleware | Express | npm install @nis2shield/express-middleware |
| @nis2shield/react-guard | React | npm install @nis2shield/react-guard |
Documentation Β· NuGet Β· Changelog