Exemplo n.º 1
0
        private void AffRich(Instruction instruction, RichTextBox richTextBox, StringOutput stringOutput, byte[] buffer, ulong CodeRIP)
        {
            const int HEXBYTES_COLUMN_BYTE_LENGTH = 10;

            var formatter = new MasmFormatter();

            formatter.Format(instruction, stringOutput);
            richTextBox.AppendText(instruction.IP.ToString("X16"));
            richTextBox.AppendText(" ");
            int instrLen      = instruction.Length;
            int byteBaseIndex = (int)(instruction.IP - CodeRIP);

            for (int i = 0; i < instrLen; i++)
            {
                richTextBox.AppendText(buffer[byteBaseIndex + i].ToString("X2"));
            }

            int missingBytes = HEXBYTES_COLUMN_BYTE_LENGTH - instrLen;

            for (int i = 0; i < missingBytes; i++)
            {
                richTextBox.AppendText("  ");
            }

            richTextBox.AppendText(" ");
            string endasm = stringOutput.ToStringAndReset().PadRight(60);

            richTextBox.AppendText(endasm + Environment.NewLine);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            int exampleCodeBitness = 32;

            if (args.Length == 2)
            {
                switch (args[0])
                {
                case "32": { exampleCodeBitness = 32; break; }

                case "64": { exampleCodeBitness = 64; break; }

                default:
                    break;
                }
            }
            else
            {
                Console.Write("Must use arguments: <bit-length[32|64]> <target file path>");
                return;
            }

            string filePath = args[1];

            BinaryReader reader    = new BinaryReader(File.Open(filePath, FileMode.Open));
            var          fileSize  = Convert.ToInt32(new FileInfo(filePath).Length);
            var          codeBytes = reader.ReadBytes(fileSize);

            var sw = Stopwatch.StartNew();

            var codeReader = new ByteArrayCodeReader(codeBytes);
            var decoder    = Decoder.Create(exampleCodeBitness, codeReader);

            decoder.IP = exampleCodeRIP;
            ulong endRip = decoder.IP + (uint)codeBytes.Length;

            var instructions = new InstructionList();

            while (decoder.IP < endRip)
            {
                decoder.Decode(out instructions.AllocUninitializedElement());
            }

            var formatter = new MasmFormatter();

            formatter.Options.DigitSeparator        = "`";
            formatter.Options.FirstOperandCharIndex = 10;
            var output = new StringOutput();

            foreach (ref var instr in instructions)
            {
                formatter.Format(instr, output);
                Console.WriteLine(instr.IP.ToString("X8") + " " + output.ToStringAndReset());
            }

            sw.Stop();
            Console.Error.WriteLine("Total dump time: {0:F} sec.", sw.Elapsed.TotalSeconds);
        }
Exemplo n.º 3
0
        public static string Format(this MethodAsmBody src)
        {
            if (src.Instructions.Length == 0)
            {
                return(string.Empty);
            }

            Span <byte> nativeData = src.NativeBlocks.Single().Data;

            var baseAddress = src.Instructions[0].IP;

            var formatter = new MasmFormatter(FormatOptions);
            var sb        = sbuild();

            var writer = new StringWriter(sb);
            var output = new AsmFormatterOutput(writer, baseAddress);

            for (var j = 0; j < src.Instructions.Length; j++)
            {
                ref var i             = ref src.Instructions[j];
                var     startAddress  = i.IP;
                var     relAddress    = (int)(startAddress - baseAddress);
                var     relAddressFmt = relAddress.ToString("x4");

                sb.Append($"{relAddressFmt}h  ");
                formatter.Format(ref i, output);

                var padding  = "   ";
                var encoding = i.Encoding == EncodingKind.Legacy ? string.Empty : $" ({i.Encoding} encoded)";
                var encoded  = embrace(nativeData.Slice(relAddress, i.ByteLength).FormatHex(false, ','));
                sb.Append($"{padding}; opcode := {i.Code}{encoding} | encoded := {encoded} ({i.ByteLength} bytes)");

                if (j != src.Instructions.Length - 1)
                {
                    sb.AppendLine();
                }
            }