browser-forensic-core 0.3.0

Core types for browser forensic analysis
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
#![cfg_attr(
    test,
    allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::no_effect_underscore_binding
    )
)]
//! Core types for browser forensic analysis.

pub mod analyze;
pub mod finding;
pub mod reconstruct;
pub mod sqlite;
pub mod test_utils;
pub mod timestamp;

use std::collections::HashMap;
use std::path::Path;

use serde::{Deserialize, Serialize};

pub use forensicnomicon::evidence::EvidenceStrength;

pub use finding::{
    Confidence, EvidenceSource, EvidenceState, Finding, Priority, Provenance, TimestampBasis,
    UserActionClaim,
};

/// Browser engine family.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum BrowserFamily {
    Chromium,
    Firefox,
    Safari,
    /// Internet Explorer (Trident) — history/cookies/cache live in the ESE
    /// `WebCacheV01.dat`, not SQLite.
    InternetExplorer,
    /// Legacy (EdgeHTML/Spartan) Microsoft Edge — shares the ESE
    /// `WebCacheV01.dat` store with Internet Explorer.
    EdgeLegacy,
}

impl std::fmt::Display for BrowserFamily {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Chromium => write!(f, "Chromium"),
            Self::Firefox => write!(f, "Firefox"),
            Self::Safari => write!(f, "Safari"),
            Self::InternetExplorer => write!(f, "Internet Explorer"),
            Self::EdgeLegacy => write!(f, "Edge (Legacy)"),
        }
    }
}

/// Kind of browser artifact.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum ArtifactKind {
    History,
    Cookies,
    Downloads,
    Extensions,
    LoginData,
    Cache,
    Bookmarks,
    Autofill,
    Session,
    Integrity,
    Carved,
    Memory,
    Preferences,
    LocalStorage,
    /// Per-site permission grant (geolocation, camera, mic, notifications, …).
    Permission,
    /// Stored payment-card metadata. The card number is never decrypted.
    CreditCard,
    /// OAuth / sync auth-token metadata. The token itself is never decrypted.
    AuthToken,
    /// A domain/origin the browser contacted, recovered from a network/state
    /// artifact that survives a history wipe (HTTP server properties, NEL/
    /// Reporting, DIPS/BTM bounce records, HSTS). Read-only, no secrets.
    RecoveredDomain,
    /// A page the browser stored a favicon for (Chromium `Favicons`). The
    /// `page_url` is an independent, cleartext source of visited URLs.
    Favicon,
    /// A most-visited page cached for the new-tab page (Chromium `Top Sites`).
    /// Frecency-ranked; no per-visit timestamp.
    TopSite,
    /// A string the user typed into the omnibox and the URL they selected
    /// (Chromium `Shortcuts`). Direct evidence of user intent.
    Shortcut,
    /// A (often partial) omnibox string the user typed and the URL Chromium
    /// learned to predict (Chromium `Network Action Predictor`).
    NetworkPrediction,
    /// Audio/video playback recorded by Chromium `Media History` (watch time,
    /// resume position, media title).
    MediaPlayback,
    /// A string the user typed into the address bar and the page it resolved to
    /// (Firefox `moz_inputhistory`). Direct evidence of typed intent; carries a
    /// decayed `use_count`, not a per-keystroke timestamp.
    TypedInput,
    /// A page annotation recorded by Firefox (`moz_annos` +
    /// `moz_anno_attributes`): a named key/value the browser attached to a page
    /// (reading-list state, visit-count metadata, …). Stated as recorded.
    Annotation,
    /// A bookmark found in a Firefox `bookmarkbackups/*.jsonlz4` backup but
    /// absent from the current `moz_bookmarks` — consistent with deletion after
    /// that backup was written. The backup date bounds *when*, not who or why.
    RecoveredBookmark,
}

impl std::fmt::Display for ArtifactKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::History => write!(f, "History"),
            Self::Cookies => write!(f, "Cookies"),
            Self::Downloads => write!(f, "Downloads"),
            Self::Extensions => write!(f, "Extensions"),
            Self::LoginData => write!(f, "LoginData"),
            Self::Cache => write!(f, "Cache"),
            Self::Bookmarks => write!(f, "Bookmarks"),
            Self::Autofill => write!(f, "Autofill"),
            Self::Session => write!(f, "Session"),
            Self::Integrity => write!(f, "Integrity"),
            Self::Carved => write!(f, "Carved"),
            Self::Memory => write!(f, "Memory"),
            Self::Preferences => write!(f, "Preferences"),
            Self::LocalStorage => write!(f, "LocalStorage"),
            Self::Permission => write!(f, "Permission"),
            Self::CreditCard => write!(f, "CreditCard"),
            Self::AuthToken => write!(f, "AuthToken"),
            Self::RecoveredDomain => write!(f, "RecoveredDomain"),
            Self::Favicon => write!(f, "Favicon"),
            Self::TopSite => write!(f, "TopSite"),
            Self::Shortcut => write!(f, "Shortcut"),
            Self::NetworkPrediction => write!(f, "NetworkPrediction"),
            Self::MediaPlayback => write!(f, "MediaPlayback"),
            Self::TypedInput => write!(f, "TypedInput"),
            Self::Annotation => write!(f, "Annotation"),
            Self::RecoveredBookmark => write!(f, "RecoveredBookmark"),
        }
    }
}

/// A single browser forensic event.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct BrowserEvent {
    pub timestamp_ns: i64,
    pub browser: BrowserFamily,
    pub artifact: ArtifactKind,
    pub source: String,
    pub description: String,
    pub attrs: HashMap<String, serde_json::Value>,
}

impl BrowserEvent {
    #[must_use]
    pub fn new(
        timestamp_ns: i64,
        browser: BrowserFamily,
        artifact: ArtifactKind,
        source: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self {
            timestamp_ns,
            browser,
            artifact,
            source: source.into(),
            description: description.into(),
            attrs: HashMap::new(),
        }
    }

    #[must_use]
    pub fn with_attr(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.attrs.insert(key.into(), value);
        self
    }
}

/// Generate the JSON Schema (draft 2020-12) for [`BrowserEvent`] and its
/// sub-types.
///
/// The schema is *derived* from the Rust types via schemars, so it never drifts
/// from the serialized shape. `br4n6 schema` emits it, and a sync test keeps the
/// committed `docs/browserevent.schema.json` in step.
#[cfg(feature = "schema")]
#[must_use]
pub fn browser_event_schema() -> schemars::Schema {
    schemars::schema_for!(BrowserEvent)
}

/// Generate the JSON Schema (draft 2020-12) for [`Finding`] and its sub-types.
///
/// Like [`browser_event_schema`], the schema is *derived* from the Rust types via
/// schemars, so it never drifts from the serialized shape. The committed
/// `docs/finding.schema.json` is kept in step by a sync test.
#[cfg(feature = "schema")]
#[must_use]
pub fn finding_schema() -> schemars::Schema {
    schemars::schema_for!(Finding)
}

/// Forensic metadata from forensicnomicon for a specific browser artifact.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForensicMeta {
    pub artifact_id: String,
    pub evidence_strength: Option<String>,
    pub volatility: Option<String>,
    pub caveats: Vec<String>,
}

impl ForensicMeta {
    /// Look up forensic metadata for the given artifact ID.
    /// Returns `None` if the artifact is not in forensicnomicon's catalog.
    #[must_use]
    pub fn lookup(artifact_id: &str) -> Option<Self> {
        let desc = forensicnomicon::evidence::evidence_for(artifact_id)?;
        Some(Self {
            artifact_id: artifact_id.to_string(),
            evidence_strength: desc.evidence_strength.map(|s| format!("{s:?}")),
            volatility: desc.volatility.map(|v| format!("{v:?}")),
            caveats: desc
                .evidence_caveats
                .iter()
                .map(|c| (*c).to_string())
                .collect(),
        })
    }
}

/// Detect the browser family from a file path.
///
/// Returns `None` if the path does not match a known browser artifact.
#[must_use]
pub fn detect_browser(path: &Path) -> Option<BrowserFamily> {
    let name = path.file_name()?.to_string_lossy().to_lowercase();
    let path_str = path.to_string_lossy().to_lowercase();

    // Safari
    if path_str.contains("safari") {
        let safari_files = [
            "history.db",
            "cookies.db",
            "downloads.plist",
            "bookmarks.plist",
        ];
        if safari_files.contains(&name.as_str()) {
            return Some(BrowserFamily::Safari);
        }
    }

    // Chromium family
    let chromium_vendors = [
        "chrome", "chromium", "edge", "brave", "opera", "vivaldi", "arc",
    ];
    let is_chromium_path = chromium_vendors.iter().any(|b| path_str.contains(b));
    let chromium_files = ["history", "cookies", "login data", "web data", "bookmarks"];
    if chromium_files.contains(&name.as_str()) && is_chromium_path {
        return Some(BrowserFamily::Chromium);
    }

    // Firefox family
    if name == "places.sqlite" || name == "formhistory.sqlite" {
        return Some(BrowserFamily::Firefox);
    }
    let firefox_files = [
        "cookies.sqlite",
        "logins.json",
        "extensions.json",
        "sessionstore.jsonlz4",
    ];
    if firefox_files.contains(&name.as_str())
        && (path_str.contains("firefox") || path_str.contains("mozilla"))
    {
        return Some(BrowserFamily::Firefox);
    }

    None
}

#[cfg(all(test, feature = "schema"))]
mod schema_tests {
    #[test]
    fn browser_event_schema_describes_the_event_fields() {
        let schema = super::browser_event_schema();
        let json = serde_json::to_value(&schema).expect("schema serializes");
        let props = json
            .get("properties")
            .and_then(serde_json::Value::as_object)
            .expect("BrowserEvent schema has a properties object");
        for field in [
            "timestamp_ns",
            "browser",
            "artifact",
            "source",
            "description",
            "attrs",
        ] {
            assert!(
                props.contains_key(field),
                "schema should describe the `{field}` field of BrowserEvent"
            );
        }
        // The sub-types must be present as reusable definitions.
        let defs = json
            .get("$defs")
            .and_then(serde_json::Value::as_object)
            .expect("schema exposes $defs for the sub-types");
        assert!(defs.contains_key("BrowserFamily"), "BrowserFamily in $defs");
        assert!(defs.contains_key("ArtifactKind"), "ArtifactKind in $defs");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn detect_chrome_history() {
        let p = Path::new("/home/user/.config/google-chrome/Default/History");
        assert_eq!(detect_browser(p), Some(BrowserFamily::Chromium));
    }

    #[test]
    fn detect_edge_history() {
        let p = Path::new("/home/user/.config/microsoft-edge/Default/History");
        assert_eq!(detect_browser(p), Some(BrowserFamily::Chromium));
    }

    #[test]
    fn detect_firefox_places() {
        let p = Path::new("/home/user/.mozilla/firefox/abc.default/places.sqlite");
        assert_eq!(detect_browser(p), Some(BrowserFamily::Firefox));
    }

    #[test]
    fn detect_firefox_cookies() {
        let p = Path::new("/home/user/.mozilla/firefox/abc.default/cookies.sqlite");
        assert_eq!(detect_browser(p), Some(BrowserFamily::Firefox));
    }

    #[test]
    fn detect_unknown_returns_none() {
        assert_eq!(detect_browser(Path::new("/tmp/foo.db")), None);
    }

    #[test]
    fn browser_family_has_safari_variant() {
        let _safari = BrowserFamily::Safari;
    }

    #[test]
    fn artifact_kind_has_bookmarks() {
        let _bk = ArtifactKind::Bookmarks;
    }

    #[test]
    fn artifact_kind_has_autofill() {
        let _af = ArtifactKind::Autofill;
    }

    #[test]
    fn artifact_kind_has_session() {
        let _s = ArtifactKind::Session;
    }

    #[test]
    fn detect_safari_history_db() {
        let p = Path::new("/Users/test/Library/Safari/History.db");
        assert_eq!(detect_browser(p), Some(BrowserFamily::Safari));
    }

    #[test]
    fn detect_brave_history() {
        let p = Path::new(
            "/Users/test/Library/Application Support/BraveSoftware/Brave-Browser/Default/History",
        );
        assert_eq!(detect_browser(p), Some(BrowserFamily::Chromium));
    }

    #[test]
    fn browser_family_display() {
        assert_eq!(format!("{}", BrowserFamily::Chromium), "Chromium");
        assert_eq!(format!("{}", BrowserFamily::Firefox), "Firefox");
        assert_eq!(format!("{}", BrowserFamily::Safari), "Safari");
    }

    #[test]
    fn browser_family_display_ie_and_edge_legacy() {
        assert_eq!(
            format!("{}", BrowserFamily::InternetExplorer),
            "Internet Explorer"
        );
        assert_eq!(format!("{}", BrowserFamily::EdgeLegacy), "Edge (Legacy)");
    }

    #[test]
    fn artifact_kind_display() {
        assert_eq!(format!("{}", ArtifactKind::History), "History");
        assert_eq!(format!("{}", ArtifactKind::Cookies), "Cookies");
        assert_eq!(format!("{}", ArtifactKind::Bookmarks), "Bookmarks");
        assert_eq!(format!("{}", ArtifactKind::Autofill), "Autofill");
        assert_eq!(format!("{}", ArtifactKind::Session), "Session");
    }

    #[test]
    fn browser_event_with_attr() {
        use serde_json::json;
        let ev = BrowserEvent::new(
            1_000_000,
            BrowserFamily::Chromium,
            ArtifactKind::History,
            "/path/to/History",
            "example.com",
        )
        .with_attr("url", json!("https://example.com"));
        assert_eq!(ev.attrs["url"], json!("https://example.com"));
        assert_eq!(ev.timestamp_ns, 1_000_000);
    }

    #[test]
    fn artifact_kind_has_integrity_variant() {
        let _ik = ArtifactKind::Integrity;
        assert_eq!(format!("{}", ArtifactKind::Integrity), "Integrity");
    }

    #[test]
    fn artifact_kind_has_carved_variant() {
        let _c = ArtifactKind::Carved;
        assert_eq!(format!("{}", ArtifactKind::Carved), "Carved");
    }

    #[test]
    fn artifact_kind_has_memory_variant() {
        let _m = ArtifactKind::Memory;
        assert_eq!(format!("{}", ArtifactKind::Memory), "Memory");
    }

    #[test]
    fn artifact_kind_has_preferences_variant() {
        let _p = ArtifactKind::Preferences;
        assert_eq!(format!("{}", ArtifactKind::Preferences), "Preferences");
    }

    #[test]
    fn artifact_kind_has_local_storage_variant() {
        let _ls = ArtifactKind::LocalStorage;
        assert_eq!(format!("{}", ArtifactKind::LocalStorage), "LocalStorage");
    }

    #[test]
    fn artifact_kind_has_permission_variant() {
        let _p = ArtifactKind::Permission;
        assert_eq!(format!("{}", ArtifactKind::Permission), "Permission");
    }

    #[test]
    fn artifact_kind_has_credit_card_variant() {
        let _c = ArtifactKind::CreditCard;
        assert_eq!(format!("{}", ArtifactKind::CreditCard), "CreditCard");
    }

    #[test]
    fn artifact_kind_has_auth_token_variant() {
        let _t = ArtifactKind::AuthToken;
        assert_eq!(format!("{}", ArtifactKind::AuthToken), "AuthToken");
    }

    #[test]
    fn artifact_kind_has_recovered_domain_variant() {
        let _r = ArtifactKind::RecoveredDomain;
        assert_eq!(
            format!("{}", ArtifactKind::RecoveredDomain),
            "RecoveredDomain"
        );
    }

    #[test]
    fn artifact_kind_has_favicon_variant() {
        let _f = ArtifactKind::Favicon;
        assert_eq!(format!("{}", ArtifactKind::Favicon), "Favicon");
    }

    #[test]
    fn artifact_kind_has_top_site_variant() {
        let _t = ArtifactKind::TopSite;
        assert_eq!(format!("{}", ArtifactKind::TopSite), "TopSite");
    }

    #[test]
    fn artifact_kind_has_shortcut_variant() {
        let _s = ArtifactKind::Shortcut;
        assert_eq!(format!("{}", ArtifactKind::Shortcut), "Shortcut");
    }

    #[test]
    fn artifact_kind_has_network_prediction_variant() {
        let _n = ArtifactKind::NetworkPrediction;
        assert_eq!(
            format!("{}", ArtifactKind::NetworkPrediction),
            "NetworkPrediction"
        );
    }

    #[test]
    fn artifact_kind_has_media_playback_variant() {
        let _m = ArtifactKind::MediaPlayback;
        assert_eq!(format!("{}", ArtifactKind::MediaPlayback), "MediaPlayback");
    }

    #[test]
    fn forensic_meta_lookup_chrome_history() {
        let meta = ForensicMeta::lookup("browser_chrome_history");
        assert!(meta.is_some());
        let meta = meta.unwrap();
        assert_eq!(meta.artifact_id, "browser_chrome_history");
        assert!(meta.evidence_strength.is_some());
    }

    #[test]
    fn forensic_meta_lookup_unknown_returns_none() {
        let meta = ForensicMeta::lookup("nonexistent_artifact_xyz");
        assert!(meta.is_none());
    }

    #[test]
    fn forensic_meta_all_browser_artifacts_have_profiles() {
        let artifact_ids = [
            "browser_chrome_history",
            "browser_chrome_cookies",
            "browser_chrome_downloads",
            "browser_chrome_bookmarks",
            "browser_chrome_extensions",
            "browser_chrome_autofill",
            "browser_chrome_cache",
            "browser_chrome_session",
            "browser_firefox_history",
            "browser_firefox_cookies",
            "browser_firefox_downloads",
            "browser_safari_history",
        ];

        for id in &artifact_ids {
            let meta = ForensicMeta::lookup(id);
            assert!(
                meta.is_some(),
                "ForensicMeta::lookup({id}) should return Some"
            );
        }
    }

    #[test]
    fn forensic_meta_evidence_strength_is_populated() {
        let meta = ForensicMeta::lookup("browser_chrome_downloads").expect("should exist");
        assert!(
            meta.evidence_strength.is_some(),
            "evidence_strength should be Some"
        );
        // Downloads are Strong evidence
        let strength = meta
            .evidence_strength
            .as_deref()
            .expect("should have value");
        assert!(
            strength.contains("Strong"),
            "Downloads should be Strong evidence, got: {strength}"
        );
    }
}