-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSCrypter.ps1
67 lines (57 loc) · 2.15 KB
/
PSCrypter.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Original file -> Gzip compress -> Xor encrypt
Function Crypter {
param(
[Parameter()]
[string]$File
)
if (!($File)) {
$File = Read-Host "File path"
}
# Set up a compression stream (this took a looong while)
$byteArray = Get-Content $File -Encoding Byte
[System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.GzipStream $output, ([IO.Compression.CompressionMode]::Compress)
$gzipStream.Write( $byteArray, 0, $byteArray.Length )
$gzipStream.Close()
$output.Close()
# Xor encrypt the bytes
$tmp = $output.ToArray()
$encBytes = @();
$key = "white-hat"
for ($i = 0; $i -lt $tmp.Count; $i++) {
$encBytes += $tmp[$i] -bxor $key[$i % $key.Length];
}
# Write file to disk. Resulting file is a Xor encrypted GZip compressed file.
[IO.File]::WriteAllBytes("$file.gz.enc", $encBytes)
Write-Host -ForegroundColor Green "[+] Written encrypted file: $file.gz.enc"
}
# Xor decrypt -> GZip decompress -> Write to disk
Function Decrypter {
param(
[Parameter()]
[string]$File
)
if (!($File)) {
$File = Read-Host "File path"
}
# Xor decrypt
$byteArray = Get-Content $File -Encoding Byte
$tmp = @();
$key = "white-hat"
for ($i = 0; $i -lt $byteArray.Count; $i++) {
$tmp += $byteArray[$i] -bxor $key[$i % $key.Length];
}
# Gzip decompress
$GUNZIP = New-Object IO.Compression.GZipStream([IO.MemoryStream][Convert]::FromBase64String([Convert]::ToBase64String($tmp)), [IO.Compression.CompressionMode]::Decompress)
$uncompressed = New-Object Byte[](17920) # (Statically) put the size of the ORIGINAL exe file (dunno how to make it dynamic here).
$GUNZIP.Read($uncompressed, 0, 17920) | Out-Null # Put the size of the ORIGINAL exe file.
[System.Reflection.Assembly]::Load($uncompressed)
# Write file to disk
if ($file.Contains(".gz.enc")) {
$filename = $File.Replace(".gz.enc", ".exe")
}
else {
$filename = $filename + ".exe"
}
[IO.File]::WriteAllBytes("$filename", $uncompressed)
}