Skip to main content

winreg_artifacts/
catalog_scan.rs

1//! Catalog-driven registry artifact scanner.
2//!
3//! The artifact *knowledge* — which keys matter, what they mean, and how to
4//! decode them — comes entirely from [`forensicnomicon`]'s registry catalog,
5//! never from constants hardcoded here. This module is the thin resolver that
6//! walks an open [`Hive`], looks up every catalog descriptor whose hive matches
7//! the hive under analysis, opens the descriptor's key, and emits the decoded
8//! value(s).
9//!
10//! winreg-core owns the registry-specific byte mechanics (REG_SZ is UTF-16LE on
11//! disk, REG_DWORD is little-endian, …); the catalog owns the *meaning*. The two
12//! meet here: the catalog's [`Decoder`] selects how winreg-core renders the
13//! bytes, and the catalog supplies the path, label, MITRE mapping, and id.
14//!
15//! ## Scope and catalog quirks
16//!
17//! Some catalog `key_path` values are not directly resolvable against an offline
18//! hive and are skipped (they simply produce no hit):
19//!
20//! - **Wildcards** (`*`, `**`) — the descriptor matches a family of keys, not a
21//!   single key. Glob expansion is out of scope for this resolver.
22//! - **SID / variable placeholders** (`%%users.sid%%`, `HKEY_USERS\…`) — the
23//!   Velociraptor/forensic-artifacts-sourced descriptors carry live-system
24//!   placeholders with no offline-hive equivalent.
25//!
26//! Two normalizations are applied so curated descriptors resolve cleanly:
27//!
28//! - A redundant leading hive prefix (`HKLM\`, `HKCU\`, or a leading `SOFTWARE\`
29//!   / `SYSTEM\` that merely repeats the hive name) is stripped — catalog paths
30//!   are nominally hive-relative, but some entries repeat the hive.
31//! - `CurrentControlSet` (the SYSTEM-hive symlink the live registry resolves) is
32//!   expanded by the [`crate::path_expansion`] engine to whichever
33//!   `ControlSet00N` the hive's `Select\Current` names — not assumed to be 001.
34//!
35//! Glob (`*`/`**`), control-set, and multi-user resolution all route through the
36//! single [`crate::path_expansion::expand`] engine: each is a template with one
37//! or more variable segments ranging over a domain, expanded to concrete paths
38//! tagged with [`crate::path_expansion::Binding`]s for provenance.
39//!
40//! Complex binary artifacts (UserAssist, Shimcache/AppCompatCache, Amcache,
41//! ShellBags, SAM) keep their dedicated decoders in the sibling modules; this
42//! scanner flags such hits via [`CatalogHit::needs_specialized_decoder`] and
43//! renders a best-effort placeholder, so callers can route to the right module.
44
45use std::io::Cursor;
46use std::path::Path;
47
48use forensicnomicon::catalog::{ArtifactDescriptor, ArtifactType, Decoder, HiveTarget, CATALOG};
49use winreg_core::detect::HiveType;
50use winreg_core::hive::Hive;
51use winreg_core::key::{filetime_to_datetime, Key};
52use winreg_core::value::{decode_multi_sz, decode_utf16le, Value};
53
54use crate::path_expansion::{
55    expand, resolve_control_sets, Binding, ControlSetResolver, Segment, Wildcard,
56};
57
58/// A single decoded artifact value surfaced by the catalog-driven scan.
59#[derive(Debug, Clone, serde::Serialize)]
60pub struct CatalogHit {
61    /// The catalog descriptor id that produced this hit (e.g. `"run_key_hklm"`).
62    pub catalog_id: &'static str,
63    /// Human-readable artifact name from the catalog.
64    pub artifact_name: &'static str,
65    /// Forensic meaning / significance from the catalog.
66    pub meaning: &'static str,
67    /// Registry key path actually opened (post-normalization, hive-relative).
68    pub key_path: String,
69    /// Value name, or `None` for a key-level descriptor's default value.
70    pub value_name: Option<String>,
71    /// Decoded value rendered as a string per the descriptor's decoder.
72    pub value_data: String,
73    /// MITRE ATT&CK techniques associated with the artifact (catalog-supplied).
74    pub mitre_techniques: &'static [&'static str],
75    /// `true` when the artifact needs one of the specialized binary decoders
76    /// (UserAssist, Shimcache, …) rather than this generic value renderer.
77    pub needs_specialized_decoder: bool,
78    /// The user this hit is attributed to, or `None` for machine-wide hives
79    /// (SYSTEM/SOFTWARE/SAM/SECURITY) scanned via [`scan`].
80    ///
81    /// Derived from this hit's [`Wildcard::User`] binding (when present); kept as
82    /// a distinct field so existing callers continue to work unchanged.
83    pub user: Option<UserIdentity>,
84    /// Every variable resolution that produced this hit, for provenance — the
85    /// expanded subkey name(s), the active `ControlSet00N`, and/or the user.
86    pub bindings: Vec<Binding>,
87}
88
89/// Identity of the user a per-user [`CatalogHit`] is attributed to.
90///
91/// Offline, a per-user artifact lives in one user's `NTUSER.DAT` / `UsrClass.dat`.
92/// At least one of `profile` / `sid` is populated; both may be present when the
93/// caller could resolve the SID (e.g. from `ProfileList` or the hive path).
94#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
95pub struct UserIdentity {
96    /// Profile/account name, typically the profile directory name (e.g. `"alice"`).
97    pub profile: Option<String>,
98    /// Security identifier (e.g. `"S-1-5-21-…-1001"`) when known.
99    pub sid: Option<String>,
100}
101
102/// Scan an open hive against the forensicnomicon registry catalog.
103///
104/// Only descriptors whose hive matches the hive under analysis are resolved.
105/// Descriptors whose key path is not present, or is a wildcard / SID-placeholder
106/// path, simply produce no hit.
107#[must_use]
108pub fn scan(hive: &Hive<Cursor<Vec<u8>>>) -> Vec<CatalogHit> {
109    let Some(target) = hive_target_for(hive.detect_hive_type()) else {
110        return Vec::new();
111    };
112
113    let mut hits = Vec::new();
114    let Ok(root) = hive.root_key() else {
115        return hits;
116    };
117    // HKLM SOFTWARE/SYSTEM paths sometimes repeat the hive name as a leading
118    // `SOFTWARE\`/`SYSTEM\`; that redundancy is stripped only for those hives.
119    let strip_hive_root = matches!(target, HiveTarget::HklmSoftware | HiveTarget::HklmSystem);
120    // The `CurrentControlSet` alias resolves to whichever set `Select\Current`
121    // names — only meaningful for the SYSTEM hive.
122    let control_sets = (target == HiveTarget::HklmSystem).then(|| resolve_control_sets(&root));
123    for descriptor in CATALOG.list() {
124        if !is_registry(descriptor.artifact_type) {
125            continue;
126        }
127        if descriptor.hive != Some(target) {
128            continue;
129        }
130        resolve_descriptor(
131            &root,
132            descriptor,
133            descriptor.key_path,
134            strip_hive_root,
135            control_sets.as_ref(),
136            &[],
137            &mut hits,
138        );
139    }
140    hits
141}
142
143/// A user's registry hive paired with the identity it belongs to.
144///
145/// Built by the caller (or [`discover_user_hives`]) for each `NTUSER.DAT` /
146/// `UsrClass.dat` found under a mounted image's profile root.
147pub struct UserHive {
148    /// Who this hive belongs to (profile name and/or SID).
149    pub identity: UserIdentity,
150    /// The opened per-user hive.
151    pub hive: Hive<Cursor<Vec<u8>>>,
152}
153
154/// Scan a set of per-user hives against the catalog, attributing every hit to
155/// the user it came from.
156///
157/// For each hive this applies:
158/// - the `NtUser` / `UsrClass` hive-tagged descriptors matching the hive's
159///   detected type, and
160/// - the `hive: None` registry descriptors whose path carries a live-system
161///   per-user placeholder (`HKEY_USERS\%%users.sid%%\…`, `HKU\*\…`) — offline,
162///   the placeholder segment *is* the user, so the remainder resolves against
163///   this user's hive root.
164///
165/// Every resulting [`CatalogHit`] carries `user = Some(identity)`. Machine
166/// hives (SYSTEM/SOFTWARE/SAM/SECURITY) are handled by [`scan`] instead and are
167/// unaffected.
168#[must_use]
169pub fn scan_users(user_hives: &[UserHive]) -> Vec<CatalogHit> {
170    let mut hits = Vec::new();
171    for uh in user_hives {
172        let target = hive_target_for(uh.hive.detect_hive_type());
173        let Ok(root) = uh.hive.root_key() else {
174            continue;
175        };
176        // The `User` domain binding: this hive *is* the user, so every hit it
177        // produces is tagged with the SID (preferred) or profile name.
178        let user_binding = user_binding_for(&uh.identity);
179        for descriptor in CATALOG.list() {
180            if !is_registry(descriptor.artifact_type) {
181                continue;
182            }
183            // Hive-tagged per-user descriptor whose target matches this hive.
184            let raw_path = if descriptor.hive == target {
185                Some(descriptor.key_path)
186            } else if descriptor.hive.is_none() || descriptor.hive == Some(HiveTarget::None) {
187                // Untagged descriptor that addresses a user via an HKU placeholder.
188                strip_user_placeholder_prefix(descriptor.key_path)
189            } else {
190                None
191            };
192            if let Some(path) = raw_path {
193                // Per-user hives keep `Software\…` literally — never strip it.
194                resolve_descriptor(
195                    &root,
196                    descriptor,
197                    path,
198                    false,
199                    None,
200                    user_binding.as_slice(),
201                    &mut hits,
202                );
203            }
204        }
205        // Backfill the legacy `user` field on this user's hits (the engine only
206        // carries it as a binding).
207        for hit in &mut hits {
208            if hit.user.is_none() && hit.bindings.iter().any(|b| b.kind == Wildcard::User) {
209                hit.user = Some(uh.identity.clone());
210            }
211        }
212    }
213    hits
214}
215
216/// The `User`-domain binding for an identity: the SID when known, else the
217/// profile name. Empty (no binding) only if the identity carries neither.
218fn user_binding_for(identity: &UserIdentity) -> Vec<Binding> {
219    let value = identity.sid.clone().or_else(|| identity.profile.clone());
220    match value {
221        Some(v) => vec![Binding::new(Wildcard::User, v)],
222        None => Vec::new(),
223    }
224}
225
226/// Discover every per-user hive under a mounted-image root and open it into a
227/// profile-tagged [`UserHive`], ready for [`scan_users`].
228///
229/// Delegates the filesystem walk to [`winreg_discover::discover_hives`], then
230/// keeps only the `NTUSER.DAT` / `UsrClass.dat` sources, opening each and
231/// deriving the profile name from its `Users/<name>/…` path. A hive that fails
232/// to open (truncated, wrong format) is skipped rather than aborting the scan.
233///
234/// The SID is left `None` here — it is not recoverable from the profile path
235/// alone; a caller that has the SOFTWARE hive's `ProfileList` can fill it in.
236#[must_use]
237pub fn discover_user_hives(evidence_root: &Path) -> Vec<UserHive> {
238    let mut out = Vec::new();
239    for source in winreg_discover::discover_hives(evidence_root) {
240        if !matches!(source.hive_type, HiveType::NtUser | HiveType::UsrClass) {
241            continue;
242        }
243        let Ok(hive) = Hive::from_path(&source.path) else {
244            continue;
245        };
246        out.push(UserHive {
247            identity: UserIdentity {
248                profile: profile_name_from_path(&source.path),
249                sid: None,
250            },
251            hive,
252        });
253    }
254    out
255}
256
257/// Derive the profile/account name from a `…/Users/<name>/…` hive path.
258fn profile_name_from_path(path: &Path) -> Option<String> {
259    let components: Vec<String> = path
260        .components()
261        .map(|c| c.as_os_str().to_string_lossy().to_string())
262        .collect();
263    let idx = components
264        .iter()
265        .position(|c| c.eq_ignore_ascii_case("Users"))?;
266    components.get(idx + 1).cloned()
267}
268
269/// Strip a live-system per-user root prefix (`HKEY_USERS\<sid>\` or `HKU\<sid>\`)
270/// from a descriptor path, returning the user-hive-relative remainder.
271///
272/// The `<sid>` segment is the SID placeholder the descriptor uses to address a
273/// specific user (`%%users.sid%%`, `*`, or a literal SID); offline that segment
274/// selects *which* hive, so we drop it and resolve the rest against the user's
275/// own hive root. Returns `None` if the path does not start with such a root.
276fn strip_user_placeholder_prefix(raw: &str) -> Option<&str> {
277    let rest = strip_prefix_ci(raw, "HKEY_USERS\\").or_else(|| strip_prefix_ci(raw, "HKU\\"))?;
278    // Drop the next segment (the SID / placeholder) and keep the remainder.
279    let (_sid_segment, remainder) = rest.split_once('\\')?;
280    if remainder.is_empty() {
281        None
282    } else {
283        Some(remainder)
284    }
285}
286
287/// Resolve a single descriptor against an already-open key tree rooted at
288/// `root`, routing it through the unified [`expand`] engine and pushing every
289/// produced [`CatalogHit`] onto `hits`.
290///
291/// `raw_path` is taken explicitly rather than read from `descriptor.key_path` so
292/// the multi-user scan can feed a SID-placeholder-stripped, hive-relative path
293/// while still attributing the hit to the original descriptor.
294///
295/// `control_sets` supplies the active `ControlSet00N` for any `CurrentControlSet`
296/// segment (SYSTEM hive only); `prefix_bindings` carries cross-file bindings the
297/// engine cannot derive itself — currently the per-user [`Wildcard::User`]
298/// binding from the multi-user scan.
299fn resolve_descriptor(
300    root: &Key<'_>,
301    descriptor: &ArtifactDescriptor,
302    raw_path: &str,
303    strip_hive_root: bool,
304    control_sets: Option<&ControlSetResolver>,
305    prefix_bindings: &[Binding],
306    hits: &mut Vec<CatalogHit>,
307) {
308    let Some(segments) = template_segments(raw_path, strip_hive_root) else {
309        return;
310    };
311    expand(root, &segments, control_sets, &mut |bindings, path, key| {
312        let mut all: Vec<Binding> = prefix_bindings.to_vec();
313        all.extend_from_slice(bindings);
314        emit_key(descriptor, path, key, &all, hits);
315    });
316}
317
318/// Emit the descriptor's value(s) for one concrete, already-opened key.
319fn emit_key(
320    descriptor: &ArtifactDescriptor,
321    key_path: &str,
322    key: &Key<'_>,
323    bindings: &[Binding],
324    hits: &mut Vec<CatalogHit>,
325) {
326    if let Some(vname) = descriptor.value_name {
327        // Single named value.
328        if let Ok(Some(val)) = key.value(vname) {
329            hits.push(make_hit(
330                descriptor,
331                key_path,
332                Some(vname.to_string()),
333                &val,
334                bindings,
335            ));
336        }
337    } else {
338        // Key-level descriptor: every child value is a hit.
339        let Ok(values) = key.values() else { return };
340        for val in values {
341            hits.push(make_hit(
342                descriptor,
343                key_path,
344                Some(val.name()),
345                &val,
346                bindings,
347            ));
348        }
349    }
350}
351
352/// Map winreg-core's detected hive type to a forensicnomicon hive target.
353fn hive_target_for(hive_type: HiveType) -> Option<HiveTarget> {
354    match hive_type {
355        HiveType::Software => Some(HiveTarget::HklmSoftware),
356        HiveType::System => Some(HiveTarget::HklmSystem),
357        HiveType::NtUser => Some(HiveTarget::NtUser),
358        HiveType::UsrClass => Some(HiveTarget::UsrClass),
359        HiveType::Sam => Some(HiveTarget::HklmSam),
360        HiveType::Security => Some(HiveTarget::HklmSecurity),
361        HiveType::Amcache => Some(HiveTarget::Amcache),
362        _ => None,
363    }
364}
365
366fn is_registry(at: ArtifactType) -> bool {
367    matches!(at, ArtifactType::RegistryKey | ArtifactType::RegistryValue)
368}
369
370/// Normalize a catalog key path into hive-relative expansion [`Segment`]s, or
371/// `None` if the path carries a live-system variable placeholder (`%`) or an
372/// unsupported separator/root the offline resolver cannot map.
373///
374/// This is the single entry the unified engine consumes: concrete paths become
375/// all-`Literal` templates (expanded to a single key), `*`/`**` segments become
376/// [`Wildcard::Subkey`] variables, and a leading `CurrentControlSet` becomes a
377/// [`Wildcard::ControlSet`] variable resolved via `Select\Current`.
378///
379/// The catalog stores backslash separators; some forensic-artifacts-sourced
380/// entries carry doubled backslashes (`\\`) as ordinary string contents — those
381/// are collapsed in [`normalize_path_prefixes`].
382fn template_segments(raw: &str, strip_hive_root: bool) -> Option<Vec<Segment>> {
383    // Live-system SID placeholders (`%`) and POSIX separators are out of scope.
384    if raw.contains('%') || raw.contains('/') {
385        return None;
386    }
387    let normalized = normalize_path_prefixes(raw, strip_hive_root)?;
388    let segments: Vec<Segment> = normalized
389        .split('\\')
390        .filter(|s| !s.is_empty())
391        .map(parse_segment)
392        .collect();
393    if segments.is_empty() {
394        None
395    } else {
396        Some(segments)
397    }
398}
399
400/// Classify one raw path component into an expansion [`Segment`].
401fn parse_segment(seg: &str) -> Segment {
402    if seg.eq_ignore_ascii_case("CurrentControlSet") {
403        // The SYSTEM-hive symlink — a variable over the active `ControlSet00N`.
404        Segment::Variable(Wildcard::ControlSet, seg.to_string())
405    } else if seg.contains('*') {
406        // `*` / `**` (incl. forensic-artifacts repeat suffixes like `**5`) — a
407        // variable over the subkeys of the current node.
408        Segment::Variable(Wildcard::Subkey, seg.to_string())
409    } else {
410        Segment::Literal(seg.to_string())
411    }
412}
413
414/// Apply the hive-prefix / doubled-backslash normalizations shared by every
415/// template, returning the hive-relative path string (or `None` for an
416/// unsupported placeholder root or empty result). Wildcard and
417/// `CurrentControlSet` segments are preserved verbatim for [`parse_segment`].
418///
419/// `strip_hive_root` controls whether a leading `SOFTWARE\` / `SYSTEM\` (which
420/// merely repeats an HKLM hive name) is dropped. It must be `true` for HKLM
421/// SOFTWARE/SYSTEM hives but `false` for per-user (`NtUser`/`UsrClass`) hives,
422/// where `Software` is a genuine first-level subkey, not a redundant prefix.
423fn normalize_path_prefixes(raw: &str, strip_hive_root: bool) -> Option<String> {
424    // Collapse any doubled backslashes to single separators.
425    let collapsed = raw.replace("\\\\", "\\");
426
427    // Drop a leading hive-name prefix that merely repeats the hive.
428    let mut path = collapsed.as_str();
429    for prefix in [
430        "HKEY_LOCAL_MACHINE\\",
431        "HKEY_CURRENT_USER\\",
432        "HKEY_USERS\\",
433        "HKLM\\",
434        "HKCU\\",
435        "HKU\\",
436    ] {
437        if let Some(stripped) = strip_prefix_ci(path, prefix) {
438            path = stripped;
439        }
440    }
441    // An `HK*`-prefixed path that wasn't stripped is a placeholder form we skip.
442    if path.starts_with("HK") && path.contains('\\') && looks_like_hive_root(path) {
443        return None;
444    }
445    // Strip a redundant leading SOFTWARE\ or SYSTEM\ that repeats the hive root
446    // — only for the HKLM hives where it is a duplicate, never for user hives.
447    if strip_hive_root {
448        for prefix in ["SOFTWARE\\", "SYSTEM\\"] {
449            if let Some(stripped) = strip_prefix_ci(path, prefix) {
450                path = stripped;
451            }
452        }
453    }
454
455    if path.is_empty() {
456        None
457    } else {
458        Some(path.to_string())
459    }
460}
461
462/// Case-insensitive prefix strip on `\`-delimited registry paths.
463fn strip_prefix_ci<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
464    if s.len() >= prefix.len() && s[..prefix.len()].eq_ignore_ascii_case(prefix) {
465        Some(&s[prefix.len()..])
466    } else {
467        None
468    }
469}
470
471/// Heuristic: the first segment looks like an `HKEY_*` root that survived
472/// prefix-stripping (i.e. an unsupported placeholder root).
473fn looks_like_hive_root(path: &str) -> bool {
474    path.split('\\')
475        .next()
476        .is_some_and(|seg| seg.eq_ignore_ascii_case("HKEY_USERS") || seg.starts_with("HKEY_"))
477}
478
479/// Build a [`CatalogHit`], rendering the value per the descriptor's decoder.
480fn make_hit(
481    descriptor: &ArtifactDescriptor,
482    key_path: &str,
483    value_name: Option<String>,
484    val: &Value<'_>,
485    bindings: &[Binding],
486) -> CatalogHit {
487    let (value_data, specialized) = render_value(descriptor.decoder, val);
488    CatalogHit {
489        catalog_id: descriptor.id,
490        artifact_name: descriptor.name,
491        meaning: descriptor.meaning,
492        key_path: key_path.to_string(),
493        value_name,
494        value_data,
495        mitre_techniques: descriptor.mitre_techniques,
496        needs_specialized_decoder: specialized,
497        // The multi-user scan backfills this from the matching `User` binding;
498        // machine scans leave it `None`.
499        user: None,
500        bindings: bindings.to_vec(),
501    }
502}
503
504/// Render a registry value to a display string using the catalog's decoder to
505/// select the interpretation, and winreg-core for the registry byte mechanics.
506///
507/// Returns `(rendered, needs_specialized_decoder)`.
508fn render_value(decoder: Decoder, val: &Value<'_>) -> (String, bool) {
509    let raw = val.raw_data().unwrap_or_default();
510    match decoder {
511        // REG_SZ / REG_EXPAND_SZ text — UTF-16LE on disk.
512        Decoder::Identity | Decoder::Utf16Le => (decode_utf16le(&raw), false),
513        Decoder::DwordLe => (val.as_u32().unwrap_or(0).to_string(), false),
514        Decoder::MultiSz => (decode_multi_sz(&raw).join("; "), false),
515        Decoder::FiletimeAt { offset } => {
516            let ts = raw
517                .get(offset..offset + 8)
518                .map(|b| winreg_core::bytes::le_u64(b, 0))
519                .and_then(filetime_to_datetime)
520                .map(|dt| dt.format("%Y-%m-%dT%H:%M:%SZ").to_string());
521            (ts.unwrap_or_default(), false)
522        }
523        // Binary record / ROT13 / ESE artifacts have dedicated decoders elsewhere.
524        Decoder::Rot13Name
525        | Decoder::Rot13NameWithBinaryValue(_)
526        | Decoder::BinaryRecord(_)
527        | Decoder::MruListEx
528        | Decoder::EseDatabase
529        | Decoder::PipeDelimited { .. } => {
530            // Best-effort: surface the raw value as text so the hit is not empty,
531            // and flag that a specialized decoder should be consulted.
532            (decode_utf16le(&raw), true)
533        }
534        // `Decoder` is `#[non_exhaustive]`: degrade gracefully on future variants.
535        _ => (decode_utf16le(&raw), true),
536    }
537}
538
539#[cfg(test)]
540#[allow(clippy::unwrap_used, clippy::expect_used)]
541mod tests {
542    use super::*;
543
544    fn literals(segs: &[Segment]) -> Vec<&str> {
545        segs.iter()
546            .map(|s| match s {
547                Segment::Literal(n) => n.as_str(),
548                Segment::Variable(_, p) => p.as_str(),
549            })
550            .collect()
551    }
552
553    #[test]
554    fn template_strips_redundant_software_prefix() {
555        let segs =
556            template_segments(r"SOFTWARE\Microsoft\Windows NT\CurrentVersion", true).unwrap();
557        assert_eq!(
558            literals(&segs),
559            vec!["Microsoft", "Windows NT", "CurrentVersion"]
560        );
561        assert!(segs.iter().all(|s| matches!(s, Segment::Literal(_))));
562    }
563
564    #[test]
565    fn template_keeps_software_for_user_hive() {
566        // Per-user hives store `Software\…` literally — it must NOT be stripped.
567        let segs =
568            template_segments(r"Software\Microsoft\Windows\CurrentVersion\Run", false).unwrap();
569        assert_eq!(
570            literals(&segs),
571            vec!["Software", "Microsoft", "Windows", "CurrentVersion", "Run"]
572        );
573    }
574
575    #[test]
576    fn template_current_control_set_is_a_variable_segment() {
577        // The hardcoded ControlSet001 rewrite is gone: CurrentControlSet is now a
578        // ControlSet-domain variable, resolved at walk time via Select\Current.
579        let segs = template_segments(r"CurrentControlSet\Services", true).unwrap();
580        assert_eq!(
581            segs,
582            vec![
583                Segment::Variable(Wildcard::ControlSet, "CurrentControlSet".into()),
584                Segment::Literal("Services".into()),
585            ]
586        );
587    }
588
589    #[test]
590    fn template_rejects_placeholder() {
591        assert!(template_segments(r"HKEY_USERS\%%users.sid%%\Software\X", true).is_none());
592    }
593
594    #[test]
595    fn template_collapses_doubled_backslashes() {
596        let segs = template_segments(r"Microsoft\\Windows\\CurrentVersion\\Run", true).unwrap();
597        assert_eq!(
598            literals(&segs),
599            vec!["Microsoft", "Windows", "CurrentVersion", "Run"]
600        );
601    }
602
603    #[test]
604    fn template_strips_hk_prefix() {
605        let segs = template_segments(r"HKLM\Microsoft\Foo", true).unwrap();
606        assert_eq!(literals(&segs), vec!["Microsoft", "Foo"]);
607    }
608
609    #[test]
610    fn template_parses_wildcard_segments() {
611        let segs = template_segments(r"Microsoft\Foo\*\Bar\**", true).unwrap();
612        assert_eq!(
613            segs,
614            vec![
615                Segment::Literal("Microsoft".into()),
616                Segment::Literal("Foo".into()),
617                Segment::Variable(Wildcard::Subkey, "*".into()),
618                Segment::Literal("Bar".into()),
619                Segment::Variable(Wildcard::Subkey, "**".into()),
620            ]
621        );
622    }
623
624    #[test]
625    fn template_rejects_placeholder_in_wildcard_path() {
626        assert!(template_segments(r"Foo\%%users.sid%%\*", true).is_none());
627    }
628
629    #[test]
630    fn parse_segment_classifies_double_star_and_control_set() {
631        assert_eq!(
632            parse_segment("**5"),
633            Segment::Variable(Wildcard::Subkey, "**5".into())
634        );
635        assert_eq!(
636            parse_segment("currentcontrolset"),
637            Segment::Variable(Wildcard::ControlSet, "currentcontrolset".into())
638        );
639    }
640
641    #[test]
642    fn strips_hku_and_users_placeholder_prefix() {
643        assert_eq!(
644            strip_user_placeholder_prefix(r"HKEY_USERS\%%users.sid%%\Software\X\Y"),
645            Some(r"Software\X\Y")
646        );
647        assert_eq!(
648            strip_user_placeholder_prefix(r"HKU\*\Software\Run"),
649            Some(r"Software\Run")
650        );
651        // Not an HKU-rooted path.
652        assert!(strip_user_placeholder_prefix(r"Software\X").is_none());
653        // No remainder after the SID segment.
654        assert!(strip_user_placeholder_prefix(r"HKEY_USERS\S-1-5-21").is_none());
655    }
656}