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
//! Bundles of STIX objects
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::StixObjectEnum;
/// A minimal STIX Bundle type
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Bundle {
pub r#type: String,
pub id: String,
pub objects: Vec<StixObjectEnum>,
}
impl Bundle {
/// Create a new bundle; sets `type` to "bundle" and auto-generates an id
pub fn new(objects: Vec<StixObjectEnum>) -> Self {
Self {
r#type: "bundle".to_string(),
id: format!("bundle--{}", Uuid::new_v4()),
objects,
}
}
/// Find a specific object by its ID
///
/// # Examples
///
/// ```
/// use stix_rs::{Bundle, Identity, IdentityClass, StixObject};
///
/// let identity = Identity::builder()
/// .name("ACME")
/// .class(IdentityClass::Organization)
/// .build()
/// .unwrap();
///
/// let id = identity.id().to_string();
/// let bundle = Bundle::new(vec![identity.into()]);
///
/// assert!(bundle.get(&id).is_some());
/// assert!(bundle.get("nonexistent--id").is_none());
/// ```
pub fn get(&self, id: &str) -> Option<&StixObjectEnum> {
self.objects.iter().find(|obj| obj.id() == id)
}
/// Returns an iterator over all objects in the bundle
pub fn iter(&self) -> impl Iterator<Item = &StixObjectEnum> {
self.objects.iter()
}
/// Returns a mutable iterator over all objects in the bundle
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut StixObjectEnum> {
self.objects.iter_mut()
}
/// Filter objects by type string (e.g., "malware", "indicator")
///
/// # Examples
///
/// ```
/// use stix_rs::{Bundle, Identity, Malware, IdentityClass};
///
/// let identity = Identity::builder()
/// .name("ACME")
/// .class(IdentityClass::Organization)
/// .build()
/// .unwrap();
///
/// let malware = Malware::builder()
/// .name("BadWare")
/// .malware_types(vec!["trojan".into()])
/// .build()
/// .unwrap();
///
/// let bundle = Bundle::new(vec![identity.into(), malware.into()]);
///
/// let malware_objects = bundle.filter_by_type("malware");
/// assert_eq!(malware_objects.len(), 1);
/// ```
pub fn filter_by_type(&self, type_name: &str) -> Vec<&StixObjectEnum> {
self.objects
.iter()
.filter(|obj| obj.type_() == type_name)
.collect()
}
/// Get all Identity objects
pub fn identities(&self) -> Vec<&crate::Identity> {
self.objects
.iter()
.filter_map(|obj| match obj {
StixObjectEnum::Identity(i) => Some(i),
_ => None,
})
.collect()
}
/// Get all Malware objects
pub fn malware(&self) -> Vec<&crate::Malware> {
self.objects
.iter()
.filter_map(|obj| match obj {
StixObjectEnum::Malware(m) => Some(m),
_ => None,
})
.collect()
}
/// Get all Indicator objects
pub fn indicators(&self) -> Vec<&crate::Indicator> {
self.objects
.iter()
.filter_map(|obj| match obj {
StixObjectEnum::Indicator(i) => Some(i),
_ => None,
})
.collect()
}
/// Get all ThreatActor objects
pub fn threat_actors(&self) -> Vec<&crate::sdos::ThreatActor> {
self.objects
.iter()
.filter_map(|obj| match obj {
StixObjectEnum::ThreatActor(t) => Some(t),
_ => None,
})
.collect()
}
/// Get all AttackPattern objects
pub fn attack_patterns(&self) -> Vec<&crate::sdos::AttackPattern> {
self.objects
.iter()
.filter_map(|obj| match obj {
StixObjectEnum::AttackPattern(a) => Some(a),
_ => None,
})
.collect()
}
/// Get all Campaign objects
pub fn campaigns(&self) -> Vec<&crate::sdos::Campaign> {
self.objects
.iter()
.filter_map(|obj| match obj {
StixObjectEnum::Campaign(c) => Some(c),
_ => None,
})
.collect()
}
/// Get all Relationship objects
pub fn relationships(&self) -> Vec<&crate::sros::Relationship> {
self.objects
.iter()
.filter_map(|obj| match obj {
StixObjectEnum::Relationship(r) => Some(r),
_ => None,
})
.collect()
}
/// Count objects by type
///
/// # Examples
///
/// ```
/// use stix_rs::{Bundle, Malware};
///
/// let m1 = Malware::builder().name("M1").malware_types(vec!["trojan".into()]).build().unwrap();
/// let m2 = Malware::builder().name("M2").malware_types(vec!["ransomware".into()]).build().unwrap();
///
/// let bundle = Bundle::new(vec![m1.into(), m2.into()]);
///
/// assert_eq!(bundle.count_by_type("malware"), 2);
/// assert_eq!(bundle.count_by_type("indicator"), 0);
/// ```
pub fn count_by_type(&self, type_name: &str) -> usize {
self.objects
.iter()
.filter(|obj| obj.type_() == type_name)
.count()
}
/// Get all unique object types in this bundle
///
/// # Examples
///
/// ```
/// use stix_rs::{Bundle, Identity, Malware, IdentityClass};
///
/// let identity = Identity::builder()
/// .name("ACME")
/// .class(IdentityClass::Organization)
/// .build()
/// .unwrap();
///
/// let malware = Malware::builder()
/// .name("BadWare")
/// .malware_types(vec!["trojan".into()])
/// .build()
/// .unwrap();
///
/// let bundle = Bundle::new(vec![identity.into(), malware.into()]);
/// let types = bundle.object_types();
///
/// assert!(types.contains(&"identity"));
/// assert!(types.contains(&"malware"));
/// assert_eq!(types.len(), 2);
/// ```
pub fn object_types(&self) -> Vec<&str> {
let mut types: Vec<&str> = self.objects.iter().map(|obj| obj.type_()).collect();
types.sort_unstable();
types.dedup();
types
}
/// Find objects that reference a specific ID
///
/// Useful for finding relationships or objects that reference a specific object
pub fn find_references_to(&self, target_id: &str) -> Vec<&StixObjectEnum> {
self.objects
.iter()
.filter(|obj| match obj {
StixObjectEnum::Relationship(r) => {
r.source_ref == target_id || r.target_ref == target_id
}
StixObjectEnum::Sighting(s) => s.sighting_of_ref == target_id,
// Could extend to check created_by_ref, etc.
_ => false,
})
.collect()
}
/// Returns the number of objects in the bundle
pub fn len(&self) -> usize {
self.objects.len()
}
/// Returns true if the bundle contains no objects
pub fn is_empty(&self) -> bool {
self.objects.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Identity, IdentityClass, Malware, StixObjectEnum};
use crate::common::StixObject;
#[test]
fn bundle_serializes_objects() {
let idty = Identity::builder()
.name("ACME")
.class(IdentityClass::Organization)
.build()
.unwrap();
let obj = StixObjectEnum::Identity(idty);
let bundle = Bundle::new(vec![obj]);
let s = serde_json::to_string(&bundle).unwrap();
assert!(s.contains("\"type\":\"bundle\""));
assert!(s.contains("\"objects\""));
assert!(s.contains("\"type\":\"identity\""));
}
#[test]
fn bundle_get_by_id() {
let identity = Identity::builder()
.name("Test Org")
.class(IdentityClass::Organization)
.build()
.unwrap();
let id = identity.id().to_string();
let bundle = Bundle::new(vec![identity.into()]);
assert!(bundle.get(&id).is_some());
assert!(bundle.get("nonexistent--id").is_none());
}
#[test]
fn bundle_filter_by_type() {
let identity = Identity::builder()
.name("Org")
.class(IdentityClass::Organization)
.build()
.unwrap();
let malware = Malware::builder()
.name("BadWare")
.malware_types(vec!["trojan".into()])
.build()
.unwrap();
let bundle = Bundle::new(vec![identity.into(), malware.into()]);
let malware_objects = bundle.filter_by_type("malware");
assert_eq!(malware_objects.len(), 1);
let identity_objects = bundle.filter_by_type("identity");
assert_eq!(identity_objects.len(), 1);
}
#[test]
fn bundle_typed_getters() {
let identity = Identity::builder()
.name("Org")
.class(IdentityClass::Organization)
.build()
.unwrap();
let m1 = Malware::builder()
.name("M1")
.malware_types(vec!["trojan".into()])
.build()
.unwrap();
let m2 = Malware::builder()
.name("M2")
.malware_types(vec!["ransomware".into()])
.build()
.unwrap();
let bundle = Bundle::new(vec![identity.into(), m1.into(), m2.into()]);
assert_eq!(bundle.identities().len(), 1);
assert_eq!(bundle.malware().len(), 2);
assert_eq!(bundle.indicators().len(), 0);
}
#[test]
fn bundle_count_and_types() {
let m1 = Malware::builder()
.name("M1")
.malware_types(vec!["trojan".into()])
.build()
.unwrap();
let m2 = Malware::builder()
.name("M2")
.malware_types(vec!["ransomware".into()])
.build()
.unwrap();
let bundle = Bundle::new(vec![m1.into(), m2.into()]);
assert_eq!(bundle.count_by_type("malware"), 2);
assert_eq!(bundle.len(), 2);
assert!(!bundle.is_empty());
let types = bundle.object_types();
assert_eq!(types, vec!["malware"]);
}
}