纯 MoonBit 实现的 Windows 注册表 hive(REGF)读取与写入库,不依赖 Windows API
moon add Xcsa-ee/moonregfimport {
"Xcsa-ee/moonregf",
}match @moonregf.parse_header(bytes) {
Ok(header) => println(header.file_name)
Err(error) => println(error.message())
}for entry in @moonregf.walk_key_tree(bytes, header.root_cell_offset, 3) {
println(entry.path)
}
match @moonregf.list_values(bytes, key) {
Ok(values) => for value in values { println(value.name + " = " + value.data_type.label()) }
Err(error) => println(error.message())
}let builder = @moonregf.HiveBuilder::new()
let software = builder.add_key(0, "Software")
builder.set_string(software, "Install", "Hello")
builder.set_dword(software, "Count", 42)
match builder.build() {
Ok(bytes) => println(bytes.length())
Err(error) => println(error.message())
}match @moonregf.parse_reg_bytes(raw) {
Ok(file) => match file.to_builder() {
Ok(builder) => builder.build()
Err(error) => println(error.message())
}
Err(error) => println(error.message())
}moon run cmd/main -- <path-to-NTUSER.DAT>
moon run cmd/main -- <path-to-NTUSER.DAT> --values
moon run cmd/main -- <path-to-NTUSER.DAT> --security
moon run cmd/main -- --build demo.hive
moon run cmd/main -- --import-reg backup.reg rebuilt.hive
moon run cmd/main -- --export-reg rebuilt.hive out.reg HKEY_CURRENT_USER
moon run cmd/main -- --replay-log dirty.hive dirty.hive.LOG1 clean.hive
moon run cmd/main -- --rebuild in.hive out.hiveI/O error: Failed to read file C:\Users\<你>\NTUSER.DAT: 另一个程序正在使用此文件,进程无法访问。 (os error 32)moon run cmd/main -- --build demo.hive
moon run cmd/main -- demo.hive
moon run cmd/main -- demo.hive --valueslet editor = @moonregf.HiveEditor::open(raw)
if let Some(key) = editor.find("ROOT/Software/Vendor") {
editor.set_dword(key, "Count", 7)
let _ = editor.delete_value(key, "Install")
let added = editor.add_key(key, "Inner")
editor.set_string(added, "Note", "added")
}
let bytes = editor.build()moon run examples/basic| 结构 | 读取 | 写入 | 说明 |
|---|---|---|---|
| base block | 是 | 是 | 校验和按前 508 字节逐 u32 异或重算 |
| hbin / cell | 是 | 是 | cell 长度含 4 字节头,按 8 字节对齐 |
| nk 键节点 | 是 | 是 | 读取支持压缩与 UTF-16 名称;写入按名称自动选压缩或 UTF-16LE |
| lf / lh / li | 是 | 是 | 按子键数与名称编码自动选择,也可显式指定 |
| ri 根索引 | 是 | 是 | 子键超过 500 个时自动生成,每片 500 项 |
| vk 值记录 | 是 | 是 | 类型码 1 / 2 / 3 / 4 / 7 / 11 已映射 |
| 内联数据 | 是 | 是 | 数据不超过 4 字节时直接存在 vk 里 |
| big data(db) | 是 | 是 | 读取按段拼接;写入按 16344 字节分段 |
| del(已删除键) | 是 | 不适用 | --orphans 扫出未被活动键树引用的 nk 与 del 记录 |
| sk 安全描述符 | 是 | 是 | 解析属主与属组 SID、ACL 是否存在;写入按给定描述符生成 sk 记录 |
| .reg 文本 | 是 | 是 | 导入支持 REGEDIT4 与 version 5.00;导出固定 version 5.00 |
| .reg 删除段 | 否 | 否 | 遇到 [-key] 返回 UnsupportedDelete |
| HvLE 事务日志 | 是 | 否 | 只解析 8.1 及更新版本;写入与生成日志不在范围内 |
pub(all) enum AceType {
AccessAllowed
AccessDenied
SystemAudit
SystemAlarm
AccessAllowedObject
AccessDeniedObject
SystemAuditObject
SystemAlarmObject
Unknown(Int)
}pub(all) struct EditableKey {
name : String
parent : Int
children : Array[Int]
values : Array[EditableValue]
high : UInt
low : UInt
access : UInt
flags : UInt
class_bytes : Bytes
security : Bytes
}pub struct HiveBuilder {
names : Array[String]
parents : Array[Int]
children : Array[Array[Int]]
values : Array[Array[BuildValue]]
kinds : Array[Int]
securities : Array[Bytes]
highs : Array[UInt]
lows : Array[UInt]
access : Array[UInt]
extras : Array[UInt]
classes : Array[Bytes]
file_name : String
sequence : Int
stamp_low : Int
stamp_high : Int
}fn HiveBuilder::set_header_metadata(self : HiveBuilder, sequence : Int, stamp_low : Int, stamp_high : Int) -> Unitfn HiveBuilder::set_key_metadata(self : HiveBuilder, key_index : Int, high : UInt, low : UInt, access : UInt, extra_flags : UInt) -> Unitfn HiveBuilder::set_string(self : HiveBuilder, key_index : Int, name : String, text : String) -> Unitfn HiveBuilder::set_value(self : HiveBuilder, key_index : Int, name : String, data_type : HiveValueType, data : Bytes) -> Unitpub(all) struct HiveCell {
offset : UInt
payload_size : Int
allocated : Bool
}pub struct HiveEditor {
keys : Array[EditableKey]
file_name : String
sequence : Int
stamp_low : Int
stamp_high : Int
skipped_values : Int
skipped_subkey_lists : Int
}fn HiveEditor::set_value(self : HiveEditor, key : Int, name : String, data_type : HiveValueType, data : Bytes) -> Unitpub(all) enum HiveFileType {
Primary
Log
External
Unknown(UInt)
}pub(all) struct HiveHeader {
primary_sequence : UInt
secondary_sequence : UInt
last_written_high : UInt
last_written_low : UInt
major_version : UInt
minor_version : UInt
file_type : HiveFileType
file_format : UInt
root_cell_offset : UInt
hive_bins_data_size : UInt
clustering_factor : UInt
file_name : String
checksum : UInt
}pub(all) struct HiveKeyEntry {
path : String
offset : UInt
subkey_count : Int
value_count : Int
}pub(all) struct HiveKeyNode {
offset : UInt
flags : UInt
last_written_high : UInt
last_written_low : UInt
access_bits : UInt
parent_offset : UInt
subkey_count : UInt
volatile_subkey_count : UInt
subkey_list_offset : UInt
volatile_subkey_list_offset : UInt
value_count : UInt
value_list_offset : UInt
security_offset : UInt
class_name_offset : UInt
name_length : UInt
class_name_length : UInt
name : String
}pub(all) struct HiveSubkeyList {
offset : UInt
kind : SubkeyListKind
count : Int
key_offsets : Array[UInt]
}pub(all) struct HiveValue {
offset : UInt
name : String
data_type : HiveValueType
data_size : UInt
data_offset : UInt
inline_data : Bool
data : Bytes
}pub(all) enum HiveValueType {
Nil
Sz
ExpandSz
Binary
Dword
DwordBigEndian
Link
MultiSz
ResourceList
FullResourceDescriptor
ResourceRequirementsList
Qword
Unknown(UInt)
}pub(all) enum IndexKind {
Auto
FastLeaf
HashLeaf
IndexLeaf
RootIndex
}pub(all) struct LogEntry {
sequence : UInt
bins_data_size : UInt
size : Int
pages : Array[LogPageRef]
data : Bytes
}pub(all) struct LogPageRef {
offset : UInt
size : Int
}pub(all) struct LogReplay {
bytes : Bytes
applied : Int
final_sequence : UInt
}pub(all) struct OrphanRecord {
offset : Int
signature : String
name : String
deleted : Bool
}pub(all) enum RegfError {
TooShort(Int)
BadSignature(String)
BadChecksum(UInt, UInt)
BadCellOffset(Int)
BadCellSize(Int)
BadKeyNode(Int)
BadSubkeyList(Int)
BadValue(Int)
BadValueData(Int)
UnsupportedName(Int)
ValueTooLarge(Int)
BadRegFile(String)
UnsupportedDelete(String)
NotALogFile(UInt)
UnsupportedLogVersion(UInt)
BadLogEntry(Int)
BadLogPage(Int)
BadSecurityDescriptor(String)
LossyHive(Int, Int)
}pub(all) enum SubkeyListKind {
FastLeaf
HashLeaf
IndexLeaf
RootIndex
Unknown(String)
}fn decode_single_byte(bytes : Bytes, offset : Int, count : Int) -> Stringfn decode_utf16le(bytes : Bytes, offset : Int, unit_count : Int) -> Stringfn read_u16_le(bytes : Bytes, offset : Int) -> UIntfn read_u32_le(bytes : Bytes, offset : Int) -> UIntfn sid_to_string(bytes : Bytes, offset : Int) -> StringInstall
Download zip纯 MoonBit 实现的 Windows 注册表 hive(REGF)读取与写入库,不依赖 Windows API