コード例 #1
0
        /// <summary>
        ///     Assembles mnemonics and injects the corresponding assembly code into the remote process.
        /// </summary>
        /// <param name="asm">The mnemonics to inject.</param>
        /// <returns>The address where the assembly code is injected.</returns>
        public IAAllocatedMemory Inject(string asm)
        {
            // Assemble the assembly code
            var code = Assembler.Assemble(asm);
            // Allocate a chunk of memory to store the assembly code
            var memory = Process.MemoryFactory.Allocate(ARandomizer.GenerateString(), code.GetTotalBytes());

            // Inject the code
            Inject(asm, memory.BaseAddress);
            // Return the memory allocated
            return(memory);
        }
コード例 #2
0
        /// <summary>
        ///     Marshals the value into the remote process.
        /// </summary>
        private void Marshal()
        {
            // If the type is string, it's a special case
            if (typeof(T) == typeof(string))
            {
                var text = Value.ToString();
                // Allocate memory in the remote process (string + '\0')
                Allocated = Process.MemoryFactory.Allocate(ARandomizer.GenerateString(), text.Length + 1);
                // Write the value
                Allocated.Write(0, text, Encoding.UTF8);
                // Get the pointer
                Reference = Allocated.BaseAddress;
            }
            else
            {
                // For all other types
                // Convert the value into a byte array
                var byteArray = MarshalType <T> .ObjectToByteArray(Value);

                // If the value can be stored directly in registers
                if (MarshalType <T> .CanBeStoredInRegisters)
                {
                    // Convert the byte array into a pointer
                    Reference = MarshalType <IntPtr> .ByteArrayToObject(byteArray);
                }
                else
                {
                    // It's a bit more complicated, we must allocate some space into
                    // the remote process to store the value and get its pointer

                    // Allocate memory in the remote process
                    Allocated = Process.MemoryFactory.Allocate(ARandomizer.GenerateString(), MarshalType <T> .Size);
                    // Write the value
                    Allocated.Write(0, Value);
                    // Get the pointer
                    Reference = Allocated.BaseAddress;
                }
            }
        }