fse_dump 3.1.6

Dumps the fseventsd entries from a mac
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
//! FSEvents file parsing implementation
//!
//! This module provides the core functionality to parse compressed FSEvents files,
//! handling multiple file format versions and broadcasting records through a bus.

use std::{
    fs::File,
    io::{BufReader, ErrorKind, prelude::*},
    path::Path,
    sync::Arc,
};

use bus::Bus;
use byteorder::{LittleEndian, ReadBytesExt};
use color_eyre::{Result, eyre::eyre};
use flate2::read::MultiGzDecoder;
use jiff::Timestamp;

use crate::{
    record::{Record, RecordFilter},
    version,
};

/// Parses an FSEvents file and broadcasts records through the provided bus
///
/// The file is automatically decompressed using gzip if needed.
/// Supports multiple FSEvents versions (V1, V2, V3) within a single file.
///
/// # Arguments
/// * `in_file` - Path to the FSEvents file to parse
/// * `bus` - Message bus to broadcast parsed records
/// * `filter` - Filter to determine which records to broadcast
///
/// # Returns
/// `Ok(())` on success, or an error if the file cannot be parsed
///
/// # Errors
/// Returns an error if:
/// - The file cannot be opened or read
/// - The file has an unsupported format version
/// - Record lengths don't match expected values
pub fn parse_file(in_file: &Path, bus: &mut Bus<Arc<Record>>, filter: &RecordFilter) -> Result<()> {
    info!("Parsing {}", in_file.display());

    // Get file modification time
    let file_timestamp = in_file
        .metadata()
        .ok()
        .and_then(|m| m.modified().ok())
        .and_then(|st| Timestamp::try_from(st).ok());

    let mut reader = BufReader::new(MultiGzDecoder::new(File::open(in_file)?));

    loop {
        debug!("starting loop");
        let v = match version::Version::from_reader(&mut reader) {
            Err(e) => {
                if e.kind() == ErrorKind::UnexpectedEof {
                    debug!("eof");
                    break;
                }

                return Err(e.into());
            }

            Ok(Some(v)) => v,

            Ok(None) => {
                return Err(eyre!(
                    "Unsupported or invalid file version for: {}",
                    in_file.display()
                ));
            }
        };
        let parse_fun = v.get_parser();

        reader.read_exact(&mut [0u8; 4])?;
        let p_len = reader.read_u32::<LittleEndian>()? as usize;

        debug!("{v:?} :: {p_len}");

        let mut read = 12usize;

        loop {
            let mut rec = match parse_fun(&mut reader)? {
                None => break,
                Some((s, rec)) => {
                    debug!("Read {s} bits");
                    read += s;
                    rec
                }
            };

            // Set the file timestamp on the record
            rec.file_timestamp = file_timestamp;

            // Check length before filtering to avoid reading past page boundary
            if read >= p_len {
                if read == p_len {
                    debug!("Wanted len");
                    // Still broadcast if filter accepts it
                    if filter.want(&rec) {
                        bus.broadcast(Arc::new(rec));
                    } else {
                        debug!("Skipping {rec:?} due to the filters");
                    }
                    break;
                } else {
                    return Err(eyre!("Length of page records didn't match expected length",));
                }
            }

            if !filter.want(&rec) {
                debug!("Skipping {rec:?} due to the filters");
                continue;
            }

            bus.broadcast(Arc::new(rec));
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use bus::Bus;

    use crate::record::RecordFilter;

    use super::parse_file;

    #[test]
    fn test_v3() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();
        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't find test file");
        drop(bus);

        let count = recv.iter().count();
        assert_eq!(count, 2730);
    }

    #[test]
    fn test_v3_with_path_filter() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        // Filter for paths containing "Library"
        let filter = RecordFilter::new(&Some("Library".to_string()), &[], &[]).unwrap();

        parse_file(&path, &mut bus, &filter).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();
        let count = records.len();

        // Should be fewer than total (2730)
        assert!(count > 0, "Should find some Library paths");
        assert!(count < 2730, "Should filter out some records");

        // Verify all records match the filter
        for rec in records {
            assert!(
                rec.path.contains("Library"),
                "Record path should contain 'Library': {}",
                rec.path
            );
        }
    }

    #[test]
    fn test_v3_with_flag_filter() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        // Filter for Modified flag
        let filter = RecordFilter::new(&None, &["Modified".to_string()], &[]).unwrap();

        parse_file(&path, &mut bus, &filter).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();
        let count = records.len();

        assert!(count > 0, "Should find some Modified records");
        assert!(count < 2730, "Should filter out some records");

        // Verify all records have the Modified flag
        for rec in records {
            assert!(
                rec.flags.contains("Modified"),
                "Record should have Modified flag: {}",
                rec.flags
            );
        }
    }

    #[test]
    fn test_v3_with_all_flags_filter() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        // Filter for records that have BOTH FileEvent AND Modified
        let filter = RecordFilter::new(
            &None,
            &[],
            &["FileEvent".to_string(), "Modified".to_string()],
        )
        .unwrap();

        parse_file(&path, &mut bus, &filter).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // Verify all records have both flags
        for rec in records {
            assert!(
                rec.flags.contains("FileEvent"),
                "Record should have FileEvent flag: {}",
                rec.flags
            );
            assert!(
                rec.flags.contains("Modified"),
                "Record should have Modified flag: {}",
                rec.flags
            );
        }
    }

    #[test]
    fn test_v3_with_combined_filters() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        // Combine path and flag filters
        let filter = RecordFilter::new(
            &Some(r"\.(log|txt|plist)$".to_string()),
            &["Created".to_string(), "Modified".to_string()],
            &[],
        )
        .unwrap();

        parse_file(&path, &mut bus, &filter).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // Verify all records match both filters
        for rec in records {
            assert!(
                rec.path.ends_with(".log")
                    || rec.path.ends_with(".txt")
                    || rec.path.ends_with(".plist"),
                "Record path should end with .log, .txt, or .plist: {}",
                rec.path
            );
            assert!(
                rec.flags.contains("Created") || rec.flags.contains("Modified"),
                "Record should have Created or Modified flag: {}",
                rec.flags
            );
        }
    }

    #[test]
    fn test_v3_filter_returns_no_matches() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        // Use a filter that shouldn't match anything
        let filter = RecordFilter::new(
            &Some(r"^/this/path/definitely/does/not/exist/in/the/test/file/xyz123$".to_string()),
            &[],
            &[],
        )
        .unwrap();

        parse_file(&path, &mut bus, &filter).expect("Couldn't parse test file");
        drop(bus);

        let count = recv.iter().count();
        assert_eq!(count, 0, "Filter should exclude all records");
    }

    #[test]
    fn test_v3_collect_specific_data() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // Test that we can access record fields
        assert!(!records.is_empty());

        // Check that we have diverse flag types
        let has_created = records.iter().any(|r| r.flags.contains("Created"));
        let has_modified = records.iter().any(|r| r.flags.contains("Modified"));
        let has_file_event = records.iter().any(|r| r.flags.contains("FileEvent"));

        // Real forensics data should have various event types
        assert!(
            has_created || has_modified || has_file_event,
            "Should have at least one common flag type"
        );

        // Check that event IDs are present
        let first_event_id = records[0].event_id;
        let last_event_id = records[records.len() - 1].event_id;

        // Event IDs should be non-zero in real data
        assert!(
            first_event_id > 0 || last_event_id > 0,
            "Should have non-zero event IDs"
        );
    }

    #[test]
    fn test_v3_multiple_receivers() {
        let mut bus = Bus::new(4096);
        let mut recv1 = bus.add_rx();
        let mut recv2 = bus.add_rx();
        let mut recv3 = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't parse test file");
        drop(bus);

        let count1 = recv1.iter().count();
        let count2 = recv2.iter().count();
        let count3 = recv3.iter().count();

        // All receivers should get the same number of records
        assert_eq!(count1, 2730);
        assert_eq!(count2, 2730);
        assert_eq!(count3, 2730);
    }

    #[test]
    fn test_v3_arc_sharing() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // Test that Arc cloning works as expected
        if let Some(first_rec) = records.first() {
            let cloned = first_rec.clone();
            assert_eq!(first_rec.path, cloned.path);
            assert_eq!(first_rec.event_id, cloned.event_id);
            assert_eq!(first_rec.flag, cloned.flag);
        }
    }

    #[test]
    fn test_nonexistent_file() {
        let mut bus = Bus::new(4096);
        let path: PathBuf = "testfiles/nonexistent/file.gz".into();

        let result = parse_file(&path, &mut bus, &RecordFilter::default());
        assert!(result.is_err(), "Should error on nonexistent file");
    }

    #[test]
    fn test_v3_check_node_ids() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // V3 files should have node_id populated
        let has_node_ids = records.iter().any(|r| r.node_id.is_some());
        assert!(has_node_ids, "V3 files should have node IDs");

        // Count how many have node IDs
        let with_node_ids = records.iter().filter(|r| r.node_id.is_some()).count();
        assert!(
            with_node_ids > 0,
            "Should have at least some records with node IDs"
        );
    }

    #[cfg(feature = "extra_id")]
    #[test]
    fn test_v3_check_extra_ids() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // V3 files should have extra_id populated
        let has_extra_ids = records.iter().any(|r| r.extra_id.is_some());
        assert!(has_extra_ids, "V3 files should have extra IDs");
    }

    #[test]
    fn test_v3_path_variety() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // Collect unique paths
        let unique_paths: std::collections::HashSet<_> =
            records.iter().map(|r| r.path.as_str()).collect();

        // Should have multiple unique paths (real forensics data has variety)
        assert!(unique_paths.len() > 1, "Should have multiple unique paths");
        assert!(
            unique_paths.len() < records.len(),
            "Should have some repeated paths"
        );
    }

    #[test]
    fn test_v3_flag_variety() {
        let mut bus = Bus::new(4096);
        let mut recv = bus.add_rx();

        let path: PathBuf = "testfiles/v3/test_1.gz".into();

        parse_file(&path, &mut bus, &RecordFilter::default()).expect("Couldn't parse test file");
        drop(bus);

        let records: Vec<_> = recv.iter().collect();

        // Collect unique flag combinations
        let unique_flags: std::collections::HashSet<_> = records.iter().map(|r| r.flags).collect();

        // Real forensics data should have multiple flag types
        assert!(
            unique_flags.len() > 1,
            "Should have multiple unique flag combinations"
        );
    }

    #[test]
    fn test_uncompressed_file() {
        let mut bus = Bus::new(4096);
        let path: PathBuf = "testfiles/v3/000000000342c4f2".into();

        // MultiGzDecoder should handle uncompressed files too
        let result = parse_file(&path, &mut bus, &RecordFilter::default());

        // This should also work since MultiGzDecoder handles both compressed and uncompressed
        if result.is_ok() {
            // Great, it worked!
        } else {
            // Some versions might not handle this, which is also acceptable
            // Just check that it fails gracefully
            assert!(result.is_err());
        }
    }
}