Exemple #1
0
        public static long ResolveReadFileOffset(long filePointer, LARGE_INTEGER *byteOffset)
        {
            var offset    = filePointer;
            var reqOffset = (byteOffset != null || (byteOffset != null && byteOffset->HighPart == -1 && byteOffset->LowPart == FILE_USE_FILE_POINTER_POSITION)) ?
                            byteOffset->QuadPart : -1;

            return(reqOffset == -1 ? offset : reqOffset);
        }
 public static extern BOOL RollforwardTransactionManager(HANDLE TransactionManagerHandle, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *TmVirtualClock);
 public static extern BOOL SinglePhaseReject(HANDLE EnlistmentHandle, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *TmVirtualClock);
Exemple #4
0
 public HRESULT GetFileID(LARGE_INTEGER *pliFileID)
 {
     return(((delegate * unmanaged <IFileSystemBindData2 *, LARGE_INTEGER *, int>)(lpVtbl[6]))((IFileSystemBindData2 *)Unsafe.AsPointer(ref this), pliFileID));
 }
Exemple #5
0
 public abstract unsafe NtStatus NtReadFileImpl(IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext,
                                                ref IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, LARGE_INTEGER *byteOffset, IntPtr key);
Exemple #6
0
 public static extern int SetFilePointerEx([NativeTypeName("HANDLE")] IntPtr hFile, LARGE_INTEGER liDistanceToMove, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *lpNewFilePointer, [NativeTypeName("DWORD")] uint dwMoveMethod);
        public override unsafe Native.NtStatus NtReadFileImpl(IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext,
                                                              ref Native.IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, LARGE_INTEGER *byteOffset, IntPtr key)
        {
            NtStatus result;
            var      packHandle = mPacksByHandle[handle];
            var      pack       = packHandle.Instance;
            var      offset     = packHandle.FilePointer;
            var      reqOffset  = (byteOffset != null || (byteOffset != null && byteOffset->HighPart == -1 && byteOffset->LowPart == FILE_USE_FILE_POINTER_POSITION)) ?
                                  byteOffset->QuadPart : -1;
            var effOffset  = reqOffset == -1 ? offset : reqOffset;
            var dataOffset = pack.Native.Data - pack.Native.Ptr;

            if ((effOffset + length) <= dataOffset)
            {
                // Header read
                if (effOffset >= sizeof(DwPackHeader))
                {
                    // Ensure entry is redirected before entry is read
                    // This improves startup times greatly, as otherwise thousands of redirections could potentially have to be done at
                    // startup
                    var entryIndex = ( int )((effOffset - sizeof(DwPackHeader)) / sizeof(DwPackEntry));
                    if (!pack.Entries[entryIndex].RedirectAttempted)
                    {
                        pack.Entries[entryIndex].Redirect(mModDb);
                    }
                }

                Unsafe.CopyBlock(buffer, pack.Native.Ptr + effOffset, length);
                SetBytesRead(handle, ( int )offset, ( int )length, ref ioStatus);
                result = NtStatus.Success;
            }
            else if (effOffset >= dataOffset && effOffset < pack.VirtualFileSize)
            {
                result = ReadFile(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key, pack, offset, effOffset);
            }
            else
            {
                mLogger.Error($"{pack.FileName} Hnd: {handle} Unexpected read request!! Offset: {effOffset:X8} Length: {length:X8}");
                result = mHooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key);
            }

            if (result != NtStatus.Success)
            {
                mLogger.Error($"{pack.FileName} Hnd: {handle} NtReadFile failed with {result}!!!");
            }

            return(result);
        }
 public static extern int QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
Exemple #9
0
 public static extern int QueryPerformanceFrequency([NativeTypeName("LARGE_INTEGER *")] LARGE_INTEGER *lpFrequency);
Exemple #10
0
 public static extern int QueryPerformanceCounter([NativeTypeName("LARGE_INTEGER *")] LARGE_INTEGER *lpPerformanceCount);
 public unsafe static extern uint ZwMapViewOfSection(IntPtr section, IntPtr process, ref IntPtr baseAddr, int zeroBits, ulong commitSize, LARGE_INTEGER *stuff, ref ulong viewSize, int inheritDispo, uint alloctype, uint prot);
Exemple #12
0
 internal static object ResolveReadFileOffset(object filePointer, LARGE_INTEGER *byteOffset) => throw new NotImplementedException();
Exemple #13
0
 public static extern int NtCreateFile([NativeTypeName("PHANDLE")] HANDLE *FileHandle, [NativeTypeName("ACCESS_MASK")] uint DesiredAccess, [NativeTypeName("POBJECT_ATTRIBUTES")] OBJECT_ATTRIBUTES *ObjectAttributes, [NativeTypeName("PIO_STATUS_BLOCK")] IO_STATUS_BLOCK *IoStatusBlock, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *AllocationSize, [NativeTypeName("ULONG")] uint FileAttributes, [NativeTypeName("ULONG")] uint ShareAccess, [NativeTypeName("ULONG")] uint CreateDisposition, [NativeTypeName("ULONG")] uint CreateOptions, [NativeTypeName("PVOID")] void *EaBuffer, [NativeTypeName("ULONG")] uint EaLength);
 public static extern BOOL GetCurrentClockTransactionManager(HANDLE TransactionManagerHandle, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *TmVirtualClock);
        public override unsafe Native.NtStatus NtReadFileImpl(IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext,
                                                              ref Native.IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, LARGE_INTEGER *byteOffset, IntPtr key)
        {
            var cpk       = mCpkByHandle[handle];
            var effOffset = Utils.ResolveReadFileOffset(cpk.FilePointer, byteOffset);

            Unsafe.CopyBlock(buffer, (byte *)cpk.Instance.Native.Ptr + effOffset, length);
            SetBytesRead(handle, (int)cpk.FilePointer, (int)length, ref ioStatus);
            return(NtStatus.Success);
        }
 public static extern int QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);
 public static extern BOOL GetFileSizeEx(HANDLE hFile, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *lpFileSize);
        private NtStatus ReadFile(IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext,
                                  ref Native.IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, LARGE_INTEGER *byteOffset, IntPtr key,
                                  VirtualDwPack pack, long offset, long effOffset)
        {
            // File data read
            NtStatus result = NtStatus.Success;

            for (int i = 0; i < pack.Entries.Count; i++)
            {
                var entry      = pack.Entries[i];
                var dataOffset = (pack.Native.Data + entry.Native->DataOffset) - pack.Native.Ptr;
                if (effOffset < dataOffset || effOffset >= (dataOffset + entry.Native->CompressedSize))
                {
                    continue;
                }

                var fileDataOffset = effOffset - dataOffset;
                var readEndOffset  = fileDataOffset + length;
                if (readEndOffset > entry.Native->CompressedSize)
                {
                    continue;
                }

                // Make sure the file has been redirected
                // This is done as late as possible to improve startup times
                if (!entry.IsRedirected)
                {
                    mLogger.Info($"{pack.FileName} Hnd: {handle} {entry.Native->Path} Idx: ({i}) Data access Offset: 0x{effOffset:X8} Length: 0x{length:X8}");
                    result = mHooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key);
                }
                else
                {
                    mLogger.Info($"{pack.FileName} Hnd: {handle} {entry.Native->Path} Idx: ({i}) Data access Offset: 0x{effOffset:X8} Length: 0x{length:X8} redirected to {entry.RedirectedFilePath}");
                    result = NtStatus.Success;

                    if (fileDataOffset < 0)
                    {
                        mLogger.Error($"{pack.FileName} Hnd: {handle} {entry.Native->Path} Idx: ({i}) Offset is before start of data!!!");
                    }
                    else if (fileDataOffset > entry.RedirectedFileSize)
                    {
                        mLogger.Error($"{pack.FileName} Hnd: {handle} {entry.Native->Path} Idx: ({i}) Offset is after end of data!!!");
                    }

                    mLogger.Debug($"{pack.FileName} Hnd: {handle} {entry.Native->Path} Idx: ({i}) Reading 0x{length:X8} bytes from redirected file at offset 0x{fileDataOffset:X8}");

                    // Get cached file stream if the file was previously opened or open a new file
                    Stream redirectedStream;
                    if (mCachedFile == entry)
                    {
                        redirectedStream = mCachedFileStream;
                    }
                    else
                    {
                        mCachedFileStream?.Close();
                        mCachedFile       = entry;
                        mCachedFileStream = redirectedStream = entry.OpenRead();
                    }

                    // Read from redirected file into the buffer
                    try
                    {
                        redirectedStream.Seek(fileDataOffset, SeekOrigin.Begin);
                        var readBytes = redirectedStream.Read(new Span <byte>(( void * )buffer, ( int )length));
                        SetBytesRead(handle, ( int )offset, ( int )length, ref ioStatus);

                        if (readBytes != length)
                        {
                            mLogger.Error($"{pack.FileName} Hnd: {handle} {entry.Native->Path} Idx: ({i}) File read length doesnt match requested read length!! Expected 0x{length:X8}, Actual 0x{readBytes:X8}");
                        }

                        mLogger.Debug($"{pack.FileName} Hnd: {handle} {entry.Native->Path} Idx: ({i}) Wrote redirected file to buffer");
                    }
                    catch (Exception e)
                    {
                        mLogger.Debug($"{pack.FileName} Hnd: {handle} Idx: {i} {entry.Native->Path} Unhandled exception thrown during reading {entry.RedirectedFilePath}: {e}");
                    }
                }

                // Return early, we're done here
                return(result);
            }

            mLogger.Error($"{pack.FileName} Hnd: {handle} Unhandled file data read request!! Offset: 0x{effOffset:X8} Length: 0x{length:X8}");
            return(mHooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key));
        }
Exemple #19
0
        private unsafe NtStatus NtReadFileImpl(IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext,
                                               ref IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, LARGE_INTEGER *byteOffset, IntPtr key)
        {
            lock ( _readLock )
            {
                foreach (var filter in _filters)
                {
                    if (filter.Accept(handle))
                    {
                        try
                        {
                            return(filter.NtReadFileImpl(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"[modloader:FileAccessServer] Hnd: {handle} NtReadFileImpl exception thrown: {e}");
                            return(_hooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key));
                        }
                    }
                }

                return(_hooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key));
            }
        }
Exemple #20
0
 public static extern int GetFileSizeEx([NativeTypeName("HANDLE")] IntPtr hFile, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *lpFileSize);
 public static extern BOOL ReadOnlyEnlistment(HANDLE EnlistmentHandle, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *TmVirtualClock);
 public HRESULT CheckInterfaceSupport([NativeTypeName("const GUID &")] Guid *InterfaceName, LARGE_INTEGER *pUMDVersion)
 {
     return(((delegate * unmanaged <IDXGIAdapter *, Guid *, LARGE_INTEGER *, int>)(lpVtbl[9]))((IDXGIAdapter *)Unsafe.AsPointer(ref this), InterfaceName, pUMDVersion));
 }
 public static extern BOOL RollbackComplete(HANDLE EnlistmentHandle, [NativeTypeName("PLARGE_INTEGER")] LARGE_INTEGER *TmVirtualClock);
 public int CheckInterfaceSupport([NativeTypeName("const GUID &")] Guid *InterfaceName, [NativeTypeName("LARGE_INTEGER *")] LARGE_INTEGER *pUMDVersion)
 {
     return(((delegate * stdcall <IDXGIAdapter2 *, Guid *, LARGE_INTEGER *, int>)(lpVtbl[9]))((IDXGIAdapter2 *)Unsafe.AsPointer(ref this), InterfaceName, pUMDVersion));
 }
Exemple #25
0
        private unsafe NtStatus NtReadFileImpl(IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext,
                                               ref IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, LARGE_INTEGER *byteOffset, IntPtr key)
        {
#if DEBUG
            try
            {
#endif
            foreach (var filter in _filters)
            {
                if (filter.Accept(handle))
                {
                    return(filter.NtReadFileImpl(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key));
                }
            }
#if DEBUG
        }

        catch (Exception e)
        {
            _logger.WriteLine($"[{nameof(modloader)}:{nameof(FileAccessServer)}] {nameof(NtReadFileImpl)} Exception Thrown: Hnd: {handle} {e.Message}\nStack Trace: {e.StackTrace}");
        }
#endif

            return(_hooks.NtReadFileHook.OriginalFunction(handle, hEvent, apcRoutine, apcContext, ref ioStatus, buffer, length, byteOffset, key));
        }