Repository navigation
[WIP] Add dsregcmd /status analyzer to CMTrace Open #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d9b6cba
Initial plan
Copilot 3f8db52
Add dsregcmd analyzer workspace
adamgell ee86556
Address dsregcmd review feedback
adamgell 6608dca
Merge branch 'main' into copilot/add-dsregcmd-status-analyzer
adamgell b63711e
Fix PR branch CI blockers
adamgell ea8ff42
Handle unknown dsregcmd PRT state
adamgell 2bdf371
Preserve PascalCase dsregcmd join types
adamgell 97fb4b4
Harden dsregcmd signature parsing
adamgell db99fba
Use native Authenticode for dsregcmd
adamgell 1e92a62
Add dsregcmd analyzer workflow and troubleshooting guide
adamgell 36b99d3
Refactor imports in dsregcmd.rs for clarity and consistency
adamgell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| use crate::dsregcmd::{analyze_text, DsregcmdAnalysisResult}; | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| #[tauri::command] | ||
| pub fn analyze_dsregcmd(input: String) -> Result<DsregcmdAnalysisResult, String> { | ||
| eprintln!( | ||
| "event=dsregcmd_analysis_start input_chars={} input_lines={}", | ||
| input.len(), | ||
| input.lines().count() | ||
| ); | ||
|
|
||
| let result = analyze_text(&input)?; | ||
|
|
||
| eprintln!( | ||
| "event=dsregcmd_analysis_complete diagnostics_count={} join_type={:?}", | ||
| result.diagnostics.len(), | ||
| result.derived.join_type | ||
| ); | ||
|
|
||
| Ok(result) | ||
| } | ||
|
|
||
| #[tauri::command] | ||
| pub fn capture_dsregcmd() -> Result<String, String> { | ||
| capture_dsregcmd_impl() | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| fn capture_dsregcmd_impl() -> Result<String, String> { | ||
| eprintln!("event=dsregcmd_capture_start platform=windows"); | ||
|
|
||
| let dsregcmd_path = resolve_system32_binary("dsregcmd.exe")?; | ||
| verify_dsregcmd_signature(&dsregcmd_path)?; | ||
|
|
||
| let output = std::process::Command::new(&dsregcmd_path) | ||
| .arg("/status") | ||
| .output() | ||
| .map_err(|error| { | ||
| format!( | ||
| "Failed to execute '{}' /status: {}", | ||
| dsregcmd_path.display(), | ||
| error | ||
| ) | ||
| })?; | ||
|
|
||
| if !output.status.success() { | ||
| let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); | ||
| let exit_code = output.status.code().unwrap_or_default(); | ||
| return Err(if stderr.is_empty() { | ||
| format!("dsregcmd.exe /status failed with exit code {}", exit_code) | ||
| } else { | ||
| format!( | ||
| "dsregcmd.exe /status failed with exit code {}: {}", | ||
| exit_code, stderr | ||
| ) | ||
| }); | ||
| } | ||
|
|
||
| let stdout = String::from_utf8_lossy(&output.stdout).to_string(); | ||
| eprintln!( | ||
| "event=dsregcmd_capture_complete platform=windows stdout_chars={} stdout_lines={}", | ||
| stdout.len(), | ||
| stdout.lines().count() | ||
| ); | ||
| Ok(stdout) | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "windows"))] | ||
| fn capture_dsregcmd_impl() -> Result<String, String> { | ||
| Err("dsregcmd capture is only supported on Windows.".to_string()) | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| fn resolve_system32_binary(file_name: &str) -> Result<PathBuf, String> { | ||
| let Some(windir) = std::env::var_os("WINDIR") else { | ||
| return Err("WINDIR is not set; could not resolve the Windows system path.".to_string()); | ||
| }; | ||
|
|
||
| let path = PathBuf::from(windir).join("System32").join(file_name); | ||
| if !path.is_file() { | ||
| return Err(format!( | ||
| "Expected Windows system binary was not found at '{}'.", | ||
| path.display() | ||
| )); | ||
| } | ||
|
|
||
| Ok(path) | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| fn verify_dsregcmd_signature(dsregcmd_path: &Path) -> Result<(), String> { | ||
| let powershell_path = resolve_system32_binary(r"WindowsPowerShell\v1.0\powershell.exe")?; | ||
| let script = r#" | ||
| $sig = Get-AuthenticodeSignature -LiteralPath $args[0] | ||
| [pscustomobject]@{ | ||
| Status = $sig.Status.ToString() | ||
| Subject = if ($sig.SignerCertificate) { $sig.SignerCertificate.Subject } else { $null } | ||
| } | ConvertTo-Json -Compress | ||
| "#; | ||
|
|
||
| let output = std::process::Command::new(&powershell_path) | ||
| .args(["-NoProfile", "-NonInteractive", "-Command", script]) | ||
| .arg(dsregcmd_path) | ||
| .output() | ||
| .map_err(|error| { | ||
| format!( | ||
| "Failed to verify the digital signature of '{}': {}", | ||
| dsregcmd_path.display(), | ||
| error | ||
| ) | ||
| })?; | ||
|
|
||
| if !output.status.success() { | ||
| let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); | ||
| let exit_code = output.status.code().unwrap_or_default(); | ||
| return Err(if stderr.is_empty() { | ||
| format!( | ||
| "Digital signature verification failed for '{}' with exit code {}.", | ||
| dsregcmd_path.display(), | ||
| exit_code | ||
| ) | ||
| } else { | ||
| format!( | ||
| "Digital signature verification failed for '{}' with exit code {}: {}", | ||
| dsregcmd_path.display(), | ||
| exit_code, | ||
| stderr | ||
| ) | ||
| }); | ||
| } | ||
|
|
||
| let signature = serde_json::from_slice::<WindowsSignatureCheck>(&output.stdout).map_err(|error| { | ||
| format!( | ||
| "Could not parse the digital signature check output for '{}': {}", | ||
| dsregcmd_path.display(), | ||
| error | ||
| ) | ||
| })?; | ||
|
|
||
| if is_expected_dsregcmd_signature( | ||
| signature.status.as_str(), | ||
| signature.subject.as_deref(), | ||
| ) { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| Err(format!( | ||
| "Refusing to execute '{}': expected a valid Microsoft digital signature but got status '{}' and subject '{}'.", | ||
| dsregcmd_path.display(), | ||
| signature.status, | ||
| signature.subject.unwrap_or_else(|| "(missing signer subject)".to_string()) | ||
| )) | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| #[derive(serde::Deserialize)] | ||
| struct WindowsSignatureCheck { | ||
| #[serde(rename = "Status")] | ||
| status: String, | ||
| #[serde(rename = "Subject")] | ||
| subject: Option<String>, | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| fn is_expected_dsregcmd_signature(status: &str, subject: Option<&str>) -> bool { | ||
| if !status.eq_ignore_ascii_case("Valid") { | ||
| return false; | ||
| } | ||
|
|
||
| let Some(subject) = subject else { | ||
| return false; | ||
| }; | ||
|
|
||
| subject.to_ascii_lowercase().contains("microsoft") | ||
| } | ||
|
adamgell marked this conversation as resolved.
Resolved
|
||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| #[cfg(not(target_os = "windows"))] | ||
| use super::capture_dsregcmd; | ||
| #[cfg(target_os = "windows")] | ||
| use super::is_expected_dsregcmd_signature; | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| #[test] | ||
| fn expected_signature_requires_valid_microsoft_subject() { | ||
| assert!(is_expected_dsregcmd_signature( | ||
| "Valid", | ||
| Some("CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US") | ||
| )); | ||
| assert!(!is_expected_dsregcmd_signature("UnknownError", Some("CN=Microsoft Windows"))); | ||
| assert!(!is_expected_dsregcmd_signature("Valid", Some("CN=Contoso Test"))); | ||
| assert!(!is_expected_dsregcmd_signature("Valid", None)); | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "windows"))] | ||
| #[test] | ||
| fn capture_command_returns_clear_error_on_unsupported_platform() { | ||
| let error = capture_dsregcmd().expect_err("expected unsupported platform error"); | ||
| assert!(error.contains("only supported on Windows")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod dsregcmd; | ||
| pub mod error_lookup; | ||
| pub mod file_ops; | ||
| pub mod filter; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| pub mod models; | ||
| pub mod parser; | ||
| pub mod rules; | ||
|
|
||
| pub use models::{ | ||
| DsregcmdAnalysisResult, DsregcmdDerived, DsregcmdDiagnosticInsight, DsregcmdFacts, | ||
| DsregcmdJoinType, | ||
| }; | ||
|
|
||
| pub fn analyze_text(input: &str) -> Result<DsregcmdAnalysisResult, String> { | ||
| let facts = parser::parse_dsregcmd(input)?; | ||
| Ok(rules::analyze_facts(facts, input)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.