stix-rs 0.1.0

STIX 2.1 types and helpers for Rust
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::common::{CommonProperties, StixObject};
use crate::vocab::{IdentityClass, IndicatorPatternType};
fn default_pattern_type() -> IndicatorPatternType { IndicatorPatternType::Stix }
fn default_valid_from() -> DateTime<Utc> { Utc::now() }
use crate::pattern::validate_pattern;

// Re-export BuilderError from sdos to avoid duplication
pub use crate::sdos::BuilderError;

/// A kill chain phase (used by Malware and Indicator)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, )]
#[serde(rename_all = "snake_case")]
pub struct KillChainPhase {
    #[serde(rename = "kill_chain_name")]
    pub name: String,

    #[serde(rename = "phase_name")]
    pub phase_name: String,
}

/// Identity Domain Object
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, )]
#[serde(rename_all = "snake_case")]
pub struct Identity {
    #[serde(flatten)]
    pub common: CommonProperties,

    pub name: String,

    pub identity_class: Option<IdentityClass>,

    pub sectors: Option<Vec<String>>,
}

impl Identity {
    pub fn builder() -> IdentityBuilder {
        IdentityBuilder::default()
    }
}

#[derive(Debug, Default)]
pub struct IdentityBuilder {
    name: Option<String>,
    identity_class: Option<IdentityClass>,
    sectors: Option<Vec<String>>,
    created_by_ref: Option<String>,
    custom_properties: std::collections::HashMap<String, serde_json::Value>,
}

impl IdentityBuilder {
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set identity class (e.g., System, Organization)
    pub fn identity_class(mut self, identity_class: IdentityClass) -> Self {
        self.identity_class = Some(identity_class);
        self
    }

    // Backwards-compatible alias
    pub fn class(self, identity_class: IdentityClass) -> Self {
        self.identity_class(identity_class)
    }

    pub fn sectors(mut self, sectors: Vec<String>) -> Self {
        self.sectors = Some(sectors);
        self
    }

    /// Add a custom/stix extension property (e.g., x_my_tag)
    pub fn property(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        self.custom_properties.insert(key.into(), value.into());
        self
    }

    pub fn created_by_ref(mut self, r: impl Into<String>) -> Self {
        self.created_by_ref = Some(r.into());
        self
    }

    pub fn build(mut self) -> Result<Identity, BuilderError> {
        let name = self.name.ok_or(BuilderError::MissingField("name"))?;
        let identity_class = self.identity_class;

        let mut common = CommonProperties::new("identity", self.created_by_ref);
        // Attach any custom properties provided by the builder
        if !self.custom_properties.is_empty() {
            common.custom_properties.extend(self.custom_properties.drain());
        }

        Ok(Identity {
            common,
            name,
            identity_class,
            sectors: self.sectors,
        })
    }
}

impl StixObject for Identity {
    fn id(&self) -> &str {
        &self.common.id
    }

    fn type_(&self) -> &str {
        &self.common.r#type
    }

    fn created(&self) -> DateTime<Utc> {
        self.common.created
    }
}

// Allow converting domain objects into the StixObjectEnum for easy bundling
impl From<Identity> for crate::StixObjectEnum {
    fn from(i: Identity) -> Self {
        crate::StixObjectEnum::Identity(i)
    }
}
/// Malware Domain Object
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, )]
#[serde(rename_all = "snake_case")]
pub struct Malware {
    #[serde(flatten)]
    pub common: CommonProperties,

    pub name: String,

    pub description: Option<String>,

        #[serde(default)]
    pub malware_types: Vec<String>,

        #[serde(default)]
    pub is_family: bool,

    pub aliases: Option<Vec<String>>,

    pub kill_chain_phases: Option<Vec<KillChainPhase>>,

    pub first_seen: Option<DateTime<Utc>>,

    pub last_seen: Option<DateTime<Utc>>,

    pub operating_system_refs: Option<Vec<String>>,

    pub architecture_execution_envs: Option<Vec<String>>,

    pub implementation_languages: Option<Vec<String>>,

    pub capabilities: Option<Vec<String>>,

    pub sample_refs: Option<Vec<String>>,
}

impl Malware {
    pub fn builder() -> MalwareBuilder {
        MalwareBuilder::default()
    }
}

#[derive(Debug, Default)]
pub struct MalwareBuilder {
    name: Option<String>,
    description: Option<String>,
    is_family: Option<bool>,
    malware_types: Option<Vec<String>>,
    aliases: Option<Vec<String>>,
    kill_chain_phases: Option<Vec<KillChainPhase>>,
    first_seen: Option<DateTime<Utc>>,
    last_seen: Option<DateTime<Utc>>,
    operating_system_refs: Option<Vec<String>>,
    architecture_execution_envs: Option<Vec<String>>,
    implementation_languages: Option<Vec<String>>,
    capabilities: Option<Vec<String>>,
    sample_refs: Option<Vec<String>>,
    created_by_ref: Option<String>,
}

impl MalwareBuilder {
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    pub fn is_family(mut self, is_family: bool) -> Self {
        self.is_family = Some(is_family);
        self
    }

    pub fn malware_types(mut self, types: Vec<String>) -> Self {
        self.malware_types = Some(types);
        self
    }

    pub fn aliases(mut self, aliases: Vec<String>) -> Self {
        self.aliases = Some(aliases);
        self
    }

    pub fn kill_chain_phases(mut self, phases: Vec<KillChainPhase>) -> Self {
        self.kill_chain_phases = Some(phases);
        self
    }

    pub fn first_seen(mut self, t: DateTime<Utc>) -> Self {
        self.first_seen = Some(t);
        self
    }

    pub fn last_seen(mut self, t: DateTime<Utc>) -> Self {
        self.last_seen = Some(t);
        self
    }

    pub fn operating_system_refs(mut self, refs: Vec<String>) -> Self {
        self.operating_system_refs = Some(refs);
        self
    }

    pub fn architecture_execution_envs(mut self, envs: Vec<String>) -> Self {
        self.architecture_execution_envs = Some(envs);
        self
    }

    pub fn implementation_languages(mut self, langs: Vec<String>) -> Self {
        self.implementation_languages = Some(langs);
        self
    }

    pub fn capabilities(mut self, caps: Vec<String>) -> Self {
        self.capabilities = Some(caps);
        self
    }

    pub fn sample_refs(mut self, refs: Vec<String>) -> Self {
        self.sample_refs = Some(refs);
        self
    }

    pub fn created_by_ref(mut self, r: impl Into<String>) -> Self {
        self.created_by_ref = Some(r.into());
        self
    }

    pub fn build(self) -> Result<Malware, BuilderError> {
        let name = self.name.ok_or(BuilderError::MissingField("name"))?;
        let is_family = self.is_family.unwrap_or(false);
        let malware_types = self.malware_types.unwrap_or_default();

        let common = CommonProperties::new("malware", self.created_by_ref);

        Ok(Malware {
            common,
            name,
            description: self.description,
            malware_types,
            is_family,
            aliases: self.aliases,
            kill_chain_phases: self.kill_chain_phases,
            first_seen: self.first_seen,
            last_seen: self.last_seen,
            operating_system_refs: self.operating_system_refs,
            architecture_execution_envs: self.architecture_execution_envs,
            implementation_languages: self.implementation_languages,
            capabilities: self.capabilities,
            sample_refs: self.sample_refs,
        })
    }
}

impl StixObject for Malware {
    fn id(&self) -> &str {
        &self.common.id
    }

    fn type_(&self) -> &str {
        &self.common.r#type
    }

    fn created(&self) -> DateTime<Utc> {
        self.common.created
    }
}

/// Indicator Domain Object
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, )]
#[serde(rename_all = "snake_case")]
pub struct Indicator {
    #[serde(flatten)]
    pub common: CommonProperties,

    pub name: Option<String>,

    pub description: Option<String>,

    pub indicator_types: Option<Vec<String>>,

    pub pattern: String,

    #[serde(default = "default_pattern_type")]
    pub pattern_type: IndicatorPatternType,

    pub pattern_version: Option<String>,

    #[serde(default = "default_valid_from")]
    pub valid_from: DateTime<Utc>,

    pub valid_until: Option<DateTime<Utc>>,

    pub kill_chain_phases: Option<Vec<KillChainPhase>>,
}

impl Indicator {
    pub fn builder() -> IndicatorBuilder {
        IndicatorBuilder::default()
    }

    /// Validate the pattern syntax
    pub fn validate_pattern(&self) -> Result<(), crate::pattern::PatternError> {
        if self.pattern_type == IndicatorPatternType::Stix {
            validate_pattern(&self.pattern)
        } else {
            // Other pattern types (PCRE, Snort, etc.) aren't validated
            Ok(())
        }
    }
}

#[derive(Debug, Default)]
pub struct IndicatorBuilder {
    name: Option<String>,
    description: Option<String>,
    indicator_types: Option<Vec<String>>,
    pattern: Option<String>,
    pattern_type: Option<IndicatorPatternType>,
    pattern_version: Option<String>,
    valid_from: Option<DateTime<Utc>>,
    valid_until: Option<DateTime<Utc>>,
    kill_chain_phases: Option<Vec<KillChainPhase>>,
    created_by_ref: Option<String>,
    validate_pattern: bool,
}

impl IndicatorBuilder {
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    pub fn indicator_types(mut self, types: Vec<String>) -> Self {
        self.indicator_types = Some(types);
        self
    }

    pub fn pattern(mut self, pattern: impl Into<String>) -> Self {
        self.pattern = Some(pattern.into());
        self
    }

    pub fn pattern_type(mut self, pt: IndicatorPatternType) -> Self {
        self.pattern_type = Some(pt);
        self
    }

    pub fn pattern_version(mut self, version: impl Into<String>) -> Self {
        self.pattern_version = Some(version.into());
        self
    }

    pub fn valid_from(mut self, t: DateTime<Utc>) -> Self {
        self.valid_from = Some(t);
        self
    }

    pub fn valid_until(mut self, t: DateTime<Utc>) -> Self {
        self.valid_until = Some(t);
        self
    }

    pub fn kill_chain_phases(mut self, phases: Vec<KillChainPhase>) -> Self {
        self.kill_chain_phases = Some(phases);
        self
    }

    pub fn created_by_ref(mut self, r: impl Into<String>) -> Self {
        self.created_by_ref = Some(r.into());
        self
    }

    /// Enable pattern validation (default: false)
    pub fn validate_pattern(mut self, validate: bool) -> Self {
        self.validate_pattern = validate;
        self
    }

    pub fn build(self) -> Result<Indicator, BuilderError> {
        let pattern = self.pattern.ok_or(BuilderError::MissingField("pattern"))?;
        let pattern_type = self.pattern_type.ok_or(BuilderError::MissingField("pattern_type"))?;
        let valid_from = self.valid_from.ok_or(BuilderError::MissingField("valid_from"))?;

        // Optionally validate STIX patterns
        if self.validate_pattern && pattern_type == IndicatorPatternType::Stix {
            validate_pattern(&pattern)
                .map_err(|_| BuilderError::MissingField("invalid pattern"))?;
        }

        let common = CommonProperties::new("indicator", self.created_by_ref);

        Ok(Indicator {
            common,
            name: self.name,
            description: self.description,
            indicator_types: self.indicator_types,
            pattern,
            pattern_type,
            pattern_version: self.pattern_version,
            valid_from,
            valid_until: self.valid_until,
            kill_chain_phases: self.kill_chain_phases,
        })
    }
}

impl StixObject for Indicator {
    fn id(&self) -> &str {
        &self.common.id
    }

    fn type_(&self) -> &str {
        &self.common.r#type
    }

    fn created(&self) -> DateTime<Utc> {
        self.common.created
    }
}

impl From<Indicator> for crate::StixObjectEnum {
    fn from(i: Indicator) -> Self {
        crate::StixObjectEnum::Indicator(i)
    }
}

impl From<Malware> for crate::StixObjectEnum {
    fn from(m: Malware) -> Self {
        crate::StixObjectEnum::Malware(m)
    }
}

/// Sighting Domain Object
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, )]
#[serde(rename_all = "snake_case")]
pub struct Sighting {
    #[serde(flatten)]
    pub common: CommonProperties,

    pub count: u32,

    pub sighting_of_ref: String,

    pub where_sighted_refs: Vec<String>,
}

impl Sighting {
    pub fn builder() -> SightingBuilder {
        SightingBuilder::default()
    }
}

#[derive(Debug, Default)]
pub struct SightingBuilder {
    count: Option<u32>,
    sighting_of_ref: Option<String>,
    where_sighted_refs: Option<Vec<String>>,
    created_by_ref: Option<String>,
}

impl SightingBuilder {
    pub fn count(mut self, count: u32) -> Self {
        self.count = Some(count);
        self
    }

    pub fn sighting_of_ref(mut self, r: impl Into<String>) -> Self {
        self.sighting_of_ref = Some(r.into());
        self
    }

    pub fn where_sighted_refs(mut self, refs: Vec<String>) -> Self {
        self.where_sighted_refs = Some(refs);
        self
    }

    pub fn created_by_ref(mut self, r: impl Into<String>) -> Self {
        self.created_by_ref = Some(r.into());
        self
    }

    pub fn build(self) -> Result<Sighting, BuilderError> {
        let count = self.count.ok_or(BuilderError::MissingField("count"))?;
        let sighting_of_ref = self.sighting_of_ref.ok_or(BuilderError::MissingField("sighting_of_ref"))?;
        let where_sighted_refs = self.where_sighted_refs.ok_or(BuilderError::MissingField("where_sighted_refs"))?;

        let common = CommonProperties::new("sighting", self.created_by_ref);

        Ok(Sighting {
            common,
            count,
            sighting_of_ref,
            where_sighted_refs,
        })
    }
}

impl StixObject for Sighting {
    fn id(&self) -> &str {
        &self.common.id
    }

    fn type_(&self) -> &str {
        &self.common.r#type
    }

    fn created(&self) -> DateTime<Utc> {
        self.common.created
    }
}

impl From<Sighting> for crate::StixObjectEnum {
    fn from(s: Sighting) -> Self {
        crate::StixObjectEnum::Sighting(s)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vocab::{IdentityClass, IndicatorPatternType};
    use serde_json::Value;

    #[test]
    fn identity_builder_and_serialize() {
        let idty = Identity::builder()
            .name("ACME")
            .class(IdentityClass::Organization)
            .sectors(vec!["technology".into()])
            .build()
            .unwrap();

        let s = serde_json::to_string(&idty).unwrap();
        let v: Value = serde_json::from_str(&s).unwrap();
        assert_eq!(v.get("type").and_then(Value::as_str).unwrap(), "identity");
        assert_eq!(v.get("identity_class").and_then(Value::as_str).unwrap(), "organization");
        let id_field = v.get("id").and_then(Value::as_str).unwrap();
        assert!(id_field.starts_with("identity--"));
    }

    #[test]
    fn malware_builder_and_serialize() {
        let mw = Malware::builder()
            .name("BadWare")
            .is_family(true)
            .malware_types(vec!["ransomware".into()])
            .build()
            .unwrap();

        let s = serde_json::to_string(&mw).unwrap();
        let v: Value = serde_json::from_str(&s).unwrap();
        assert_eq!(v.get("type").and_then(Value::as_str).unwrap(), "malware");
        assert_eq!(v.get("is_family").and_then(Value::as_bool).unwrap(), true);
    }

    #[test]
    fn indicator_builder_and_serialize() {
        let ind = Indicator::builder()
            .name("Test")
            .pattern("[file:hashes.'SHA-256' = '...']")
            .pattern_type(IndicatorPatternType::Stix)
            .valid_from(Utc::now())
            .build()
            .unwrap();

        let s = serde_json::to_string(&ind).unwrap();
        let v: Value = serde_json::from_str(&s).unwrap();
        assert_eq!(v.get("type").and_then(Value::as_str).unwrap(), "indicator");
        assert_eq!(v.get("pattern_type").and_then(Value::as_str).unwrap(), "stix");
    }

    #[test]
    fn sighting_builder_and_serialize() {
        let s = Sighting::builder()
            .count(2)
            .sighting_of_ref("malware--1111")
            .where_sighted_refs(vec!["sensor--1".into()])
            .build()
            .unwrap();

        let j = serde_json::to_string(&s).unwrap();
        let v: Value = serde_json::from_str(&j).unwrap();
        assert_eq!(v.get("type").and_then(Value::as_str).unwrap(), "sighting");
        assert_eq!(v.get("sighting_of_ref").and_then(Value::as_str).unwrap(), "malware--1111");
    }

    #[test]
    fn missing_required_field_errors() {
        // identity_class is now optional — builder should succeed
        let r = Identity::builder().name("No Class").build();
        assert!(r.is_ok());

        // name is required for Malware — builder should still fail when missing
        let r2 = Malware::builder().is_family(false).build();
        assert!(r2.is_err());
    }
}