Making quote insertion less error prone

This commit is contained in:
Wietze
2024-04-20 18:49:09 +01:00
parent 32adc1b714
commit 754dfe9c2f
2 changed files with 12 additions and 8 deletions
+6 -1
View File
@@ -50,10 +50,15 @@ function Invoke-ArgFuscator {
$ErrorModifiers = @();
for ($i = 0; $i -lt $n; $i++) {
$Tokens = [System.Collections.ArrayList]@();
$OriginalTokens = [System.Collections.ArrayList]@();
foreach ($type_value in $JSONData.command) {
$Token = [Token]::new($type_value.PSObject.Properties.Value.ToCharArray());
$Token.Type = $type_value.PSObject.Properties.Name;
$Tokens.Add($Token) | Out-Null;
$Token = [Token]::new($type_value.PSObject.Properties.Value.ToCharArray());
$Token.Type = $type_value.PSObject.Properties.Name;
$OriginalTokens.Add($Token) | Out-Null;
}
@@ -95,7 +100,7 @@ function Invoke-ArgFuscator {
$Output = $tokens[0]
if ($Tokens.Count -gt 1) {
ForEach ($Index in (1..($Tokens.Count - 1))) {
$Output = -join ($Output, $(if (($Tokens[$Index - 1].Type -eq "argument" -or $Tokens[$Index - 1].Type -eq "value") -and ($Tokens[$Index - 1].TokenContent[-1] -eq "=")) { "" } else { " " }), ($Tokens[$Index].ToString()))
$Output = -join ($Output, $(if (($Tokens[$Index - 1].Type -eq "argument" -or $Tokens[$Index - 1].Type -eq "value") -and ($OriginalTokens[$Index - 1].TokenContent[-1] -match '[=:]')) { "" } else { " " }), ($Tokens[$Index].ToString()))
}
}
+6 -7
View File
@@ -17,14 +17,13 @@ class QuoteInsertion : Modifier {
if (!$this.ExcludedTypes.Contains($Token.Type)) {
$Content = $Token.ToString();
foreach ($Char in $Token.TokenContent) {
$nextChar = if ($index -lt $Content.Length) { $Content[$index+1] } else { "" }
$NewTokenContent.Add($Char);
if ([Modifier]::CoinFlip($this.Probability) -and `
( ( $Content[-1] -ne [QuoteInsertion]::QuoteChar ) `
-or ( ($index -gt 0) -and `
($Char -match '^[a-zA-Z0-9]$') -and `
($index -lt ($Content.Length - 2)) -and `
($Content[$index+1] -match '^[a-zA-Z0-9]$')) `
)){
-or ( ($index -gt 0) -and ($index -lt ($Content.Length - 1)))) `
-and ($Char -match '^[a-zA-Z0-9\-\/]$') -and ($nextChar -match '^[a-zA-Z0-9\-\/]{0,1}$') `
){
$NewTokenContent.Add([QuoteInsertion]::QuoteChar);
$i++;
}
@@ -46,13 +45,13 @@ class QuoteInsertion : Modifier {
}
}
# Edge case: Check if we added a quote after a value char
if (($NewTokenContent[-1] -eq [QuoteInsertion]::QuoteChar) -and ([Modifier]::ValueChars.Contains($NewTokenContent[-2]))) {
if (($NewTokenContent[-1] -eq [QuoteInsertion]::QuoteChar) -and (([Modifier]::ValueChars) -contains ($NewTokenContent[-2]))) {
$NewTokenContent.RemoveAt($NewTokenContent.Count - 1); # Remove the final quote (leaving it may cause issues)
$NewTokenContent.RemoveAt($NewTokenContent.lastIndexOf([QuoteInsertion]::QuoteChar)); # Also remove the right-most quote character to balance the number of quotes out again
}
$Token.TokenContent = $NewTokenContent;
}
}