/// <summary> /// Change memory protection /// </summary> /// <param name="protection">New protection flags</param> /// <param name="offset">Memory offset in block</param> /// <param name="size">Block size</param> /// <param name="pOld">Old protection flags</param> /// <returns>Status</returns> NTSTATUS MemBlock::Protect( DWORD protection, size_t offset /*= 0*/, size_t size /*= 0*/, DWORD* pOld /*= nullptr */ ) { if (size == 0) size = _size; return _memory->Protect( _ptr + offset, size, CastProtection( protection, _memory->core().DEP() ), pOld ); }
/// <summary> /// Change memory protection /// </summary> /// <param name="pAddr">Memory address</param> /// <param name="size">Region size</param> /// <param name="flProtect">New memory protection flags</param> /// <param name="pOld">Old protection flags</param> /// <returns>Status</returns> NTSTATUS ProcessMemory::Protect( ptr_t pAddr, size_t size, DWORD flProtect, DWORD *pOld /*= NULL*/ ) { DWORD junk = 0; if (pOld == nullptr) pOld = &junk; return _core.native()->VirtualProtectExT( pAddr, size, CastProtection( flProtect, _core.DEP() ), pOld ); }
/// <summary> /// Change memory protection /// </summary> /// <param name="protection">New protection flags</param> /// <param name="offset">Memory offset in block</param> /// <param name="size">Block size</param> /// <param name="pOld">Old protection flags</param> /// <returns>Status</returns> NTSTATUS MemBlock::Protect( DWORD protection, uintptr_t offset /*= 0*/, size_t size /*= 0*/, DWORD* pOld /*= nullptr */ ) { auto prot = CastProtection( protection, _memory->core().DEP() ); if (size == 0) size = _size; return _physical ? Driver().ProtectMem( _memory->core().pid(), _ptr + offset, size, prot ) : _memory->Protect( _ptr + offset, size, prot, pOld ); }
/// <summary> /// Change memory protection /// </summary> /// <param name="protection">New protection flags</param> /// <param name="offset">Memory offset in block</param> /// <param name="size">Block size</param> /// <param name="pOld">Old protection flags</param> /// <returns>Status</returns> NTSTATUS MemBlock::Protect( DWORD protection, uintptr_t offset /*= 0*/, size_t size /*= 0*/, DWORD* pOld /*= nullptr */ ) { if (!_pImpl) return STATUS_MEMORY_NOT_ALLOCATED; auto prot = CastProtection( protection, _pImpl->_memory->core().DEP() ); if (size == 0) size = _pImpl->_size; return _pImpl->_physical ? Driver().ProtectMem( _pImpl->_memory->core().pid(), _pImpl->_ptr + offset, size, prot ) : _pImpl->_memory->Protect( _pImpl->_ptr + offset, size, prot, pOld ); }
/// <summary> /// Allocate new memory block /// </summary> /// <param name="process">Process memory routines</param> /// <param name="size">Block size</param> /// <param name="desired">Desired base address of new block</param> /// <param name="protection">Memory protection</param> /// <returns>Memory block. If failed - returned block will be invalid</returns> MemBlock MemBlock::Allocate( ProcessMemory& process, size_t size, ptr_t desired /*= 0*/, DWORD protection /*= PAGE_EXECUTE_READWRITE */ ) { ptr_t desired64 = desired; DWORD newProt = CastProtection( protection, process.core().DEP() ); if (process.core().native()->VirualAllocExT( desired64, size, MEM_COMMIT, newProt ) != STATUS_SUCCESS) { desired64 = 0; if (process.core().native()->VirualAllocExT( desired64, size, MEM_COMMIT, newProt ) == STATUS_SUCCESS) LastNtStatus( STATUS_IMAGE_NOT_AT_BASE ); else desired64 = 0; } return MemBlock( &process, desired64, size, protection ); }
/// <summary> /// Allocate new memory block /// </summary> /// <param name="process">Process memory routines</param> /// <param name="size">Block size</param> /// <param name="desired">Desired base address of new block</param> /// <param name="protection">Memory protection</param> /// <param name="own">false if caller will be responsible for block deallocation</param> /// <returns>Memory block. If failed - returned block will be invalid</returns> call_result_t<MemBlock> MemBlock::Allocate( ProcessMemory& process, size_t size, ptr_t desired /*= 0*/, DWORD protection /*= PAGE_EXECUTE_READWRITE */, bool own /*= true*/ ) { ptr_t desired64 = desired; DWORD newProt = CastProtection( protection, process.core().DEP() ); NTSTATUS status = process.core().native()->VirtualAllocExT( desired64, size, MEM_COMMIT, newProt ); if (!NT_SUCCESS( status )) { desired64 = 0; status = process.core().native()->VirtualAllocExT( desired64, size, MEM_COMMIT, newProt ); if (NT_SUCCESS( status )) return call_result_t<MemBlock>( MemBlock( &process, desired64, size, protection, own ), STATUS_IMAGE_NOT_AT_BASE ); else return status; } return MemBlock( &process, desired64, size, protection, own ); }