Esempio n. 1
0
        /// <summary>
        /// Reads bytes at the specified address and returns it.
        /// </summary>
        /// <param name="Address">The address.</param>
        /// <param name="Size">The size.</param>
        public byte[] Read(ulong Address, uint Size)
        {
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException(nameof(BekoEngine), "The memory handler is disposed");
            }

            if (Address >= 0x7FFFFFFFFFFF)
            {
                throw new ArgumentException("Address is outside userspace virtual memory range");
            }

            if (Address + Size > 0x7FFFFFFFFFFF)
            {
                throw new ArgumentException("Address plus size is outside userspace virtual memory range");
            }

            var Buffer = new byte[Size];
            var Read   = 0u;

            if (!NativeMemoryHandler.ReadProcessMemory(this.Handle, Address, Buffer, Size, ref Read))
            {
                throw new Exception("Failed to read memory from remote process");
            }

            return(Buffer);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads bytes at the specified address and returns a structure from it.
        /// </summary>
        /// <param name="Address">The address.</param>
        public T Read <T>(ulong Address)
        {
            if (this.IsDisposed)
            {
                throw new ObjectDisposedException(nameof(BekoEngine), "The memory handler is disposed");
            }

            if (Address >= 0x7FFFFFFFFFFF)
            {
                throw new ArgumentException("Address is outside userspace virtual memory range");
            }

            var Size = Marshal.SizeOf <T>();

            if (Address + (ulong)Size > 0x7FFFFFFFFFFF)
            {
                throw new ArgumentException("Address plus size is outside userspace virtual memory range");
            }

            var Buffer = default(T);

            var Allocation = Marshal.AllocHGlobal(Size);
            var Read       = 0u;

            if (Allocation == IntPtr.Zero)
            {
                throw new InsufficientMemoryException("Couldn't allocate memory for the buffer");
            }

            var Success = NativeMemoryHandler.ReadProcessMemory(this.Handle, Address, Buffer, (uint)Size, ref Read);

            if (Success)
            {
                Buffer = Marshal.PtrToStructure <T>(Allocation);
            }

            Marshal.FreeHGlobal(Allocation);

            if (!Success)
            {
                throw new Exception("Failed to read memory from remote process");
            }

            return(Buffer);
        }