Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit 130d970

Browse files
author
mattifestation
committed
Out-EncryptedScript uses FIPS-compliant crypto #60
Thanks, @aconite33 for the suggestion. - TripleDESCryptoServiceProvider is now used as the crypto algorithm because it won't break the script when FIPS compliance is enabled in the registry. - I actually implemented the InitializationVector parameter - Cleaned up the output script - Cleaned up comment-based help
1 parent 8b6f759 commit 130d970

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

ScriptModification/Out-EncryptedScript.ps1

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ Optional Dependencies: None
1313
1414
.DESCRIPTION
1515
16-
Out-EncryptedScript will encrypt a script (or any text file for that matter) and output the results to a minimally obfuscated script - evil.ps1.
16+
Out-EncryptedScript will encrypt a script (or any text file for that
17+
matter) and output the results to a minimally obfuscated script -
18+
evil.ps1 by default.
1719
1820
.PARAMETER ScriptPath
1921
@@ -27,13 +29,22 @@ Password to encrypt/decrypt the script
2729
2830
Salt value for encryption/decryption. This can be any string value.
2931
32+
.PARAMETER InitializationVector
33+
34+
Specifies a 16-character the initialization vector to be used. This
35+
is randomly generated by default.
36+
3037
.EXAMPLE
3138
3239
C:\PS> Out-EncryptedScript .\Naughty-Script.ps1 password salty
3340
3441
Description
3542
-----------
36-
Encrypt the contents of this file with a password and salt. This will make analysis of the script impossible without the correct password and salt combination. This command will generate evil.ps1 that can dropped onto the victim machine. It only consists of a decryption function 'de' and the base64-encoded ciphertext.
43+
Encrypt the contents of this file with a password and salt. This will
44+
make analysis of the script impossible without the correct password
45+
and salt combination. This command will generate evil.ps1 that can
46+
dropped onto the victim machine. It only consists of a decryption
47+
function 'de' and the base64-encoded ciphertext.
3748
3849
.EXAMPLE
3950
@@ -44,15 +55,13 @@ C:\PS> Invoke-Expression $decrypted
4455
4556
Description
4657
-----------
47-
This series of instructions assumes you've already encrypted a script and named it evil.ps1. The contents are then decrypted and the unencrypted script is called via Invoke-Expression
58+
This series of instructions assumes you've already encrypted a script
59+
and named it evil.ps1. The contents are then decrypted and the
60+
unencrypted script is called via Invoke-Expression
4861
4962
.NOTES
5063
5164
This command can be used to encrypt any text-based file/script
52-
53-
.LINK
54-
55-
http://www.exploit-monday.com
5665
#>
5766

5867
[CmdletBinding()] Param (
@@ -69,22 +78,23 @@ http://www.exploit-monday.com
6978
$Salt,
7079

7180
[Parameter(Position = 3)]
81+
[ValidateLength(16, 16)]
7282
[String]
73-
$InitializationVector = ( @( foreach ($i in 1..16) { [Char](Get-Random -Min 0x41 -Max 0x5B) } ) -join '' ), # Generate random 16 character IV
83+
$InitializationVector = ((1..16 | % {[Char](Get-Random -Min 0x41 -Max 0x5B)}) -join ''),
7484

7585
[Parameter(Position = 4)]
7686
[String]
7787
$FilePath = '.\evil.ps1'
7888
)
7989

8090
$AsciiEncoder = New-Object System.Text.ASCIIEncoding
81-
$ivBytes = $AsciiEncoder.GetBytes("CRACKMEIFYOUCAN!")
91+
$ivBytes = $AsciiEncoder.GetBytes($InitializationVector)
8292
# While this can be used to encrypt any file, it's primarily designed to encrypt itself.
83-
[Byte[]] $scriptBytes = Get-Content -Encoding byte -Path $ScriptPath
93+
[Byte[]] $scriptBytes = [Text.Encoding]::ASCII.GetBytes((Get-Content -Encoding Ascii -Path $ScriptPath))
8494
$DerivedPass = New-Object System.Security.Cryptography.PasswordDeriveBytes($Password, $AsciiEncoder.GetBytes($Salt), "SHA1", 2)
85-
$Key = New-Object System.Security.Cryptography.RijndaelManaged
95+
$Key = New-Object System.Security.Cryptography.TripleDESCryptoServiceProvider
8696
$Key.Mode = [System.Security.Cryptography.CipherMode]::CBC
87-
[Byte[]] $KeyBytes = $DerivedPass.GetBytes(32)
97+
[Byte[]] $KeyBytes = $DerivedPass.GetBytes(16)
8898
$Encryptor = $Key.CreateEncryptor($KeyBytes, $ivBytes)
8999
$MemStream = New-Object System.IO.MemoryStream
90100
$CryptoStream = New-Object System.Security.Cryptography.CryptoStream($MemStream, $Encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
@@ -97,33 +107,31 @@ http://www.exploit-monday.com
97107
$Cipher = [Convert]::ToBase64String($CipherTextBytes)
98108

99109
# Generate encrypted PS1 file. All that will be included is the base64-encoded ciphertext and a slightly 'obfuscated' decrypt function
100-
$Output = 'function de([String] $b, [String] $c)
110+
$Output = @"
111+
function de([String] `$b, [String] `$c)
101112
{
102-
$a = "'
103-
$Output += $cipher
104-
$Output += '"'
105-
$Output += ';
106-
$encoding = New-Object System.Text.ASCIIEncoding;
107-
$dd = $encoding.GetBytes("CRACKMEIFYOUCAN!");
108-
$aa = [Convert]::FromBase64String($a);
109-
$derivedPass = New-Object System.Security.Cryptography.PasswordDeriveBytes($b, $encoding.GetBytes($c), "SHA1", 2);
110-
[Byte[]] $e = $derivedPass.GetBytes(32);
111-
$f = New-Object System.Security.Cryptography.RijndaelManaged;
112-
$f.Mode = [System.Security.Cryptography.CipherMode]::CBC;
113-
[Byte[]] $h = New-Object Byte[]($aa.Length);
114-
$g = $f.CreateDecryptor($e, $dd);
115-
$i = New-Object System.IO.MemoryStream($aa, $True);
116-
$j = New-Object System.Security.Cryptography.CryptoStream($i, $g, [System.Security.Cryptography.CryptoStreamMode]::Read);
117-
$r = $j.Read($h, 0, $h.Length);
118-
$i.Close();
119-
$j.Close();
120-
$f.Clear();
121-
return $encoding.GetString($h,0,$h.Length);
122-
}'
113+
`$a = "$Cipher";
114+
`$encoding = New-Object System.Text.ASCIIEncoding;
115+
`$dd = `$encoding.GetBytes("$InitializationVector");
116+
`$aa = [Convert]::FromBase64String(`$a);
117+
`$derivedPass = New-Object System.Security.Cryptography.PasswordDeriveBytes(`$b, `$encoding.GetBytes(`$c), "SHA1", 2);
118+
[Byte[]] `$e = `$derivedPass.GetBytes(16);
119+
`$f = New-Object System.Security.Cryptography.TripleDESCryptoServiceProvider;
120+
`$f.Mode = [System.Security.Cryptography.CipherMode]::CBC;
121+
[Byte[]] `$h = New-Object Byte[](`$aa.Length);
122+
`$g = `$f.CreateDecryptor(`$e, `$dd);
123+
`$i = New-Object System.IO.MemoryStream(`$aa, `$True);
124+
`$j = New-Object System.Security.Cryptography.CryptoStream(`$i, `$g, [System.Security.Cryptography.CryptoStreamMode]::Read);
125+
`$r = `$j.Read(`$h, 0, `$h.Length);
126+
`$i.Close();
127+
`$j.Close();
128+
`$f.Clear();
129+
return `$encoding.GetString(`$h,0,`$h.Length);
130+
}
131+
"@
123132

124133
# Output decrypt function and ciphertext to evil.ps1
125134
Out-File -InputObject $Output -Encoding ASCII $FilePath
126135

127136
Write-Verbose "Encrypted PS1 file saved to: $(Resolve-Path $FilePath)"
128-
129137
}

0 commit comments

Comments
 (0)