From 8885b9778fc003cc570488e3f502db8bebbe46da Mon Sep 17 00:00:00 2001 From: yck1509 Date: Mon, 29 Dec 2014 01:26:59 +0800 Subject: [PATCH] Add prototype Advanced API --- Confuser.Core/API/APIStore.cs | 98 +++++++++++++++++++ Confuser.Core/API/IDataStore.cs | 53 ++++++++++ Confuser.Core/API/IOpaquePredicate.cs | 79 +++++++++++++++ Confuser.Core/Confuser.Core.csproj | 3 + Confuser.Core/ConfuserEngine.cs | 8 +- Confuser.Core/CoreComponent.cs | 7 ++ Confuser.Core/Services/MarkerService.cs | 25 ++++- Confuser.Core/Utils.cs | 2 +- Confuser.Protections/AntiDebugProtection.cs | 4 +- Confuser.Protections/AntiDumpProtection.cs | 2 +- Confuser.Protections/AntiTamper/JITMode.cs | 4 +- Confuser.Protections/AntiTamper/NormalMode.cs | 2 +- Confuser.Protections/Constants/CEContext.cs | 1 + Confuser.Protections/Constants/InjectPhase.cs | 11 ++- Confuser.Protections/Constants/x86Mode.cs | 2 +- Confuser.Protections/ControlFlow/CFContext.cs | 1 + .../ControlFlow/ControlFlowPhase.cs | 3 +- .../ControlFlow/x86Predicate.cs | 2 +- .../ReferenceProxy/MildMode.cs | 2 +- .../ReferenceProxy/RPContext.cs | 1 + Confuser.Protections/ReferenceProxy/RPMode.cs | 2 +- .../ReferenceProxy/ReferenceProxyPhase.cs | 3 +- .../ReferenceProxy/StrongMode.cs | 12 +-- .../ReferenceProxy/x86Encoding.cs | 2 +- Confuser.Protections/Resources/InjectPhase.cs | 8 +- Confuser.Renamer/NameService.cs | 6 +- Confuser.Renamer/RickRoller.cs | 4 +- 27 files changed, 307 insertions(+), 40 deletions(-) create mode 100644 Confuser.Core/API/APIStore.cs create mode 100644 Confuser.Core/API/IDataStore.cs create mode 100644 Confuser.Core/API/IOpaquePredicate.cs diff --git a/Confuser.Core/API/APIStore.cs b/Confuser.Core/API/APIStore.cs new file mode 100644 index 000000000..be173a26b --- /dev/null +++ b/Confuser.Core/API/APIStore.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using Confuser.Core.Services; +using dnlib.DotNet; + +namespace Confuser.Core.API { + internal class APIStore : IAPIStore { + readonly ConfuserContext context; + readonly RandomGenerator random; + readonly SortedList> dataStores; + readonly List predicates; + + /// + /// Initializes a new instance of the class. + /// + /// The working context. + public APIStore(ConfuserContext context) { + this.context = context; + random = context.Registry.GetService().GetRandomGenerator("APIStore"); + + dataStores = new SortedList>(); + predicates = new List(); + } + + /// + public void AddStore(IDataStore dataStore) { + dataStores.AddListEntry(dataStore.Priority, dataStore); + } + + /// + public void AddPredicate(IOpaquePredicateDescriptor predicate) { + predicates.Add(predicate); + } + + /// + public IDataStore GetStore(MethodDef method) { + for (int i = dataStores.Count - 1; i >= 0; i--) { + var list = dataStores[i]; + for (int j = list.Count - 1; j >= 0; i--) { + if (list[j].IsUsable(method)) + return list[j]; + } + } + return null; + } + + /// + public IOpaquePredicateDescriptor GetPredicate(MethodDef method, OpaquePredicateType? type, params int[] argCount) { + var randomPredicates = predicates.ToArray(); + random.Shuffle(randomPredicates); + foreach (var predicate in randomPredicates) { + if (predicate.IsUsable(method) && + (type == null || predicate.Type == type.Value) && + (argCount == null || Array.IndexOf(argCount, predicate.ArgumentCount) != -1)) + return predicate; + } + return null; + } + } + + /// + /// Provides storage for API interfaces + /// + public interface IAPIStore { + /// + /// Adds the specified data store into this store. + /// + /// The data store. + void AddStore(IDataStore dataStore); + + /// + /// Finds a suitable data store for the specified method, with the + /// specified number of keys. + /// + /// The method. + /// The suitable data store if found, or null if not found. + /// + /// It should never returns null --- ConfuserEx has internal data store. + /// + IDataStore GetStore(MethodDef method); + + /// + /// Adds the specified opaque predicate into this store. + /// + /// The opaque predicate. + void AddPredicate(IOpaquePredicateDescriptor predicate); + + /// + /// Finds a suitable opaque predicate for the specified method, with the + /// specified properties. + /// + /// The method. + /// The required type of predicate, or null if it does not matter. + /// The required numbers of arguments, or null if it does not matter. + /// The suitable opaque predicate if found, or null if not found. + IOpaquePredicateDescriptor GetPredicate(MethodDef method, OpaquePredicateType? type, params int[] argCount); + } +} \ No newline at end of file diff --git a/Confuser.Core/API/IDataStore.cs b/Confuser.Core/API/IDataStore.cs new file mode 100644 index 000000000..e8aa12f3a --- /dev/null +++ b/Confuser.Core/API/IDataStore.cs @@ -0,0 +1,53 @@ +using System; +using dnlib.DotNet; +using dnlib.DotNet.Emit; + +namespace Confuser.Core.API { + /// + /// A data store. + /// + public interface IDataStore { + /// + /// Gets the priority of this data store; higher priority means it + /// would be tried earlier. + /// + /// The priority of this data store. + int Priority { get; } + + /// + /// Gets the number of keys this predicate has. + /// + /// + /// Keys are used by the data store to encrypt data/whatever purpose. + /// + /// The number of keys this data store has. + int KeyCount { get; } + + /// + /// Determines whether this data store can be used in the specified method. + /// + /// The method. + /// true if this data store can be used in the specified method; otherwise, false. + bool IsUsable(MethodDef method); + + /// + /// Creates an accessor of this data store for the specified method. + /// + /// The method. + /// The keys. + /// The data to store. + /// A newly accessor of this data store. + IDataStoreAccessor CreateAccessor(MethodDef method, uint[] keys, byte[] data); + } + + /// + /// An accessor of data store. + /// + public interface IDataStoreAccessor { + /// + /// Emits the runtime instruction sequence for this accessor. + /// + /// An instruction sequence that returns the stored data. + Instruction[] Emit(); + } +} \ No newline at end of file diff --git a/Confuser.Core/API/IOpaquePredicate.cs b/Confuser.Core/API/IOpaquePredicate.cs new file mode 100644 index 000000000..a1eef14b5 --- /dev/null +++ b/Confuser.Core/API/IOpaquePredicate.cs @@ -0,0 +1,79 @@ +using System; +using dnlib.DotNet; +using dnlib.DotNet.Emit; + +namespace Confuser.Core.API { + /// + /// The descriptor of a type of opaque predicate. + /// + public interface IOpaquePredicateDescriptor { + /// + /// Gets the type of the opaque predicate. + /// + /// The type of the opaque predicate. + OpaquePredicateType Type { get; } + + /// + /// Gets the number of arguments this predicate has. + /// + /// + /// When is , + /// there can be 0 or more arguments. + /// When is , + /// there must be more than 0 arguments. + /// + /// The number of arguments this predicate has. + int ArgumentCount { get; } + + /// + /// Determines whether this predicate can be used with the specified method. + /// + /// The method. + /// true if this predicate can be used with the specified method; otherwise, false. + bool IsUsable(MethodDef method); + + /// + /// Creates a new opaque predicate for the specified method. + /// + /// The method. + /// A newly create opaque predicate. + IOpaquePredicate CreatePredicate(MethodDef method); + } + + /// + /// An instance of opaque predicate. + /// + public interface IOpaquePredicate { + /// + /// Emits the runtime instruction sequence for this predicate. + /// + /// + /// A function that returns an instruction sequence that returns the input value, + /// or null if is 0. + /// + /// An instruction sequence that returns the value of this predicate. + Instruction[] Emit(Func loadArg); + + /// + /// Computes the value of this predicate with the specified argument. + /// + /// The argument to this predicate. + /// The return value of this predicate. + uint GetValue(uint[] arg); + } + + /// + /// The type of opaque predicate. + /// + public enum OpaquePredicateType { + /// + /// A function, in a mathematics sense, with one input and one output. + /// + Function, + + /// + /// A constant function, always returning the same value. + /// + Invariant + } +} \ No newline at end of file diff --git a/Confuser.Core/Confuser.Core.csproj b/Confuser.Core/Confuser.Core.csproj index 1b749cf3c..68e91add8 100644 --- a/Confuser.Core/Confuser.Core.csproj +++ b/Confuser.Core/Confuser.Core.csproj @@ -52,6 +52,9 @@ Properties\GlobalAssemblyInfo.cs + + + diff --git a/Confuser.Core/ConfuserEngine.cs b/Confuser.Core/ConfuserEngine.cs index fe4fa7390..b7fbbdb99 100644 --- a/Confuser.Core/ConfuserEngine.cs +++ b/Confuser.Core/ConfuserEngine.cs @@ -280,11 +280,11 @@ static void Inspection(ConfuserContext context) { modType = new TypeDefUser("", "", null); modType.Attributes = TypeAttributes.AnsiClass; module.Types.Add(modType); - marker.Mark(modType); + marker.Mark(modType, null); } MethodDef cctor = modType.FindOrCreateStaticConstructor(); if (!marker.IsMarked(cctor)) - marker.Mark(cctor); + marker.Mark(cctor, null); } context.Logger.Debug("Watermarking..."); @@ -292,7 +292,7 @@ static void Inspection(ConfuserContext context) { TypeRef attrRef = module.CorLibTypes.GetTypeRef("System", "Attribute"); var attrType = new TypeDefUser("", "ConfusedByAttribute", attrRef); module.Types.Add(attrType); - marker.Mark(attrType); + marker.Mark(attrType, null); var ctor = new MethodDefUser( ".ctor", @@ -305,7 +305,7 @@ static void Inspection(ConfuserContext context) { ctor.Body.Instructions.Add(OpCodes.Call.ToInstruction(new MemberRefUser(module, ".ctor", MethodSig.CreateInstance(module.CorLibTypes.Void), attrRef))); ctor.Body.Instructions.Add(OpCodes.Ret.ToInstruction()); attrType.Methods.Add(ctor); - marker.Mark(ctor); + marker.Mark(ctor, null); var attr = new CustomAttribute(ctor); attr.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, Version)); diff --git a/Confuser.Core/CoreComponent.cs b/Confuser.Core/CoreComponent.cs index 7ce689039..3c6016925 100644 --- a/Confuser.Core/CoreComponent.cs +++ b/Confuser.Core/CoreComponent.cs @@ -1,4 +1,5 @@ using System; +using Confuser.Core.API; using Confuser.Core.Services; namespace Confuser.Core { @@ -31,6 +32,11 @@ public class CoreComponent : ConfuserComponent { /// public const string _CompressionServiceId = "Confuser.Compression"; + /// + /// The service ID of API Store + /// + public const string _APIStoreId = "Confuser.APIStore"; + readonly Marker marker; readonly ConfuserParameters parameters; @@ -71,6 +77,7 @@ protected internal override void Initialize(ConfuserContext context) { context.Registry.RegisterService(_TraceServiceId, typeof(ITraceService), new TraceService(context)); context.Registry.RegisterService(_RuntimeServiceId, typeof(IRuntimeService), new RuntimeService()); context.Registry.RegisterService(_CompressionServiceId, typeof(ICompressionService), new CompressionService(context)); + context.Registry.RegisterService(_APIStoreId, typeof(IAPIStore), new APIStore(context)); } /// diff --git a/Confuser.Core/Services/MarkerService.cs b/Confuser.Core/Services/MarkerService.cs index ef1cd58b2..f982904b7 100644 --- a/Confuser.Core/Services/MarkerService.cs +++ b/Confuser.Core/Services/MarkerService.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Generic; using dnlib.DotNet; namespace Confuser.Core.Services { internal class MarkerService : IMarkerService { readonly ConfuserContext context; readonly Marker marker; + readonly Dictionary helperParents; /// /// Initializes a new instance of the class. @@ -14,10 +16,11 @@ internal class MarkerService : IMarkerService { public MarkerService(ConfuserContext context, Marker marker) { this.context = context; this.marker = marker; + helperParents = new Dictionary(); } /// - public void Mark(IDnlibDef member) { + public void Mark(IDnlibDef member, ConfuserComponent parentComp) { if (member == null) throw new ArgumentNullException("member"); if (member is ModuleDef) @@ -26,12 +29,22 @@ public void Mark(IDnlibDef member) { return; marker.MarkMember(member, context); + if (parentComp != null) + helperParents[member] = parentComp; } /// public bool IsMarked(IDnlibDef def) { return ProtectionParameters.GetParameters(context, def) != null; } + + /// + public ConfuserComponent GetHelperParent(IDnlibDef def) { + ConfuserComponent parent; + if (!helperParents.TryGetValue(def, out parent)) + return null; + return parent; + } } /// @@ -42,9 +55,10 @@ public interface IMarkerService { /// Marks the helper member. /// /// The helper member. + /// The parent component. /// is a . /// is null. - void Mark(IDnlibDef member); + void Mark(IDnlibDef member, ConfuserComponent parentComp); /// /// Determines whether the specified definition is marked. @@ -52,5 +66,12 @@ public interface IMarkerService { /// The definition. /// true if the specified definition is marked; otherwise, false. bool IsMarked(IDnlibDef def); + + /// + /// Gets the parent component of the specified helper. + /// + /// The helper definition. + /// The parent component of the helper, or null if the specified definition is not a helper. + ConfuserComponent GetHelperParent(IDnlibDef def); } } \ No newline at end of file diff --git a/Confuser.Core/Utils.cs b/Confuser.Core/Utils.cs index 104e7a29b..19849eb2e 100644 --- a/Confuser.Core/Utils.cs +++ b/Confuser.Core/Utils.cs @@ -58,7 +58,7 @@ public static TValue GetValueOrDefaultLazy( /// The key of the element to add. /// The value of the element to add. /// key is null. - public static void AddListEntry(this Dictionary> self, TKey key, TValue value) { + public static void AddListEntry(this IDictionary> self, TKey key, TValue value) { if (key == null) throw new ArgumentNullException("key"); List list; diff --git a/Confuser.Protections/AntiDebugProtection.cs b/Confuser.Protections/AntiDebugProtection.cs index 36cde3358..028bf78be 100644 --- a/Confuser.Protections/AntiDebugProtection.cs +++ b/Confuser.Protections/AntiDebugProtection.cs @@ -78,7 +78,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa attr = rt.GetRuntimeType(attrName); module.Types.Add(attr = InjectHelper.Inject(attr, module)); foreach (IDnlibDef member in attr.FindDefinitions()) { - marker.Mark(member); + marker.Mark(member, (Protection)Parent); name.Analyze(member); } name.SetCanRename(attr, false); @@ -94,7 +94,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init)); foreach (IDnlibDef member in members) { - marker.Mark(member); + marker.Mark(member, (Protection)Parent); name.Analyze(member); bool ren = true; diff --git a/Confuser.Protections/AntiDumpProtection.cs b/Confuser.Protections/AntiDumpProtection.cs index 217a1d84d..77b02700d 100644 --- a/Confuser.Protections/AntiDumpProtection.cs +++ b/Confuser.Protections/AntiDumpProtection.cs @@ -68,7 +68,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, init)); foreach (IDnlibDef member in members) - name.MarkHelper(member, marker); + name.MarkHelper(member, marker, (Protection)Parent); } } } diff --git a/Confuser.Protections/AntiTamper/JITMode.cs b/Confuser.Protections/AntiTamper/JITMode.cs index 68e14fab0..a99b38999 100644 --- a/Confuser.Protections/AntiTamper/JITMode.cs +++ b/Confuser.Protections/AntiTamper/JITMode.cs @@ -113,7 +113,7 @@ public void HandleInject(AntiTamperProtection parent, ConfuserContext context, P cctorRepl.Body = new CilBody(); cctorRepl.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); context.CurrentModule.GlobalType.Methods.Add(cctorRepl); - name.MarkHelper(cctorRepl, marker); + name.MarkHelper(cctorRepl, marker, parent); MutationHelper.InjectKeys(defs.OfType().Single(method => method.Name == "HookHandler"), new[] { 0 }, new[] { (int)key }); @@ -131,7 +131,7 @@ public void HandleInject(AntiTamperProtection parent, ConfuserContext context, P foreach (FieldDef f in fields) dataType.Fields.Add(f); } - name.MarkHelper(def, marker); + name.MarkHelper(def, marker, parent); if (def is MethodDef) parent.ExcludeMethod(context, (MethodDef)def); } diff --git a/Confuser.Protections/AntiTamper/NormalMode.cs b/Confuser.Protections/AntiTamper/NormalMode.cs index b72975537..9fd9d570a 100644 --- a/Confuser.Protections/AntiTamper/NormalMode.cs +++ b/Confuser.Protections/AntiTamper/NormalMode.cs @@ -83,7 +83,7 @@ public void HandleInject(AntiTamperProtection parent, ConfuserContext context, P var name = context.Registry.GetService(); var marker = context.Registry.GetService(); foreach (IDnlibDef def in members) { - name.MarkHelper(def, marker); + name.MarkHelper(def, marker, parent); if (def is MethodDef) parent.ExcludeMethod(context, (MethodDef)def); } diff --git a/Confuser.Protections/Constants/CEContext.cs b/Confuser.Protections/Constants/CEContext.cs index d3aef725e..101bb92a1 100644 --- a/Confuser.Protections/Constants/CEContext.cs +++ b/Confuser.Protections/Constants/CEContext.cs @@ -10,6 +10,7 @@ namespace Confuser.Protections.Constants { internal class CEContext { public ConfuserContext Context; + public ConstantProtection Protection; public ModuleDef Module; public FieldDef BufferField; diff --git a/Confuser.Protections/Constants/InjectPhase.cs b/Confuser.Protections/Constants/InjectPhase.cs index 15a1d059c..c8d0c3318 100644 --- a/Confuser.Protections/Constants/InjectPhase.cs +++ b/Confuser.Protections/Constants/InjectPhase.cs @@ -31,6 +31,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa var marker = context.Registry.GetService(); var rt = context.Registry.GetService(); var moduleCtx = new CEContext { + Protection = (ConstantProtection)Parent, Random = context.Registry.GetService().GetRandomGenerator(Parent.Id), Context = context, Module = context.CurrentModule, @@ -61,7 +62,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa // Inject helpers MethodDef decomp = compression.GetRuntimeDecompressor(context.CurrentModule, member => { - name.MarkHelper(member, marker); + name.MarkHelper(member, marker, (Protection)Parent); if (member is MethodDef) ProtectionParameters.GetParameters(context, member).Remove(Parent); }); @@ -88,7 +89,7 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu moduleCtx.BufferField = (FieldDef)member; else if (member.Name == "Initialize") moduleCtx.InitMethod = (MethodDef)member; - moduleCtx.Name.MarkHelper(member, moduleCtx.Marker); + moduleCtx.Name.MarkHelper(member, moduleCtx.Marker, (Protection)Parent); } ProtectionParameters.GetParameters(context, moduleCtx.InitMethod).Remove(Parent); @@ -98,14 +99,14 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu dataType.IsSealed = true; moduleCtx.DataType = dataType; context.CurrentModule.GlobalType.NestedTypes.Add(dataType); - moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker); + moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker, (Protection)Parent); moduleCtx.DataField = new FieldDefUser(moduleCtx.Name.RandomName(), new FieldSig(dataType.ToTypeSig())) { IsStatic = true, Access = FieldAttributes.CompilerControlled }; context.CurrentModule.GlobalType.Fields.Add(moduleCtx.DataField); - moduleCtx.Name.MarkHelper(moduleCtx.DataField, moduleCtx.Marker); + moduleCtx.Name.MarkHelper(moduleCtx.DataField, moduleCtx.Marker, (Protection)Parent); MethodDef decoder = rt.GetRuntimeType("Confuser.Runtime.Constant").FindMethod("Get"); moduleCtx.Decoders = new List>(); @@ -127,7 +128,7 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu } } context.CurrentModule.GlobalType.Methods.Add(decoderInst); - moduleCtx.Name.MarkHelper(decoderInst, moduleCtx.Marker); + moduleCtx.Name.MarkHelper(decoderInst, moduleCtx.Marker, (Protection)Parent); ProtectionParameters.GetParameters(context, decoderInst).Remove(Parent); var decoderDesc = new DecoderDesc(); diff --git a/Confuser.Protections/Constants/x86Mode.cs b/Confuser.Protections/Constants/x86Mode.cs index 688c81a02..36a6c4e67 100644 --- a/Confuser.Protections/Constants/x86Mode.cs +++ b/Confuser.Protections/Constants/x86Mode.cs @@ -98,7 +98,7 @@ public void Compile(CEContext ctx) { //native.HasSecurity = true; ctx.Module.GlobalType.Methods.Add(native); - ctx.Name.MarkHelper(native, ctx.Marker); + ctx.Name.MarkHelper(native, ctx.Marker, ctx.Protection); x86Register? reg; var codeGen = new x86CodeGen(); diff --git a/Confuser.Protections/ControlFlow/CFContext.cs b/Confuser.Protections/ControlFlow/CFContext.cs index f9961efb6..8ff06ce53 100644 --- a/Confuser.Protections/ControlFlow/CFContext.cs +++ b/Confuser.Protections/ControlFlow/CFContext.cs @@ -20,6 +20,7 @@ internal enum PredicateType { internal class CFContext { public ConfuserContext Context; + public ControlFlowProtection Protection; public int Depth; public IDynCipherService DynCipher; diff --git a/Confuser.Protections/ControlFlow/ControlFlowPhase.cs b/Confuser.Protections/ControlFlow/ControlFlowPhase.cs index 41a37362e..9c5c09c22 100644 --- a/Confuser.Protections/ControlFlow/ControlFlowPhase.cs +++ b/Confuser.Protections/ControlFlow/ControlFlowPhase.cs @@ -25,7 +25,7 @@ public override string Name { get { return "Control flow mangling"; } } - static CFContext ParseParameters(MethodDef method, ConfuserContext context, ProtectionParameters parameters, RandomGenerator random, bool disableOpti) { + CFContext ParseParameters(MethodDef method, ConfuserContext context, ProtectionParameters parameters, RandomGenerator random, bool disableOpti) { var ret = new CFContext(); ret.Type = parameters.GetParameter(context, method, "type", CFType.Switch); ret.Predicate = parameters.GetParameter(context, method, "predicate", PredicateType.Normal); @@ -36,6 +36,7 @@ static CFContext ParseParameters(MethodDef method, ConfuserContext context, Prot ret.JunkCode = parameters.GetParameter(context, method, "junk", false) && !disableOpti; + ret.Protection = (ControlFlowProtection)Parent; ret.Random = random; ret.Method = method; ret.Context = context; diff --git a/Confuser.Protections/ControlFlow/x86Predicate.cs b/Confuser.Protections/ControlFlow/x86Predicate.cs index ed55790ff..99023c193 100644 --- a/Confuser.Protections/ControlFlow/x86Predicate.cs +++ b/Confuser.Protections/ControlFlow/x86Predicate.cs @@ -67,7 +67,7 @@ public void Compile(CFContext ctx) { //native.HasSecurity = true; ctx.Method.Module.GlobalType.Methods.Add(native); - ctx.Context.Registry.GetService().Mark(native); + ctx.Context.Registry.GetService().Mark(native, ctx.Protection); ctx.Context.Registry.GetService().SetCanRename(native, false); x86Register? reg; diff --git a/Confuser.Protections/ReferenceProxy/MildMode.cs b/Confuser.Protections/ReferenceProxy/MildMode.cs index ef5944f48..b51f6dc56 100644 --- a/Confuser.Protections/ReferenceProxy/MildMode.cs +++ b/Confuser.Protections/ReferenceProxy/MildMode.cs @@ -37,7 +37,7 @@ public override void ProcessCall(RPContext ctx, int instrIndex) { sig.Params.RemoveAt(0); } - ctx.Marker.Mark(proxy); + ctx.Marker.Mark(proxy, ctx.Protection); ctx.Name.Analyze(proxy); ctx.Name.SetCanRename(proxy, false); diff --git a/Confuser.Protections/ReferenceProxy/RPContext.cs b/Confuser.Protections/ReferenceProxy/RPContext.cs index a7f88d45a..40a5b6560 100644 --- a/Confuser.Protections/ReferenceProxy/RPContext.cs +++ b/Confuser.Protections/ReferenceProxy/RPContext.cs @@ -21,6 +21,7 @@ internal enum EncodingType { } internal class RPContext { + public ReferenceProxyProtection Protection; public CilBody Body; public HashSet BranchTargets; public ConfuserContext Context; diff --git a/Confuser.Protections/ReferenceProxy/RPMode.cs b/Confuser.Protections/ReferenceProxy/RPMode.cs index ab34ebe67..66c040d32 100644 --- a/Confuser.Protections/ReferenceProxy/RPMode.cs +++ b/Confuser.Protections/ReferenceProxy/RPMode.cs @@ -81,7 +81,7 @@ protected static TypeDef GetDelegateType(RPContext ctx, MethodSig sig) { ctx.Module.Types.Add(ret); foreach (IDnlibDef def in ret.FindDefinitions()) { - ctx.Marker.Mark(def); + ctx.Marker.Mark(def, ctx.Protection); ctx.Name.SetCanRename(def, false); } diff --git a/Confuser.Protections/ReferenceProxy/ReferenceProxyPhase.cs b/Confuser.Protections/ReferenceProxy/ReferenceProxyPhase.cs index efbd4bf85..b69121955 100644 --- a/Confuser.Protections/ReferenceProxy/ReferenceProxyPhase.cs +++ b/Confuser.Protections/ReferenceProxy/ReferenceProxyPhase.cs @@ -22,7 +22,7 @@ public override string Name { get { return "Encoding reference proxies"; } } - static RPContext ParseParameters(MethodDef method, ConfuserContext context, ProtectionParameters parameters, RPStore store) { + RPContext ParseParameters(MethodDef method, ConfuserContext context, ProtectionParameters parameters, RPStore store) { var ret = new RPContext(); ret.Mode = parameters.GetParameter(context, method, "mode", Mode.Mild); ret.Encoding = parameters.GetParameter(context, method, "encoding", EncodingType.Normal); @@ -41,6 +41,7 @@ static RPContext ParseParameters(MethodDef method, ConfuserContext context, Prot .SelectMany(instr => (Instruction[])instr.Operand)) .Where(target => target != null)); + ret.Protection = (ReferenceProxyProtection)Parent; ret.Random = store.random; ret.Context = context; ret.Marker = context.Registry.GetService(); diff --git a/Confuser.Protections/ReferenceProxy/StrongMode.cs b/Confuser.Protections/ReferenceProxy/StrongMode.cs index affd55b9b..53d66cefa 100644 --- a/Confuser.Protections/ReferenceProxy/StrongMode.cs +++ b/Confuser.Protections/ReferenceProxy/StrongMode.cs @@ -177,7 +177,7 @@ MethodDef CreateBridge(RPContext ctx, TypeDef delegateType, FieldDef field, Meth delegateType.Methods.Add(method); - ctx.Context.Registry.GetService().Mark(method); + ctx.Context.Registry.GetService().Mark(method, ctx.Protection); ctx.Name.SetCanRename(method, false); return method; @@ -196,7 +196,7 @@ FieldDef CreateField(RPContext ctx, TypeDef delegateType) { field.CustomAttributes.Add(new CustomAttribute(GetKeyAttr(ctx).FindInstanceConstructors().First())); delegateType.Fields.Add(field); - ctx.Marker.Mark(field); + ctx.Marker.Mark(field, ctx.Protection); ctx.Name.SetCanRename(field, false); return field; @@ -238,11 +238,11 @@ TypeDef GetKeyAttr(RPContext ctx) { foreach (IDnlibDef def in injectedAttr.FindDefinitions()) { if (def.Name == "GetHashCode") { - ctx.Name.MarkHelper(def, ctx.Marker); + ctx.Name.MarkHelper(def, ctx.Marker, ctx.Protection); ((MethodDef)def).Access = MethodAttributes.Public; } else - ctx.Name.MarkHelper(def, ctx.Marker); + ctx.Name.MarkHelper(def, ctx.Marker, ctx.Protection); } } return keyAttrs[index].Item1; @@ -262,7 +262,7 @@ InitMethodDesc GetInitMethod(RPContext ctx, IRPEncoding encoding) { injectedMethod.Access = MethodAttributes.PrivateScope; injectedMethod.Name = ctx.Name.RandomName(); ctx.Name.SetCanRename(injectedMethod, false); - ctx.Marker.Mark(injectedMethod); + ctx.Marker.Mark(injectedMethod, ctx.Protection); var desc = new InitMethodDesc { Method = injectedMethod }; @@ -318,7 +318,7 @@ public override void Finalize(RPContext ctx) { foreach (TypeDef delegateType in ctx.Delegates.Values) { MethodDef cctor = delegateType.FindOrCreateStaticConstructor(); - ctx.Marker.Mark(cctor); + ctx.Marker.Mark(cctor, ctx.Protection); ctx.Name.SetCanRename(cctor, false); } diff --git a/Confuser.Protections/ReferenceProxy/x86Encoding.cs b/Confuser.Protections/ReferenceProxy/x86Encoding.cs index 0a84b4e19..ff07efb33 100644 --- a/Confuser.Protections/ReferenceProxy/x86Encoding.cs +++ b/Confuser.Protections/ReferenceProxy/x86Encoding.cs @@ -40,7 +40,7 @@ void Compile(RPContext ctx, out Func expCompiled, out MethodDef native native.ImplAttributes = MethodImplAttributes.Native | MethodImplAttributes.Unmanaged | MethodImplAttributes.PreserveSig; ctx.Module.GlobalType.Methods.Add(native); - ctx.Context.Registry.GetService().Mark(native); + ctx.Context.Registry.GetService().Mark(native, ctx.Protection); ctx.Context.Registry.GetService().SetCanRename(native, false); x86Register? reg; diff --git a/Confuser.Protections/Resources/InjectPhase.cs b/Confuser.Protections/Resources/InjectPhase.cs index 3446030b2..540591750 100644 --- a/Confuser.Protections/Resources/InjectPhase.cs +++ b/Confuser.Protections/Resources/InjectPhase.cs @@ -60,7 +60,7 @@ protected override void Execute(ConfuserContext context, ProtectionParameters pa // Inject helpers MethodDef decomp = compression.GetRuntimeDecompressor(context.CurrentModule, member => { - name.MarkHelper(member, marker); + name.MarkHelper(member, marker, (Protection)Parent); if (member is MethodDef) ProtectionParameters.GetParameters(context, member).Remove(Parent); }); @@ -82,7 +82,7 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu foreach (IDnlibDef member in members) { if (member.Name == "Initialize") moduleCtx.InitMethod = (MethodDef)member; - moduleCtx.Name.MarkHelper(member, moduleCtx.Marker); + moduleCtx.Name.MarkHelper(member, moduleCtx.Marker, (Protection)Parent); } var dataType = new TypeDefUser("", moduleCtx.Name.RandomName(), context.CurrentModule.CorLibTypes.GetTypeRef("System", "ValueType")); @@ -92,7 +92,7 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu dataType.ClassLayout = new ClassLayoutUser(1, 0); moduleCtx.DataType = dataType; context.CurrentModule.GlobalType.NestedTypes.Add(dataType); - moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker); + moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker, (Protection)Parent); moduleCtx.DataField = new FieldDefUser(moduleCtx.Name.RandomName(), new FieldSig(dataType.ToTypeSig())) { IsStatic = true, @@ -101,7 +101,7 @@ void InjectHelpers(ConfuserContext context, ICompressionService compression, IRu Access = FieldAttributes.CompilerControlled }; context.CurrentModule.GlobalType.Fields.Add(moduleCtx.DataField); - moduleCtx.Name.MarkHelper(moduleCtx.DataField, moduleCtx.Marker); + moduleCtx.Name.MarkHelper(moduleCtx.DataField, moduleCtx.Marker, (Protection)Parent); } void MutateInitializer(REContext moduleCtx, MethodDef decomp) { diff --git a/Confuser.Renamer/NameService.cs b/Confuser.Renamer/NameService.cs index 1774f1554..45c740a6c 100644 --- a/Confuser.Renamer/NameService.cs +++ b/Confuser.Renamer/NameService.cs @@ -31,7 +31,7 @@ public interface INameService { void SetOriginalName(object obj, string name); void SetOriginalNamespace(object obj, string ns); - void MarkHelper(IDnlibDef def, IMarkerService marker); + void MarkHelper(IDnlibDef def, IMarkerService marker, ConfuserComponent parentComp); } internal class NameService : INameService { @@ -169,7 +169,7 @@ public T FindRenamer() { return Renamers.OfType().Single(); } - public void MarkHelper(IDnlibDef def, IMarkerService marker) { + public void MarkHelper(IDnlibDef def, IMarkerService marker, ConfuserComponent parentComp) { if (marker.IsMarked(def)) return; if (def is MethodDef) { @@ -194,7 +194,7 @@ public void MarkHelper(IDnlibDef def, IMarkerService marker) { } SetCanRename(def, false); Analyze(def); - marker.Mark(def); + marker.Mark(def, parentComp); } #region Charsets diff --git a/Confuser.Renamer/RickRoller.cs b/Confuser.Renamer/RickRoller.cs index 55697ab1f..bff2baf42 100644 --- a/Confuser.Renamer/RickRoller.cs +++ b/Confuser.Renamer/RickRoller.cs @@ -40,8 +40,8 @@ public static void CommenceRickroll(ConfuserContext context, ModuleDef module) { trap.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); newType.Methods.Add(trap); - marker.Mark(newType); - marker.Mark(trap); + marker.Mark(newType, null); + marker.Mark(trap, null); nameService.SetCanRename(trap, false); foreach (var method in module.GetTypes().SelectMany(type => type.Methods)) {