From 1236d8e95e096ebb24fdca1a0aeeab912c7bb56e Mon Sep 17 00:00:00 2001 From: Hal <22532613+HalRiveria@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:53:28 -0500 Subject: [PATCH] Quote a list for cmd.exe under a drive-relative path too Batch-file quoting was picked by a pattern that needed a slash or the start of the path before 'cmd', so C:cmd.exe and C:cmd got the ordinary quoting, where a quote is written \" and cmd.exe doesn't honor it: an element like 'a" & echo X & "' ran a second command. The pattern now accepts a colon there too. Harness section AE runs the injection payload through C:cmd.exe, C:cmd and C:CMD.EXE. from System32. --- ParallelKit.Harness.ps1 | 18 ++++++++++++++++++ ParallelKit.ps1 | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ParallelKit.Harness.ps1 b/ParallelKit.Harness.ps1 index b6a1171..0537669 100644 --- a/ParallelKit.Harness.ps1 +++ b/ParallelKit.Harness.ps1 @@ -3075,6 +3075,24 @@ if ($compiled) { Test-Assert ($cmdHits -eq 0 -and $cmdThrew -eq 0) ("a quote in a list element can't make cmd.exe /c run another command (" + $cmdHits + " of " + ($cmdNames.Count * $injPayloads.Count) + " did, " + $cmdThrew + " failed to run)") $cmdBatch = Invoke-PkProcessBatch -MaxConcurrent 1 -Commands @(@{ FilePath = 'cmd.exe'; WorkingDirectory = $injDir; Arguments = @('/d', '/c', 'echo', $injPayloads[0]) }) Test-Assert (-not [System.IO.File]::Exists($injMarker) -and $cmdBatch.Results[0].ExitCode -eq 0) "the same for cmd.exe through the batch" + # A drive-relative path (C:cmd.exe, from System32 as that drive's current + # folder) still names cmd.exe, so it gets the same quoting. + $sys32 = Join-Path $env:SystemRoot 'System32' + $cmdDrive = $sys32.Substring(0, 2) + $relHits = 0 + $relThrew = 0 + $relRan = 0 + Push-Location -LiteralPath $sys32 + try { + foreach ($cmdName in @(($cmdDrive + 'cmd.exe'), ($cmdDrive + 'cmd'), ($cmdDrive + 'CMD.EXE.'))) { + try { + $relResult = Invoke-PkProcessWithTimeout -FilePath $cmdName -Arguments @('/d', '/c', 'echo', $injPayloads[0]) -WorkingDirectory $injDir -TimeoutMs 10000 + if ($relResult.ExitCode -eq 0) { $relRan++ } + } catch { $relThrew++; Write-Host " injection probe threw: $_" } + if ([System.IO.File]::Exists($injMarker)) { $relHits++; [System.IO.File]::Delete($injMarker) } + } + } finally { Pop-Location } + Test-Assert ($relHits -eq 0 -and $relThrew -eq 0 -and $relRan -eq 3) ("nor under a drive-relative path to cmd.exe (" + $relHits + " of 3 did, " + $relThrew + " failed to run, " + $relRan + " ran)") # A line break would end cmd.exe's command line and drop the rest, so it's # refused for a batch file, and still fine for any other program. diff --git a/ParallelKit.ps1 b/ParallelKit.ps1 index 269f4c8..9cace24 100644 --- a/ParallelKit.ps1 +++ b/ParallelKit.ps1 @@ -1593,9 +1593,9 @@ function Resolve-PkArgumentList { # Windows drops trailing dots and spaces from a file name, so # 'run.cmd.' still runs through cmd.exe. cmd.exe itself, given a list # (cmd /c tool "a b"), reads its command line the same way, so it gets - # the same quoting. + # the same quoting, under a drive-relative path (C:cmd.exe) too. $exeName = $FilePath.Trim().Trim('"').TrimEnd(' ', '.') - $forBatchFile = ($exeName -match '\.(bat|cmd)$' -or $exeName -match '(^|[\\/])cmd(\.exe)?$') + $forBatchFile = ($exeName -match '\.(bat|cmd)$' -or $exeName -match '(^|[\\/:])cmd(\.exe)?$') return (ConvertTo-PkArgumentStringCore -Arguments @($Arguments) -ForBatchFile:$forBatchFile) } return [string]$Arguments