1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//! # What a detection declares
//!
//! The `[detection]` table every detection carries, whichever tier runs it: its
//! identity, the cheap gate that decides whether it runs for a port at all, and
//! the capabilities and intrusiveness [class](Class) it asks the operator to
//! grant. A [flow](super::flow) and a [compute module](super::compute) differ in
//! their body, steps versus code, but declare themselves the same way, so the
//! manifest is one vocabulary both tiers share rather than each restating.
//!
//! ## The class is the request, not the grant
//!
//! Nothing here self-reports a permission. A detection declares a
//! [`Class`] and a [`CapabilitySpec`]; the [envelope](crate::config::envelope) decides
//! what to serve, and the runtime serves exactly that. The class a detection asks
//! for is the set of capabilities an envelope will hand it, so a `passive`
//! detection cannot reach the network however it is authored, the boundary is
//! the serving, not the label.
//!
//! ## Authoring types, kept free of the model
//!
//! These deserialize from TOML and are separate from the
//! [`model`](crate::model) types they map onto, for the reason the fingerprint
//! signature schema is: the model stays serde-free, so a detection's `class` is
//! parsed here and [converted](Class::into_model) into the model's own vocabulary
//! when a detection produces a finding. Keeping this file free of any dependency
//! on the rest of the crate is also what lets `build.rs` share it, and validate
//! the corpus with the very types the runtime deserializes.
// `build.rs` compiles this file too, to validate the detection corpus, and its
// checks read only a subset of these fields, the rest are a detection's declared
// budget the runtime reads. Within the library every field is public API and
// live; the unread-field lint fires only in the build-script crate, so it is
// silenced here rather than field by field.
use Deserialize;
/// `[detection]`, what a detection is and what it asks to be handed, shared
/// by every tier that runs one.
///
/// Deserialized, never built by hand: `non_exhaustive` so a
/// field a later capability adds is not a breaking change for a caller who parses a
/// detection. A caller adds one as TOML through
/// [`Detections::builder`](crate::detect::Detections::builder), not as a literal.
/// `[detection.when]`, the rule that gates the whole detection, nmap's portrule.
/// Every set field ANDs; an empty table means "any port the level offers".
///
/// `non_exhaustive` for the reason [`DetectionManifest`] is;
/// [`Rule::default`] still builds the empty gate.
/// `[detection.capabilities]`, what a detection asks to be handed. The class is
/// the capability set an envelope will serve; nothing here self-reports.
///
/// Named a spec for the same reason the flow schema's other authoring types are:
/// it is a specification a detection writes, distinct from the served
/// [`Capabilities`](super::compute::Capabilities) a compute module holds at run
/// and the [`Grant`](super::compute::Grant) an envelope produces from it.
///
/// `non_exhaustive` for the reason [`DetectionManifest`] is.
/// The intrusiveness a detection declares. Deserializes from the wire names and
/// maps onto the model's [`DetectionClass`](crate::model::finding::DetectionClass)
/// through [`into_model`](Self::into_model) in the runtime `convert` module, so
/// this stays free of the model.
/// What a detection may `speak` to. One value for now; the enum is the room to
/// grow.
/// The byte budget a detection that declares no `max_bytes` runs under, counting
/// what it sends and what comes back across the whole run. Both tiers fall back to
/// it, so it is defined once here beside the field it stands in for.
pub const DEFAULT_MAX_BYTES: u64 = 64 * 1024;
/// The wall-clock budget, in milliseconds, a detection that declares no
/// `max_millis` runs under.
pub const DEFAULT_MAX_MILLIS: u64 = 2_000;
/// The connection budget a detection that declares no `max_connections` runs
/// under, the widest a single bounded loop can be.
pub const DEFAULT_MAX_CONNECTIONS: u32 = 64;
// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║ ██║ █████╗ ███████╗ ██║ ███████╗ ║
// ║ ██║ ██╔══╝ ╚════██║ ██║ ╚════██║ ║
// ║ ██║ ███████╗███████║ ██║ ███████║ ║
// ║ ╚═╝ ╚══════╝╚══════╝ ╚═╝ ╚══════╝ ║
// ╚════════════════════════════════════════════╝