Exemple #1
0
        private int QueryInformationFileImpl(IntPtr hfile, out Native.Native.IO_STATUS_BLOCK ioStatusBlock, void *fileInformation, uint length, Native.Native.FileInformationClass fileInformationClass)
        {
            lock (_getInfoLock)
            {
                if (_handleToInfoMap.ContainsKey(hfile) && fileInformationClass == Native.Native.FileInformationClass.FileStandardInformation)
                {
                    var result      = _getFileSizeHook.OriginalFunction(hfile, out ioStatusBlock, fileInformation, length, fileInformationClass);
                    var information = (Native.Native.FILE_STANDARD_INFORMATION *)fileInformation;
                    var newFileSize = OnGetFileSize(hfile);
                    if (newFileSize != -1)
                    {
                        information->EndOfFile      = newFileSize;
                        information->AllocationSize = Utilities.RoundUp(newFileSize, 4096);
                    }

#if DEBUG
                    Console.WriteLine($"[ONEHook] QueryInformationFile: Alloc Size: {information->AllocationSize}, EndOfFile: {information->EndOfFile}");
#endif

                    return(result);
                }

                return(_getFileSizeHook.OriginalFunction(hfile, out ioStatusBlock, fileInformation, length, fileInformationClass));
            }
        }
Exemple #2
0
        private int SetInformationFileImpl(IntPtr hfile, out Native.Native.IO_STATUS_BLOCK ioStatusBlock, void *fileInformation, uint length, Native.Native.FileInformationClass fileInformationClass)
        {
            lock (_setInfoLock)
            {
                if (_handleToInfoMap.ContainsKey(hfile) && fileInformationClass == Native.Native.FileInformationClass.FilePositionInformation)
                {
                    var pointer = *(long *)fileInformation;
                    _handleToInfoMap[hfile].FilePointer = pointer;
                }

                return(_setFilePointerHook.OriginalFunction(hfile, out ioStatusBlock, fileInformation, length, fileInformationClass));
            }
        }
Exemple #3
0
        private int NtCreateFileImpl(out IntPtr handle, FileAccess access, ref Native.Native.OBJECT_ATTRIBUTES objectAttributes, ref Native.Native.IO_STATUS_BLOCK ioStatus, ref long allocSize, uint fileAttributes, FileShare share, uint createDisposition, uint createOptions, IntPtr eaBuffer, uint eaLength)
        {
            lock (_createLock)
            {
                string oldFileName = objectAttributes.ObjectName.ToString();
                if (!TryGetFullPath(oldFileName, out var newFilePath))
                {
                    return(_createFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes, share, createDisposition, createOptions, eaBuffer, eaLength));
                }

                // Check if ONE file and register if it is.
                if (newFilePath.Contains(Constants.OneExtension, StringComparison.OrdinalIgnoreCase))
                {
                    var result = _createFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes, share, createDisposition, createOptions, eaBuffer, eaLength);
                    DisableRedirectionHooks();
                    if (IsOneFile(newFilePath))
                    {
#if DEBUG
                        Console.WriteLine($"[ONEHook] ONE Handle Opened: {handle}, File: {newFilePath}");
#endif
                        _handleToInfoMap[handle] = new FileInfo(newFilePath, 0);
                        OnOneHandleOpened(handle, newFilePath);
                    }
                    EnableRedirectionHooks();
                    return(result);
                }

                var ntStatus = _createFileHook.OriginalFunction(out handle, access, ref objectAttributes, ref ioStatus, ref allocSize, fileAttributes, share, createDisposition, createOptions, eaBuffer, eaLength);

                // Invalidate Duplicate Handles (until we implement NtClose hook).
                if (_handleToInfoMap.ContainsKey(handle))
                {
                    _handleToInfoMap.TryRemove(handle, out var value);
#if DEBUG
                    Console.WriteLine($"[ONEHook] Removed old disposed handle: {handle}, File: {value.FilePath}");
#endif
                }

                return(ntStatus);
            }
        }
Exemple #4
0
        private unsafe int NtReadFileImpl(IntPtr handle, IntPtr hEvent, IntPtr *apcRoutine, IntPtr *apcContext, ref Native.Native.IO_STATUS_BLOCK ioStatus, byte *buffer, uint length, long *byteOffset, IntPtr key)
        {
            lock (_readLock)
            {
                if (_handleToInfoMap.ContainsKey(handle))
                {
                    long offset          = _handleToInfoMap[handle].FilePointer;
                    long requestedOffset = byteOffset != (void *)0 ? *byteOffset : -1;
#if DEBUG
                    Console.WriteLine($"[ONEHook] Read Request, Buffer: {(long)buffer:X}, Length: {length}, Offset (via SetInformationFile): {offset}, Requested Offset (Optional): {requestedOffset}");
#endif

                    DisableRedirectionHooks();
                    bool result;
                    int  numReadBytes;
                    result = requestedOffset != -1 ? OnOneReadData(handle, buffer, length, requestedOffset, out numReadBytes)
                                                   : OnOneReadData(handle, buffer, length, offset, out numReadBytes);
                    EnableRedirectionHooks();

                    if (result)
                    {
                        offset += length;
                        SetInformationFileImpl(handle, out _, &offset, sizeof(long), Native.Native.FileInformationClass.FilePositionInformation);

                        // Set number of read bytes.
                        ioStatus.Status      = 0;
                        ioStatus.Information = new IntPtr(numReadBytes);
                        return(0);
                    }
                }

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