예제 #1
0
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                       fileHandle,
                       false,
                       readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                       ))
            {
                _size   = (int)fileHandle.GetSize();
                _view   = section.MapView(_size);
                _memory = _view;

                byte *start = (byte *)_memory;

                if (start[0] != 'M' || start[1] != 'Z')
                {
                    throw new Exception("The file is not a valid executable image.");
                }

                _ntHeaders = this.GetNtHeaders();
                _sections  = (ImageSectionHeader *)((byte *)&_ntHeaders->OptionalHeader + _ntHeaders->FileHeader.SizeOfOptionalHeader);
                _magic     = _ntHeaders->OptionalHeader.Magic;

                if (_magic != Win32.Pe32Magic && _magic != Win32.Pe32PlusMagic)
                {
                    throw new Exception("The file is not a PE32 or PE32+ image.");
                }
            }
        }
예제 #2
0
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                       fileHandle,
                       false,
                       readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                       ))
            {
                _size = (int)fileHandle.GetSize();
                _view = section.MapView(_size);

                this.Load(_view);
            }
        }
예제 #3
0
        /// <summary>
        /// Creates a section backed by a file.
        /// </summary>
        /// <param name="name">The name of the section.</param>
        /// <param name="fileHandle">A file handle.</param>
        /// <param name="image">Whether to treat the file as an executable image.</param>
        /// <param name="protection">The page protection to apply to mappings.</param>
        public Section(string name, FileHandle fileHandle, bool image, MemoryProtection protection)
        {
            _originalProtection = protection;

            this.Handle = SectionHandle.Create(
                SectionAccess.All,
                name,
                ObjectFlags.OpenIf,
                null,
                fileHandle.GetSize(),
                image ? SectionAttributes.Image : SectionAttributes.Commit,
                protection,
                fileHandle
                );
        }
예제 #4
0
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                fileHandle,
                false,
                readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                ))
            {
                _size = (int)fileHandle.GetSize();
                _view = section.MapView(_size);
                _memory = _view;

                byte* start = (byte*)_memory;

                if (start[0] != 'M' || start[1] != 'Z')
                    throw new Exception("The file is not a valid executable image.");

                _ntHeaders = this.GetNtHeaders();
                _sections = (ImageSectionHeader*)((byte*)&_ntHeaders->OptionalHeader + _ntHeaders->FileHeader.SizeOfOptionalHeader);
                _magic = _ntHeaders->OptionalHeader.Magic;

                if (_magic != Win32.Pe32Magic && _magic != Win32.Pe32PlusMagic)
                    throw new Exception("The file is not a PE32 or PE32+ image.");
            }
        }
예제 #5
0
        private void MapAndLoad(FileHandle fileHandle, bool readOnly)
        {
            using (Section section = new Section(
                fileHandle,
                false,
                readOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite
                ))
            {
                _size = (int)fileHandle.GetSize();
                _view = section.MapView(_size);

                this.Load(_view);
            }
        }