Ejemplo n.º 1
1
 public static void VirtualMemorySetProtection(void* pointer, IntPtr size, MemoryProtection protection)
 {
     int old;
     switch (protection)
     {
         case MemoryProtection.Execute:
             Kernel32.VirtualProtect(pointer, size, Kernel32.VirtualMemProtectionFlag.PageExecute, out old);
             break;
         case MemoryProtection.ReadWrite:
             Kernel32.VirtualProtect(pointer, size, Kernel32.VirtualMemProtectionFlag.PageReadWrite, out old);
             break;
     }
 }
Ejemplo n.º 2
1
        public MemoryMappedFileStream(string name, uint maxLength, MemoryProtection protection)
        {
            if(string.IsNullOrEmpty(name))
            {
                throw(new ArgumentException("Argument_Name"));
            }

            if(maxLength < 0)
            {
                throw(new ArgumentOutOfRangeException("ArgumentOutOfRange_MaxLength"));
            }

            objectName = name;

            if (protection < MemoryProtection.PageNoAccess ||
                protection > MemoryProtection.SecReserve)
            {
                throw new ArgumentOutOfRangeException("ArgumentOutOfRange_Enum");
            }

            if( maxLength == 0 )
            {
                mapLength = 5120;
            }
            else
            {
                mapLength = maxLength;
            }

            memProtection = protection;

            isReadable = true;
            isWritable = true;
            isSeekable = true;

            // Initialize mapViewPointer
            mapViewPointer = new IntPtr(-1);

            SECURITY_ATTRIBUTES sa = SECURITY_ATTRIBUTES.GetNullDacl();
            try
            {
                mapHandle = MemoryMappedFileHelper.CreateFileMapping(
                                                    IntPtr.Zero,
                                                    sa,
                                                    memProtection,
                                                    0,
                                                    mapLength,
                                                    objectName);
            }
            finally
            {
                Marshal.FreeHGlobal(sa.lpSecurityDescriptor);
                sa.lpSecurityDescriptor = IntPtr.Zero;
            }

            if(mapHandle == IntPtr.Zero )
            {
                uint lastError = MemoryMappedFileHelper.GetLastError();
                throw new IOException(
                    MemoryMappedFileHelper.GetWin32ErrorMessage( lastError ) );
            }
        }
Ejemplo n.º 3
0
 public PhysicalPagesMapping Map(IntPtr address, MemoryProtection protection)
 {
     IntPtr allocAddress = ProcessHandle.Current.AllocateMemory(
         address,
         _count * Windows.PageSize,
         MemoryFlags.Reserve | MemoryFlags.Physical,
         protection
         );
     if (!Win32.MapUserPhysicalPages(
         allocAddress,
         new IntPtr(_count),
         _pfnArray
         ))
         Win32.ThrowLastError();
     return new PhysicalPagesMapping(this, allocAddress);
 }
Ejemplo n.º 4
0
 static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 5
0
 internal static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, MemoryProtection flNewProtect, ref MemoryProtection lpflOldProtect);
		public static extern bool VirtualProtectEx(
			IntPtr hProcess,
			IntPtr lpAddress,
			uint dwSize,
			MemoryProtection flNewProtect,
			[Out] MemoryProtection lpflOldProtect);
Ejemplo n.º 7
0
 public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, 
     int dwSize, MemoryProtection prot, out MemoryProtection oldProt);
Ejemplo n.º 8
0
 public static extern IntPtr VirtualAlloc(IntPtr blockAddress, UIntPtr sizeInBytes,
     MemoryAllocationType allocationType, MemoryProtection protection);
 public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
 private static extern UIntPtr VirtualAlloc(
     UIntPtr lpAddress,
     UIntPtr dwSize,
     AllocationType flAllocationType,
     MemoryProtection flProtect);
Ejemplo n.º 11
0
        internal MemoryProtection ProtectVirtualMemory(IntPtr baseAddress, int protectionSize, MemoryProtection protectionType)
        {
            if (!VirtualProtectEx(_processHandle, baseAddress, protectionSize, protectionType, out var oldProtectionType))
            {
                ExceptionHandler.ThrowWin32Exception("Failed to protect a region of virtual memory in the remote process");
            }

            return(oldProtectionType);
        }
Ejemplo n.º 12
0
 private static extern IntPtr VirtualAlloc(UInt32 lpStartAddr, UInt32 size, UInt32 flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 13
0
 public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, int nSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
Ejemplo n.º 14
0
 /// <summary>
 /// Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process.
 /// Memory allocated by this function is automatically initialized to zero.
 /// </summary>
 /// <param name="sizeInBytes">The size of the region, in bytes</param>
 /// <param name="allocationType">The type of memory allocation.</param>
 /// <param name="protection">The memory protection for the region of pages to be allocated.</param>
 /// <returns>The base address of the allocated region of pages.</returns>
 public static void* VirtualAlloc(ulong sizeInBytes, MemoryAllocationType allocationType, MemoryProtection protection)
 {
     var pointer = VirtualAlloc(IntPtr.Zero, new UIntPtr(sizeInBytes), allocationType, protection).ToPointer();
     if (pointer == null)
         throw new InvalidOperationException(Marshal.GetLastWin32Error().ToString());
     return pointer;
 }
Ejemplo n.º 15
0
 public static int mprotect(IntPtr addr, UIntPtr len, MemoryProtection prot) => mprotect(addr, len, (int)prot);
Ejemplo n.º 16
0
 private static extern bool __VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress,
                                               UIntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
Ejemplo n.º 17
0
 static extern IntPtr VirtualAllocEx(IntPtr hProcess,
                                     IntPtr lpAddress,
                                     int dwSize,
                                     AllocationType flAllocationType,
                                     MemoryProtection flProtect);
Ejemplo n.º 18
0
 public static void VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress,
                                     UIntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect)
 {
     if (!__VirtualProtectEx(hProcess, lpAddress, dwSize, flNewProtect, out lpflOldProtect))
     {
         throw new Win32Exception();
     }
 }
Ejemplo n.º 19
0
 public static void VirtualMemorySetProtection(void* pointer, IntPtr size, MemoryProtection protection)
 {
     NativeUtilsImplementation.VirtualMemorySetProtection(pointer, size, protection);
 }
Ejemplo n.º 20
0
 public static extern int VirtualAllocEx(IntPtr hProcess, Int32 lpAddress,
                                         Int32 dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 21
0
        public void Protect(SafeProcessHandle handle, uint offset, uint size, MemoryProtection newProtect)
        {
            if (Address == IntPtr.Zero)
                throw new ObjectDisposedException("RemoteMemoryRegion");
            if ((offset + size) > (Size))
                throw new ArgumentException("Size too large for region");

            MemoryProtection oldProtect;
            int result = Win32.VirtualProtectEx(
                handle.DangerousGetHandle(),
                (uint)(Address.ToInt64() + offset),
                size, newProtect, out oldProtect
            );

            if (result == 0) {
                var error = Win32.GetLastError();
                throw new Exception(String.Format("Protect failed: Error {0:x8}", error));
            }
        }
Ejemplo n.º 22
0
 public static IntPtr Alloc(Int32 size, MemoryProtection protection, AllocationType allocationType)
 {
     return(Memory.Alloc(IntPtr.Zero, size, protection, allocationType));
 }
Ejemplo n.º 23
0
 public static extern DWORD_PTR VirtualAllocEx( IntPtr hProcess, DWORD_PTR lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect );
Ejemplo n.º 24
0
 public static IntPtr Alloc(IntPtr address, Int32 size, MemoryProtection protection, AllocationType allocationType)
 {
     return(Kernel32.VirtualAlloc(address, size, allocationType, protection));
 }
Ejemplo n.º 25
0
 public PhysicalPagesMapping Map(MemoryProtection protection)
 {
     return this.Map(IntPtr.Zero, protection);
 }
Ejemplo n.º 26
0
 public static IntPtr Alloc(Int32 size, MemoryProtection protection)
 {
     return(Memory.Alloc(size, protection, AllocationType.Commit));
 }
Ejemplo n.º 27
0
        public static Win32FileMapAccess GetWin32FileMapAccess( 
			MemoryProtection protection )
        {
            switch ( protection )
            {
                case MemoryProtection.PageReadOnly:
                    return Win32FileMapAccess.FILE_MAP_READ;
                case MemoryProtection.PageWriteCopy:
                    return Win32FileMapAccess.FILE_MAP_WRITE;
                default:
                    return Win32FileMapAccess.FILE_MAP_ALL_ACCESS;
            }
        }
Ejemplo n.º 28
0
 public static extern ulong VirtualAllocEx(IntPtr hProcess, ulong lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 29
0
 internal static extern IntPtr VirtualAllocEx(SafeProcessHandle processHandle, IntPtr address, SizeT size, AllocationType flAllocationType,
     MemoryProtection flProtect);
Ejemplo n.º 30
0
 public SafeVirtualMemoryHandle(IntPtr processHandle, Int64 address, Int64 size, AllocationType allocation, MemoryProtection protection)
     : base(true)
 {
     _processHandle = processHandle;
     _size          = size;
     handle         = Kernel32.VirtualAllocEx(processHandle, (IntPtr)address, (IntPtr)_size, allocation, protection);
     if (IsInvalid)
     {
         throw new Win32Exception();
     }
 }
Ejemplo n.º 31
0
 public static IntPtr mmap(IntPtr addr, UIntPtr length, MemoryProtection prot, int flags, int fd, IntPtr offset) => mmap(addr, length, (int)prot, flags, fd, offset);
Ejemplo n.º 32
0
 private static extern IntPtr VirtualAllocEx(SafeMemoryHandle hProcess, IntPtr lpAddress,
                                             UIntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
 public static extern void *VirtualAlloc(void *lpAddress, SizeT dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 34
0
 public SafeVirtualMemoryHandle Allocate(Int64 size, AllocationType allocation, MemoryProtection protection)
 {
     return(new SafeVirtualMemoryHandle(handle, 0, size, allocation, protection));
 }
Ejemplo n.º 35
0
 static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType lAllocationType, MemoryProtection flProtect);
Ejemplo n.º 36
0
        public static bool Reprotect(IntPtr address, IntPtr size, Memory.MemoryProtection protection)
        {
            MemoryProtection prot = GetProtection(protection);

            return(VirtualProtect(address, size, prot, out _));
        }
Ejemplo n.º 37
0
 public static extern bool VirtualProtect(IntPtr address, uint size, MemoryProtection newProtect, out MemoryProtection oldProtect);
Ejemplo n.º 38
0
 public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress,
                                            IntPtr dwSize, MemoryProtection flNewProtect, ref MemoryProtection lpflOldProtect);
Ejemplo n.º 39
0
 internal static extern IntPtr VirtualAllocEx(SafeProcessHandle hProcess, uint dwAddress, int nSize, AllocationType dwAllocationType, MemoryProtection dwProtect);
Ejemplo n.º 40
0
 public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize,
                                          MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
Ejemplo n.º 41
0
 public static IntPtr VirtualAlloc(UIntPtr sizeInBytes, MemoryAllocationType allocationType,
     MemoryProtection protection)
 {
     return VirtualAlloc(IntPtr.Zero, sizeInBytes, allocationType, protection);
 }
Ejemplo n.º 42
0
 public static extern bool VirtualProtectEx([In] Handle hProcess, [In] IntPtr lpAddress, [In] int dwSize,
                                            [In] MemoryProtection flNewProtect, [Out] out MemoryProtection lpflOldProtect);
Ejemplo n.º 43
0
Archivo: Win32.cs Proyecto: sq/PETools
 public static extern int VirtualProtectEx(
     IntPtr hProcess, UInt32 lpAddress,
     uint dwSize, MemoryProtection flNewProtect,
     out MemoryProtection flOldProtect
 );
Ejemplo n.º 44
0
 public static extern bool VirtualProtect(byte *lpAddress, UIntPtr dwSize,
                                          MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
Ejemplo n.º 45
0
 public static extern IntPtr VirtualAlloc(IntPtr address, Int32 size, AllocationType allocationType, MemoryProtection protection);
Ejemplo n.º 46
0
 public static extern byte *VirtualAlloc(byte *lpAddress, UIntPtr dwSize,
                                         AllocationType flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 47
0
        public MemoryFileSystem(string fileName, MfsOpenMode mode, bool readOnly, MfsParameters createParams)
        {
            FileCreationDispositionWin32 cdWin32;

            if (readOnly && mode != MfsOpenMode.Open)
                throw new ArgumentException("Invalid mode for read only access.");

            switch (mode)
            {
                case MfsOpenMode.Open:
                    cdWin32 = FileCreationDispositionWin32.OpenExisting;
                    break;
                default:
                case MfsOpenMode.OpenIf:
                    cdWin32 = FileCreationDispositionWin32.OpenAlways;
                    break;
                case MfsOpenMode.OverwriteIf:
                    cdWin32 = FileCreationDispositionWin32.CreateAlways;
                    break;
            }

            using (FileHandle fhandle = FileHandle.CreateWin32(
                fileName,
                FileAccess.GenericRead | (!readOnly ? FileAccess.GenericWrite : 0),
                FileShareMode.Read,
                cdWin32
                ))
            {
                bool justCreated = false;

                _readOnly = readOnly;
                _protection = !readOnly ? MemoryProtection.ReadWrite : MemoryProtection.ReadOnly;

                if (fhandle.FileSize == 0)
                {
                    if (readOnly)
                    {
                        throw new MfsInvalidFileSystemException();
                    }
                    
                    // File is too small. Make it 1 byte large and we'll deal with it soon.
                    fhandle.SetEnd(1);
                }

                _section = new Section(fhandle, _protection);

                _blockSize = MfsBlockSizeBase; // fake block size to begin with; we'll fix it up later.

                if (fhandle.FileSize < _blockSize)
                {
                    if (readOnly)
                    {
                        throw new MfsInvalidFileSystemException();
                    }
                    
                    // We're creating a new file system. We need the correct block size now.
                    if (createParams != null)
                        this._blockSize = createParams.BlockSize;
                    else
                        this._blockSize = MfsDefaultBlockSize;

                    this._section.Extend(this._blockSize);

                    using (SectionView view = this._section.MapView(0, this._blockSize, this._protection))
                    {
                        this.InitializeFs((MfsFsHeader*)view.Memory, createParams);
                    }

                    justCreated = true;
                }

                _header = (MfsFsHeader*)this.ReferenceBlock(0);

                // Check the magic.
                if (_header->Magic != MfsMagic)
                    throw new MfsInvalidFileSystemException();

                // Set up the local constants.
                _blockSize = _header->BlockSize;
                _cellSize = _header->CellSize;

                // Backwards compatibility.
                if (_blockSize == 0)
                    _blockSize = MfsDefaultBlockSize;
                if (_cellSize == 0)
                    _cellSize = MfsDefaultCellSize;

                // Validate the parameters.
                this.ValidateFsParameters(_blockSize, _cellSize);

                _blockMask = _blockSize - 1;
                _cellCount = _blockSize / _cellSize;
                _dataCellDataMaxLength = _cellSize - MfsDataCell.DataOffset;

                // Remap block 0 with the correct block size.
                this.DereferenceBlock(0);

                // If we just created a new file system, fix the section size.
                if (justCreated)
                    _section.Extend(_blockSize);

                _header = (MfsFsHeader*)this.ReferenceBlock(0);

                // Set up the root object.
                _rootObjectMo = new MemoryObject(this, _rootObjectCellId, true);

                if (_header->NextFreeBlock != 1 && !readOnly)
                {
                    ushort lastBlockId = (ushort)(_header->NextFreeBlock - 1);

                    this.ReferenceBlock(lastBlockId);
                    _cachedLastBlockView = _views[lastBlockId];
                }
            }
        }
Ejemplo n.º 48
0
 public static extern int VirtualAllocEx(IntPtr hProcess, Int32 lpAddress,
    Int32 dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 49
0
 public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize,
    MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
 internal static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr address, int size, AllocationType allocationType, MemoryProtection protect);
Ejemplo n.º 51
0
        public PhysicalPagesMapping Map(IntPtr address, MemoryProtection protection)
        {
            // Reserve an address range.
            IntPtr allocAddress = ProcessHandle.Current.AllocateMemory(
                address,
                _count * Windows.PageSize,
                MemoryFlags.Reserve | MemoryFlags.Physical,
                protection
                );

            // Map the physical memory into the address range.
            if (!Win32.MapUserPhysicalPages(
                allocAddress,
                new IntPtr(_count),
                _pfnArray
                ))
                Win32.Throw();

            return new PhysicalPagesMapping(this, allocAddress);
        }
Ejemplo n.º 52
0
 private static extern int VirtualProtect(IntPtr lpAddress, IntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
Ejemplo n.º 53
0
        public static extern IntPtr CreateFileMapping(
			IntPtr hFile, // Handle to file
            SECURITY_ATTRIBUTES lpAttributes, // Security
			MemoryProtection flProtect, // protection
			uint dwMaximumSizeHigh, // High-order DWORD of size
			uint dwMaximumSizeLow, // Low-order DWORD of size
			string lpName // Object name
			);
Ejemplo n.º 54
0
 internal static extern bool VirtualProtect(IntPtr baseAddress, int protectionSize, MemoryProtection protectionType, out MemoryProtection oldProtectionType);
Ejemplo n.º 55
0
        private string GetProtectStr(MemoryProtection protect)
        {
            string protectStr;

            if (protect == MemoryProtection.AccessDenied)
                protectStr = "";
            else if ((protect & MemoryProtection.Execute) != 0)
                protectStr = "X";
            else if ((protect & MemoryProtection.ExecuteRead) != 0)
                protectStr = "RX";
            else if ((protect & MemoryProtection.ExecuteReadWrite) != 0)
                protectStr = "RWX";
            else if ((protect & MemoryProtection.ExecuteWriteCopy) != 0)
                protectStr = "WCX";
            else if ((protect & MemoryProtection.NoAccess) != 0)
                protectStr = "NA";
            else if ((protect & MemoryProtection.ReadOnly) != 0)
                protectStr = "R";
            else if ((protect & MemoryProtection.ReadWrite) != 0)
                protectStr = "RW";
            else if ((protect & MemoryProtection.WriteCopy) != 0)
                protectStr = "WC";
            else
                protectStr = "?";

            if ((protect & MemoryProtection.Guard) != 0)
                protectStr += "+G";
            if ((protect & MemoryProtection.NoCache) != 0)
                protectStr += "+NC";
            if ((protect & MemoryProtection.WriteCombine) != 0)
                protectStr = "+WCM";

            return protectStr;
        }
 public static extern IntPtr VirtualAllocEx(SafeProcessHandle processHandle, IntPtr address, UIntPtr size, AllocationType allocationType, MemoryProtection memoryProtection);
Ejemplo n.º 57
0
     public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize,
 AllocationType flAllocationType, MemoryProtection flProtect);
Ejemplo n.º 58
0
 public static extern IntPtr VirtualAlloc(IntPtr address, Int32 size, AllocationType allocationType, MemoryProtection protection);
Ejemplo n.º 59
0
 internal static extern IntPtr VirtualAlloc(IntPtr startAddress, UIntPtr size, AllocationType allocationType, MemoryProtection protect);
Ejemplo n.º 60
0
 public MemoryMappedFileStream(string name, MemoryProtection protection)
     : this(name,0,protection)
 {
 }