ntfs-forensic 0.6.1

Forensic anomaly auditor for NTFS — timestomping, alternate data streams, deleted records, and MFT-record slack as graded report::Finding, built on ntfs-core
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
//! Anti-forensics and threat detection from USN Journal records.
//!
//! Provides heuristic detectors for:
//! - Secure deletion tool artifacts (`SDelete`, `CCleaner`, cipher /w)
//! - USN journal clearing / tampering
//! - Ransomware-like mass rename/encrypt patterns
//! - Timestamp manipulation (timestomping)

use std::collections::HashMap;

use chrono::{DateTime, Duration, Utc};

use ntfs_core::usn::{UsnReason, UsnRecord};

// ═══════════════════════════════════════════════════════════════════════════════
// Secure Deletion Detection
// ═══════════════════════════════════════════════════════════════════════════════

/// Indicator of secure deletion tool usage.
#[derive(Debug, Clone)]
pub struct SecureDeletionIndicator {
    /// The type of secure deletion pattern detected.
    pub pattern: SecureDeletionPattern,
    /// Filenames involved in the pattern.
    pub filenames: Vec<String>,
    /// Time window during which the pattern was observed.
    pub time_start: DateTime<Utc>,
    /// End of the time window during which the pattern was observed.
    pub time_end: DateTime<Utc>,
    /// Confidence score (0.0 - 1.0).
    pub confidence: f64,
}

/// Known secure deletion tool patterns.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SecureDeletionPattern {
    /// `SDelete` creates files named with repeating chars (AAA, ZZZ, 000) then deletes them.
    SDelete,
    /// `CCleaner` and similar tools create/delete many .tmp files rapidly.
    BulkTempDeletion,
    /// `cipher /w` creates large files to overwrite free space.
    CipherWipe,
}

/// Detect patterns indicative of secure deletion tools.
///
/// Looks for:
/// - Rapid create-delete cycles for files named with `SDelete` patterns (AAA, ZZZ, 000)
/// - Bulk .tmp file deletion in Temp folders
#[must_use]
pub fn detect_secure_deletion(records: &[UsnRecord]) -> Vec<SecureDeletionIndicator> {
    let mut indicators = Vec::new();

    // ── SDelete detection ────────────────────────────────────────────────
    // SDelete renames files to sequences of repeating characters before deletion.
    // Look for create+delete of files matching patterns like AAAAAA, ZZZZZZ, 000000.

    let sdelete_patterns = detect_sdelete_patterns(records);
    indicators.extend(sdelete_patterns);

    // ── Bulk temp file deletion ──────────────────────────────────────────
    let temp_indicators = detect_bulk_temp_deletion(records);
    indicators.extend(temp_indicators);

    indicators
}

/// Detect `SDelete`-style repeating character filename patterns.
fn detect_sdelete_patterns(records: &[UsnRecord]) -> Vec<SecureDeletionIndicator> {
    let mut indicators = Vec::new();

    // Collect files matching SDelete naming patterns with create or delete events
    let mut sdelete_events: Vec<&UsnRecord> = Vec::new();

    for record in records {
        if is_sdelete_filename(&record.filename)
            && (record.reason.contains(UsnReason::FILE_CREATE)
                || record.reason.contains(UsnReason::FILE_DELETE))
        {
            sdelete_events.push(record);
        }
    }

    if sdelete_events.len() < 3 {
        return indicators;
    }

    // Group by time windows (within 60 seconds)
    let mut groups: Vec<Vec<&UsnRecord>> = Vec::new();
    let mut current_group: Vec<&UsnRecord> = vec![sdelete_events[0]];

    for event in &sdelete_events[1..] {
        let within_window = current_group
            .last()
            .is_some_and(|last| event.timestamp - last.timestamp <= Duration::seconds(60));
        if within_window {
            current_group.push(event);
        } else {
            if current_group.len() >= 3 {
                groups.push(std::mem::take(&mut current_group));
            } else {
                current_group.clear();
            }
            current_group.push(event);
        }
    }
    if current_group.len() >= 3 {
        groups.push(current_group);
    }

    for group in groups {
        let filenames: Vec<String> = group.iter().map(|r| r.filename.clone()).collect();
        let Some(time_start) = group.first().map(|r| r.timestamp) else {
            continue; // cov:unreachable: group is non-empty (only groups with len>=3 are built), so first() is always Some
        };
        let Some(time_end) = group.last().map(|r| r.timestamp) else {
            continue; // cov:unreachable: group is non-empty (only groups with len>=3 are built), so last() is always Some
        };

        // Higher confidence if we see both creates and deletes
        let has_creates = group
            .iter()
            .any(|r| r.reason.contains(UsnReason::FILE_CREATE));
        let has_deletes = group
            .iter()
            .any(|r| r.reason.contains(UsnReason::FILE_DELETE));
        let confidence = if has_creates && has_deletes { 0.9 } else { 0.6 };

        indicators.push(SecureDeletionIndicator {
            pattern: SecureDeletionPattern::SDelete,
            filenames,
            time_start,
            time_end,
            confidence,
        });
    }

    indicators
}

/// Check if a filename matches `SDelete`'s repeating character pattern.
fn is_sdelete_filename(name: &str) -> bool {
    // Strip extension if present
    let base = name.split('.').next().unwrap_or(name);
    if base.len() < 3 {
        return false;
    }
    let Some(first) = base.chars().next() else {
        return false; // cov:unreachable: base has >=3 bytes (checked above), so chars().next() is always Some
    };
    // SDelete uses repeating single characters: AAA, ZZZ, 000
    base.chars().all(|c| c == first) && (first.is_ascii_uppercase() || first.is_ascii_digit())
}

/// Detect bulk .tmp file deletion indicative of cleaning tools.
fn detect_bulk_temp_deletion(records: &[UsnRecord]) -> Vec<SecureDeletionIndicator> {
    let mut indicators = Vec::new();

    // Find delete events for .tmp files
    let tmp_deletes: Vec<&UsnRecord> = records
        .iter()
        .filter(|r| {
            r.reason.contains(UsnReason::FILE_DELETE) && r.filename.to_lowercase().ends_with(".tmp")
        })
        .collect();

    if tmp_deletes.len() < 10 {
        return indicators;
    }

    // Group by 30-second windows
    let mut groups: Vec<Vec<&UsnRecord>> = Vec::new();
    let mut current_group: Vec<&UsnRecord> = vec![tmp_deletes[0]];

    for event in &tmp_deletes[1..] {
        let within_window = current_group
            .last()
            .is_some_and(|last| event.timestamp - last.timestamp <= Duration::seconds(30));
        if within_window {
            current_group.push(event);
        } else {
            if current_group.len() >= 10 {
                groups.push(std::mem::take(&mut current_group));
            } else {
                current_group.clear();
            }
            current_group.push(event);
        }
    }
    if current_group.len() >= 10 {
        groups.push(current_group);
    }

    for group in groups {
        let Some(time_start) = group.first().map(|r| r.timestamp) else {
            continue; // cov:unreachable: group is non-empty (only groups with len>=10 are built), so first() is always Some
        };
        let Some(time_end) = group.last().map(|r| r.timestamp) else {
            continue; // cov:unreachable: group is non-empty (only groups with len>=10 are built), so last() is always Some
        };
        indicators.push(SecureDeletionIndicator {
            pattern: SecureDeletionPattern::BulkTempDeletion,
            filenames: group.iter().map(|r| r.filename.clone()).collect(),
            time_start,
            time_end,
            confidence: 0.7,
        });
    }

    indicators
}

// ═══════════════════════════════════════════════════════════════════════════════
// Journal Clearing Detection
// ═══════════════════════════════════════════════════════════════════════════════

/// Result of journal clearing analysis.
#[derive(Debug, Clone)]
pub struct JournalClearingResult {
    /// Whether journal clearing was detected.
    pub clearing_detected: bool,
    /// The first USN value seen (high values suggest prior clearing).
    pub first_usn: Option<i64>,
    /// Timestamp gaps detected (sudden jumps in time).
    pub timestamp_gaps: Vec<TimestampGap>,
    /// Overall confidence (0.0 - 1.0).
    pub confidence: f64,
}

/// A detected gap in the journal timeline.
#[derive(Debug, Clone)]
pub struct TimestampGap {
    /// Timestamp before the gap.
    pub before: DateTime<Utc>,
    /// Timestamp after the gap.
    pub after: DateTime<Utc>,
    /// Duration of the gap.
    pub gap_duration: Duration,
    /// USN value before the gap.
    pub usn_before: i64,
    /// USN value after the gap.
    pub usn_after: i64,
}

/// Detect if the USN journal was cleared or tampered with.
///
/// Indicators:
/// - First record's USN is very high (older records were removed)
/// - Sudden large timestamp jumps between consecutive records
#[must_use]
pub fn detect_journal_clearing(records: &[UsnRecord]) -> JournalClearingResult {
    let Some(first) = records.first() else {
        return JournalClearingResult {
            clearing_detected: false,
            first_usn: None,
            timestamp_gaps: Vec::new(),
            confidence: 0.0,
        };
    };

    let first_usn = first.usn;
    let mut confidence = 0.0;

    // A high first USN suggests the journal has been in use but older entries were cleared.
    // Typical threshold: if first USN > 1GB of journal data, it's suspicious.
    const USN_CLEARING_THRESHOLD: i64 = 1_073_741_824; // 1 GiB
    let high_usn = first_usn > USN_CLEARING_THRESHOLD;
    if high_usn {
        confidence += 0.5;
    }

    // Detect timestamp gaps (jumps of > 24 hours between consecutive records)
    let mut timestamp_gaps = Vec::new();
    let gap_threshold = Duration::hours(24);

    for window in records.windows(2) {
        let gap = window[1].timestamp - window[0].timestamp;
        if gap > gap_threshold {
            timestamp_gaps.push(TimestampGap {
                before: window[0].timestamp,
                after: window[1].timestamp,
                gap_duration: gap,
                usn_before: window[0].usn,
                usn_after: window[1].usn,
            });
        }
    }

    if !timestamp_gaps.is_empty() {
        // More gaps = higher confidence
        let gap_factor = (timestamp_gaps.len() as f64 * 0.2).min(0.5);
        confidence += gap_factor;
    }

    let clearing_detected = confidence >= 0.4;

    JournalClearingResult {
        clearing_detected,
        first_usn: Some(first_usn),
        timestamp_gaps,
        confidence,
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Ransomware Detection
// ═══════════════════════════════════════════════════════════════════════════════

/// Indicator of ransomware-like activity.
#[derive(Debug, Clone)]
pub struct RansomwareIndicator {
    /// The suspicious extension being added to files.
    pub extension: String,
    /// Number of files affected.
    pub affected_count: usize,
    /// Sample filenames that were renamed.
    pub sample_filenames: Vec<String>,
    /// Time window of the activity.
    pub time_start: DateTime<Utc>,
    /// End of the time window of the activity.
    pub time_end: DateTime<Utc>,
    /// Confidence score (0.0 - 1.0).
    pub confidence: f64,
}

/// Known ransomware extensions to check for.
const RANSOMWARE_EXTENSIONS: &[&str] = &[
    ".encrypted",
    ".locked",
    ".crypto",
    ".crypt",
    ".enc",
    ".locky",
    ".cerber",
    ".zepto",
    ".odin",
    ".thor",
    ".aesir",
    ".zzzzz",
    ".micro",
    ".crypted",
    ".crinf",
    ".r5a",
    ".xrtn",
    ".xtbl",
    ".crypz",
    ".cryp1",
    ".ransom",
    ".wallet",
    ".onion",
    ".wncry",
    ".wcry",
    ".wncryt",
];

/// Detect ransomware-like behavior patterns in USN records.
///
/// Looks for:
/// - Mass file renames with known ransomware extensions
/// - High rate of `DATA_OVERWRITE` followed by `RENAME_NEW_NAME`
/// - Many files getting the same new extension in a short time window
#[must_use]
pub fn detect_ransomware_patterns(records: &[UsnRecord]) -> Vec<RansomwareIndicator> {
    let mut indicators = Vec::new();

    // ── Known extension detection ────────────────────────────────────────
    indicators.extend(detect_known_ransomware_extensions(records));

    // ── Mass rename with same new extension ──────────────────────────────
    indicators.extend(detect_mass_rename_patterns(records));

    indicators
}

/// Detect files being renamed to known ransomware extensions.
fn detect_known_ransomware_extensions(records: &[UsnRecord]) -> Vec<RansomwareIndicator> {
    let mut indicators = Vec::new();

    // Group renames by extension
    let mut extension_groups: HashMap<String, Vec<&UsnRecord>> = HashMap::new();

    for record in records {
        if record.reason.contains(UsnReason::RENAME_NEW_NAME) {
            let lower = record.filename.to_lowercase();
            for ext in RANSOMWARE_EXTENSIONS {
                if lower.ends_with(ext) {
                    extension_groups
                        .entry((*ext).to_string())
                        .or_default()
                        .push(record);
                    break;
                }
            }
        }
    }

    for (ext, group) in &extension_groups {
        if group.len() >= 3 {
            let Some(time_start) = group.iter().map(|r| r.timestamp).min() else {
                continue; // cov:unreachable: the enclosing len()>=3 check guarantees a non-empty iterator, so min() is always Some
            };
            let Some(time_end) = group.iter().map(|r| r.timestamp).max() else {
                continue; // cov:unreachable: the enclosing len()>=3 check guarantees a non-empty iterator, so max() is always Some
            };
            let sample: Vec<String> = group.iter().take(10).map(|r| r.filename.clone()).collect();

            let confidence = if group.len() >= 20 {
                0.95
            } else if group.len() >= 10 {
                0.85
            } else {
                0.6
            };

            indicators.push(RansomwareIndicator {
                extension: ext.clone(),
                affected_count: group.len(),
                sample_filenames: sample,
                time_start,
                time_end,
                confidence,
            });
        }
    }

    indicators
}

/// Detect mass rename patterns where many files get the same new extension.
fn detect_mass_rename_patterns(records: &[UsnRecord]) -> Vec<RansomwareIndicator> {
    let mut indicators = Vec::new();

    // Find DATA_OVERWRITE followed by RENAME_NEW_NAME patterns
    // Group renames by new extension in 5-minute windows
    let rename_records: Vec<&UsnRecord> = records
        .iter()
        .filter(|r| r.reason.contains(UsnReason::RENAME_NEW_NAME))
        .collect();

    if rename_records.len() < 20 {
        return indicators;
    }

    // Group by extension (excluding known ransomware - already handled above)
    let mut ext_groups: HashMap<String, Vec<&UsnRecord>> = HashMap::new();

    for record in &rename_records {
        if let Some(dot_pos) = record.filename.rfind('.') {
            let ext = record.filename[dot_pos..].to_lowercase();
            // Skip common extensions
            if !is_common_extension(&ext) {
                let lower = ext.clone();
                let is_known = RANSOMWARE_EXTENSIONS.iter().any(|&re| lower == re);
                if !is_known {
                    ext_groups.entry(ext).or_default().push(record);
                }
            }
        }
    }

    // Report extensions with abnormally high rename counts in tight time windows
    for (ext, group) in &ext_groups {
        if group.len() >= 20 {
            let Some(time_start) = group.iter().map(|r| r.timestamp).min() else {
                continue; // cov:unreachable: the enclosing len()>=20 check guarantees a non-empty iterator, so min() is always Some
            };
            let Some(time_end) = group.iter().map(|r| r.timestamp).max() else {
                continue; // cov:unreachable: the enclosing len()>=20 check guarantees a non-empty iterator, so max() is always Some
            };
            let duration = time_end - time_start;

            // 20+ renames to the same unusual extension within 10 minutes is suspicious
            if duration <= Duration::minutes(10) {
                let sample: Vec<String> =
                    group.iter().take(10).map(|r| r.filename.clone()).collect();

                indicators.push(RansomwareIndicator {
                    extension: ext.clone(),
                    affected_count: group.len(),
                    sample_filenames: sample,
                    time_start,
                    time_end,
                    confidence: 0.75,
                });
            }
        }
    }

    indicators
}

/// Check if an extension is common/benign.
fn is_common_extension(ext: &str) -> bool {
    matches!(
        ext,
        ".txt"
            | ".doc"
            | ".docx"
            | ".xls"
            | ".xlsx"
            | ".pdf"
            | ".jpg"
            | ".jpeg"
            | ".png"
            | ".gif"
            | ".mp3"
            | ".mp4"
            | ".avi"
            | ".zip"
            | ".rar"
            | ".exe"
            | ".dll"
            | ".sys"
            | ".log"
            | ".tmp"
            | ".bak"
            | ".html"
            | ".htm"
            | ".css"
            | ".js"
            | ".py"
            | ".rs"
            | ".c"
            | ".h"
            | ".cpp"
            | ".java"
            | ".xml"
            | ".json"
            | ".csv"
            | ".ppt"
            | ".pptx"
    )
}

// ═══════════════════════════════════════════════════════════════════════════════
// Timestomping Detection
// ═══════════════════════════════════════════════════════════════════════════════

/// Indicator of timestamp manipulation.
#[derive(Debug, Clone)]
pub struct TimestompIndicator {
    /// The filename whose timestamps may have been manipulated.
    pub filename: String,
    /// MFT entry number.
    pub mft_entry: u64,
    /// The timestamp of the `BASIC_INFO_CHANGE` event.
    pub change_timestamp: DateTime<Utc>,
    /// Whether data modification events were found nearby.
    pub has_nearby_data_change: bool,
    /// Confidence score (0.0 - 1.0).
    pub confidence: f64,
}

/// Detect timestamp manipulation from USN records.
///
/// Timestomping is often visible in USN journals because:
/// - Modifying `$STANDARD_INFORMATION` timestamps generates a `BASIC_INFO_CHANGE` event
/// - Legitimate timestamp changes usually accompany data modifications
/// - Isolated `BASIC_INFO_CHANGE` events without nearby data changes are suspicious
#[must_use]
pub fn detect_timestomping(records: &[UsnRecord]) -> Vec<TimestompIndicator> {
    let mut indicators = Vec::new();

    // Build a map of MFT entry -> records for correlation
    let mut entry_events: HashMap<u64, Vec<&UsnRecord>> = HashMap::new();
    for record in records {
        entry_events
            .entry(record.mft_entry)
            .or_default()
            .push(record);
    }

    // For each file, look for isolated BASIC_INFO_CHANGE events
    for (&mft_entry, events) in &entry_events {
        for (i, event) in events.iter().enumerate() {
            if !event.reason.contains(UsnReason::BASIC_INFO_CHANGE) {
                continue;
            }

            // Check if this BASIC_INFO_CHANGE has no accompanying data changes
            // within a window of nearby events (5 events before/after or 60 seconds)
            let has_nearby_data_change = events.iter().enumerate().any(|(j, other)| {
                if i == j {
                    return false;
                }
                let time_diff = if other.timestamp >= event.timestamp {
                    other.timestamp - event.timestamp
                } else {
                    event.timestamp - other.timestamp
                };
                if time_diff > Duration::seconds(60) {
                    return false;
                }
                other.reason.contains(UsnReason::DATA_OVERWRITE)
                    || other.reason.contains(UsnReason::DATA_EXTEND)
                    || other.reason.contains(UsnReason::DATA_TRUNCATION)
                    || other.reason.contains(UsnReason::FILE_CREATE)
            });

            // Isolated BASIC_INFO_CHANGE is suspicious
            if !has_nearby_data_change {
                // Check if reason is ONLY BASIC_INFO_CHANGE (possibly with CLOSE)
                let reason_without_close = event.reason & !UsnReason::CLOSE;
                let is_isolated = reason_without_close == UsnReason::BASIC_INFO_CHANGE;

                let confidence = if is_isolated { 0.8 } else { 0.5 };

                indicators.push(TimestompIndicator {
                    filename: event.filename.clone(),
                    mft_entry,
                    change_timestamp: event.timestamp,
                    has_nearby_data_change: false,
                    confidence,
                });
            }
        }
    }

    indicators
}

// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{DateTime, Duration, Utc};
    use ntfs_core::usn::{FileAttributes, UsnReason, UsnRecord};

    /// Helper to build a synthetic `UsnRecord` for testing.
    fn make_record(
        mft_entry: u64,
        filename: &str,
        reason: UsnReason,
        timestamp: DateTime<Utc>,
        usn: i64,
    ) -> UsnRecord {
        UsnRecord {
            mft_entry,
            mft_sequence: 1,
            parent_mft_entry: 5,
            parent_mft_sequence: 1,
            usn,
            timestamp,
            reason,
            filename: filename.to_string(),
            file_attributes: FileAttributes::ARCHIVE,
            source_info: 0,
            security_id: 0,
            major_version: 2,
        }
    }

    fn ts(secs_offset: i64) -> DateTime<Utc> {
        DateTime::from_timestamp(1_700_000_000 + secs_offset, 0).unwrap()
    }

    // ─── Secure Deletion Tests ───────────────────────────────────────────

    #[test]
    fn test_detect_sdelete_pattern() {
        let records = vec![
            make_record(100, "AAAAAAA", UsnReason::FILE_CREATE, ts(0), 1000),
            make_record(100, "AAAAAAA", UsnReason::FILE_DELETE, ts(1), 1100),
            make_record(101, "ZZZZZZZ", UsnReason::FILE_CREATE, ts(2), 1200),
            make_record(101, "ZZZZZZZ", UsnReason::FILE_DELETE, ts(3), 1300),
            make_record(102, "0000000", UsnReason::FILE_CREATE, ts(4), 1400),
            make_record(102, "0000000", UsnReason::FILE_DELETE, ts(5), 1500),
        ];

        let indicators = detect_secure_deletion(&records);
        assert!(!indicators.is_empty());
        assert_eq!(indicators[0].pattern, SecureDeletionPattern::SDelete);
        assert!(indicators[0].confidence >= 0.9);
    }

    #[test]
    fn test_sdelete_not_triggered_by_normal_files() {
        let records = vec![
            make_record(100, "document.docx", UsnReason::FILE_CREATE, ts(0), 1000),
            make_record(101, "report.pdf", UsnReason::FILE_CREATE, ts(1), 1100),
            make_record(102, "image.png", UsnReason::FILE_DELETE, ts(2), 1200),
        ];

        let indicators = detect_secure_deletion(&records);
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_detect_bulk_temp_deletion() {
        let mut records = Vec::new();
        for i in 0..15 {
            records.push(make_record(
                100 + i,
                &format!("tmp{i:04}.tmp"),
                UsnReason::FILE_DELETE,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_secure_deletion(&records);
        assert!(!indicators.is_empty());
        assert_eq!(
            indicators[0].pattern,
            SecureDeletionPattern::BulkTempDeletion
        );
    }

    #[test]
    fn test_no_bulk_temp_with_few_files() {
        let records = vec![
            make_record(100, "tmp001.tmp", UsnReason::FILE_DELETE, ts(0), 1000),
            make_record(101, "tmp002.tmp", UsnReason::FILE_DELETE, ts(1), 1100),
        ];

        let indicators = detect_secure_deletion(&records);
        assert!(indicators.is_empty());
    }

    // ─── Journal Clearing Tests ──────────────────────────────────────────

    #[test]
    fn test_detect_high_starting_usn() {
        let records = vec![
            make_record(
                100,
                "file.txt",
                UsnReason::FILE_CREATE,
                ts(0),
                2_000_000_000, // 2GB - way above threshold
            ),
            make_record(
                101,
                "file2.txt",
                UsnReason::FILE_CREATE,
                ts(1),
                2_000_001_000,
            ),
        ];

        let result = detect_journal_clearing(&records);
        assert!(result.clearing_detected);
        assert!(result.confidence >= 0.4);
        assert_eq!(result.first_usn, Some(2_000_000_000));
    }

    #[test]
    fn test_detect_timestamp_gap() {
        let records = vec![
            make_record(100, "before.txt", UsnReason::FILE_CREATE, ts(0), 1000),
            // 48-hour gap
            make_record(
                101,
                "after.txt",
                UsnReason::FILE_CREATE,
                ts(48 * 3600),
                1100,
            ),
        ];

        let result = detect_journal_clearing(&records);
        assert!(!result.timestamp_gaps.is_empty());
        assert!(result.timestamp_gaps[0].gap_duration > Duration::hours(24));
    }

    #[test]
    fn test_no_clearing_for_normal_journal() {
        let records = vec![
            make_record(100, "a.txt", UsnReason::FILE_CREATE, ts(0), 100),
            make_record(101, "b.txt", UsnReason::FILE_CREATE, ts(60), 200),
            make_record(102, "c.txt", UsnReason::FILE_CREATE, ts(120), 300),
        ];

        let result = detect_journal_clearing(&records);
        assert!(!result.clearing_detected);
        assert!(result.timestamp_gaps.is_empty());
    }

    #[test]
    fn test_clearing_empty_records() {
        let result = detect_journal_clearing(&[]);
        assert!(!result.clearing_detected);
        assert!(result.first_usn.is_none());
    }

    // ─── Ransomware Detection Tests ──────────────────────────────────────

    #[test]
    fn test_detect_known_ransomware_extension() {
        let mut records = Vec::new();
        for i in 0..5 {
            records.push(make_record(
                100 + i,
                &format!("document{i}.docx.encrypted"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        assert!(!indicators.is_empty());
        assert_eq!(indicators[0].extension, ".encrypted");
        assert_eq!(indicators[0].affected_count, 5);
    }

    #[test]
    fn test_detect_mass_rename_unknown_extension() {
        let mut records = Vec::new();
        for i in 0..25 {
            records.push(make_record(
                100 + i,
                &format!("file{i}.xyz_ransom"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        assert!(!indicators.is_empty());
    }

    #[test]
    fn test_no_ransomware_for_normal_renames() {
        let records = vec![
            make_record(100, "doc1.docx", UsnReason::RENAME_NEW_NAME, ts(0), 1000),
            make_record(101, "image.png", UsnReason::RENAME_NEW_NAME, ts(100), 1100),
            make_record(102, "report.pdf", UsnReason::RENAME_NEW_NAME, ts(200), 1200),
        ];

        let indicators = detect_ransomware_patterns(&records);
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_ransomware_multiple_known_extensions() {
        let mut records = Vec::new();
        // .locked files
        for i in 0..5 {
            records.push(make_record(
                100 + i,
                &format!("file{i}.locked"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }
        // .crypto files
        for i in 0..4 {
            records.push(make_record(
                200 + i,
                &format!("photo{i}.crypto"),
                UsnReason::RENAME_NEW_NAME,
                ts(100 + i as i64),
                2000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        // Should detect at least the .locked group (5 >= 3 threshold)
        let locked_indicators: Vec<_> = indicators
            .iter()
            .filter(|i| i.extension == ".locked")
            .collect();
        assert!(!locked_indicators.is_empty());
    }

    // ─── Timestomping Detection Tests ────────────────────────────────────

    #[test]
    fn test_detect_isolated_basic_info_change() {
        let records = vec![make_record(
            100,
            "suspicious.exe",
            UsnReason::BASIC_INFO_CHANGE,
            ts(1000),
            5000,
        )];

        let indicators = detect_timestomping(&records);
        assert!(!indicators.is_empty());
        assert_eq!(indicators[0].filename, "suspicious.exe");
        assert!(!indicators[0].has_nearby_data_change);
        assert!(indicators[0].confidence >= 0.7);
    }

    #[test]
    fn test_no_timestomp_with_data_change() {
        let records = vec![
            make_record(100, "normal.docx", UsnReason::DATA_OVERWRITE, ts(999), 4900),
            make_record(
                100,
                "normal.docx",
                UsnReason::BASIC_INFO_CHANGE,
                ts(1000),
                5000,
            ),
        ];

        let indicators = detect_timestomping(&records);
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_timestomp_with_distant_data_change() {
        // Data change is > 60 seconds away, so BASIC_INFO_CHANGE is still suspicious
        let records = vec![
            make_record(
                100,
                "suspicious.exe",
                UsnReason::DATA_OVERWRITE,
                ts(0),
                1000,
            ),
            make_record(
                100,
                "suspicious.exe",
                UsnReason::BASIC_INFO_CHANGE,
                ts(120), // 2 minutes later
                5000,
            ),
        ];

        let indicators = detect_timestomping(&records);
        assert!(!indicators.is_empty());
    }

    #[test]
    fn test_timestomp_multiple_files() {
        let records = vec![
            make_record(
                100,
                "malware1.exe",
                UsnReason::BASIC_INFO_CHANGE,
                ts(0),
                1000,
            ),
            make_record(
                200,
                "malware2.dll",
                UsnReason::BASIC_INFO_CHANGE,
                ts(5),
                1500,
            ),
            make_record(300, "normal.txt", UsnReason::DATA_OVERWRITE, ts(10), 2000),
            make_record(
                300,
                "normal.txt",
                UsnReason::BASIC_INFO_CHANGE,
                ts(11),
                2100,
            ),
        ];

        let indicators = detect_timestomping(&records);
        // malware1.exe and malware2.dll should be flagged, normal.txt should not
        let flagged_files: Vec<&str> = indicators.iter().map(|i| i.filename.as_str()).collect();
        assert!(flagged_files.contains(&"malware1.exe"));
        assert!(flagged_files.contains(&"malware2.dll"));
        assert!(!flagged_files.contains(&"normal.txt"));
    }

    #[test]
    fn test_no_timestomp_on_create() {
        // FILE_CREATE is a legitimate reason for BASIC_INFO_CHANGE
        let records = vec![
            make_record(100, "newfile.txt", UsnReason::FILE_CREATE, ts(0), 1000),
            make_record(
                100,
                "newfile.txt",
                UsnReason::BASIC_INFO_CHANGE,
                ts(1),
                1100,
            ),
        ];

        let indicators = detect_timestomping(&records);
        assert!(indicators.is_empty());
    }

    // ─── Additional coverage tests ──────────────────────────────────────

    #[test]
    fn test_is_sdelete_filename_short() {
        assert!(!is_sdelete_filename("AB"));
        assert!(!is_sdelete_filename("A"));
        assert!(!is_sdelete_filename(""));
    }

    #[test]
    fn test_is_sdelete_filename_mixed_chars() {
        assert!(!is_sdelete_filename("ABCDEF"));
        assert!(!is_sdelete_filename("aaaaaa")); // lowercase not matched
    }

    #[test]
    fn test_is_sdelete_filename_with_extension() {
        assert!(is_sdelete_filename("AAAA.txt"));
        assert!(is_sdelete_filename("ZZZZZ.dat"));
        assert!(is_sdelete_filename("00000.bin"));
    }

    #[test]
    fn test_is_common_extension() {
        assert!(is_common_extension(".txt"));
        assert!(is_common_extension(".exe"));
        assert!(is_common_extension(".dll"));
        assert!(is_common_extension(".pdf"));
        assert!(!is_common_extension(".xyz_ransom"));
        assert!(!is_common_extension(".custom"));
    }

    #[test]
    fn test_sdelete_only_creates_lower_confidence() {
        // Only create events, no deletes -> lower confidence
        let records = vec![
            make_record(100, "AAAAAAA", UsnReason::FILE_CREATE, ts(0), 1000),
            make_record(101, "BBBBBBB", UsnReason::FILE_CREATE, ts(1), 1100),
            make_record(102, "CCCCCCC", UsnReason::FILE_CREATE, ts(2), 1200),
        ];

        let indicators = detect_secure_deletion(&records);
        assert!(!indicators.is_empty());
        assert!(indicators[0].confidence < 0.9);
    }

    #[test]
    fn test_sdelete_events_spread_over_time() {
        // Events more than 60 seconds apart should be separate groups
        let records = vec![
            make_record(100, "AAAAAAA", UsnReason::FILE_CREATE, ts(0), 1000),
            make_record(101, "AAAAAAA", UsnReason::FILE_DELETE, ts(1), 1100),
            // Gap > 60 seconds
            make_record(102, "BBBBBBB", UsnReason::FILE_CREATE, ts(120), 1200),
            make_record(103, "BBBBBBB", UsnReason::FILE_DELETE, ts(121), 1300),
        ];

        // Each pair has only 2 events, below the threshold of 3
        let indicators = detect_secure_deletion(&records);
        let sdelete_indicators: Vec<_> = indicators
            .iter()
            .filter(|i| i.pattern == SecureDeletionPattern::SDelete)
            .collect();
        assert!(sdelete_indicators.is_empty());
    }

    #[test]
    fn test_bulk_temp_deletion_spread_over_time() {
        // .tmp deletes more than 30 seconds apart should form separate groups
        let mut records = Vec::new();
        for i in 0..5 {
            records.push(make_record(
                100 + i,
                &format!("tmp{i:04}.tmp"),
                UsnReason::FILE_DELETE,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }
        // Gap > 30 seconds
        for i in 0..5 {
            let n = 100 + i;
            records.push(make_record(
                200 + i,
                &format!("tmp{n:04}.tmp"),
                UsnReason::FILE_DELETE,
                ts(60 + i as i64),
                2000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_secure_deletion(&records);
        let bulk = indicators
            .iter()
            .filter(|i| i.pattern == SecureDeletionPattern::BulkTempDeletion)
            .count();
        assert_eq!(bulk, 0);
    }

    #[test]
    fn test_mass_rename_with_common_extension_ignored() {
        // Mass renames to .txt should NOT trigger ransomware detection
        let mut records = Vec::new();
        for i in 0..25 {
            records.push(make_record(
                100 + i,
                &format!("file{i}.txt"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_ransomware_high_count_high_confidence() {
        // 20+ renames to known extension should have 0.95 confidence
        let mut records = Vec::new();
        for i in 0..25 {
            records.push(make_record(
                100 + i,
                &format!("document{i}.docx.encrypted"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        assert!(!indicators.is_empty());
        let encrypted_ind = indicators
            .iter()
            .find(|i| i.extension == ".encrypted")
            .unwrap();
        assert!(encrypted_ind.confidence >= 0.95);
    }

    #[test]
    fn test_ransomware_medium_count_medium_confidence() {
        // 10-19 renames should have 0.85 confidence
        let mut records = Vec::new();
        for i in 0..12 {
            records.push(make_record(
                100 + i,
                &format!("file{i}.locked"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        let locked = indicators
            .iter()
            .find(|i| i.extension == ".locked")
            .unwrap();
        assert!((locked.confidence - 0.85).abs() < 0.01);
    }

    #[test]
    fn test_mass_rename_over_long_time_not_ransomware() {
        // Mass renames to same unknown extension but spread over > 10 minutes
        let mut records = Vec::new();
        for i in 0..25 {
            records.push(make_record(
                100 + i,
                &format!("file{i}.xyz_spread"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64 * 60), // 1 minute apart, total > 10 min
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        let spread = indicators
            .iter()
            .filter(|i| i.extension == ".xyz_spread")
            .count();
        assert_eq!(spread, 0);
    }

    #[test]
    fn test_detect_journal_clearing_multiple_gaps() {
        // Multiple 24+ hour gaps increase confidence
        let records = vec![
            make_record(100, "a.txt", UsnReason::FILE_CREATE, ts(0), 100),
            make_record(101, "b.txt", UsnReason::FILE_CREATE, ts(25 * 3600), 200),
            make_record(102, "c.txt", UsnReason::FILE_CREATE, ts(50 * 3600), 300),
            make_record(103, "d.txt", UsnReason::FILE_CREATE, ts(75 * 3600), 400),
        ];

        let result = detect_journal_clearing(&records);
        assert_eq!(result.timestamp_gaps.len(), 3);
        assert!(result.confidence > 0.0);
    }

    #[test]
    fn test_timestomp_basic_info_change_with_close() {
        // BASIC_INFO_CHANGE | CLOSE (not isolated) should have lower confidence
        let records = vec![make_record(
            100,
            "stomped.exe",
            UsnReason::BASIC_INFO_CHANGE | UsnReason::CLOSE | UsnReason::SECURITY_CHANGE,
            ts(1000),
            5000,
        )];

        let indicators = detect_timestomping(&records);
        // A lone BASIC_INFO_CHANGE | CLOSE | SECURITY_CHANGE has no nearby data
        // change, so it is always flagged; the reason is not isolated (it carries
        // SECURITY_CHANGE), so confidence is the lower 0.5.
        assert!(!indicators.is_empty());
        assert!(indicators[0].confidence <= 0.5);
    }

    #[test]
    fn test_sdelete_grouping_splits_on_time_gap() {
        // Line 96: groups.push when a group of >= 3 SDelete events is followed
        // by a time gap > 60 seconds, then more events start a new group.
        let mut records = Vec::new();

        // First group: 4 SDelete events within 60 seconds
        for i in 0..4u64 {
            records.push(make_record(
                100 + i,
                &"A".repeat(7),
                UsnReason::FILE_CREATE,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        // Time gap of 120 seconds (> 60s threshold)
        // Small group of 2 SDelete events (< 3, should be cleared not pushed)
        records.push(make_record(
            200,
            &"B".repeat(7),
            UsnReason::FILE_DELETE,
            ts(124),
            2000,
        ));
        records.push(make_record(
            201,
            &"B".repeat(7),
            UsnReason::FILE_DELETE,
            ts(125),
            2100,
        ));

        // Another time gap
        // Third group: 3 more SDelete events
        for i in 0..3u64 {
            records.push(make_record(
                300 + i,
                &"C".repeat(7),
                UsnReason::FILE_CREATE,
                ts(300 + i as i64),
                3000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_secure_deletion(&records);
        // Should detect at least the first group (4 events) and third group (3 events)
        let sdelete_indicators: Vec<_> = indicators
            .iter()
            .filter(|i| i.pattern == SecureDeletionPattern::SDelete)
            .collect();
        assert!(!sdelete_indicators.is_empty());
    }

    #[test]
    fn test_bulk_temp_deletion_grouping_splits_on_time_gap() {
        // Line 173: groups.push when a group of >= 10 .tmp deletions is followed
        // by a time gap > 30 seconds, then more events.
        let mut records = Vec::new();

        // First group: 12 tmp file deletions within 30 seconds
        for i in 0..12u64 {
            records.push(make_record(
                100 + i,
                &format!("tmp{i:04}.tmp"),
                UsnReason::FILE_DELETE,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        // Time gap of 60 seconds (> 30s threshold)
        // Small group of 5 tmp deletions (< 10, should be cleared)
        for i in 0..5u64 {
            records.push(make_record(
                200 + i,
                &format!("tmpB{i:04}.tmp"),
                UsnReason::FILE_DELETE,
                ts(72 + i as i64),
                2000 + (i as i64) * 100,
            ));
        }

        // Another time gap, then another group of 10
        for i in 0..10u64 {
            records.push(make_record(
                300 + i,
                &format!("tmpC{i:04}.tmp"),
                UsnReason::FILE_DELETE,
                ts(200 + i as i64),
                3000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_secure_deletion(&records);
        let bulk_indicators: Vec<_> = indicators
            .iter()
            .filter(|i| i.pattern == SecureDeletionPattern::BulkTempDeletion)
            .collect();
        assert!(!bulk_indicators.is_empty());
    }

    #[test]
    fn test_timestomping_other_before_event() {
        // Line 532: other.timestamp - event.timestamp path
        // when other event happens BEFORE the BASIC_INFO_CHANGE event
        // This tests the branch where other.timestamp < event.timestamp
        // so the abs difference is event.timestamp - other.timestamp.
        // Wait, line 531-532 actually is:
        //   let time_diff = if other.timestamp >= event.timestamp {
        //       other.timestamp - event.timestamp    // line 532
        //   } else {
        //       event.timestamp - other.timestamp
        //   };
        // Line 532 fires when other.timestamp >= event.timestamp.
        // The existing tests have nearby data changes AFTER the BASIC_INFO_CHANGE.
        // We need a test where the data change is AT or AFTER the event.
        // Actually, we need an ISOLATED BASIC_INFO_CHANGE where nearby events
        // DON'T have data changes but DO have timestamps >= event.timestamp.

        let records = vec![
            // A BASIC_INFO_CHANGE event (possible timestomping)
            make_record(
                100,
                "stomped.exe",
                UsnReason::BASIC_INFO_CHANGE,
                ts(1000),
                5000,
            ),
            // A nearby event AFTER the BASIC_INFO_CHANGE (> its timestamp)
            // but with SECURITY_CHANGE (not a data change), so it doesn't
            // suppress the timestomping detection
            make_record(
                101,
                "other.txt",
                UsnReason::SECURITY_CHANGE,
                ts(1010), // 10 seconds after, within 60s window
                5100,
            ),
        ];

        let indicators = detect_timestomping(&records);
        assert!(!indicators.is_empty());
        assert_eq!(indicators[0].filename, "stomped.exe");
    }

    #[test]
    fn test_ransomware_renames_without_extension() {
        // Renames with no extension should not cause issues
        let mut records = Vec::new();
        for i in 0..25 {
            records.push(make_record(
                100 + i,
                &format!("file{i}"), // No extension
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_ransomware_patterns(&records);
        // Should not crash, and no indicator for files without extensions
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_timestomp_other_timestamp_after_event() {
        // Cover line 557: `other.timestamp - event.timestamp` branch.
        // Create a BASIC_INFO_CHANGE event with a DATA_OVERWRITE event that occurs
        // AFTER it (within 60 seconds). This exercises the `other.timestamp >= event.timestamp`
        // path where `time_diff = other.timestamp - event.timestamp`.
        let records = vec![
            make_record(100, "file.exe", UsnReason::BASIC_INFO_CHANGE, ts(100), 5000),
            make_record(
                100,
                "file.exe",
                UsnReason::DATA_OVERWRITE,
                ts(110), // 10 seconds AFTER the BASIC_INFO_CHANGE
                5100,
            ),
        ];

        let indicators = detect_timestomping(&records);
        // The DATA_OVERWRITE is within 60 seconds and after the BASIC_INFO_CHANGE,
        // so it should NOT be flagged as timestomping.
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_known_ext_rename_without_matching_extension() {
        // detect_known_ransomware_extensions: a RENAME_NEW_NAME whose filename
        // ends with NONE of the known ransomware extensions exercises the path
        // where the inner `for ext` loop completes without `break`, letting
        // control fall through the end of the `if reason.contains(..)` block.
        let records = vec![
            make_record(100, "renamed.docx", UsnReason::RENAME_NEW_NAME, ts(0), 1000),
            make_record(101, "moved.pdf", UsnReason::RENAME_NEW_NAME, ts(1), 1100),
            make_record(102, "data.bin", UsnReason::RENAME_NEW_NAME, ts(2), 1200),
            // A non-rename record so the per-record `if reason.contains(RENAME_NEW_NAME)`
            // guard is skipped (its false arm), and a rename whose name matches no
            // ransomware extension so the inner `for ext` loop completes without break.
            make_record(103, "plain.txt", UsnReason::FILE_CREATE, ts(3), 1300),
        ];

        let indicators = detect_known_ransomware_extensions(&records);
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_known_ext_group_below_threshold_skips() {
        // detect_known_ransomware_extensions: an extension group with fewer than
        // 3 members fails `group.len() >= 3` and falls through the `if` body,
        // reaching the loop-iteration tail of the per-extension loop.
        let records = vec![
            make_record(100, "a.locked", UsnReason::RENAME_NEW_NAME, ts(0), 1000),
            make_record(101, "b.locked", UsnReason::RENAME_NEW_NAME, ts(1), 1100),
        ];

        let indicators = detect_known_ransomware_extensions(&records);
        // Only 2 .locked renames — below the 3-member threshold.
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_mass_rename_group_below_threshold_skips() {
        // detect_mass_rename_patterns: with 20+ total renames (passes the early
        // guard) but each unusual extension group having fewer than 20 members,
        // every group fails `group.len() >= 20` and falls through the `if` body.
        let mut records = Vec::new();
        for i in 0..25u64 {
            // Alternate between two distinct unusual extensions so neither group
            // reaches 20, while the overall rename count exceeds the 20 guard.
            let ext = if i % 2 == 0 { "aaa" } else { "bbb" };
            records.push(make_record(
                100 + i,
                &format!("file{i}.{ext}"),
                UsnReason::RENAME_NEW_NAME,
                ts(i as i64),
                1000 + (i as i64) * 100,
            ));
        }

        let indicators = detect_mass_rename_patterns(&records);
        // No single unusual-extension group reaches 20 members.
        assert!(indicators.is_empty());
    }

    #[test]
    fn test_timestomp_with_close_falls_through_when_empty() {
        // Companion to test_timestomp_basic_info_change_with_close: drive
        // detect_timestomping to an EMPTY result so the `if !indicators.is_empty()`
        // guard there evaluates false and falls through its closing brace.
        // A lone DATA_OVERWRITE (no BASIC_INFO_CHANGE) yields no indicators.
        let records = vec![make_record(
            100,
            "plain.bin",
            UsnReason::DATA_OVERWRITE,
            ts(1000),
            5000,
        )];

        let indicators = detect_timestomping(&records);
        assert!(indicators.is_empty());
    }
}