Ejemplo n.º 1
0
        private IntPtr OpenImageFromData(byte[] assembly)
        {
            IntPtr statusPtr = _memory.Allocate(4);

            IntPtr addr = Native.VirtualAllocEx(_handle, IntPtr.Zero, assembly.Length,
                                                AllocationType.MEM_COMMIT, MemoryProtection.PAGE_READWRITE);

            if (addr == IntPtr.Zero)
            {
                throw new InjectorException("Failed to allocate process memory", new Win32Exception(Marshal.GetLastWin32Error()));
            }

            _memory.Write(addr, assembly);
            IntPtr rawImage = Execute(Exports[mono_image_open_from_data],
                                      addr,
                                      (IntPtr)assembly.Length,
                                      (IntPtr)0,
                                      statusPtr
                                      );

            MonoImageOpenStatus status = (MonoImageOpenStatus)_memory.ReadInt(statusPtr);

            if (status != MonoImageOpenStatus.MONO_IMAGE_OK)
            {
                IntPtr messagePtr = Execute(Exports[mono_image_strerror], (IntPtr)status);
                string message    = _memory.ReadString(messagePtr, 256, Encoding.UTF8);
                throw new InjectorException($"{mono_image_open_from_data}() failed: {message}");
            }

            _memory.Write(addr, new byte[ClearHeaderBytes]);

            return(rawImage);
        }
Ejemplo n.º 2
0
        public IntPtr Allocate(int size)
        {
            IntPtr addr = Native.VirtualAllocEx(_handle, IntPtr.Zero, size, AllocationType.MEM_COMMIT, MemoryProtection.PAGE_EXECUTE_READWRITE);

            if (addr == IntPtr.Zero)
            {
                throw new InjectorException("Failed to allocate process memory", new Win32Exception(Marshal.GetLastWin32Error()));
            }

            _allocations.Add(addr, size);
            return(addr);
        }