mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Fix some logic
This commit is contained in:
parent
b7f793f1f9
commit
5c431291c7
1 changed files with 37 additions and 23 deletions
|
|
@ -108,6 +108,40 @@ function Invoke-RealCommand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Get-ReconstructedArguments {
|
||||||
|
param(
|
||||||
|
[string]$RawLine,
|
||||||
|
[int]$RawOffset
|
||||||
|
)
|
||||||
|
|
||||||
|
if (-not $RawLine) { return $null }
|
||||||
|
|
||||||
|
$tokens = [System.Management.Automation.PSParser]::Tokenize($RawLine, [ref]$null)
|
||||||
|
$newArgs = @()
|
||||||
|
$foundCommand = $false
|
||||||
|
|
||||||
|
foreach ($t in $tokens) {
|
||||||
|
if (-not $foundCommand) {
|
||||||
|
if ($t.Start -eq ($RawOffset - 1)) { $foundCommand = $true }
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($t.Type -eq 'Operator' -and $t.Content -match '[|;&]') { break }
|
||||||
|
|
||||||
|
# Stop if complex variable expansion is used
|
||||||
|
if ($t.Type -eq 'Variable' -or $t.Type -eq 'Group' -or $t.Type -eq 'SubExpression') {
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
$newArgs += $t.Content
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($foundCommand) {
|
||||||
|
return ,$newArgs
|
||||||
|
}
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
function Invoke-WrappedCommand {
|
function Invoke-WrappedCommand {
|
||||||
param(
|
param(
|
||||||
[string]$OriginalCmd,
|
[string]$OriginalCmd,
|
||||||
|
|
@ -118,29 +152,9 @@ function Invoke-WrappedCommand {
|
||||||
|
|
||||||
# Use raw line parsing to recover arguments like '--' that PowerShell consumes
|
# Use raw line parsing to recover arguments like '--' that PowerShell consumes
|
||||||
if ($RawLine) {
|
if ($RawLine) {
|
||||||
$tokens = [System.Management.Automation.PSParser]::Tokenize($RawLine, [ref]$null)
|
$reconstructedArgs = Get-ReconstructedArguments $RawLine $RawOffset
|
||||||
$newArgs = @()
|
if ($null -ne $reconstructedArgs) {
|
||||||
$foundCommand = $false
|
$Arguments = $reconstructedArgs
|
||||||
$canUseRaw = $true
|
|
||||||
|
|
||||||
foreach ($t in $tokens) {
|
|
||||||
# Find the command token based on offset
|
|
||||||
if (-not $foundCommand) {
|
|
||||||
if ($t.Start -eq ($RawOffset - 1)) { $foundCommand = $true }
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
# Stop at command separators
|
|
||||||
if ($t.Type -eq 'Operator' -and $t.Content -match '[|;&]') { break }
|
|
||||||
# Stop if complex variable expansion is used
|
|
||||||
if ($t.Type -eq 'Variable' -or $t.Type -eq 'Group' -or $t.Type -eq 'SubExpression') {
|
|
||||||
$canUseRaw = $false
|
|
||||||
break
|
|
||||||
}
|
|
||||||
$newArgs += $t.Content
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($foundCommand -and $canUseRaw) {
|
|
||||||
$Arguments = $newArgs
|
|
||||||
Write-Host "Safe-chain Powershell Wrapper: Reconstructed args: $($Arguments -join ' ')"
|
Write-Host "Safe-chain Powershell Wrapper: Reconstructed args: $($Arguments -join ' ')"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue