Skip to content

Commit b4dbb90

Browse files
Merge pull request #101 from mechubsec/feat/unifi-catchall-recovery
feat(vector): keep UniFi recoveries and a minimal catch-all, drop the severity shim
2 parents 9954f8c + 8c8a07d commit b4dbb90

2 files changed

Lines changed: 254 additions & 62 deletions

File tree

‎infra/vector/vector.toml‎

Lines changed: 226 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,10 @@ if cerr != null {
713713
# differ between Network versions and documentation sources (only "Blocked by
714714
# Firewall" is verified live on 11.0.99), so a name match fails safer. Client
715715
# connect/disconnect/roam events (ids 400-404) are dropped explicitly, and any
716-
# unrecognised name is dropped (abort), so new controller events never reach
717-
# ssdf.events unreviewed. Threat events stay on
718-
# unifi_cef_threat -> unifi_ips. See onboarding/unifi/ips-syslog.md.
716+
# unrecognised non-client name becomes a low-priority "unifi_other" row that keeps
717+
# ONLY event id/name/severity/category (see below), so an outage whose real name
718+
# matches no pattern shows up as a name and a count instead of vanishing. Threat
719+
# events stay on unifi_cef_threat -> unifi_ips. See onboarding/unifi/ips-syslog.md.
719720
[transforms.unifi_events]
720721
type = "remap"
721722
inputs = ["unifi_syslog"]
@@ -730,6 +731,8 @@ if derr != null { abort }
730731
name = string(doc.name) ?? ""
731732
lname = downcase(name)
732733
if match(lname, r'^(wifi |wired )?client') { abort }
734+
# IPS detections are unifi_ips' (the unifi_cef_threat filter matches the same names).
735+
if match(lname, r'^threat detected( and blocked)?$') { abort }
733736
734737
action = ""
735738
if match(lname, r'blocked by firewall') {
@@ -738,14 +741,18 @@ if match(lname, r'blocked by firewall') {
738741
action = "wan_failover"
739742
} else if match(lname, r'(internet|wan) (down|offline|disconnected)') {
740743
action = "wan_down"
744+
} else if match(lname, r'(internet|wan) (up|restored|back online|online|recovered|reconnected|connected)') {
745+
action = "wan_up"
746+
} else if match(lname, r'devices? (is )?(back online|online|reconnected|restored)|^back online') {
747+
action = "device_online"
741748
} else if match(lname, r'(multiple|temporary) (internet|wan) disconnection') {
742749
action = "wan_flap"
743750
} else if match(lname, r'devices? (offline|disconnected|unreachable)|lost contact') {
744751
action = "device_offline"
745752
} else if match(lname, r'ip.*conflict|conflict.*ip|duplicate ip') {
746753
action = "ip_conflict"
747754
} else {
748-
abort
755+
action = "unifi_other"
749756
}
750757
751758
event_ts = now()
@@ -768,63 +775,92 @@ ev.observer_hostname = ""
768775
ev.user_name = ""
769776
ev.raw = raw
770777
ext = {}
771-
if action == "flow_deny" {
778+
if action == "unifi_other" {
779+
# Catch-all: id/name/severity/category only. No raw line (its extension can carry
780+
# admin usernames, emails and message bodies), no msg, no client or device fields,
781+
# no addresses. raw is rebuilt from the CEF header; a name that looks like it
782+
# embeds an address or email is replaced.
783+
safe_name = slice!(name, 0, 128)
784+
# the last alternative is IPv6 (also catches a clock time; when unsure, redact)
785+
if match(safe_name, r'@|[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|(?i)([0-9a-f]{2}[:-]){5}[0-9a-f]{2}|[0-9a-f]{0,4}(:[0-9a-f]{0,4}){2,7}') { safe_name = "redacted" }
786+
event_id_text = slice!(string(doc.deviceEventClassId) ?? "", 0, 16)
787+
severity_text = slice!(string(doc.severity) ?? "", 0, 8)
788+
ev.event_kind = "event"
789+
ev.event_outcome = "unknown"
790+
ev.observer_ingress_zone = ""
791+
ev.observer_egress_zone = ""
792+
ev.raw = slice!("CEF:0|Ubiquiti|UniFi Network|" + (string(doc.deviceVersion) ?? "") + "|" + event_id_text + "|" + safe_name + "|" + severity_text, 0, 512)
793+
ext = set!(ext, ["unifi.event.id"], event_id_text)
794+
ext = set!(ext, ["unifi.event.name"], safe_name)
795+
ext = set!(ext, ["unifi.event.severity"], severity_text)
796+
category_text = slice!(string(doc.UNIFIcategory) ?? "", 0, 64)
797+
if category_text != "" { ext = set!(ext, ["unifi.event.category"], category_text) }
798+
ev.ext = ext
799+
. = ev
800+
} else if action == "flow_deny" {
772801
ev.event_kind = "event"
773802
ev.rule_name = string(doc.UNIFIfirewallPolicy) ?? string(doc.UNIFIpolicyName) ?? ""
774803
ev.observer_hostname = string(doc.UNIFIdeviceName) ?? string(doc.UNIFIdstDeviceName) ?? ""
775804
} else {
805+
# Outages and conflicts are alerts (severity from unifi.event.severity, which
806+
# mcp-query maps); recoveries are plain events that mark the end of an outage.
776807
ev.event_kind = "alert"
777-
# recent_alerts hides rows without a severity ext key (default floor "high");
778-
# reuse its syslog scale (3 = high) so outages surface by default.
779-
ext = set!(ext, ["syslog.severity"], "3")
780-
if action != "device_offline" && action != "ip_conflict" { ev.observer_hostname = string(doc.UNIFIdeviceName) ?? "" }
781-
}
782-
proto = downcase(string(doc.proto) ?? "")
783-
if match(proto, r'^[a-z0-9_-]{1,64}$') { ev.network_transport = proto }
784-
sp = to_int(doc.spt) ?? null
785-
if sp != null { ev.source_port = sp }
786-
dp = to_int(doc.dpt) ?? null
787-
if dp != null { ev.destination_port = dp }
788-
789-
# source_ip/destination_ip are Nullable(IPv4): first IPv4 candidate wins, anything
790-
# else (IPv6, MAC, hostname) stays out of the typed column.
791-
for_each(["src", "UNIFIsrcClientIp", "UNIFIdeviceIp", "UNIFIclientIp"]) -> |_i, key| {
792-
candidate = string(get!(doc, [key])) ?? ""
793-
_n, nerr = ip_aton(candidate)
794-
if nerr == null && !exists(ev.source_ip) { ev.source_ip = candidate }
795-
}
796-
for_each(["dst", "UNIFIdstClientIp", "UNIFIdstDeviceIp"]) -> |_i, key| {
797-
candidate = string(get!(doc, [key])) ?? ""
798-
_n, nerr = ip_aton(candidate)
799-
if nerr == null && !exists(ev.destination_ip) { ev.destination_ip = candidate }
808+
if action == "wan_up" || action == "device_online" {
809+
ev.event_kind = "event"
810+
ev.event_outcome = "success"
811+
}
812+
if action != "device_offline" && action != "device_online" && action != "ip_conflict" { ev.observer_hostname = string(doc.UNIFIdeviceName) ?? "" }
800813
}
814+
# Everything below copies addresses and client/device fields; the unifi_other row above
815+
# already assigned its minimal event and must not reach it.
816+
if action != "unifi_other" {
817+
proto = downcase(string(doc.proto) ?? "")
818+
if match(proto, r'^[a-z0-9_-]{1,64}$') { ev.network_transport = proto }
819+
sp = to_int(doc.spt) ?? null
820+
if sp != null { ev.source_port = sp }
821+
dp = to_int(doc.dpt) ?? null
822+
if dp != null { ev.destination_port = dp }
823+
824+
# source_ip/destination_ip are Nullable(IPv4): first IPv4 candidate wins, anything
825+
# else (IPv6, MAC, hostname) stays out of the typed column.
826+
for_each(["src", "UNIFIsrcClientIp", "UNIFIdeviceIp", "UNIFIclientIp"]) -> |_i, key| {
827+
candidate = string(get!(doc, [key])) ?? ""
828+
_n, nerr = ip_aton(candidate)
829+
if nerr == null && !exists(ev.source_ip) { ev.source_ip = candidate }
830+
}
831+
for_each(["dst", "UNIFIdstClientIp", "UNIFIdstDeviceIp"]) -> |_i, key| {
832+
candidate = string(get!(doc, [key])) ?? ""
833+
_n, nerr = ip_aton(candidate)
834+
if nerr == null && !exists(ev.destination_ip) { ev.destination_ip = candidate }
835+
}
801836
802-
ext = set!(ext, ["unifi.event.id"], string(doc.deviceEventClassId) ?? "")
803-
ext = set!(ext, ["unifi.event.name"], name)
804-
ext = set!(ext, ["unifi.event.severity"], string(doc.severity) ?? "")
805-
for_each({
806-
"UNIFIcategory": "unifi.event.category",
807-
"UNIFIdeviceName": "unifi.device_name",
808-
"UNIFIdeviceIp": "unifi.device_ip",
809-
"UNIFIdeviceMac": "unifi.device_mac",
810-
"UNIFIwanName": "unifi.wan_name",
811-
"UNIFIwanId": "unifi.wan_id",
812-
"UNIFIwanIsp": "unifi.wan_isp",
813-
"UNIFIwanSubnet": "unifi.wan_subnet",
814-
"UNIFIsrcClientMac": "unifi.src_mac",
815-
"UNIFIsrcClientAlias": "unifi.src_alias",
816-
"UNIFIdstClientMac": "unifi.dst_mac",
817-
"UNIFIdstClientAlias": "unifi.dst_alias",
818-
"UNIFIdirection": "unifi.direction",
819-
"UNIFIpolicyType": "unifi.policy_type",
820-
"UNIFIdeviceList": "unifi.device_list",
821-
"msg": "unifi.msg",
822-
}) -> |cef_key, ext_key| {
823-
value = string(get!(doc, [cef_key])) ?? ""
824-
if value != "" { ext = set!(ext, [ext_key], slice!(value, 0, 512)) }
837+
ext = set!(ext, ["unifi.event.id"], string(doc.deviceEventClassId) ?? "")
838+
ext = set!(ext, ["unifi.event.name"], name)
839+
ext = set!(ext, ["unifi.event.severity"], string(doc.severity) ?? "")
840+
for_each({
841+
"UNIFIcategory": "unifi.event.category",
842+
"UNIFIdeviceName": "unifi.device_name",
843+
"UNIFIdeviceIp": "unifi.device_ip",
844+
"UNIFIdeviceMac": "unifi.device_mac",
845+
"UNIFIwanName": "unifi.wan_name",
846+
"UNIFIwanId": "unifi.wan_id",
847+
"UNIFIwanIsp": "unifi.wan_isp",
848+
"UNIFIwanSubnet": "unifi.wan_subnet",
849+
"UNIFIsrcClientMac": "unifi.src_mac",
850+
"UNIFIsrcClientAlias": "unifi.src_alias",
851+
"UNIFIdstClientMac": "unifi.dst_mac",
852+
"UNIFIdstClientAlias": "unifi.dst_alias",
853+
"UNIFIdirection": "unifi.direction",
854+
"UNIFIpolicyType": "unifi.policy_type",
855+
"UNIFIdeviceList": "unifi.device_list",
856+
"msg": "unifi.msg",
857+
}) -> |cef_key, ext_key| {
858+
value = string(get!(doc, [cef_key])) ?? ""
859+
if value != "" { ext = set!(ext, [ext_key], slice!(value, 0, 512)) }
860+
}
861+
ev.ext = ext
862+
. = ev
825863
}
826-
ev.ext = ext
827-
. = ev
828864
'''
829865

830866
# Keep only the known Proxmox security lines (pvedaemon/pveproxy auth+task, sshd
@@ -2239,7 +2275,8 @@ assert_eq!(.event_action, "wan_down")
22392275
assert_eq!(.event_outcome, "failure")
22402276
assert_eq!(.source_ip, "192.0.2.1")
22412277
assert_eq!(.observer_hostname, "Example Gateway")
2242-
assert_eq!(.ext."syslog.severity", "3")
2278+
assert_eq!(.ext."unifi.event.severity", "10")
2279+
assert!(!exists(.ext."syslog.severity"))
22432280
assert_eq!(.ext."unifi.event.id", "100")
22442281
assert_eq!(.ext."unifi.event.severity", "10")
22452282
assert_eq!(.ext."unifi.wan_id", "WAN1")
@@ -2382,12 +2419,145 @@ type = "raw"
23822419
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|402|WiFi Client Roamed|1|UNIFIcategory=Client Devices msg=roamed'
23832420

23842421
[[tests]]
2385-
name = "unifi_events_drops_unlisted_event"
2422+
name = "unifi_events_unlisted_event_becomes_unifi_other_without_personal_fields"
2423+
[[tests.inputs]]
2424+
insert_at = "unifi_events"
2425+
type = "raw"
2426+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|546|Config Modified|5|UNIFIcategory=Audit UNIFIsite=Default suser=example-admin UNIFIadminEmail=admin@example.invalid UNIFIdeviceName=Example Gateway UNIFIdeviceIp=192.0.2.1 UNIFIsrcClientMac=02:00:5e:00:00:60 UNIFIsrcClientAlias=example-laptop UNIFIutcTime=2026-11-12T10:07:00.000Z msg=example-admin changed the firewall at 192.0.2.77'
2427+
[[tests.outputs]]
2428+
extract_from = "unifi_events"
2429+
[[tests.outputs.conditions]]
2430+
type = "vrl"
2431+
source = '''
2432+
assert_eq!(.event_kind, "event")
2433+
assert_eq!(.event_action, "unifi_other")
2434+
assert_eq!(.event_provider, "unifi")
2435+
assert_eq!(.event_outcome, "unknown")
2436+
assert_eq!(.timestamp, t'2026-11-12T10:07:00Z')
2437+
assert_eq!(.ext."unifi.event.id", "546")
2438+
assert_eq!(.ext."unifi.event.name", "Config Modified")
2439+
assert_eq!(.ext."unifi.event.severity", "5")
2440+
assert_eq!(.ext."unifi.event.category", "Audit")
2441+
assert_eq!(.user_name, "")
2442+
assert!(!exists(.source_ip))
2443+
assert!(!exists(.destination_ip))
2444+
assert_eq!(.observer_hostname, "")
2445+
assert_eq!(length!(.ext), 4)
2446+
raw = string!(.raw)
2447+
assert_eq!(raw, "CEF:0|Ubiquiti|UniFi Network|11.0.99|546|Config Modified|5")
2448+
assert!(!contains(raw, "example-admin"))
2449+
assert!(!contains(raw, "192.0.2."))
2450+
assert!(!contains(raw, "example.invalid"))
2451+
'''
2452+
2453+
[[tests]]
2454+
name = "unifi_events_unifi_other_redacts_a_name_that_embeds_an_address"
2455+
[[tests.inputs]]
2456+
insert_at = "unifi_events"
2457+
type = "raw"
2458+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|777|Login from 192.0.2.88 by someone@example.invalid|3|UNIFIcategory=System msg=x'
2459+
[[tests.outputs]]
2460+
extract_from = "unifi_events"
2461+
[[tests.outputs.conditions]]
2462+
type = "vrl"
2463+
source = '''
2464+
assert_eq!(.event_action, "unifi_other")
2465+
assert_eq!(.ext."unifi.event.name", "redacted")
2466+
assert_eq!(.ext."unifi.event.id", "777")
2467+
assert!(!contains(string!(.raw), "192.0.2.88"))
2468+
assert!(!contains(string!(.raw), "example.invalid"))
2469+
'''
2470+
2471+
[[tests]]
2472+
name = "unifi_events_unifi_other_redacts_a_name_that_embeds_an_ipv6_address"
2473+
[[tests.inputs]]
2474+
insert_at = "unifi_events"
2475+
type = "raw"
2476+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|778|Login from 2001:db8::88 by someone|3|UNIFIcategory=System msg=x'
2477+
[[tests.outputs]]
2478+
extract_from = "unifi_events"
2479+
[[tests.outputs.conditions]]
2480+
type = "vrl"
2481+
source = '''
2482+
assert_eq!(.event_action, "unifi_other")
2483+
assert_eq!(.ext."unifi.event.name", "redacted")
2484+
assert!(!contains(string!(.raw), "2001:db8"))
2485+
'''
2486+
2487+
[[tests]]
2488+
name = "unifi_events_internet_restored_maps_to_wan_up_event"
2489+
[[tests.inputs]]
2490+
insert_at = "unifi_events"
2491+
type = "raw"
2492+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|102|Internet Restored|1|UNIFIcategory=Internet UNIFIsite=Default UNIFIdeviceName=Example Gateway UNIFIdeviceIp=192.0.2.1 UNIFIwanId=WAN1 UNIFIutcTime=2026-11-12T10:09:00.000Z msg=Internet connection WAN1 is back.'
2493+
[[tests.outputs]]
2494+
extract_from = "unifi_events"
2495+
[[tests.outputs.conditions]]
2496+
type = "vrl"
2497+
source = '''
2498+
assert_eq!(.event_kind, "event")
2499+
assert_eq!(.event_action, "wan_up")
2500+
assert_eq!(.event_outcome, "success")
2501+
assert_eq!(.observer_hostname, "Example Gateway")
2502+
assert_eq!(.ext."unifi.event.name", "Internet Restored")
2503+
assert_eq!(.ext."unifi.wan_id", "WAN1")
2504+
'''
2505+
2506+
[[tests]]
2507+
name = "unifi_events_wan_connected_and_wan_disconnected_stay_distinct"
2508+
[[tests.inputs]]
2509+
insert_at = "unifi_events"
2510+
type = "raw"
2511+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|105|WAN Disconnected|8|UNIFIcategory=Internet UNIFIdeviceName=Example Gateway'
2512+
[[tests.outputs]]
2513+
extract_from = "unifi_events"
2514+
[[tests.outputs.conditions]]
2515+
type = "vrl"
2516+
source = '''
2517+
assert_eq!(.event_action, "wan_down")
2518+
assert_eq!(.event_kind, "alert")
2519+
'''
2520+
2521+
[[tests]]
2522+
name = "unifi_events_device_back_online_maps_to_device_online_event"
2523+
[[tests.inputs]]
2524+
insert_at = "unifi_events"
2525+
type = "raw"
2526+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|513|Device Back Online|1|UNIFIcategory=System UNIFIsite=Default UNIFIdeviceName=Example Switch UNIFIdeviceIp=192.0.2.20 UNIFIutcTime=2026-11-12T10:10:00.000Z'
2527+
[[tests.outputs]]
2528+
extract_from = "unifi_events"
2529+
[[tests.outputs.conditions]]
2530+
type = "vrl"
2531+
source = '''
2532+
assert_eq!(.event_kind, "event")
2533+
assert_eq!(.event_action, "device_online")
2534+
assert_eq!(.event_outcome, "success")
2535+
assert_eq!(.source_ip, "192.0.2.20")
2536+
assert_eq!(.observer_hostname, "")
2537+
'''
2538+
2539+
[[tests]]
2540+
name = "unifi_events_device_offline_stays_an_alert"
2541+
[[tests.inputs]]
2542+
insert_at = "unifi_events"
2543+
type = "raw"
2544+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|512|Device Offline|7|UNIFIcategory=System UNIFIdeviceName=Example Switch UNIFIdeviceIp=192.0.2.20'
2545+
[[tests.outputs]]
2546+
extract_from = "unifi_events"
2547+
[[tests.outputs.conditions]]
2548+
type = "vrl"
2549+
source = '''
2550+
assert_eq!(.event_kind, "alert")
2551+
assert_eq!(.event_action, "device_offline")
2552+
'''
2553+
2554+
[[tests]]
2555+
name = "unifi_events_still_drops_client_events_by_name_even_with_new_catch_all"
23862556
no_outputs_from = ["unifi_events"]
23872557
[[tests.inputs]]
23882558
insert_at = "unifi_events"
23892559
type = "raw"
2390-
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|546|Config Modified|5|UNIFIcategory=Audit msg=changed'
2560+
value = 'Nov 12 10:00:00 ctrl-example CEF:0|Ubiquiti|UniFi Network|11.0.99|999|Client Device Restored|1|UNIFIcategory=Client Devices UNIFIclientMac=02:00:5e:00:00:50'
23912561

23922562
[[tests]]
23932563
name = "unifi_events_drops_gateway_syslog_noise"

0 commit comments

Comments
 (0)