public static SpDi.Native.NTSTATUS NtCreateSection( ref IntPtr SectionHandle, uint DesiredAccess, IntPtr ObjectAttributes, ref ulong MaximumSize, uint SectionPageProtection, uint AllocationAttributes, IntPtr FileHandle) { object[] funcargs = { SectionHandle, DesiredAccess, ObjectAttributes, MaximumSize, SectionPageProtection, AllocationAttributes, FileHandle }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtCreateSection", typeof(DELEGATES.NtCreateSection), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Unable to create section, " + retValue); } SectionHandle = (IntPtr)funcargs[0]; MaximumSize = (ulong)funcargs[3]; return(retValue); }
public static IntPtr NtOpenThread(int TID, SpDi.Win32.Kernel32.ThreadAccess DesiredAccess) { IntPtr ThreadHandle = IntPtr.Zero; SpDi.Native.OBJECT_ATTRIBUTES oa = new SpDi.Native.OBJECT_ATTRIBUTES(); SpDi.Native.CLIENT_ID ci = new SpDi.Native.CLIENT_ID(); ci.UniqueThread = (IntPtr)TID; object[] funcargs = { ThreadHandle, DesiredAccess, oa, ci }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtOpenThread", typeof(DELEGATES.NtOpenProcess), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success && retValue == SpDi.Native.NTSTATUS.InvalidCid) { throw new InvalidOperationException("An invalid client ID was specified."); } if (retValue != SpDi.Native.NTSTATUS.Success) { throw new UnauthorizedAccessException("Access is denied."); } ThreadHandle = (IntPtr)funcargs[0]; return(ThreadHandle); }
public static SpDi.Native.NTSTATUS NtCreateThreadEx( ref IntPtr threadHandle, SpDi.Win32.WinNT.ACCESS_MASK desiredAccess, IntPtr objectAttributes, IntPtr processHandle, IntPtr startAddress, IntPtr parameter, bool createSuspended, int stackZeroBits, int sizeOfStack, int maximumStackSize, IntPtr attributeList) { object[] funcargs = { threadHandle, desiredAccess, objectAttributes, processHandle, startAddress, parameter, createSuspended, stackZeroBits, sizeOfStack, maximumStackSize, attributeList }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtCreateThreadEx", typeof(DELEGATES.NtCreateThreadEx), ref funcargs); threadHandle = (IntPtr)funcargs[0]; return(retValue); }
public static SpDi.Native.NTSTATUS NtMapViewOfSection( IntPtr SectionHandle, IntPtr ProcessHandle, ref IntPtr BaseAddress, IntPtr ZeroBits, IntPtr CommitSize, IntPtr SectionOffset, ref ulong ViewSize, uint InheritDisposition, uint AllocationType, uint Win32Protect) { object[] funcargs = { SectionHandle, ProcessHandle, BaseAddress, ZeroBits, CommitSize, SectionOffset, ViewSize, InheritDisposition, AllocationType, Win32Protect }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtMapViewOfSection", typeof(DELEGATES.NtMapViewOfSection), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success && retValue != SpDi.Native.NTSTATUS.ImageNotAtBase) { throw new InvalidOperationException("Unable to map view of section, " + retValue); } BaseAddress = (IntPtr)funcargs[2]; ViewSize = (ulong)funcargs[6]; return(retValue); }
public static SpDi.Native.NTSTATUS RtlCreateUserThread( IntPtr Process, IntPtr ThreadSecurityDescriptor, bool CreateSuspended, IntPtr ZeroBits, IntPtr MaximumStackSize, IntPtr CommittedStackSize, IntPtr StartAddress, IntPtr Parameter, ref IntPtr Thread, IntPtr ClientId) { object[] funcargs = { Process, ThreadSecurityDescriptor, CreateSuspended, ZeroBits, MaximumStackSize, CommittedStackSize, StartAddress, Parameter, Thread, ClientId }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"RtlCreateUserThread", typeof(DELEGATES.RtlCreateUserThread), ref funcargs); Thread = (IntPtr)funcargs[8]; return(retValue); }
public static SpDi.Native.PROCESS_BASIC_INFORMATION NtQueryInformationProcessBasicInformation(IntPtr hProcess) { SpDi.Native.NTSTATUS retValue = NtQueryInformationProcess(hProcess, SpDi.Native.PROCESSINFOCLASS.ProcessBasicInformation, out IntPtr pProcInfo); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new UnauthorizedAccessException("Access is denied."); } return((SpDi.Native.PROCESS_BASIC_INFORMATION)Marshal.PtrToStructure(pProcInfo, typeof(SpDi.Native.PROCESS_BASIC_INFORMATION))); }
public static SpDi.Native.NTSTATUS NtUnmapViewOfSection(IntPtr hProc, IntPtr baseAddr) { object[] funcargs = { hProc, baseAddr }; SpDi.Native.NTSTATUS result = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtUnmapViewOfSection", typeof(DELEGATES.NtUnmapViewOfSection), ref funcargs); return(result); }
public static IntPtr NtAllocateVirtualMemory(IntPtr ProcessHandle, ref IntPtr BaseAddress, IntPtr ZeroBits, ref IntPtr RegionSize, UInt32 AllocationType, UInt32 Protect) { object[] funcargs = { ProcessHandle, BaseAddress, ZeroBits, RegionSize, AllocationType, Protect }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtAllocateVirtualMemory", typeof(DELEGATES.NtAllocateVirtualMemory), ref funcargs); if (retValue == SpDi.Native.NTSTATUS.AccessDenied) { throw new UnauthorizedAccessException("Access is denied."); } if (retValue == SpDi.Native.NTSTATUS.AlreadyCommitted) { throw new InvalidOperationException("The specified address range is already committed."); } if (retValue == SpDi.Native.NTSTATUS.CommitmentLimit) { throw new InvalidOperationException("Your system is low on virtual memory."); } if (retValue == SpDi.Native.NTSTATUS.ConflictingAddresses) { throw new InvalidOperationException("The specified address range conflicts with the address space."); } if (retValue == SpDi.Native.NTSTATUS.InsufficientResources) { throw new InvalidOperationException("Insufficient system resources exist to complete the API call."); } if (retValue == SpDi.Native.NTSTATUS.InvalidHandle) { throw new InvalidOperationException("An invalid HANDLE was specified."); } if (retValue == SpDi.Native.NTSTATUS.InvalidPageProtection) { throw new InvalidOperationException("The specified page protection was not valid."); } if (retValue == SpDi.Native.NTSTATUS.NoMemory) { throw new InvalidOperationException("Not enough virtual memory or paging file quota is available to complete the specified operation."); } if (retValue == SpDi.Native.NTSTATUS.ObjectTypeMismatch) { throw new InvalidOperationException("There is a mismatch between the type of object that is required by the requested operation and the type of object that is specified in the request."); } if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("An attempt was made to duplicate an object handle into or out of an exiting process."); } BaseAddress = (IntPtr)funcargs[1]; return(BaseAddress); }
public static void NtQueueApcThread(IntPtr ThreadHandle, IntPtr ApcRoutine, IntPtr ApcArgument1, IntPtr ApcArgument2, IntPtr ApcArgument3) { object[] funcargs = { ThreadHandle, ApcRoutine, ApcArgument1, ApcArgument2, ApcArgument3 }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtQueueApcThread", typeof(DELEGATES.NtQueueApcThread), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Unable to queue APC, " + retValue); } }
public static SpDi.Native.NTSTATUS LdrLoadDll(IntPtr PathToFile, UInt32 dwFlags, ref SpDi.Native.UNICODE_STRING ModuleFileName, ref IntPtr ModuleHandle) { object[] funcargs = { PathToFile, dwFlags, ModuleFileName, ModuleHandle }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"LdrLoadDll", typeof(DELEGATES.LdrLoadDll), ref funcargs); ModuleHandle = (IntPtr)funcargs[3]; return(retValue); }
public static bool NtQueryInformationProcessWow64Information(IntPtr hProcess) { SpDi.Native.NTSTATUS retValue = NtQueryInformationProcess(hProcess, SpDi.Native.PROCESSINFOCLASS.ProcessWow64Information, out IntPtr pProcInfo); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new UnauthorizedAccessException("Access is denied."); } if (Marshal.ReadIntPtr(pProcInfo) == IntPtr.Zero) { return(false); } return(true); }
public static IntPtr LoadModuleFromDisk(string DLLPath) { SpDi.Native.UNICODE_STRING uModuleName = new SpDi.Native.UNICODE_STRING(); Native.RtlInitUnicodeString(ref uModuleName, DLLPath); IntPtr hModule = IntPtr.Zero; SpDi.Native.NTSTATUS CallResult = Native.LdrLoadDll(IntPtr.Zero, 0, ref uModuleName, ref hModule); if (CallResult != SpDi.Native.NTSTATUS.Success || hModule == IntPtr.Zero) { return(IntPtr.Zero); } return(hModule); }
public static void RtlGetVersion(ref SpDi.Native.OSVERSIONINFOEX VersionInformation) { object[] funcargs = { VersionInformation }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"RtlGetVersion", typeof(DELEGATES.RtlGetVersion), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Failed get procedure address, " + retValue); } VersionInformation = (SpDi.Native.OSVERSIONINFOEX)funcargs[0]; }
public static IntPtr LdrGetProcedureAddress(IntPtr hModule, IntPtr FunctionName, IntPtr Ordinal, ref IntPtr FunctionAddress) { object[] funcargs = { hModule, FunctionName, Ordinal, FunctionAddress }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"LdrGetProcedureAddress", typeof(DELEGATES.LdrGetProcedureAddress), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Failed get procedure address, " + retValue); } FunctionAddress = (IntPtr)funcargs[3]; return(FunctionAddress); }
public static IntPtr NtOpenFile(ref IntPtr FileHandle, SpDi.Win32.Kernel32.FileAccessFlags DesiredAccess, ref SpDi.Native.OBJECT_ATTRIBUTES ObjAttr, ref SpDi.Native.IO_STATUS_BLOCK IoStatusBlock, SpDi.Win32.Kernel32.FileShareFlags ShareAccess, SpDi.Win32.Kernel32.FileOpenFlags OpenOptions) { object[] funcargs = { FileHandle, DesiredAccess, ObjAttr, IoStatusBlock, ShareAccess, OpenOptions }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtOpenFile", typeof(DELEGATES.NtOpenFile), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Failed to open file, " + retValue); } FileHandle = (IntPtr)funcargs[0]; return(FileHandle); }
public static UInt32 NtReadVirtualMemory(IntPtr ProcessHandle, IntPtr BaseAddress, IntPtr Buffer, ref UInt32 NumberOfBytesToRead) { UInt32 NumberOfBytesRead = 0; object[] funcargs = { ProcessHandle, BaseAddress, Buffer, NumberOfBytesToRead, NumberOfBytesRead }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtReadVirtualMemory", typeof(DELEGATES.NtReadVirtualMemory), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Failed to read memory, " + retValue); } NumberOfBytesRead = (UInt32)funcargs[4]; return(NumberOfBytesRead); }
public static UInt32 NtWriteVirtualMemory(IntPtr ProcessHandle, IntPtr BaseAddress, IntPtr Buffer, UInt32 BufferLength) { UInt32 BytesWritten = 0; object[] funcargs = { ProcessHandle, BaseAddress, Buffer, BufferLength, BytesWritten }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtWriteVirtualMemory", typeof(DELEGATES.NtWriteVirtualMemory), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Failed to write memory, " + retValue); } BytesWritten = (UInt32)funcargs[4]; return(BytesWritten); }
public static UInt32 NtProtectVirtualMemoryLoPE(IntPtr ProcessHandle, ref ulong BaseAddress, ref ulong RegionSize, UInt32 NewProtect) { UInt32 OldProtect = 0; object[] funcargs = { ProcessHandle, BaseAddress, RegionSize, NewProtect, OldProtect }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtProtectVirtualMemory", typeof(DELEGATES.NtProtectVirtualMemory), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("Failed to change memory protection, " + retValue); } OldProtect = (UInt32)funcargs[4]; return(OldProtect); }
public static string GetFilenameFromMemoryPointer(IntPtr hProc, IntPtr pMem) { IntPtr pBase = IntPtr.Zero; IntPtr RegionSize = (IntPtr)0x500; IntPtr pAlloc = NtAllocateVirtualMemory(hProc, ref pBase, IntPtr.Zero, ref RegionSize, SpDi.Win32.Kernel32.MEM_COMMIT | SpDi.Win32.Kernel32.MEM_RESERVE, SpDi.Win32.WinNT.PAGE_READWRITE); SpDi.Native.MEMORYINFOCLASS memoryInfoClass = SpDi.Native.MEMORYINFOCLASS.MemorySectionName; UInt32 MemoryInformationLength = 0x500; UInt32 Retlen = 0; object[] funcargs = { hProc, pMem, memoryInfoClass, pAlloc, MemoryInformationLength, Retlen }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtQueryVirtualMemory", typeof(DELEGATES.NtQueryVirtualMemory), ref funcargs); string FilePath = string.Empty; if (retValue == SpDi.Native.NTSTATUS.Success) { SpDi.Native.UNICODE_STRING sn = (SpDi.Native.UNICODE_STRING)Marshal.PtrToStructure(pAlloc, typeof(SpDi.Native.UNICODE_STRING)); FilePath = Marshal.PtrToStringUni(sn.Buffer); } NtFreeVirtualMemory(hProc, ref pAlloc, ref RegionSize, SpDi.Win32.Kernel32.MEM_RELEASE); if (retValue == SpDi.Native.NTSTATUS.AccessDenied) { throw new UnauthorizedAccessException("Access is denied."); } if (retValue == SpDi.Native.NTSTATUS.AccessViolation) { throw new InvalidOperationException("The specified base address is an invalid virtual address."); } if (retValue == SpDi.Native.NTSTATUS.InfoLengthMismatch) { throw new InvalidOperationException("The MemoryInformation buffer is larger than MemoryInformationLength."); } if (retValue == SpDi.Native.NTSTATUS.InvalidParameter) { throw new InvalidOperationException("The specified base address is outside the range of accessible addresses."); } return(FilePath); }
public static void NtFreeVirtualMemory(IntPtr ProcessHandle, ref IntPtr BaseAddress, ref IntPtr RegionSize, UInt32 FreeType) { object[] funcargs = { ProcessHandle, BaseAddress, RegionSize, FreeType }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtFreeVirtualMemory", typeof(DELEGATES.NtFreeVirtualMemory), ref funcargs); if (retValue == SpDi.Native.NTSTATUS.AccessDenied) { throw new UnauthorizedAccessException("Access is denied."); } if (retValue == SpDi.Native.NTSTATUS.InvalidHandle) { throw new InvalidOperationException("An invalid HANDLE was specified."); } if (retValue != SpDi.Native.NTSTATUS.Success) { throw new InvalidOperationException("There is a mismatch between the type of object that is required by the requested operation and the type of object that is specified in the request."); } }
public static SpDi.Native.NTSTATUS NtQueryInformationProcess(IntPtr hProcess, SpDi.Native.PROCESSINFOCLASS processInfoClass, out IntPtr pProcInfo) { int processInformationLength; UInt32 RetLen = 0; switch (processInfoClass) { case SpDi.Native.PROCESSINFOCLASS.ProcessWow64Information: pProcInfo = Marshal.AllocHGlobal(IntPtr.Size); RtlZeroMemory(pProcInfo, IntPtr.Size); processInformationLength = IntPtr.Size; break; case SpDi.Native.PROCESSINFOCLASS.ProcessBasicInformation: SpDi.Native.PROCESS_BASIC_INFORMATION PBI = new SpDi.Native.PROCESS_BASIC_INFORMATION(); pProcInfo = Marshal.AllocHGlobal(Marshal.SizeOf(PBI)); RtlZeroMemory(pProcInfo, Marshal.SizeOf(PBI)); Marshal.StructureToPtr(PBI, pProcInfo, true); processInformationLength = Marshal.SizeOf(PBI); break; default: throw new InvalidOperationException($"Invalid ProcessInfoClass: {processInfoClass}"); } object[] funcargs = { hProcess, processInfoClass, pProcInfo, processInformationLength, RetLen }; SpDi.Native.NTSTATUS retValue = (SpDi.Native.NTSTATUS)Generic.DynamicAPIInvoke(@"ntdll.dll", @"NtQueryInformationProcess", typeof(DELEGATES.NtQueryInformationProcess), ref funcargs); if (retValue != SpDi.Native.NTSTATUS.Success) { throw new UnauthorizedAccessException("Access is denied."); } pProcInfo = (IntPtr)funcargs[2]; return(retValue); }
public static PE.PE_MANUAL_MAP MapModuleFromDisk(string DLLPath) { if (!File.Exists(DLLPath)) { throw new InvalidOperationException("Filepath not found."); } SpDi.Native.UNICODE_STRING ObjectName = new SpDi.Native.UNICODE_STRING(); SpDi2.Native.RtlInitUnicodeString(ref ObjectName, (@"\??\" + DLLPath)); IntPtr pObjectName = Marshal.AllocHGlobal(Marshal.SizeOf(ObjectName)); Marshal.StructureToPtr(ObjectName, pObjectName, true); SpDi.Native.OBJECT_ATTRIBUTES objectAttributes = new SpDi.Native.OBJECT_ATTRIBUTES(); objectAttributes.Length = Marshal.SizeOf(objectAttributes); objectAttributes.ObjectName = pObjectName; objectAttributes.Attributes = 0x40; SpDi.Native.IO_STATUS_BLOCK ioStatusBlock = new SpDi.Native.IO_STATUS_BLOCK(); IntPtr hFile = IntPtr.Zero; SpDi2.Native.NtOpenFile( ref hFile, SpDi.Win32.Kernel32.FileAccessFlags.FILE_READ_DATA | SpDi.Win32.Kernel32.FileAccessFlags.FILE_EXECUTE | SpDi.Win32.Kernel32.FileAccessFlags.FILE_READ_ATTRIBUTES | SpDi.Win32.Kernel32.FileAccessFlags.SYNCHRONIZE, ref objectAttributes, ref ioStatusBlock, SpDi.Win32.Kernel32.FileShareFlags.FILE_SHARE_READ | SpDi.Win32.Kernel32.FileShareFlags.FILE_SHARE_DELETE, SpDi.Win32.Kernel32.FileOpenFlags.FILE_SYNCHRONOUS_IO_NONALERT | SpDi.Win32.Kernel32.FileOpenFlags.FILE_NON_DIRECTORY_FILE ); IntPtr hSection = IntPtr.Zero; ulong MaxSize = 0; SpDi.Native.NTSTATUS ret = SpDi2.Native.NtCreateSection( ref hSection, (UInt32)SpDi.Win32.WinNT.ACCESS_MASK.SECTION_ALL_ACCESS, IntPtr.Zero, ref MaxSize, SpDi.Win32.WinNT.PAGE_READONLY, SpDi.Win32.WinNT.SEC_IMAGE, hFile ); IntPtr pBaseAddress = IntPtr.Zero; SpDi2.Native.NtMapViewOfSection( hSection, (IntPtr)(-1), ref pBaseAddress, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, ref MaxSize, 0x2, 0x0, SpDi.Win32.WinNT.PAGE_READWRITE ); PE.PE_MANUAL_MAP SecMapObject = new PE.PE_MANUAL_MAP { PEINFO = SpDi2.Generic.GetPeMetaData(pBaseAddress), ModuleBase = pBaseAddress }; return(SecMapObject); }