/* Building functions to hook. */

        private void BuildAdd()
        {
            int wordSize = IntPtr.Size;

            string[] addFunction = new string[]
            {
                $"{_use32}",
                $"push {_ebp}",
                $"mov {_ebp}, {_esp}",

                $"mov {_eax}, [{_ebp} + {wordSize * 2}]", // Left Parameter
                $"mov {_ecx}, [{_ebp} + {wordSize * 3}]", // Right Parameter
                $"add {_eax}, {_ecx}",

                $"pop {_ebp}",
                $"ret"
            };

            var result = _assembler.Assemble(addFunction);

            Add = _memory.Allocate(result.Length);
            _memory.WriteRaw(Add, result);
        }
Ejemplo n.º 2
0
        private void BuildAdd()
        {
            string[] addFunction = new string[]
            {
                $"use64",
                $"lea eax, [rcx+rdx]",
                $"ret",
            };

            var result = _assembler.Assemble(addFunction);

            Add = _memory.Allocate(result.Length);
            _memory.WriteRaw(Add, result);
        }
        private void BuildAdd()
        {
            string[] addFunction = new string[]
            {
                $"use32",
                $"mov eax, [esp+8]", // Right Parameter
                $"add eax, [esp+4]", // Left Parameter
                $"ret 8",
            };

            var result = _assembler.Assemble(addFunction);

            Add = _memory.Allocate(result.Length);
            _memory.WriteRaw(Add, result);
        }
        /// <summary>
        /// Assembles the given mnemonics.
        /// </summary>
        /// <param name="mnemonics">The mnemonics to assemble; delimited by new line \n for each new instruction.</param>
        /// <param name="passLimit">The maximum number of passes to perform when assembling data.</param>
        /// <exception cref="FasmWrapperException">Your text to be assembled is too large to fit in the preallocated region for program text.</exception>
        /// <exception cref="FasmException">An error thrown by the native FASM compiler.</exception>
        public byte[] Assemble(string mnemonics, ushort passLimit = 100)
        {
            // Convert Text & Append
            byte[] mnemonicBytes = Encoding.ASCII.GetBytes(mnemonics + "\0");

            if (mnemonicBytes.Length > _textSize)
            {
                throw new FasmWrapperException($"Your supplied array of mnemonics to be assembled is too large ({mnemonicBytes.Length} > {_textSize} bytes)." +
                                               "Consider simplifying your code or creating a new Assembler with greater textSize.");
            }

            lock (_lock)
            {
                _processMemory.WriteRaw(_textAddress, mnemonicBytes);

                // Assemble and check result.
                var result = _assembleFunction(_textAddress, _resultAddress, (IntPtr)_resultSize, passLimit, IntPtr.Zero);

                //    As stated in FASMDLL.TXT, at the beginning of the block, the FASM_STATE structure will reside.
                //    It is defined in FASM.ASH. We read it here.

                FasmState state;
                _processMemory.Read(_resultAddress, out state);

                if (result == FasmResult.Ok)
                {
                    byte[] assembledBytes = new byte[state.OutputLength];
                    Marshal.Copy((IntPtr)state.OutputData, assembledBytes, 0, assembledBytes.Length);
                    return(assembledBytes);
                }

                // TODO: Make this exception more detailed in time with FASMX64's development.

                /* For now, I still do not know if FASMX64 will ever plan to change the pointer size,
                 *     so for now, I will opt to not get the line details and/or other more "complex" info. */

                string[] originalMnemonics = mnemonics.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                var      lineHeader        = state.GetLineHeader();

                throw new FasmException(state.ErrorCode, state.Condition, lineHeader.LineNumber, originalMnemonics);
            }
        }