93 lines
8.7 KiB
Plaintext
93 lines
8.7 KiB
Plaintext
def slug: tostring | gsub("[^A-Za-z0-9._:-]+"; "-") | gsub("^-|-$"; "") | ascii_downcase | if . == "" then "unknown" else . end;
|
|
def timestamp: type == "string" and test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T.*(Z|[+-][0-9]{2}:[0-9]{2})$");
|
|
def epoch:
|
|
capture("^(?<date>[0-9]{4}-[0-9]{2}-[0-9]{2})T(?<time>[0-9]{2}:[0-9]{2}:[0-9]{2})(?:\\.[0-9]+)?(?<zone>Z|[+-][0-9]{2}:[0-9]{2})$") as $p |
|
|
(($p.date+"T"+$p.time+"Z")|fromdateiso8601) as $base |
|
|
if $p.zone=="Z" then $base else ($p.zone|capture("(?<sign>[+-])(?<hour>[0-9]{2}):(?<minute>[0-9]{2})")) as $z |
|
|
$base - ((if $z.sign=="+" then 1 else -1 end)*(($z.hour|tonumber)*3600+($z.minute|tonumber)*60)) end;
|
|
def ptr($parts): "/" + ($parts | map(tostring | gsub("~";"~0") | gsub("/";"~1")) | join("/"));
|
|
def normalize_section:
|
|
if type != "object" then {status:"unavailable",record_count:null,truncated:false,reason:"Legacy section is not an object."}
|
|
else . as $s | ($s.status // null) as $source |
|
|
(if ["ok","skipped","unavailable"] | index($source) then $source else "unavailable" end) as $status |
|
|
{status:$status,
|
|
record_count:(if ($s.count|type)=="number" and $s.count >= 0 and ($s.count|floor)==$s.count then $s.count elif ($s.data|type)=="array" then ($s.data|length) else null end),
|
|
truncated:($s.truncated // false | if . then true else false end)} +
|
|
(if $status == "ok" then {} else {reason:(if ($s.error|type)=="string" and ($s.error|test("\\S")) then $s.error elif ["skipped","unavailable"]|index($source) then "Legacy capture marked section \($status) without a reason." else "Legacy capture used unsupported section status: \($source)." end)} end)
|
|
end;
|
|
def rule_for($rules;$provider;$id): first($rules[] | select(.provider == $provider and (.event_id|tostring)==($id|tostring))) // null;
|
|
def event_sig($provider;$id;$time;$message): [$provider,($id|tostring),($time|tostring|sub("\\.[0-9]+Z$";"Z")),($message//""|tostring|gsub("\\s+";" "))] | @json;
|
|
def add_event($section;$index;$event;$time_field;$provider_field;$id_field):
|
|
($event[$provider_field] // "Unknown provider" | tostring) as $provider |
|
|
($event[$id_field] // "unknown") as $id |
|
|
{id:"event-\($section|slug)-\($index+1)",section:$section,source_pointer:ptr(["sections",$section,"data",$index]),value:$event,
|
|
_provider:$provider,_event_id:$id,_signature:event_sig($provider;$id;$event[$time_field];$event.Message)} +
|
|
(if ($event[$time_field]|timestamp) then {observed_at:$event[$time_field]} else {} end);
|
|
def watch_matches($capture;$matcher):
|
|
($capture.sections[$matcher.section] // null) as $section |
|
|
if ($section|type)!="object" or $section.status != "ok" or ($section.data|type)!="array" then [] else
|
|
[$section.data | to_entries[] | select((.value|type)=="object") |
|
|
. as $entry | ($entry.value[$matcher.field] // "" | tostring | gsub("^\"|\"$";"") | gsub("\\\\";"/")) as $raw |
|
|
(if $matcher.basename then ($raw|split("/")|last) else $raw end) as $identity |
|
|
select([$matcher.values[]|tostring|ascii_downcase] | index(($identity|tostring|ascii_downcase))) |
|
|
select(($matcher.versions // null)==null or (($matcher.versions|index($entry.value[$matcher.version_field // "DriverVersion"]|tostring)) != null)) |
|
|
select(($matcher.enabled_only // false)|not or ([2,"Enabled","enabled"]|index($entry.value.State) != null)) |
|
|
{section:$matcher.section,index:$entry.key,item:$entry.value}]
|
|
end;
|
|
|
|
($rules[0]) as $rules | ($watchlist[0]) as $watchlist | . as $capture |
|
|
($capture.schema_version) as $version |
|
|
if (($version|type)!="number" or ([$version]|inside([1,2])|not)) then error("unsupported legacy capture schema version")
|
|
elif ($capture.sections|type)!="object" then error("legacy capture requires an object at /sections")
|
|
elif ($capture.generated_at|timestamp|not) then error("/generated_at: expected a timezone-qualified ISO timestamp")
|
|
elif ($reference_time|timestamp|not) then error("--reference-time: expected a timezone-qualified ISO timestamp")
|
|
else
|
|
($capture.sections | with_entries(.value |= normalize_section)) as $sections |
|
|
(if ($sections|length)>0 and all($sections[]; .status=="ok" and (.truncated|not)) then "complete" else "partial" end) as $collection_status |
|
|
(if $version==1 then ["recent_system_errors","recent_application_errors"] else ["recent_system_events","recent_application_events"] end) as $event_sections |
|
|
(reduce $event_sections[] as $name ([];
|
|
. + [($capture.sections[$name].data // [] | to_entries[] | select((.value|type)=="object") | add_event($name;.key;.value;"TimeCreated";"ProviderName";"Id"))]
|
|
)) as $primary |
|
|
(reduce (($capture.sections.reliability_records.data // []) | to_entries[]) as $entry
|
|
({items:$primary,signatures:($primary|map(._signature))};
|
|
($entry.value.SourceName // "Unknown provider"|tostring) as $provider |
|
|
($entry.value.EventIdentifier // "unknown") as $id |
|
|
(rule_for($rules;$provider;$id)) as $rule |
|
|
(event_sig($provider;$id;$entry.value.TimeGenerated;$entry.value.Message)) as $sig |
|
|
if $rule != null and (["warning","critical"]|index($rule.severity)) and (.signatures|index($sig)|not) then
|
|
.items += [add_event("reliability_records";$entry.key;$entry.value;"TimeGenerated";"SourceName";"EventIdentifier")] | .signatures += [$sig]
|
|
else . end
|
|
).items) as $events |
|
|
(reduce $events[] as $event ([];
|
|
($event._provider+"\u0000"+($event._event_id|tostring)) as $key |
|
|
if any(.[]; .key==$key) then map(if .key==$key then .evidence += [$event.id] else . end)
|
|
else . + [{key:$key,provider:$event._provider,event_id:$event._event_id,evidence:[$event.id]}] end
|
|
)) as $groups |
|
|
([$groups[] | . as $group | (rule_for($rules;.provider;.event_id)) as $rule |
|
|
if $rule == null then {id:"event-\(.provider|slug)-\(.event_id|slug)",severity:"unanalysed",confidence:0,title:"Unanalysed event: \(.provider) / \(.event_id)",description:"The event is preserved for review; no migrated technical rule matches it.",evidence}
|
|
else {id:"event-\(.provider|slug)-\(.event_id|slug)",severity:$rule.severity,confidence:(if $collection_status=="complete" then $rule.confidence else ([50,$rule.confidence]|min) end),title:"\(.provider) event \(.event_id)",description:$rule.description,evidence} +
|
|
(if ($rule.fading//false) then
|
|
([.evidence[] as $id | $events[] | select(.id==$id and .observed_at) | .observed_at | epoch] | max // null) as $latest |
|
|
if $latest==null then {relevance_weight:1} else (($reference_time|epoch)-$latest)/86400|floor as $age |
|
|
{relevance_weight:(if $age<=7 then 1 elif $age>=90 then 0.5 else (1-0.5*($age-7)/83)*1000000|round/1000000 end),age_days:([$age,0]|max)} end
|
|
else {} end)
|
|
end]) as $event_findings |
|
|
(reduce $watchlist[] as $entry ({observations:$events,findings:$event_findings};
|
|
([$entry.match[] | watch_matches($capture;.)[]]) as $matches |
|
|
([$entry.requires[]? | watch_matches($capture;.)]) as $requirements |
|
|
(if $entry.events then [.observations[] | select((.value.ProviderName//"")==$entry.events.provider and ($entry.events.ids|map(tostring)|index(.value.Id|tostring))) | .id] else [] end) as $event_evidence |
|
|
if ($matches|length)==0 or any($requirements[];length==0) or ($entry.events and (($event_evidence|length)<$entry.events.minimum)) then .
|
|
else ([$matches[], $requirements[][]] | unique_by([.section,.index])) as $all |
|
|
reduce $all[] as $match (.;
|
|
("watch-\($entry.id|slug)-\($match.section|slug)-\($match.index+1)") as $id |
|
|
if any(.observations[];.id==$id) then . else .observations += [{id:$id,section:$match.section,source_pointer:ptr(["sections",$match.section,"data",$match.index]),value:$match.item}] end) |
|
|
.findings += [{id:"watchlist-\($entry.id)",severity:"warning",confidence:(if $collection_status=="complete" then 85 else 50 end),title:$entry.title,description:$entry.description,evidence:(([$all[]|"watch-\($entry.id|slug)-\(.section|slug)-\(.index+1)"]+$event_evidence)|unique)}]
|
|
end
|
|
)) as $analysed |
|
|
{schema_version:1,metadata:{producer:{name:"win-diagnostics",version:$producer_version},collected_at:$capture.generated_at,analysis_reference_time:$reference_time,source:{artifact:$artifact,schema_version:$version,sha256:$artifact_hash}},
|
|
system:({host:($capture.host//{})}+(if ($capture.summary|type)=="object" then {legacy_summary:$capture.summary} else {} end)),
|
|
collection:{mode:($capture.mode//"Full"),status:$collection_status,sections:$sections},
|
|
observations:[$analysed.observations[]|del(._provider,._event_id,._signature)],findings:$analysed.findings,
|
|
warnings:([$capture.warnings[]?|tostring|select(test("\\S"))]+(if $collection_status!="complete" then ["Legacy capture collection is incomplete; finding confidence is capped at 50."] else [] end)+["Migrated analyser supports \($rules|length) event rules and \($watchlist|length) candidate watchlist entries; unmatched events remain unanalysed."])}
|
|
end
|