Ny central normalisering

This commit is contained in:
larssand
2026-06-29 18:55:18 +02:00
parent 0b31e6c55e
commit 20b0e0a92e
13 changed files with 146 additions and 43 deletions

43
src/fgai/normalization.py Normal file
View File

@@ -0,0 +1,43 @@
from __future__ import annotations
FIELD_ALIASES: dict[str, tuple[str, ...]] = {
"srcip": ("srcip", "src_ip", "source_ip", "source.ip", "src", "src_addr", "srcaddr", "sourceaddress", "client_ip", "client.ip", "clientip", "client", "remote_addr", "remote_ip", "ip", "ipaddress", "gl2_remote_ip", "winlog_event_data_ipaddress", "event_data_ipaddress"),
"dstip": ("dstip", "dst_ip", "destination_ip", "destination.ip", "dst", "dst_addr", "dstaddr", "destinationaddress", "dest_ip", "dest", "server_ip", "server.ip", "server", "upstream"),
"srcport": ("srcport", "src_port", "source_port", "source.port", "sport", "client_port", "client.port", "clientport"),
"dstport": ("dstport", "dst_port", "destination_port", "destination.port", "dest_port", "dport", "server_port", "server.port", "serverport"),
"eventtime": ("eventtime", "timestamp", "time", "event_time", "eventtime_ms", "created_at"),
"severity": ("severity", "level", "priority", "sev", "loglevel", "log_level", "event_severity"),
"action": ("action", "act", "fw_action", "rule_action", "policy_action", "event_action", "disposition", "outcome", "result", "status", "event_outcome"),
"type": ("type", "event_type", "log_type", "category", "event_category", "facility"),
"subtype": ("subtype", "sub_type", "event_subtype", "subcategory"),
"service": ("service", "service_name", "dst_service", "app", "appname", "application", "application_name", "proto", "protocol", "transport", "network.transport", "query_type", "qt"),
"dns_query": ("dns_query", "query_domain", "qh", "dns_question", "question", "queried_domain"),
"context": ("query_domain", "qh", "dns_query", "url", "uri", "request", "request_uri", "path", "domain", "hostname", "message", "msg", "full_message", "event_message"),
}
DEFAULT_SEARCH_FIELDS = tuple(dict.fromkeys(field for aliases in FIELD_ALIASES.values() for field in aliases))
def first_field(fields: dict[str, str], *names: str) -> str:
for name in names:
value = str(fields.get(name.lower(), "")).strip()
if value and value not in {"-", "unknown", "n/a", "none", "null"}:
return value
return ""
def canonical_value(fields: dict[str, str], canonical: str) -> str:
return first_field(fields, *FIELD_ALIASES.get(canonical, (canonical,)))
def normalize_fields(fields: dict[str, str], mapping: dict[str, object] | None = None) -> dict[str, str]:
normalized = dict(fields)
mapping = mapping or {}
for canonical, aliases in FIELD_ALIASES.items():
mapped = mapping.get(canonical)
candidates = (str(mapped).lower(),) if isinstance(mapped, str) and mapped else aliases
value = first_field(normalized, *candidates)
if value:
normalized[canonical] = value
return normalized