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