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
# stix-rs

[![Rust](https://img.shields.io/badge/rust-1.70%2B-blue.svg)](https://www.rust-lang.org)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

**A complete, production-ready Rust implementation of STIX 2.1 (Structured Threat Information Expression)**

`stix-rs` provides full support for creating, parsing, and manipulating STIX 2.1 cyber threat intelligence data. Built for performance and type safety, it's ready for use in threat intelligence platforms, TAXII servers, and security tools.

---

##  Features

###  **100% STIX 2.1 Compliant**
- **All 18 STIX Domain Objects (SDOs)** - Malware, Indicator, ThreatActor, Campaign, etc.
-**All 17 Cyber Observable Objects (SCOs)** - File, Network Traffic, Process, etc.
-**All Relationship Objects (SROs)** - Relationship, Sighting
-**All Meta Objects** - Bundle, Marking Definition, Language Content, etc.
-**17 Vocabulary Enums** - Complete type-safe enumerations

###  **Production-Ready Features**
- 🔍 **Bundle Query Helpers** - Powerful search and filter APIs
-**ID Validation** - Prevent invalid STIX references
- 📡 **MIME Type Constants** - Standard HTTP/TAXII content types
- 🔄 **Object Versioning** - Proper STIX object update handling
- 🎨 **Pattern Validation** - STIX pattern language syntax checking
- 📦 **Builder Pattern** - Ergonomic object construction

### 🛡️ **Type-Safe & Fast**
- Full Rust type safety with no runtime overhead
- Comprehensive error handling with `thiserror`
- Efficient serialization/deserialization with `serde`
- Zero-copy parsing where possible

---

##  Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
stix-rs = "0.1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = "0.4"
```

---

##  Quick Start

### Creating STIX Objects

```rust
use stix_rs::*;
use chrono::Utc;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a Malware object
    let malware = Malware::builder()
        .name("Poison Ivy")
        .description("Advanced persistent threat RAT")
        .malware_types(vec!["remote-access-trojan".into()])
        .is_family(true)
        .aliases(vec!["PIVY".into()])
        .first_seen(Utc::now())
        .build()?;

    // Create an Indicator
    let indicator = Indicator::builder()
        .name("Malicious domain")
        .pattern("[domain-name:value = 'evil.com']")
        .pattern_type(IndicatorPatternType::Stix)
        .valid_from(Utc::now())
        .validate_pattern(true)  // Enable pattern validation
        .build()?;

    // Create a Bundle
    let bundle = Bundle::new(vec![
        malware.into(),
        indicator.into(),
    ]);

    // Serialize to JSON
    let json = serde_json::to_string_pretty(&bundle)?;
    println!("{}", json);

    Ok(())
}
```

### Working with Bundles

```rust
use stix_rs::*;

// Load a bundle from JSON
let bundle: Bundle = serde_json::from_str(&json_data)?;

// Query by type
let all_malware = bundle.malware();
let all_indicators = bundle.indicators();
let all_threats = bundle.threat_actors();

// Find by ID
if let Some(obj) = bundle.get("malware--abc-123") {
    println!("Found: {}", obj.type_());
}

// Filter generically
let identities = bundle.filter_by_type("identity");

// Count objects
println!("Total malware: {}", bundle.count_by_type("malware"));
println!("Object types: {:?}", bundle.object_types());

// Find references
let refs = bundle.find_references_to("malware--abc-123");

// Iterate
for obj in bundle.iter() {
    println!("{}: {}", obj.type_(), obj.id());
}
```

### Common Properties & Marking

```rust
use stix_rs::*;

let mut malware = Malware::builder()
    .name("BadWare")
    .malware_types(vec!["trojan".into()])
    .build()?;

// Add common properties
malware.common.labels = Some(vec!["apt".into(), "targeted".into()]);
malware.common.confidence = Some(95);
malware.common.lang = Some("en".into());

// Add external references (CVE, ATT&CK)
malware.common.external_references = Some(vec![
    ExternalReference::builder()
        .source_name("mitre-attack")
        .external_id("S0020")
        .url("https://attack.mitre.org/software/S0020/")
        .build()?,
]);

// Add TLP marking
let tlp_red = MarkingDefinition::tlp("red");
malware.common.object_marking_refs = Some(vec![tlp_red.id().to_string()]);
```

### Pattern Validation

```rust
use stix_rs::pattern::{validate_pattern, PatternBuilder};

// Validate patterns
validate_pattern("[file:hashes.MD5 = 'abc123']")?;
validate_pattern("[ipv4-addr:value = '192.168.1.1']")?;

// Build patterns programmatically
let pattern = PatternBuilder::new()
    .compare("file", "name", "=", "'malware.exe'")
    .and()
    .compare("file", "size", ">", "1000")
    .build();

println!("{}", pattern);
// Output: [file:name = 'malware.exe' AND file:size > 1000]
```

### ID Validation

```rust
use stix_rs::*;

// Validate STIX IDs
assert!(is_valid_stix_id("malware--550e8400-e29b-41d4-a716-446655440000"));
assert!(!is_valid_stix_id("invalid-id"));

// Extract type from ID
let obj_type = extract_type_from_id("malware--abc-123");
assert_eq!(obj_type, Some("malware"));

// Validate reference types
assert!(is_valid_ref_for_type(
    "malware--abc-123",
    "malware"
));
```

### Object Versioning

```rust
use stix_rs::*;

let mut threat_actor = ThreatActor::builder()
    .name("APT28")
    .threat_actor_types(vec!["nation-state".into()])
    .build()?;

// Make updates
threat_actor.description = Some("Also known as Fancy Bear".into());

// Create new version (updates modified timestamp, preserves ID)
threat_actor.common.new_version();
```

---

## 🌐 HTTP/TAXII Integration

### MIME Type Constants

```rust
use stix_rs::*;

// Use standard STIX/TAXII MIME types
println!("{}", MEDIA_TYPE_STIX);   // application/stix+json;version=2.1
println!("{}", MEDIA_TYPE_TAXII);  // application/taxii+json;version=2.1
```

### Example TAXII Server (Axum)

```rust
use axum::{Router, routing::get, Json, http::StatusCode};
use stix_rs::*;

async fn get_objects() -> (StatusCode, [(String, String); 1], Json<Bundle>) {
    let bundle = load_threat_intel();

    (
        StatusCode::OK,
        [("Content-Type".to_string(), MEDIA_TYPE_STIX.to_string())],
        Json(bundle)
    )
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/collections/1/objects/", get(get_objects));

    // ... serve
}
```

---

##  Complete Object Support

### Domain Objects (SDOs)

| Object | Builder | Tests | Fields |
|--------|---------|-------|--------|
| Attack Pattern ||| Complete |
| Campaign ||| Complete |
| Course of Action ||| Complete |
| Grouping ||| Complete |
| Identity ||| Complete |
| Incident ||| Complete |
| Indicator ||| Complete + Validation |
| Infrastructure ||| Complete |
| Intrusion Set ||| Complete |
| Location ||| Complete |
| Malware ||| Complete + Extended |
| Malware Analysis ||| Complete |
| Note ||| Complete |
| Observed Data ||| Complete |
| Opinion ||| Complete |
| Report ||| Complete |
| Threat Actor ||| Complete |
| Tool ||| Complete |
| Vulnerability ||| Complete |

### Cyber Observables (SCOs)

| Object | Support |
|--------|---------|
| Artifact ||
| Autonomous System ||
| Directory ||
| Domain Name ||
| Email Address ||
| Email Message ||
| File ||
| IPv4 Address ||
| IPv6 Address ||
| MAC Address ||
| Mutex ||
| Network Traffic ||
| Process ||
| Software ||
| URL ||
| User Account ||
| Windows Registry Key ||
| X.509 Certificate ||

### Relationship Objects (SROs)

| Object | Support |
|--------|---------|
| Relationship ||
| Sighting ||

### Meta Objects

| Object | Support |
|--------|---------|
| Bundle | ✅ + Query Helpers |
| Marking Definition | ✅ + TLP Support |
| Language Content ||
| Extension Definition ||
| External Reference | ✅ + Builder |
| Granular Marking ||

---

##  Vocabulary Enums

All STIX 2.1 open vocabularies are implemented as type-safe enums:

- `MalwareType` - ransomware, trojan, backdoor, etc. (20 types)
- `ThreatActorType` - nation-state, criminal, hacktivist, etc. (12 types)
- `ThreatActorRole` - director, agent, sponsor, etc. (5 roles)
- `ThreatActorSophistication` - minimal, intermediate, advanced, etc. (7 levels)
- `AttackMotivation` - ideology, dominance, personal-gain, etc. (10 types)
- `AttackResourceLevel` - individual, club, organization, government (6 levels)
- `ToolType` - exploitation, remote-access, etc. (8 types)
- `InfrastructureType` - command-and-control, botnet, etc. (11 types)
- `ReportType` - threat-report, attack-pattern, etc. (11 types)
- `IndustrySector` - financial, healthcare, government, etc. (40+ sectors)
- `IndicatorType` - malicious-activity, anomalous-activity, etc.
- `ImplementationLanguage` - Python, C++, JavaScript, etc. (20+ languages)
- `IndicatorPatternType` - stix, pcre, snort, yara, suricata
- `IdentityClass` - individual, group, organization, system
- `HashAlgorithm` - MD5, SHA-1, SHA-256, SHA-512
- `RelationshipType` - targets, uses, indicates, etc.
- `EncryptionAlgorithm` - AES-256-GCM, ChaCha20-Poly1305

---

## 🧪 Testing

```bash
# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test bundle_query

# Check for compilation issues
cargo check

# Build release
cargo build --release
```

**Test Coverage:** 78 tests passing (57 unit + 21 integration/doc tests)

---

##  Documentation

- **[Official STIX 2.1 Spec]https://docs.oasis-open.org/cti/stix/v2.1/os/stix-v2.1-os.html** - OASIS specification
- **[STIX 2.1 Examples]https://oasis-open.github.io/cti-documentation/stix/examples.html** - Official examples

### Generate API Docs

```bash
cargo doc --open
```

---

## Use Cases

### Threat Intelligence Platforms
Query and analyze threat data with type-safe APIs:
```rust
let apt_malware: Vec<_> = bundle.malware()
    .iter()
    .filter(|m| m.name.contains("APT"))
    .collect();
```

### TAXII 2.1 Servers
Serve STIX bundles with proper MIME types:
```rust
response.header("Content-Type", MEDIA_TYPE_STIX);
```

### Security Orchestration (SOAR)
Parse and create STIX indicators programmatically:
```rust
let indicator = Indicator::builder()
    .pattern(pattern)
    .valid_from(Utc::now())
    .build()?;
```

### Threat Feed Aggregators
Merge multiple STIX feeds efficiently:
```rust
let mut combined = Bundle::new(vec![]);
combined.objects.extend(feed1.objects);
combined.objects.extend(feed2.objects);
```

### Intelligence Sharing
Exchange standardized threat intelligence:
```rust
let bundle = Bundle::new(vec![
    malware.into(),
    threat_actor.into(),
    relationship.into(),
]);
```

---

## Advanced Features

### Custom Properties

```rust
let identity = Identity::builder()
    .name("ACME Corp")
    .class(IdentityClass::Organization)
    .property("x_industry", "financial")
    .property("x_priority", 5)
    .build()?;
```

### Extensions

```rust
let extension = ExtensionDefinition::builder()
    .name("my-extension")
    .version("1.0.0")
    .schema("https://example.com/schema.json")
    .extension_types(vec!["property-extension".into()])
    .build()?;
```

### Granular Markings

```rust
malware.common.granular_markings = Some(vec![
    GranularMarking {
        marking_ref: Some("marking-definition--tlp-red".into()),
        selectors: vec!["name".into(), "description".into()],
        lang: None,
    }
]);
```

---

### Development

```bash
# Clone the repository
git clone https://github.com/yourusername/stix-rs
cd stix-rs

# Run tests
cargo test

# Check formatting
cargo fmt --check

# Run clippy
cargo clippy

# Build docs
cargo doc --open
```

---

## 📝 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

## Acknowledgments

- [OASIS CTI Technical Committee]https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=cti - STIX specification