예제 #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;
     }
 }
예제 #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 ) );
            }
        }
예제 #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);
 }
예제 #4
0
 static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
예제 #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);
예제 #7
0
 public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, 
     int dwSize, MemoryProtection prot, out MemoryProtection oldProt);
예제 #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);
예제 #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);
        }
예제 #12
0
 private static extern IntPtr VirtualAlloc(UInt32 lpStartAddr, UInt32 size, UInt32 flAllocationType, MemoryProtection flProtect);
예제 #13
0
 public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, int nSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
예제 #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;
 }
예제 #15
0
 public static int mprotect(IntPtr addr, UIntPtr len, MemoryProtection prot) => mprotect(addr, len, (int)prot);
예제 #16
0
 private static extern bool __VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress,
                                               UIntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
예제 #17
0
 static extern IntPtr VirtualAllocEx(IntPtr hProcess,
                                     IntPtr lpAddress,
                                     int dwSize,
                                     AllocationType flAllocationType,
                                     MemoryProtection flProtect);
예제 #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();
     }
 }
예제 #19
0
 public static void VirtualMemorySetProtection(void* pointer, IntPtr size, MemoryProtection protection)
 {
     NativeUtilsImplementation.VirtualMemorySetProtection(pointer, size, protection);
 }
예제 #20
0
 public static extern int VirtualAllocEx(IntPtr hProcess, Int32 lpAddress,
                                         Int32 dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
예제 #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));
            }
        }
예제 #22
0
 public static IntPtr Alloc(Int32 size, MemoryProtection protection, AllocationType allocationType)
 {
     return(Memory.Alloc(IntPtr.Zero, size, protection, allocationType));
 }
예제 #23
0
파일: WinAPI.cs 프로젝트: aghili/nwohack
 public static extern DWORD_PTR VirtualAllocEx( IntPtr hProcess, DWORD_PTR lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect );
예제 #24
0
 public static IntPtr Alloc(IntPtr address, Int32 size, MemoryProtection protection, AllocationType allocationType)
 {
     return(Kernel32.VirtualAlloc(address, size, allocationType, protection));
 }
예제 #25
0
 public PhysicalPagesMapping Map(MemoryProtection protection)
 {
     return this.Map(IntPtr.Zero, protection);
 }
예제 #26
0
 public static IntPtr Alloc(Int32 size, MemoryProtection protection)
 {
     return(Memory.Alloc(size, protection, AllocationType.Commit));
 }
예제 #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;
            }
        }
예제 #28
0
 public static extern ulong VirtualAllocEx(IntPtr hProcess, ulong lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
예제 #29
0
 internal static extern IntPtr VirtualAllocEx(SafeProcessHandle processHandle, IntPtr address, SizeT size, AllocationType flAllocationType,
     MemoryProtection flProtect);
예제 #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();
     }
 }
예제 #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);
예제 #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);
예제 #34
0
 public SafeVirtualMemoryHandle Allocate(Int64 size, AllocationType allocation, MemoryProtection protection)
 {
     return(new SafeVirtualMemoryHandle(handle, 0, size, allocation, protection));
 }
예제 #35
0
 static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize, AllocationType lAllocationType, MemoryProtection flProtect);
예제 #36
0
        public static bool Reprotect(IntPtr address, IntPtr size, Memory.MemoryProtection protection)
        {
            MemoryProtection prot = GetProtection(protection);

            return(VirtualProtect(address, size, prot, out _));
        }
예제 #37
0
 public static extern bool VirtualProtect(IntPtr address, uint size, MemoryProtection newProtect, out MemoryProtection oldProtect);
예제 #38
0
 public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress,
                                            IntPtr dwSize, MemoryProtection flNewProtect, ref MemoryProtection lpflOldProtect);
예제 #39
0
 internal static extern IntPtr VirtualAllocEx(SafeProcessHandle hProcess, uint dwAddress, int nSize, AllocationType dwAllocationType, MemoryProtection dwProtect);
예제 #40
0
 public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize,
                                          MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
예제 #41
0
 public static IntPtr VirtualAlloc(UIntPtr sizeInBytes, MemoryAllocationType allocationType,
     MemoryProtection protection)
 {
     return VirtualAlloc(IntPtr.Zero, sizeInBytes, allocationType, protection);
 }
예제 #42
0
 public static extern bool VirtualProtectEx([In] Handle hProcess, [In] IntPtr lpAddress, [In] int dwSize,
                                            [In] MemoryProtection flNewProtect, [Out] out MemoryProtection lpflOldProtect);
예제 #43
0
파일: Win32.cs 프로젝트: sq/PETools
 public static extern int VirtualProtectEx(
     IntPtr hProcess, UInt32 lpAddress,
     uint dwSize, MemoryProtection flNewProtect,
     out MemoryProtection flOldProtect
 );
예제 #44
0
 public static extern bool VirtualProtect(byte *lpAddress, UIntPtr dwSize,
                                          MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
예제 #45
0
 public static extern IntPtr VirtualAlloc(IntPtr address, Int32 size, AllocationType allocationType, MemoryProtection protection);
예제 #46
0
 public static extern byte *VirtualAlloc(byte *lpAddress, UIntPtr dwSize,
                                         AllocationType flAllocationType, MemoryProtection flProtect);
        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];
                }
            }
        }
예제 #48
0
파일: WinApi.cs 프로젝트: Lamael/tools
 public static extern int VirtualAllocEx(IntPtr hProcess, Int32 lpAddress,
    Int32 dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
예제 #49
0
파일: WinApi.cs 프로젝트: Xileck/tibiaapi
 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);
예제 #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);
        }
예제 #52
0
 private static extern int VirtualProtect(IntPtr lpAddress, IntPtr dwSize, MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
예제 #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
			);
예제 #54
0
파일: PInvoke.cs 프로젝트: zhouzu/Juno
 internal static extern bool VirtualProtect(IntPtr baseAddress, int protectionSize, MemoryProtection protectionType, out MemoryProtection oldProtectionType);
예제 #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);
예제 #57
0
파일: Opcode.cs 프로젝트: adamsp/beastmon
     public static extern IntPtr VirtualAlloc(IntPtr lpAddress, UIntPtr dwSize,
 AllocationType flAllocationType, MemoryProtection flProtect);
예제 #58
0
 public static extern IntPtr VirtualAlloc(IntPtr address, Int32 size, AllocationType allocationType, MemoryProtection protection);
예제 #59
0
 internal static extern IntPtr VirtualAlloc(IntPtr startAddress, UIntPtr size, AllocationType allocationType, MemoryProtection protect);
예제 #60
0
 public MemoryMappedFileStream(string name, MemoryProtection protection)
     : this(name,0,protection)
 {
 }