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); }
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); }
private static Formatter CreateDefaultFormatter() { var formatter = new MasmFormatter(); formatter.Options.FirstOperandCharIndex = 10; // pad right the mnemonic formatter.Options.HexSuffix = default; // don't print "h" at the end of every hex address formatter.Options.TabSize = 0; // use spaces return(formatter); }
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(); } }
private async Task ExeDiff(CodeByte codebyteOriginal, CodeByte codebytePatched) { done = false; int nbdiff = 0; int curline = 0; bool equal = true; int nb_inst_O = 0; int nb_inst_P = 0; try { var instructionsOriginal = codebyteOriginal.instructions; var instructionsPatched = codebytePatched.instructions; var formatter = new MasmFormatter(); formatter.Options.DigitSeparator = ""; formatter.Options.FirstOperandCharIndex = 10; var outputO = new StringOutput(); var outputP = new StringOutput(); progressBar1.Maximum = instructionsOriginal.Count(); richTextBoxOriginal.BackColor = Color.MintCream; richTextBoxOriginal.ForeColor = Color.DarkGreen; richTextBoxPatched.BackColor = Color.MintCream; richTextBoxPatched.ForeColor = Color.DarkGreen; await Task.Run(() => { // Async CODE Start while (nb_inst_O < instructionsOriginal.Count() && nb_inst_P < instructionsPatched.Count()) { var instrO = instructionsOriginal[nb_inst_O]; var instrP = instructionsPatched[nb_inst_P]; while ((instrO.ToString() != instrP.ToString()) || (instrO.IP != instrP.IP)) { if (equal) { AffRich(instructionsOriginal[nb_inst_O - 1], richTextBoxOriginal, outputO, codebyteOriginal.hexcode, codebyteOriginal.CodeRIP); AffRich(instructionsPatched[nb_inst_P - 1], richTextBoxPatched, outputP, codebytePatched.hexcode, codebytePatched.CodeRIP); curline++; equal = false; RedProgess(((float)nb_inst_O / (float)instructionsOriginal.Count() * 100)); } AffRich(instructionsOriginal[nb_inst_O], richTextBoxOriginal, outputO, codebyteOriginal.hexcode, codebyteOriginal.CodeRIP); AffRich(instructionsPatched[nb_inst_P], richTextBoxPatched, outputP, codebytePatched.hexcode, codebytePatched.CodeRIP); ModifColor(richTextBoxOriginal, curline); ModifColor(richTextBoxPatched, curline); curline++; nb_inst_O++; nb_inst_P++; instrO = instructionsOriginal[nb_inst_O]; instrP = instructionsPatched[nb_inst_P]; while (instrO.IP < instrP.IP) { AffRich(instructionsOriginal[nb_inst_O], richTextBoxOriginal, outputO, codebyteOriginal.hexcode, codebyteOriginal.CodeRIP); richTextBoxPatched.AppendText(Environment.NewLine); ModifColor(richTextBoxOriginal, curline); curline++; nb_inst_O++; instrO = instructionsOriginal[nb_inst_O]; } while (instrO.IP > instrP.IP) { AffRich(instructionsPatched[nb_inst_P], richTextBoxPatched, outputP, codebytePatched.hexcode, codebytePatched.CodeRIP); richTextBoxOriginal.AppendText(Environment.NewLine); ModifColor(richTextBoxPatched, curline); curline++; nb_inst_P++; instrP = instructionsPatched[nb_inst_P]; } nbdiff++; } if (!equal) { AffRich(instructionsOriginal[nb_inst_O], richTextBoxOriginal, outputO, codebyteOriginal.hexcode, codebyteOriginal.CodeRIP); AffRich(instructionsPatched[nb_inst_P], richTextBoxPatched, outputP, codebytePatched.hexcode, codebytePatched.CodeRIP); curline++; richTextBoxOriginal.AppendText("----------------------------------------------------" + Environment.NewLine); richTextBoxPatched.AppendText("----------------------------------------------------" + Environment.NewLine); curline++; equal = true; } nb_inst_O++; nb_inst_P++; if (nb_inst_O % PROGRESS_MODULO == 0) { progressBar1.Value = nb_inst_O; } } progressBar1.Value = 0; done = true; }); // Async CODE End } catch (Exception exept) { MessageBoxException(exept, "Patch Code"); } }