From e0d60514768246596d17dc219e2dd4c8b38ce8a2 Mon Sep 17 00:00:00 2001 From: yck1509 Date: Wed, 24 Dec 2014 02:57:56 +0800 Subject: [PATCH] Minor fixes/modification --- Confuser.Core/ProtectionPipeline.cs | 2 +- .../AntiTamper/AntiTamperProtection.cs | 10 ++++++++-- Confuser.Protections/AntiTamper/JITMode.cs | 14 +++++++------- .../ControlFlow/ExpressionPredicate.cs | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Confuser.Core/ProtectionPipeline.cs b/Confuser.Core/ProtectionPipeline.cs index 895d2c4dc..90afd2134 100644 --- a/Confuser.Core/ProtectionPipeline.cs +++ b/Confuser.Core/ProtectionPipeline.cs @@ -173,7 +173,7 @@ private static IList Filter(ConfuserContext context, IList ProtectionSettings parameters = ProtectionParameters.GetParameters(context, def); Debug.Assert(parameters != null); if (parameters == null) { - context.Logger.ErrorFormat("'{0}' not marked for obfuscation, possibly a bug."); + context.Logger.ErrorFormat("'{0}' not marked for obfuscation, possibly a bug.", def); throw new ConfuserException(null); } return parameters.ContainsKey(phase.Parent); diff --git a/Confuser.Protections/AntiTamper/AntiTamperProtection.cs b/Confuser.Protections/AntiTamper/AntiTamperProtection.cs index 37dd01c66..abc861ffa 100644 --- a/Confuser.Protections/AntiTamper/AntiTamperProtection.cs +++ b/Confuser.Protections/AntiTamper/AntiTamperProtection.cs @@ -5,8 +5,14 @@ using dnlib.DotNet; namespace Confuser.Protections { + public interface IAntiTamperService { + + void ExcludeMethod(ConfuserContext context, MethodDef method); + + } + [BeforeProtection("Ki.ControlFlow"), AfterProtection("Ki.Constants")] - internal class AntiTamperProtection : Protection { + internal class AntiTamperProtection : Protection, IAntiTamperService { public const string _Id = "anti tamper"; public const string _FullId = "Ki.AntiTamper"; @@ -34,7 +40,7 @@ public override ProtectionPreset Preset { } protected override void Initialize(ConfuserContext context) { - // + context.Registry.RegisterService(_ServiceId, typeof(IAntiTamperService), this); } protected override void PopulatePipeline(ProtectionPipeline pipeline) { diff --git a/Confuser.Protections/AntiTamper/JITMode.cs b/Confuser.Protections/AntiTamper/JITMode.cs index 31b2f14f2..d03ae88f0 100644 --- a/Confuser.Protections/AntiTamper/JITMode.cs +++ b/Confuser.Protections/AntiTamper/JITMode.cs @@ -140,6 +140,13 @@ public void HandleInject(AntiTamperProtection parent, ConfuserContext context, P } public void HandleMD(AntiTamperProtection parent, ConfuserContext context, ProtectionParameters parameters) { + // move initialization away from module initializer + cctorRepl.Body = cctor.Body; + cctor.Body = new CilBody(); + cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Call, initMethod)); + cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Call, cctorRepl)); + cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); + methods = parameters.Targets.OfType().Where(method => method.HasBody).ToList(); context.CurrentModuleWriterListener.OnWriterEvent += OnWriterEvent; } @@ -199,13 +206,6 @@ private void CreateSection(ModuleWriter writer) { var bodyIndex = new JITBodyIndex(methods.Select(method => writer.MetaData.GetToken(method).Raw)); newSection.Add(bodyIndex, 0x10); - // move initialization away from module initializer - cctorRepl.Body = cctor.Body; - cctor.Body = new CilBody(); - cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Call, initMethod)); - cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Call, cctorRepl)); - cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); - // save methods foreach (MethodDef method in methods.WithProgress(context.Logger)) { if (!method.HasBody) diff --git a/Confuser.Protections/ControlFlow/ExpressionPredicate.cs b/Confuser.Protections/ControlFlow/ExpressionPredicate.cs index 7ff309a45..191c5bd7a 100644 --- a/Confuser.Protections/ControlFlow/ExpressionPredicate.cs +++ b/Confuser.Protections/ControlFlow/ExpressionPredicate.cs @@ -34,7 +34,7 @@ public void Init(CilBody body) { public void EmitSwitchLoad(IList instrs) { instrs.Add(Instruction.Create(OpCodes.Stloc, stateVar)); foreach (Instruction instr in invCompiled) - instrs.Add(instr); + instrs.Add(instr.Clone()); } public int GetSwitchKey(int key) {