| | | 1 | | using ArturRios.Heimdall.Query.Handlers; |
| | | 2 | | using ArturRios.Heimdall.Query.Input; |
| | | 3 | | using ArturRios.Heimdall.Query.Output; |
| | | 4 | | using ArturRios.Heimdall.Shared.Retention; |
| | | 5 | | using ArturRios.Mediator.Query; |
| | | 6 | | using ArturRios.Heimdall.WebApi.Retention; |
| | | 7 | | |
| | | 8 | | namespace ArturRios.Heimdall.WebApi.Monitoring; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Reads the security signals the system already produces and writes the ones worth acting on |
| | | 12 | | /// to the log, at warning level, with a stable marker an alerting rule can match (NFR-26). |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// <para> |
| | | 16 | | /// <b>The log is the destination, deliberately.</b> This API has no opinion about where an |
| | | 17 | | /// operator's alerts go — a mail relay, a pager, a SIEM — and building an integration for |
| | | 18 | | /// one would be guessing. What it can do is make the signal unmistakable and machine-matchable |
| | | 19 | | /// where the operator is already collecting: the logs, which #98 gave a retention period and |
| | | 20 | | /// which ship off the host. |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// <b>Every signal is logged on every run it persists.</b> There is no suppression or |
| | | 24 | | /// deduplication, which would be the obvious refinement and is the wrong one here: an |
| | | 25 | | /// attack that continues is more interesting on its tenth tick than its first, and a |
| | | 26 | | /// detector that goes quiet while the thing it detects is still happening is worse than no |
| | | 27 | | /// detector. |
| | | 28 | | /// </para> |
| | | 29 | | /// </remarks> |
| | | 30 | | public class SecurityMonitoringService( |
| | | 31 | | IServiceScopeFactory scopeFactory, |
| | | 32 | | DataRetentionOptions options, |
| | | 33 | | ILogger<SecurityMonitoringService> logger) |
| | 899 | 34 | | : ScheduledPassService(scopeFactory, options.MonitoringWindow, logger) |
| | | 35 | | { |
| | | 36 | | /// <summary> |
| | | 37 | | /// The marker every signal line carries. Stable, greppable, and the thing an alerting rule |
| | | 38 | | /// should match on rather than the prose, which is written for a human reading afterwards. |
| | | 39 | | /// </summary> |
| | | 40 | | public const string Marker = "SECURITY_SIGNAL"; |
| | | 41 | | |
| | | 42 | | protected override string StartupDescription => |
| | 899 | 43 | | $"Security signal detection scheduled every {options.MonitoringWindow}, alerting at " |
| | 899 | 44 | | + $"{options.RefusalThreshold} refusals by one identity or {options.LockoutThreshold} " |
| | 899 | 45 | | + "accounts locked out at once"; |
| | | 46 | | |
| | | 47 | | /// <remarks> |
| | | 48 | | /// Resolves the query mediator rather than the command one every other pass uses: this pass |
| | | 49 | | /// reads, and making it a command would put an audit entry on every tick saying "looked, saw |
| | | 50 | | /// nothing", burying the writes NFR-09 exists to record. |
| | | 51 | | /// </remarks> |
| | | 52 | | protected override async Task<string?> RunAsync(IServiceProvider scope) |
| | | 53 | | { |
| | 0 | 54 | | var result = await scope.GetRequiredService<QueryMediator>() |
| | 0 | 55 | | .ExecuteQueryAsync<DetectSecuritySignalsQuery, SecuritySignalsOutput>( |
| | 0 | 56 | | new DetectSecuritySignalsQuery()); |
| | | 57 | | |
| | 0 | 58 | | if (!result.Success) |
| | | 59 | | { |
| | 0 | 60 | | return $"Security signal detection failed: {string.Join("; ", result.Errors)}"; |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | var signals = result.Data?.Signals.ToList() ?? []; |
| | | 64 | | |
| | 0 | 65 | | foreach (var signal in signals) |
| | | 66 | | { |
| | 0 | 67 | | logger.LogWarning( |
| | 0 | 68 | | "{Marker} {Kind}: {Description} (count {Count}, actor {ActorId})", |
| | 0 | 69 | | Marker, signal.Kind, signal.Description, signal.Count, |
| | 0 | 70 | | signal.ActorId?.ToString() ?? "n/a"); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // Nothing extra for the caller to log: each signal has already been written above, and a |
| | | 74 | | // run that found nothing is not news. |
| | 0 | 75 | | return null; |
| | 0 | 76 | | } |
| | | 77 | | } |