only use inner powershell command for rule matching

This commit is contained in:
iceweasel-oai
2026-04-29 21:11:39 -07:00
parent 57a1d792af
commit 0c30ead917
3 changed files with 203 additions and 112 deletions
+1 -40
View File
@@ -91,8 +91,7 @@ pub fn try_parse_powershell_command_sequence(
) -> Option<Vec<Vec<String>>> {
let (executable, args) = command.split_first()?;
if is_powershell_executable(executable) {
let parser_executable = trusted_powershell_parser_executable(executable)?;
parse_powershell_invocation(&parser_executable, args, mode)
parse_powershell_invocation(executable, args, mode)
} else {
None
}
@@ -179,22 +178,6 @@ pub(crate) fn parse_powershell_script_to_commands(
try_parse_powershell_ast_commands(executable, script)
}
fn trusted_powershell_parser_executable(exe: &str) -> Option<String> {
let executable_name = std::path::Path::new(exe)
.file_name()
.and_then(|osstr| osstr.to_str())
.unwrap_or(exe)
.to_ascii_lowercase();
let parser_executable = match executable_name.as_str() {
"powershell" | "powershell.exe" => try_find_powershell_executable_blocking()?,
"pwsh" | "pwsh.exe" => try_find_pwsh_executable_blocking()?,
_ => return None,
};
Some(parser_executable.as_path().to_string_lossy().into_owned())
}
pub(crate) fn is_powershell_executable(exe: &str) -> bool {
let executable_name = std::path::Path::new(exe)
.file_name()
@@ -433,26 +416,4 @@ mod tests {
]),
);
}
#[cfg(windows)]
#[test]
fn uses_trusted_system_powershell_for_ast_parsing() {
let command = vec![
r"C:\repo\powershell.exe".to_string(),
"-NoExit".to_string(),
"-Command".to_string(),
"Get-Content Cargo.toml".to_string(),
];
assert_eq!(
try_parse_powershell_command_sequence(
&command,
PowershellCommandSequenceParseMode::ExecPolicy,
),
Some(vec![vec![
"Get-Content".to_string(),
"Cargo.toml".to_string(),
]]),
);
}
}