SecurityRonin/usnjrnl-forensic

GitHub: SecurityRonin/usnjrnl-forensic

最全面的 NTFS USN Journal 取证分析工具,实现完整路径重建、四源关联和反取证检测。

Stars: 4 | Forks: 0

# usnjrnl 取证 [![Crates.io](https://img.shields.io/crates/v/usnjrnl-forensic.svg)](https://crates.io/crates/usnjrnl-forensic) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Tests](https://img.shields.io/badge/tests-341-green.svg)](https://github.com/SecurityRonin/usnjrnl-forensic) 最全面的 NTFS USN Journal 取证分析工具。没有之一。 `usnjrnl-forensic` 解析 `$UsnJrnl:$J` 记录,通过 MFT 条目重用重建完整文件路径,关联四种 NTFS artifacts 以恢复被其他工具遗漏的证据,并通过内置取证规则检测攻击者活动。 ``` $ usnjrnl-forensic -j $J -m $MFT --mftmirr $MFTMirr --logfile $LogFile --csv timeline.csv [+] 847,293 USN records parsed [+] 112,448 MFT entries parsed [+] $MFTMirr is consistent with $MFT [+] 5,378 USN records recovered from $LogFile [+] 771 ghost records found in $LogFile (not present in $UsnJrnl) [!] 3 potential timestomping indicators [+] All paths fully resolved (0 UNKNOWN) ``` ## 安装 ### 作为 CLI 工具 ``` cargo install usnjrnl-forensic ``` 这将为您安装 `usnjrnl-forensic` 二进制文件。可在 Windows、macOS 和 Linux 上运行。无运行时依赖。 ### 作为库 添加到您的 `Cargo.toml`: ``` [dependencies] usnjrnl-forensic = "0.1" ``` 然后在您的代码中使用: ``` use usnjrnl_forensic::usn::{parse_usn_journal, UsnRecord, UsnReason}; use usnjrnl_forensic::rewind::RewindEngine; use usnjrnl_forensic::mft::MftData; // Parse USN journal records let data = std::fs::read("$J")?; let records = parse_usn_journal(&data); // Resolve full paths using MFT + Rewind let mft_data = MftData::parse(&mft_bytes)?; let mut engine = RewindEngine::new(); engine.seed_from_mft(&mft_data); let resolved = engine.resolve_all(records); ``` 可用模块:`usn`、`mft`、`rewind`、`logfile`、`mftmirr`、`correlation`、`analysis`、`rules`、`refs`、`monitor`、`output`。 完整的 CLI 参考实现包含在 crate 的 `src/main.rs` 中。 ### 从源码构建 ``` git clone https://github.com/SecurityRonin/usnjrnl-forensic cd usnjrnl-forensic cargo build --release ``` ## 用法 ### 基础:使用 MFT 路径解析解析 $UsnJrnl ``` usnjrnl-forensic -j $J -m $MFT --csv output.csv ``` ### 完整 QuadLink 分析:关联所有四个 artifacts ``` usnjrnl-forensic -j $J -m $MFT --mftmirr $MFTMirr --logfile $LogFile --sqlite analysis.db ``` ### 检测 Timestomping ``` usnjrnl-forensic -j $J -m $MFT --detect-timestomping ``` ### 一次性输出所有格式 ``` usnjrnl-forensic -j $J -m $MFT --csv out.csv --jsonl out.jsonl --sqlite out.db --body out.body --tln out.tln --xml out.xml ``` ### 仅日志模式(无 MFT) ``` usnjrnl-forensic -j $J --csv output.csv ``` 路径将不完整(仅包含父 MFT 条目号)。当 $MFT 不可用时很有用。 ## 为什么开发这个工具 市面上所有的 USN journal 解析器都有盲点。当 MFT 条目被重用时,MFTECmd 会产生“UNKNOWN”父路径。ntfs-linker 需要 C++ 编译且没有维护的构建版本。NTFS Log Tracker 仅能在 Windows 上运行。没有一个工具结合了学术论文和 DFIR 博客文章中记录的所有技术。 `usnjrnl-forensic` 填补了所有空白。它实现了 CyberCX Rewind 算法以实现 100% 的路径解析,四 artifact QuadLink 关联(扩展了 David Cowen 的 TriForce,增加了 $MFTMirr 完整性验证),并添加了现有工具无法提供的取证检测功能。 ## 功能对比 `usnjrnl-forensic` 与所有著名的 USN journal 工具(过去的和现在的)的对比。 ### 解析与文件系统支持 | Feature | usnjrnl-forensic | MFTECmd | ANJP | ntfs-linker | NTFS Log Tracker | dfir_ntfs | CyberCX Rewind | |---------|:-------:|:-------:|:----:|:-----------:|:----------------:|:---------:|:--------------:| | USN V2 parsing | Yes | Yes | Yes | Yes | Yes | Yes | Via MFTECmd | | USN V3 parsing | Yes | Yes | No | No | No | Yes | No | | USN V4 parsing | Yes | No | No | No | No | Yes | No | | ReFS support | Yes | No | No | No | No | No | No | ### 路径解析与关联 | Feature | usnjrnl-forensic | MFTECmd | ANJP | ntfs-linker | NTFS Log Tracker | dfir_ntfs | CyberCX Rewind | |---------|:-------:|:-------:|:----:|:-----------:|:----------------:|:---------:|:--------------:| | **Artifacts analyzed** | **4** | **1** | **3** | **3** | **3** | **3** | **1** | | MFT path resolution | Yes | Yes | Yes | Yes | Yes | Yes | Via MFTECmd | | Rewind (reused MFT entries) | Yes | No | No | No | No | No | Yes | | $LogFile USN extraction | Yes | No | Yes | Yes | Yes | Yes | No | | TriForce correlation | Yes | No | Yes | Yes | Partial | Partial | No | | Ghost record recovery | Yes | No | No | No | No | No | No | | $MFTMirr integrity check | Yes | No | No | No | No | No | No | ### 取证检测 | Feature | usnjrnl-forensic | MFTECmd | ANJP | ntfs-linker | NTFS Log Tracker | dfir_ntfs | CyberCX Rewind | |---------|:-------:|:-------:|:----:|:-----------:|:----------------:|:---------:|:--------------:| | Timestomping detection | Yes | No | No | Basic | Basic | No | No | | Anti-forensics detection | Yes | No | No | No | Basic | No | No | | Ransomware pattern detection | Yes | No | No | No | No | No | No | | USN record carving | Yes | No | No | No | Yes | No | No | | Custom rule engine | Yes | No | No | No | No | No | No | ### 性能与监控 | Feature | usnjrnl-forensic | MFTECmd | ANJP | ntfs-linker | NTFS Log Tracker | dfir_ntfs | CyberCX Rewind | |---------|:-------:|:-------:|:----:|:-----------:|:----------------:|:---------:|:--------------:| | Parallel processing | Yes | No | No | No | No | No | No | | Real-time monitoring | Yes | No | No | No | No | No | No | ### 输出格式 | Feature | usnjrnl-forensic | MFTECmd | ANJP | ntfs-linker | NTFS Log Tracker | dfir_ntfs | CyberCX Rewind | |---------|:-------:|:-------:|:----:|:-----------:|:----------------:|:---------:|:--------------:| | CSV | Yes | Yes | No | No | Yes | Yes | No | | JSON/JSONL | Yes | Yes | No | No | No | Yes | No | | SQLite | Yes | No | Yes | Yes | Yes | No | Yes | | Sleuthkit body | Yes | Yes | No | No | No | No | No | | TLN | Yes | No | No | No | No | No | No | | XML | Yes | No | No | No | No | No | No | ### 平台与实现 | | usnjrnl-forensic | MFTECmd | ANJP | ntfs-linker | NTFS Log Tracker | dfir_ntfs | CyberCX Rewind | |---------|:-------:|:-------:|:----:|:-----------:|:----------------:|:---------:|:--------------:| | Cross-platform | Yes | Yes | Windows | Linux | Windows | Yes | Yes | | Language | Rust | C# (.NET) | C++ | C++ | C# | Python | Python | | Open source | Yes | Yes | No | Yes | No | Yes | Yes | | Maintained (2024+) | Yes | Yes | No | No | No | Yes | Yes | ### 总结 | | usnjrnl-forensic | MFTECmd | ANJP | ntfs-linker | NTFS Log Tracker | dfir_ntfs | CyberCX Rewind | |---------|:-------:|:-------:|:----:|:-----------:|:----------------:|:---------:|:--------------:| | **Feature count** | **23/23** | **6/23** | **4/23** | **5/23** | **6/23** | **7/23** | **3/23** | 功能计数包括解析、路径解析、取证检测、性能和输出部分的所有行。Partial/Basic 算作一半。 ## 功能特性 ### 使用日志倒带 (Journal Rewind) 进行路径重建 标准工具根据当前 MFT 状态解析父目录引用。当 Windows 重用 MFT 条目号(将其分配给新文件)时,这些引用会断裂。结果:您的时间线中散布着“UNKNOWN”父路径。 Rewind 算法最初由 [CyberCX](https://cybercx.com.au/blog/ntfs-usnjrnl-rewind/) 描述,按时间倒序处理日志条目。它跟踪每个(条目,序列)到(文件名,父目录)的映射,重建每个时间点的目录树结构。结果:零个未知路径。 ``` Before Rewind: UNKNOWN\UNKNOWN\malware.exe After Rewind: .\Users\admin\AppData\Local\Temp\malware.exe ``` ### QuadLink 关联 `usnjrnl-forensic` 关联四种 NTFS artifacts —— 比任何其他工具都多: 1. **$UsnJrnl** 记录文件的创建、删除、重命名和数据更改 2. **$MFT** 存储带有时间戳的当前文件系统状态 3. **$LogFile** 包含嵌入了 USN 记录的事务日志 4. **$MFTMirr** 镜像前四个关键 MFT 条目以进行完整性验证 这建立在 [TriForce](https://www.hecfblog.com/2013/01/ntfs-triforce-deeper-look-inside.html) 技术(David Cowen,2013)之上,该技术开创了 $MFT + $LogFile + $UsnJrnl 的三 artifact 关联。`usnjrnl-forensic` 实现了完整的 TriForce 方法,并添加 $MFTMirr 作为第四个 artifact:$MFT 与其镜像之间的字节级比较,用于检测对关键系统元数据条目($MFT、$MFTMirr、$LogFile、$Volume)的篡改 —— 这是其他工具无法执行的一致性检查。 $LogFile 在其 RCRD 页中包含近期 USN 记录的副本。`usnjrnl-forensic` 提取这些记录,将它们与日志交叉引用,并将仅存在于 $LogFile 中的记录标记为“幽灵记录”(ghost records)。幽灵记录的出现有两个原因: 1. **正常的日志循环**:$UsnJrnl 具有固定大小。随着写入新记录,旧记录会被覆盖。$LogFile 具有不同的轮换周期,通常保留日志已经循环过的 USN 记录。 2. **故意清除日志**:攻击者运行 `fsutil usn deletejournal` 或类似命令。$LogFile 仍然保存着已销毁记录的副本。 这两种情况都会产生幽灵记录。调查人员必须审查时间戳和上下文以确定适用哪种场景。当幽灵记录包含攻击性工具文件名或集中在可疑时间范围内时,更有可能是被清除。 ``` [+] 5378 USN records recovered from $LogFile [+] 771 ghost records found in $LogFile (not present in $UsnJrnl) Ghost records appear when $LogFile retains USN records that $UsnJrnl has cycled past (normal wrapping) or that were deliberately cleared. [!] NOTE: $LogFile contains records OLDER than the oldest $UsnJrnl entry. This is consistent with journal wrapping or intentional journal clearing. Review ghost record timestamps and context to determine which. [+] $MFTMirr is consistent with $MFT ``` 或者当检测到篡改时: ``` [!] $MFTMirr INCONSISTENCY DETECTED: Entry 2 ($LogFile): 14 byte differences ``` ### 反取证检测 内置检测器识别四类可疑活动: **安全删除** (SDelete, CCleaner):检测这些工具在删除前使用的文件重命名模式。SDelete 在清零和删除文件之前,将文件重命名为重复字符序列(AAAA, BBBB, ..., ZZZZ)。 **日志清除**:标记 $LogFile 页中的间隙和表明使用了 `fsutil usn deletejournal` 或类似命令的幽灵记录。 **勒索软件**:在短时间窗口内识别具有已知勒索软件扩展名(.encrypted, .locked, .crypto)的大量文件重命名/删除模式。 **时间戳篡改 (Timestomping)**:根据 $FILE_NAME 时间戳和 USN journal FILE_CREATE 事件交叉验证 $STANDARD_INFORMATION 时间戳。当 SI_Created 早于该文件在日志中的第一条记录时,说明时间戳被倒签了。 ### 自定义规则引擎 定义您自己的检测规则,匹配文件名、扩展名、原因标志或其组合: ``` use usnjrnl_forensic::rules::{Rule, RuleSet, Severity, FilenameMatch}; use usnjrnl_forensic::usn::UsnReason; let mut rules = RuleSet::with_builtins(); // 5 pre-loaded forensic rules rules.add_rule(Rule { name: "lateral_movement_tools".into(), description: "Known lateral movement binaries".into(), severity: Severity::Critical, filename_match: Some(FilenameMatch::Regex( r"(?i)(psexec|wmiexec|smbexec|atexec)".into() )), exclude_pattern: None, any_reasons: Some(UsnReason::FILE_CREATE), all_reasons: None, }); ``` 内置规则检测: - 攻击性工具 (mimikatz, psexec, procdump, lazagne, rubeus, sharphound) - 勒索软件文件扩展名 - SDelete 安全删除模式 - 脚本执行 (.ps1, .vbs, .bat, .cmd, .js) - 凭据访问 (ntds.dit, SAM, SECURITY, SYSTEM) ### ReFS 支持 ReFS 卷使用 128 位文件引用号,而不是 NTFS 的 48+16 位格式。`usnjrnl-forensic` 保留完整的 128 位标识符,从 V3 记录模式自动检测 ReFS 卷,并使用仅日志倒带重建路径(ReFS 没有传统的 MFT 可供种子填充)。 ### 并行处理 对于大型日志(500MB+),`usnjrnl-forensic` 使用 rayon 将数据拆分为块并在所有 CPU 核心上解析它们。结果按 USN 偏移顺序合并,以实现确定性输出。 ### 实时监控 监控模块提供 `JournalSource` trait,用于在 Windows 上轮询实时 USN journals。它跟踪最后读取的 USN 位置,检测日志循环,并为每条新记录发出结构化事件。 ``` use usnjrnl_forensic::monitor::{JournalMonitor, MonitorConfig, MonitorEvent}; // Your JournalSource implementation reads from FSCTL_READ_USN_JOURNAL let monitor = JournalMonitor::new(source, MonitorConfig::default())?; for event in monitor.poll_once() { match event { MonitorEvent::NewRecord(record) => { /* process */ }, MonitorEvent::JournalWrap { old_usn, new_usn } => { /* handle wrap */ }, MonitorEvent::Error(msg) => { /* log error */ }, } } ``` ## 输出格式 | Format | Flag | Description | |--------|------|-------------| | CSV | `--csv` | MFTECmd-compatible columns | | Lines | `--jsonl` | One JSON object per line | | SQLite | `--sqlite` | Indexed database with USNJRNL_FullPaths and MFT tables | | Sleuthkit body | `--body` | Pipe-delimited, feeds into `mactime` and `log2timeline` | | TLN | `--tln` | 5-field pipe-delimited timeline format | | XML | `--xml` | Structured XML with full record fields | 所有格式包括:时间戳、USN 偏移、MFT 条目/序列、父条目/序列、解析的父路径、文件名、扩展名、文件属性、原因标志、源信息、安全 ID 和记录版本。 ## 架构 ``` graph LR J["$UsnJrnl:$J"] --> USN["USN Parser
V2 / V3 / V4"] USN --> Rewind["Rewind Engine"] MFT["$MFT"] --> MFTP["MFT Parser"] MFTP --> Rewind Rewind --> Resolved["Resolved Records"] Resolved --> Output["Output
CSV · JSONL · SQLite
Body · TLN · XML"] Resolved --> Rules["Rule Engine"] Resolved --> Analysis["Analysis
SDelete · Ransomware
Timestomping"] LF["$LogFile"] --> LFP["LogFile Parser"] LFP --> Extract["USN Extractor"] Extract --> Corr["Correlation Engine"] Resolved --> Corr MFTP --> Corr Corr --> Report["QuadLink Report
Ghost Records · Timeline
Journal Clearing"] Mirror["$MFTMirr"] --> Integrity["MFTMirr
Integrity Check"] MFT --> Integrity ``` 模块: - `usn`: USN_RECORD_V2, V3, V4 的二进制解析,支持流式和并行模式 - `mft`: $MFT 条目提取,包含 SI/FN 时间戳对 - `rewind`: 用于路径重建的 CyberCX Rewind 算法 - `logfile`: $LogFile RCRD 页分析和嵌入式 USN 提取 - `mftmirr`: $MFTMirr 完整性验证 - `correlation`: 连接所有四个 artifacts 的 QuadLink 引擎 - `analysis`: 反取证检测(安全删除、勒索软件、时间戳篡改、日志清除) - `rules`: 具有通配符、正则表达式和原因标志条件的模式匹配规则引擎 - `refs`: ReFS 128 位文件 ID 处理 - `monitor`: 实时日志轮询抽象 - `output`: CSV, JSONL, SQLite, Sleuthkit body, TLN, XML 导出器 ## 测试 341 个单元测试覆盖每个模块。运行方式: ``` cargo test ``` ## 参考资料 - [CyberCX: Rewriting the USN Journal](https://cybercx.com.au/blog/ntfs-usnjrnl-rewind/) (Rewind algorithm) - [NTFS TriForce](https://www.hecfblog.com/2013/01/ntfs-triforce-deeper-look-inside.html) (David Cowen, $MFT + $LogFile + $UsnJrnl correlation) - [ntfs-linker](https://github.com/strozfriedberg/ntfs-linker) (Stroz Friedberg, $LogFile USN extraction) - [MFTECmd](https://github.com/EricZimmerman/MFTECmd) (Eric Zimmerman, MFT/USN parsing) - [dfir_ntfs](https://github.com/msuhanov/dfir_ntfs) (Maxim Suhanov, Python NTFS parser) - [Microsoft USN_RECORD_V2](https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ns-winioctl-usn_record_v2) - [Microsoft USN_RECORD_V3](https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ns-winioctl-usn_record_v3) - [Microsoft USN_RECORD_V4](https://learn.microsoft.com/en-us/windows/win32/api/winioctl/ns-winioctl-usn_record_v4) - [Forensic Analysis of ReFS Journaling](https://dfrws.org/wp-content/uploads/2021/01/2021_APAC_paper-forensic_analysis_of_refs_journaling.pdf) (DFRWS APAC 2021) ## 致谢 这个工具站在巨人的肩膀上。 Rewind 算法由 [CyberCX](https://cybercx.com.au/) 的 [Yogesh Khatri](https://www.linkedin.com/in/ydkhatri/) 于 2024 年 4 月描述。他的见解简单而优雅:倒序遍历日志,跟踪遇到的每个父子关系。不再有 UNKNOWN。他的博文和 Python 概念验证在此:[cybercx.com.au/blog/ntfs-usnjrnl-rewind](https://cybercx.com.au/blog/ntfs-usnjrnl-rewind/) TriForce 关联技术来自 [David Cowen](https://www.linkedin.com/in/dcowen/)(2013),他表明 $MFT + $LogFile + $UsnJrnl 结合起来揭示的信息远超任何单一的 artifact。[hecfblog.com/2013/01/ntfs-triforce-deeper-look-inside.html](https://www.hecfblog.com/2013/01/ntfs-triforce-deeper-look-inside.html) $LogFile USN 提取方法借鉴了 [Stroz Friedberg](https://www.strozfriedberg.com/)(一家 LevelBlue 公司)的工作,以及 [ntfs-linker](https://github.com/strozfriedberg/ntfs-linker)。 [Eric Zimmerman](https://www.linkedin.com/in/eric-zimmerman-6965b22/) 的 [MFTECmd](https://github.com/EricZimmerman/MFTECmd) 树立了整个 DFIR 社区所依赖的 NTFS artifact 解析标准。 `usnjrnl-forensic` 采纳了这些想法,用 Rust 实现以确保速度和正确性,并添加了缺失的部分:$MFTMirr 完整性验证、幽灵记录恢复、反取证检测和规则引擎。 如果没有前人的研究,这一切都不存在。 ## 作者 **Albert Hui** ([@h4x0r](https://github.com/h4x0r)) 来自 [@SecurityRonin](https://github.com/SecurityRonin) 数字取证从业者兼工具开发者。致力于构建填补商业软件空白的开源 DFIR 工具。 ## 许可证 MIT
标签:HTTPS请求, Linux安全, LogFile, Master File Table, MFT, NTFS, Rust, Timestomping, USN Carving, USN Journal, Windows取证, WSL, 反取证检测, 可视化界面, 幽灵记录, 数字取证, 数据关联, 数据恢复, 文件系统, 时间戳篡改, 网络流量审计, 自动化脚本, 通知系统, 通知系统, 黄金证书