Skip to content

Commit 9a2dfad

Browse files
author
Matt Graeber
committed
Fixed mangled decrypted script output
Addresses issue PowerShellMafia#80. This was a tricky fix because the script should ideally handle Unicode and Ascii encoded scripts. I haven't tested scripts with international characters but I would imagine those script would get mangled since the decrypted output is ultimately normalized to ascii.
1 parent fdcdeab commit 9a2dfad

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

ScriptModification/Out-EncryptedScript.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ This command can be used to encrypt any text-based file/script
9090
$AsciiEncoder = New-Object System.Text.ASCIIEncoding
9191
$ivBytes = $AsciiEncoder.GetBytes($InitializationVector)
9292
# While this can be used to encrypt any file, it's primarily designed to encrypt itself.
93-
[Byte[]] $scriptBytes = [Text.Encoding]::ASCII.GetBytes((Get-Content -Encoding Ascii -Path $ScriptPath))
93+
[Byte[]] $scriptBytes = Get-Content -Encoding Byte -ReadCount 0 -Path $ScriptPath
9494
$DerivedPass = New-Object System.Security.Cryptography.PasswordDeriveBytes($Password, $AsciiEncoder.GetBytes($Salt), "SHA1", 2)
9595
$Key = New-Object System.Security.Cryptography.TripleDESCryptoServiceProvider
9696
$Key.Mode = [System.Security.Cryptography.CipherMode]::CBC
@@ -126,7 +126,8 @@ function de([String] `$b, [String] `$c)
126126
`$i.Close();
127127
`$j.Close();
128128
`$f.Clear();
129-
return `$encoding.GetString(`$h,0,`$h.Length);
129+
if ((`$h.Length -gt 3) -and (`$h[0] -eq 0xEF) -and (`$h[1] -eq 0xBB) -and (`$h[2] -eq 0xBF)) { `$h = `$h[3..(`$h.Length-1)]; }
130+
return `$encoding.GetString(`$h).TrimEnd([Char] 0);
130131
}
131132
"@
132133

0 commit comments

Comments
 (0)