From e2dd6a217d7b485913e851774b01cbc4fb7281ce Mon Sep 17 00:00:00 2001 From: de4dot Date: Fri, 21 Oct 2011 10:30:58 +0200 Subject: [PATCH 1/2] Add updated cecil submodule --- cecil | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cecil b/cecil index 009264e5..facac69d 160000 --- a/cecil +++ b/cecil @@ -1 +1 @@ -Subproject commit 009264e5a25216f9acef919c5fc5b6f2e926199f +Subproject commit facac69d2a5af6912a3ff643664b83f35d9e5a7b From 8c924617c3681be182ff23faef9532dbb1bcb673 Mon Sep 17 00:00:00 2001 From: de4dot Date: Fri, 21 Oct 2011 10:32:43 +0200 Subject: [PATCH 2/2] Update CIL output when -vv is used --- de4dot.code/ObfuscatedFile.cs | 77 ++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/de4dot.code/ObfuscatedFile.cs b/de4dot.code/ObfuscatedFile.cs index e4905e12..fceda2b3 100644 --- a/de4dot.code/ObfuscatedFile.cs +++ b/de4dot.code/ObfuscatedFile.cs @@ -484,6 +484,7 @@ namespace de4dot { IList allInstructions; IList allExceptionHandlers; Dictionary targets = new Dictionary(); + Dictionary labels = new Dictionary(); class ExInfo { public List tryStarts = new List(); @@ -507,6 +508,7 @@ namespace de4dot { this.allInstructions = null; this.allExceptionHandlers = null; targets.Clear(); + labels.Clear(); exInfos.Clear(); lastExInfo = null; } @@ -517,19 +519,37 @@ namespace de4dot { switch (instr.OpCode.OperandType) { case OperandType.ShortInlineBrTarget: case OperandType.InlineBrTarget: - var targetInstr = instr.Operand as Instruction; - if (targetInstr != null) - targets[targetInstr] = true; + setTarget(instr.Operand as Instruction); break; case OperandType.InlineSwitch: - foreach (var targetInstr2 in (Instruction[])instr.Operand) { - if (targetInstr2 != null) - targets[targetInstr2] = true; - } + foreach (var targetInstr in (Instruction[])instr.Operand) + setTarget(targetInstr); break; } } + + foreach (var ex in allExceptionHandlers) { + setTarget(ex.TryStart); + setTarget(ex.TryEnd); + setTarget(ex.FilterStart); + setTarget(ex.HandlerStart); + setTarget(ex.HandlerEnd); + } + + var sortedTargets = new List(targets.Keys); + sortedTargets.Sort((a, b) => { + if (a.Offset < b.Offset) return -1; + if (a.Offset > b.Offset) return 1; + return 0; + }); + for (int i = 0; i < sortedTargets.Count; i++) + labels[sortedTargets[i]] = string.Format("label_{0}", i); + } + + void setTarget(Instruction instr) { + if (instr != null) + targets[instr] = true; } void initExHandlers() { @@ -562,16 +582,16 @@ namespace de4dot { Log.indent(); foreach (var instr in allInstructions) { + if (targets.ContainsKey(instr)) { + Log.deIndent(); + Log.log(logLevel, "{0}:", getLabel(instr)); + Log.indent(); + } ExInfo exInfo; if (exInfos.TryGetValue(instr, out exInfo)) printExInfo(exInfo); - if (targets.ContainsKey(instr)) { - Log.deIndent(); - Log.log(logLevel, "{0}:", instr.GetLabelString()); - Log.indent(); - } var instrString = instr.GetOpCodeString(); - var operandString = instr.GetOperandString(); + var operandString = getOperandString(instr); var memberReference = instr.Operand as MemberReference; if (operandString == "") Log.log(logLevel, "{0}", instrString); @@ -584,6 +604,25 @@ namespace de4dot { Log.deIndent(); } + string getOperandString(Instruction instr) { + if (instr.Operand is Instruction) + return getLabel((Instruction)instr.Operand); + else if (instr.Operand is Instruction[]) { + var sb = new StringBuilder(); + var targets = (Instruction[])instr.Operand; + for (int i = 0; i < targets.Length; i++) { + if (i > 0) + sb.Append(','); + sb.Append(getLabel(targets[i])); + } + return sb.ToString(); + } + else if (instr.Operand is string) + return Utils.toCsharpString((string)instr.Operand); + else + return instr.GetOperandString(); + } + void printExInfo(ExInfo exInfo) { Log.deIndent(); foreach (var ex in exInfo.tryStarts) @@ -602,21 +641,21 @@ namespace de4dot { string getExceptionString(ExceptionHandler ex) { var sb = new StringBuilder(); if (ex.TryStart != null) - sb.Append(string.Format("TRY: {0}-{1}", getOffset(ex.TryStart), getOffset(ex.TryEnd))); + sb.Append(string.Format("TRY: {0}-{1}", getLabel(ex.TryStart), getLabel(ex.TryEnd))); if (ex.FilterStart != null) - sb.Append(string.Format(", FILTER: {0}", getOffset(ex.FilterStart))); + sb.Append(string.Format(", FILTER: {0}", getLabel(ex.FilterStart))); if (ex.HandlerStart != null) - sb.Append(string.Format(", HANDLER: {0}-{1}", getOffset(ex.HandlerStart), getOffset(ex.HandlerEnd))); + sb.Append(string.Format(", HANDLER: {0}-{1}", getLabel(ex.HandlerStart), getLabel(ex.HandlerEnd))); sb.Append(string.Format(", TYPE: {0}", ex.HandlerType)); if (ex.CatchType != null) sb.Append(string.Format(", CATCH: {0}", ex.CatchType)); return sb.ToString(); } - string getOffset(Instruction instr) { + string getLabel(Instruction instr) { if (instr == null) - return "END"; - return string.Format("{0:X4}", instr.Offset); + return ""; + return labels[instr]; } }