public static NTSTATUS NtAllocateVirtualMemory10(IntPtr hProcess, ref IntPtr BaseAddress, IntPtr ZeroBits, ref UIntPtr RegionSize, ulong AllocationType, ulong Protect) { byte[] syscall = bNtAllocateVirtualMemory10; GCHandle pinnedArray = GCHandle.Alloc(syscall, GCHandleType.Pinned); IntPtr memoryAddress = pinnedArray.AddrOfPinnedObject(); if (!Natives.VirtualProtect(memoryAddress, (UIntPtr)syscall.Length, memoryPtrotection, out uint oldprotect)) { throw new Win32Exception(); } Delegates.NtAllocateVirtualMemory myAssemblyFunction = (Delegates.NtAllocateVirtualMemory)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(Delegates.NtAllocateVirtualMemory)); return((NTSTATUS)myAssemblyFunction(hProcess, ref BaseAddress, ZeroBits, ref RegionSize, AllocationType, Protect)); }
public static NTSTATUS NtAllocateVirtualMemory( IntPtr ProcessHandle, ref IntPtr BaseAddress, IntPtr ZeroBits, ref UIntPtr RegionSize, uint AllocationType, uint Protect) { // set byte array of bNtAllocateVirtualMemory to new byte array called syscall byte[] syscall = bNtAllocateVirtualMemory; // specify unsafe context unsafe { // create new byte pointer and set value to our syscall byte array fixed(byte *ptr = syscall) { // cast the byte array pointer into a C# IntPtr called memoryAddress IntPtr memoryAddress = (IntPtr)ptr; // Change memory access to RX for our assembly code if (!VirtualProtectEx(Process.GetCurrentProcess().Handle, memoryAddress, (UIntPtr)syscall.Length, (uint)AllocationProtect.PAGE_EXECUTE_READWRITE, out uint oldprotect)) { throw new Win32Exception(); } // Get delegate for NtAllocateVirtualMemory Delegates.NtAllocateVirtualMemory assembledFunction = (Delegates.NtAllocateVirtualMemory)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(Delegates.NtAllocateVirtualMemory)); return((NTSTATUS)assembledFunction( ProcessHandle, ref BaseAddress, ZeroBits, ref RegionSize, AllocationType, Protect)); } } }
public static NTSTATUS NtAllocateVirtualMemory10(IntPtr hProcess, ref IntPtr BaseAddress, IntPtr ZeroBits, ref UIntPtr RegionSize, ulong AllocationType, ulong Protect) { byte[] syscall = bNtAllocateVirtualMemory10; unsafe { fixed(byte *ptr = syscall) { IntPtr memoryAddress = (IntPtr)ptr; if (!VirtualProtectEx(Process.GetCurrentProcess().Handle, memoryAddress, (UIntPtr)syscall.Length, 0x40, out uint oldprotect)) { throw new Win32Exception(); } Delegates.NtAllocateVirtualMemory myAssemblyFunction = (Delegates.NtAllocateVirtualMemory)Marshal.GetDelegateForFunctionPointer(memoryAddress, typeof(Delegates.NtAllocateVirtualMemory)); return((NTSTATUS)myAssemblyFunction(hProcess, ref BaseAddress, ZeroBits, ref RegionSize, AllocationType, Protect)); } } }