goliath_normalize/definition.rs
1//! The source definition file format.
2//!
3//! A definition is written by people who know a log source, not Rust, and is
4//! loaded as untrusted input: see `docs/adr/0007-source-and-parser-model.md`
5//! for the model and `sources/sysmon.yaml` for a complete example.
6
7use std::collections::BTreeMap;
8
9use serde::Deserialize;
10
11/// A source definition as written.
12#[non_exhaustive]
13#[derive(Debug, Clone, PartialEq, Deserialize)]
14#[serde(deny_unknown_fields)]
15pub struct SourceDefinition {
16 /// The source's name, such as `sysmon`.
17 pub name: String,
18 /// Increased whenever the output for some input changes, so a stored
19 /// event can be traced to the definition that produced it.
20 pub version: u32,
21 /// How a byte stream splits into records.
22 pub framing: Framing,
23 /// How a record becomes a field map.
24 pub decoding: Decoding,
25 /// Values a source writes for a field it has no value for, such as
26 /// Windows' `-`. A field whose source holds one is not written, as if
27 /// the source had left it out; the value is still kept under
28 /// `unmapped` where the kind keeps the object it is in.
29 #[serde(default)]
30 pub nil: Vec<String>,
31 /// A member that holds a batch of records, such as `Records` in the log
32 /// files AWS delivers. A decoded object whose only member this is,
33 /// holding an array, is taken as the records in the array, each with
34 /// its own raw bytes; any other record is taken as it is. `.` names the
35 /// record itself: a record that is an array, as many APIs answer with,
36 /// is taken as its elements.
37 pub unwrap: Option<String>,
38 /// Fields every kind writes, such as the time and the device.
39 #[serde(default)]
40 pub common: BTreeMap<String, FieldSpec>,
41 /// The ATT&CK data components the records it reads supply, by name,
42 /// such as `Process Creation`, so that coverage can tell a rule that can
43 /// fire from one whose data is not collected. See
44 /// docs/adr/0009-attack-knowledge-model.md.
45 #[serde(default)]
46 pub data_components: Vec<String>,
47 /// The kinds of record the source produces, tried in order.
48 pub kinds: Vec<KindDefinition>,
49}
50
51/// How a byte stream splits into records.
52#[non_exhaustive]
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
54#[serde(rename_all = "kebab-case")]
55pub enum Framing {
56 /// One record per line. Blank lines are skipped.
57 Lines,
58 /// JSON values one after another, separated by any whitespace, as
59 /// `evtx_dump` writes them.
60 JsonValues,
61 /// Linux audit lines, grouped into one record per event by their
62 /// `msg=audit(time:serial)`, whether or not they are adjacent. Needs
63 /// `auditd` decoding.
64 AuditEvents,
65 /// Syslog messages framed as RFC 6587 describes, each either preceded by
66 /// its length in bytes and a space or ending at its line's end, as
67 /// senders over TCP write them. Needs a `syslog` decoding.
68 Syslog,
69}
70
71/// How a record becomes a field map.
72#[non_exhaustive]
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
74#[serde(rename_all = "kebab-case")]
75pub enum Decoding {
76 /// A JSON object.
77 Json,
78 /// Linux audit records, `key=value` fields per line, as one object with
79 /// each record under its type, such as `SYSCALL.exe` or `PATH.0.name`.
80 Auditd,
81 /// A syslog message, RFC 5424 or RFC 3164, as one object with its
82 /// header's fields, such as `hostname` and `app_name`, and the text
83 /// after the header as `message`.
84 Syslog,
85 /// A syslog message whose text after the header is JSON, decoded under
86 /// `message`, for applications that log JSON over syslog.
87 SyslogJson,
88}
89
90/// One kind of record, and how it maps to an OCSF class.
91#[non_exhaustive]
92#[derive(Debug, Clone, PartialEq, Deserialize)]
93#[serde(deny_unknown_fields)]
94pub struct KindDefinition {
95 /// A name for the kind, used in errors and reports.
96 pub name: String,
97 /// Source paths and the values they must hold for a record to be of this
98 /// kind. Values compare exactly: `1` does not equal `"1"`.
99 pub when: BTreeMap<String, Scalar>,
100 /// The OCSF class and activity of the resulting event.
101 pub class: ClassSpec,
102 /// OCSF attributes and where their values come from.
103 #[serde(default)]
104 pub fields: BTreeMap<String, FieldSpec>,
105 /// Source objects whose other members are all kept under `unmapped`,
106 /// by their own names, so that nothing the source wrote is dropped. `.`
107 /// names the record itself, for sources that write every field at its
108 /// top level.
109 #[serde(default)]
110 pub unmapped: Vec<String>,
111}
112
113/// The OCSF class of a kind. The category and type follow from it.
114#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
115#[serde(deny_unknown_fields)]
116pub struct ClassSpec {
117 /// `class_uid`.
118 pub class_uid: u32,
119 /// `activity_id`.
120 pub activity_id: u32,
121}
122
123/// Where an attribute's value comes from.
124#[non_exhaustive]
125#[derive(Debug, Clone, PartialEq, Deserialize)]
126#[serde(untagged)]
127pub enum FieldSpec {
128 /// A source path, copied as it is.
129 Path(String),
130 /// A source path, converted.
131 Converted {
132 /// The source path.
133 from: String,
134 /// The conversion.
135 #[serde(rename = "as")]
136 coercion: Coercion,
137 },
138 /// A source path, translated through a table, as a source's own words
139 /// become the values of an OCSF enumeration.
140 Translated {
141 /// The source path.
142 from: String,
143 /// Source values, as text, and what each becomes. A number in the
144 /// source is looked up by its decimal text.
145 map: BTreeMap<String, Scalar>,
146 /// What a value the table does not list becomes, such as `99`, the
147 /// OCSF `Other`. Without it, such a value is kept under `unmapped`
148 /// and reported as an issue.
149 #[serde(default)]
150 otherwise: Option<Scalar>,
151 },
152 /// A constant.
153 Value {
154 /// The value.
155 value: Scalar,
156 },
157}
158
159/// A conversion of a source value to an OCSF type.
160#[non_exhaustive]
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
162#[serde(rename_all = "kebab-case")]
163pub enum Coercion {
164 /// Text; a number becomes its decimal text.
165 String,
166 /// A whole number, from a number, its decimal text, or its hexadecimal
167 /// text after `0x`, as Windows writes process and logon identifiers.
168 Integer,
169 /// An RFC 3339 time, such as `2025-12-25T14:30:27.369114Z`, as the
170 /// milliseconds since the Unix epoch OCSF stores.
171 Timestamp,
172 /// Seconds since the Unix epoch, with a fraction if any, such as
173 /// auditd's `1727251200.123`, as milliseconds.
174 UnixSeconds,
175 /// An IP address, v4 or v6, kept as written. Text that is not one, such
176 /// as the service name AWS writes where a service made a call, is
177 /// reported and kept under `unmapped`, never written as an address.
178 Ip,
179 /// An RFC 3339 time written without an offset, such as Microsoft 365's
180 /// `2026-09-28T12:00:01`, taken as UTC, as milliseconds. A time with an
181 /// offset is read with it.
182 UtcTimestamp,
183}
184
185/// A constant or a value to compare with, as YAML writes it.
186#[non_exhaustive]
187#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
188#[serde(untagged)]
189pub enum Scalar {
190 /// `true` or `false`.
191 Boolean(bool),
192 /// A whole number.
193 Integer(i64),
194 /// Text.
195 String(String),
196}
197
198impl From<&Scalar> for serde_json::Value {
199 fn from(scalar: &Scalar) -> Self {
200 match scalar {
201 Scalar::Boolean(value) => Self::Bool(*value),
202 Scalar::Integer(value) => Self::from(*value),
203 Scalar::String(value) => Self::String(value.clone()),
204 }
205 }
206}