void InjectHelpers(ConfuserContext context, ICompressionService compression, IRuntimeService rt, REContext moduleCtx) { var rtName = context.Packer != null ? "Confuser.Runtime.Resource_Packer" : "Confuser.Runtime.Resource"; IEnumerable<IDnlibDef> members = InjectHelper.Inject(rt.GetRuntimeType(rtName), context.CurrentModule.GlobalType, context.CurrentModule); foreach (IDnlibDef member in members) { if (member.Name == "Initialize") moduleCtx.InitMethod = (MethodDef)member; moduleCtx.Name.MarkHelper(member, moduleCtx.Marker, (Protection)Parent); } var dataType = new TypeDefUser("", moduleCtx.Name.RandomName(), context.CurrentModule.CorLibTypes.GetTypeRef("System", "ValueType")); dataType.Layout = TypeAttributes.ExplicitLayout; dataType.Visibility = TypeAttributes.NestedPrivate; dataType.IsSealed = true; dataType.ClassLayout = new ClassLayoutUser(1, 0); moduleCtx.DataType = dataType; context.CurrentModule.GlobalType.NestedTypes.Add(dataType); moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker, (Protection)Parent); moduleCtx.DataField = new FieldDefUser(moduleCtx.Name.RandomName(), new FieldSig(dataType.ToTypeSig())) { IsStatic = true, HasFieldRVA = true, InitialValue = new byte[0], Access = FieldAttributes.CompilerControlled }; context.CurrentModule.GlobalType.Fields.Add(moduleCtx.DataField); moduleCtx.Name.MarkHelper(moduleCtx.DataField, moduleCtx.Marker, (Protection)Parent); }
// Token: 0x06000195 RID: 405 RVA: 0x0006142C File Offset: 0x0005F62C protected override void Execute(ConfuserContext context, ProtectionParameters parameters) { bool flag = parameters.Targets.Any <IDnlibDef>(); if (flag) { bool flag2 = !UTF8String.IsNullOrEmpty(context.CurrentModule.Assembly.Culture); if (flag2) { context.Logger.DebugFormat("Skipping resource encryption for satellite assembly '{0}'.", new object[] { context.CurrentModule.Assembly.FullName }); } else { ICompressionService service = context.Registry.GetService <ICompressionService>(); INameService name = context.Registry.GetService <INameService>(); IMarkerService marker = context.Registry.GetService <IMarkerService>(); IRuntimeService service2 = context.Registry.GetService <IRuntimeService>(); REContext recontext = new REContext { Random = context.Registry.GetService <IRandomService>().GetRandomGenerator(base.Parent.Id), Context = context, Module = context.CurrentModule, Marker = marker, DynCipher = context.Registry.GetService <IDynCipherService>(), Name = name }; recontext.Mode = parameters.GetParameter <Mode>(context, context.CurrentModule, "mode", Mode.Normal); Mode mode = recontext.Mode; if (mode != Mode.Normal) { if (mode != Mode.Dynamic) { throw new UnreachableException(); } recontext.ModeHandler = new DynamicMode(); } else { recontext.ModeHandler = new NormalMode(); } MethodDef runtimeDecompressor = service.GetRuntimeDecompressor(context.CurrentModule, delegate(IDnlibDef member) { name.MarkHelper(member, marker, (Protection)this.Parent); bool flag3 = member is MethodDef; if (flag3) { ProtectionParameters.GetParameters(context, member).Remove(this.Parent); } }); this.InjectHelpers(context, service, service2, recontext); this.MutateInitializer(recontext, runtimeDecompressor); MethodDef methodDef = context.CurrentModule.GlobalType.FindStaticConstructor(); methodDef.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, recontext.InitMethod)); new MDPhase(recontext).Hook(); } } }
public IEnumerable <Instruction> EmitDecrypt(MethodDef init, REContext ctx, Local block, Local key) { for (int i = 0; i < 0x10; i++) { yield return(Instruction.Create(OpCodes.Ldloc, block)); yield return(Instruction.Create(OpCodes.Ldc_I4, i)); yield return(Instruction.Create(OpCodes.Ldloc, block)); yield return(Instruction.Create(OpCodes.Ldc_I4, i)); yield return(Instruction.Create(OpCodes.Ldelem_U4)); yield return(Instruction.Create(OpCodes.Ldloc, key)); yield return(Instruction.Create(OpCodes.Ldc_I4, i)); yield return(Instruction.Create(OpCodes.Ldelem_U4)); yield return(Instruction.Create(OpCodes.Xor)); yield return(Instruction.Create(OpCodes.Stelem_I4)); } }
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) { if (parameters.Targets.Any()) { if (!UTF8String.IsNullOrEmpty(context.CurrentModule.Assembly.Culture)) { context.Logger.LogFormat("WARN: Skipping resource encryption for satellite assembly '{0}'.", context.CurrentModule.Assembly.FullName); return; } var compression = context.Registry.GetService <ICompressionService>(); var name = context.Registry.GetService <INameService>(); var marker = context.Registry.GetService <IMarkerService>(); var rt = context.Registry.GetService <IRuntimeService>(); var moduleCtx = new REContext { Random = context.Registry.GetService <IRandomService>().GetRandomGenerator(Parent.Id), Context = context, Module = context.CurrentModule, Marker = marker, DynCipher = context.Registry.GetService <IDynCipherService>(), Name = name }; // Extract parameters moduleCtx.Mode = parameters.GetParameter(context, context.CurrentModule, "mode", Mode.Normal); switch (moduleCtx.Mode) { case Mode.Normal: moduleCtx.ModeHandler = new NormalMode(); break; case Mode.Dynamic: moduleCtx.ModeHandler = new DynamicMode(); break; default: throw new UnreachableException(); } // Inject helpers MethodDef decomp = compression.GetRuntimeDecompressor(context.CurrentModule, member => { name.MarkHelper(member, marker, (Protection)Parent); if (member is MethodDef) { ProtectionParameters.GetParameters(context, member).Remove(Parent); } }); InjectHelpers(context, compression, rt, moduleCtx); // Mutate codes MutateInitializer(moduleCtx, decomp); MethodDef cctor = context.CurrentModule.GlobalType.FindStaticConstructor(); cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, moduleCtx.InitMethod)); new MDPhase(moduleCtx).Hook(); } }
protected override void Execute(ConfuserContext context, ProtectionParameters parameters) { if (parameters.Targets.Any()) { if (!UTF8String.IsNullOrEmpty(context.CurrentModule.Assembly.Culture)) { context.Logger.DebugFormat("Skipping resource encryption for satellite assembly '{0}'.", context.CurrentModule.Assembly.FullName); return; } var compression = context.Registry.GetService<ICompressionService>(); var name = context.Registry.GetService<INameService>(); var marker = context.Registry.GetService<IMarkerService>(); var rt = context.Registry.GetService<IRuntimeService>(); var moduleCtx = new REContext { Random = context.Registry.GetService<IRandomService>().GetRandomGenerator(Parent.Id), Context = context, Module = context.CurrentModule, Marker = marker, DynCipher = context.Registry.GetService<IDynCipherService>(), Name = name }; // Extract parameters moduleCtx.Mode = parameters.GetParameter(context, context.CurrentModule, "mode", Mode.Normal); switch (moduleCtx.Mode) { case Mode.Normal: moduleCtx.ModeHandler = new NormalMode(); break; case Mode.Dynamic: moduleCtx.ModeHandler = new DynamicMode(); break; default: throw new UnreachableException(); } // Inject helpers MethodDef decomp = compression.GetRuntimeDecompressor(context.CurrentModule, member => { name.MarkHelper(member, marker, (Protection)Parent); if (member is MethodDef) ProtectionParameters.GetParameters(context, member).Remove(Parent); }); InjectHelpers(context, compression, rt, moduleCtx); // Mutate codes MutateInitializer(moduleCtx, decomp); MethodDef cctor = context.CurrentModule.GlobalType.FindStaticConstructor(); cctor.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, moduleCtx.InitMethod)); new MDPhase(moduleCtx).Hook(); } }
public IEnumerable<Instruction> EmitDecrypt(MethodDef init, REContext ctx, Local block, Local key) { for (int i = 0; i < 0x10; i++) { yield return Instruction.Create(OpCodes.Ldloc, block); yield return Instruction.Create(OpCodes.Ldc_I4, i); yield return Instruction.Create(OpCodes.Ldloc, block); yield return Instruction.Create(OpCodes.Ldc_I4, i); yield return Instruction.Create(OpCodes.Ldelem_U4); yield return Instruction.Create(OpCodes.Ldloc, key); yield return Instruction.Create(OpCodes.Ldc_I4, i); yield return Instruction.Create(OpCodes.Ldelem_U4); yield return Instruction.Create(OpCodes.Xor); yield return Instruction.Create(OpCodes.Stelem_I4); } }
// Token: 0x06000197 RID: 407 RVA: 0x000618C0 File Offset: 0x0005FAC0 private void MutateInitializer(REContext moduleCtx, MethodDef decomp) { moduleCtx.InitMethod.Body.SimplifyMacros(moduleCtx.InitMethod.Parameters); List <Instruction> list = moduleCtx.InitMethod.Body.Instructions.ToList <Instruction>(); for (int i = 0; i < list.Count; i++) { Instruction instruction = list[i]; IMethod method = instruction.Operand as IMethod; bool flag = instruction.OpCode == OpCodes.Call; if (flag) { bool flag2 = method.DeclaringType.Name == "Mutation" && method.Name == "Crypt"; if (flag2) { Instruction instruction2 = list[i - 2]; Instruction instruction3 = list[i - 1]; Debug.Assert(instruction2.OpCode == OpCodes.Ldloc && instruction3.OpCode == OpCodes.Ldloc); list.RemoveAt(i); list.RemoveAt(i - 1); list.RemoveAt(i - 2); list.InsertRange(i - 2, moduleCtx.ModeHandler.EmitDecrypt(moduleCtx.InitMethod, moduleCtx, (Local)instruction2.Operand, (Local)instruction3.Operand)); } else { bool flag3 = method.DeclaringType.Name == "Lzma" && method.Name == "Decompress"; if (flag3) { instruction.Operand = decomp; } } } } moduleCtx.InitMethod.Body.Instructions.Clear(); foreach (Instruction item in list) { moduleCtx.InitMethod.Body.Instructions.Add(item); } MutationHelper.ReplacePlaceholder(moduleCtx.InitMethod, delegate(Instruction[] arg) { List <Instruction> list2 = new List <Instruction>(); list2.AddRange(arg); list2.Add(Instruction.Create(OpCodes.Dup)); list2.Add(Instruction.Create(OpCodes.Ldtoken, moduleCtx.DataField)); list2.Add(Instruction.Create(OpCodes.Call, moduleCtx.Module.Import(typeof(RuntimeHelpers).GetMethod("InitializeArray")))); return(list2.ToArray()); }); moduleCtx.Context.Registry.GetService <IConstantService>().ExcludeMethod(moduleCtx.Context, moduleCtx.InitMethod); }
private void MutateInitializer(REContext moduleCtx, MethodDef decomp) { moduleCtx.InitMethod.Body.SimplifyMacros(moduleCtx.InitMethod.Parameters); var instrs = moduleCtx.InitMethod.Body.Instructions.ToList(); for (var i = 0; i < instrs.Count; i++) { var instr = instrs[i]; var method = instr.Operand as IMethod; if (instr.OpCode == OpCodes.Call) { if (method.DeclaringType.Name == "Mutation" && method.Name == "Crypt") { var ldBlock = instrs[i - 2]; var ldKey = instrs[i - 1]; Debug.Assert(ldBlock.OpCode == OpCodes.Ldloc && ldKey.OpCode == OpCodes.Ldloc); instrs.RemoveAt(i); instrs.RemoveAt(i - 1); instrs.RemoveAt(i - 2); instrs.InsertRange(i - 2, moduleCtx.ModeHandler.EmitDecrypt(moduleCtx.InitMethod, moduleCtx, (Local)ldBlock.Operand, (Local)ldKey.Operand)); } else if (method.DeclaringType.Name == "Lzma" && method.Name == "Decompress") { instr.Operand = decomp; } } } moduleCtx.InitMethod.Body.Instructions.Clear(); foreach (var instr in instrs) { moduleCtx.InitMethod.Body.Instructions.Add(instr); } MutationHelper.ReplacePlaceholder(moduleCtx.InitMethod, arg => { var repl = new List <Instruction>(); repl.AddRange(arg); repl.Add(Instruction.Create(OpCodes.Dup)); repl.Add(Instruction.Create(OpCodes.Ldtoken, moduleCtx.DataField)); repl.Add(Instruction.Create(OpCodes.Call, moduleCtx.Module.Import( typeof(RuntimeHelpers).GetMethod("InitializeArray")))); return(repl.ToArray()); }); moduleCtx.Context.Registry.GetService <IConstantService>().ExcludeMethod(moduleCtx.Context, moduleCtx.InitMethod); }
public IEnumerable<Instruction> EmitDecrypt(MethodDef init, REContext ctx, Local block, Local key) { StatementBlock encrypt, decrypt; ctx.DynCipher.GenerateCipherPair(ctx.Random, out encrypt, out decrypt); var ret = new List<Instruction>(); var codeGen = new CodeGen(block, key, init, ret); codeGen.GenerateCIL(decrypt); codeGen.Commit(init.Body); var dmCodeGen = new DMCodeGen(typeof(void), new[] { Tuple.Create("{BUFFER}", typeof(uint[])), Tuple.Create("{KEY}", typeof(uint[])) }); dmCodeGen.GenerateCIL(encrypt); encryptFunc = dmCodeGen.Compile<Action<uint[], uint[]>>(); return ret; }
// Token: 0x0600019C RID: 412 RVA: 0x0006205D File Offset: 0x0006025D public IEnumerable<Instruction> EmitDecrypt(MethodDef init, REContext ctx, Local block, Local key) { int num; for (int i = 0; i < 16; i = num + 1) { yield return Instruction.Create(OpCodes.Ldloc, block); yield return Instruction.Create(OpCodes.Ldc_I4, i); yield return Instruction.Create(OpCodes.Ldloc, block); yield return Instruction.Create(OpCodes.Ldc_I4, i); yield return Instruction.Create(OpCodes.Ldelem_U4); yield return Instruction.Create(OpCodes.Ldloc, key); yield return Instruction.Create(OpCodes.Ldc_I4, i); yield return Instruction.Create(OpCodes.Ldelem_U4); yield return Instruction.Create(OpCodes.Xor); yield return Instruction.Create(OpCodes.Stelem_I4); num = i; } yield break; }
public IEnumerable <Instruction> EmitDecrypt(MethodDef init, REContext ctx, Local block, Local key) { ctx.DynCipher.GenerateCipherPair(ctx.Random, out StatementBlock encrypt, out StatementBlock decrypt); var ret = new List <Instruction>(); var codeGen = new CodeGen(block, key, init, ret); codeGen.GenerateCIL(decrypt); codeGen.Commit(init.Body); var dmCodeGen = new DMCodeGen(typeof(void), new[] { Tuple.Create("{BUFFER}", typeof(uint[])), Tuple.Create("{KEY}", typeof(uint[])) }); dmCodeGen.GenerateCIL(encrypt); encryptFunc = dmCodeGen.Compile <Action <uint[], uint[]> >(); return(ret); }
// Token: 0x0600018D RID: 397 RVA: 0x00061318 File Offset: 0x0005F518 public IEnumerable <Instruction> EmitDecrypt(MethodDef init, REContext ctx, Local block, Local key) { StatementBlock statement; StatementBlock statement2; ctx.DynCipher.GenerateCipherPair(ctx.Random, out statement, out statement2); List <Instruction> list = new List <Instruction>(); DynamicMode.CodeGen codeGen = new DynamicMode.CodeGen(block, key, init, list); codeGen.GenerateCIL(statement2); codeGen.Commit(init.Body); DMCodeGen dmcodeGen = new DMCodeGen(typeof(void), new Tuple <string, Type>[] { Tuple.Create <string, Type>("{BUFFER}", typeof(uint[])), Tuple.Create <string, Type>("{KEY}", typeof(uint[])) }); dmcodeGen.GenerateCIL(statement); this.encryptFunc = dmcodeGen.Compile <Action <uint[], uint[]> >(); return(list); }
void InjectHelpers(ConfuserContext context, ICompressionService compression, IRuntimeService rt, REContext moduleCtx) { var rtName = context.Packer != null ? "Confuser.Runtime.Resource_Packer" : "Confuser.Runtime.Resource"; IEnumerable <IDnlibDef> members = Core.Helpers.InjectHelper.Inject(rt.GetRuntimeType(rtName), context.CurrentModule.GlobalType, context.CurrentModule); foreach (IDnlibDef member in members) { if (member.Name == "Initialize") { moduleCtx.InitMethod = (MethodDef)member; } moduleCtx.Name.MarkHelper(member, moduleCtx.Marker, (Protection)Parent); } var dataType = new TypeDefUser("", moduleCtx.Name.RandomName(), context.CurrentModule.CorLibTypes.GetTypeRef("System", "ValueType")); dataType.Layout = TypeAttributes.ExplicitLayout; dataType.Visibility = TypeAttributes.NestedPrivate; dataType.IsSealed = true; dataType.ClassLayout = new ClassLayoutUser(1, 0); moduleCtx.DataType = dataType; context.CurrentModule.GlobalType.NestedTypes.Add(dataType); moduleCtx.Name.MarkHelper(dataType, moduleCtx.Marker, (Protection)Parent); moduleCtx.DataField = new FieldDefUser(moduleCtx.Name.RandomName(), new FieldSig(dataType.ToTypeSig())) { IsStatic = true, HasFieldRVA = true, InitialValue = new byte[0], Access = FieldAttributes.CompilerControlled }; context.CurrentModule.GlobalType.Fields.Add(moduleCtx.DataField); moduleCtx.Name.MarkHelper(moduleCtx.DataField, moduleCtx.Marker, (Protection)Parent); }
// Token: 0x06000198 RID: 408 RVA: 0x00061B3C File Offset: 0x0005FD3C public MDPhase(REContext ctx) { this.ctx = ctx; }
public MDPhase(REContext ctx) { this.ctx = ctx; }
void MutateInitializer(REContext moduleCtx, MethodDef decomp) { moduleCtx.InitMethod.Body.SimplifyMacros(moduleCtx.InitMethod.Parameters); List<Instruction> instrs = moduleCtx.InitMethod.Body.Instructions.ToList(); for (int i = 0; i < instrs.Count; i++) { Instruction instr = instrs[i]; var method = instr.Operand as IMethod; if (instr.OpCode == OpCodes.Call) { if (method.DeclaringType.Name == "Mutation" && method.Name == "Crypt") { Instruction ldBlock = instrs[i - 2]; Instruction ldKey = instrs[i - 1]; Debug.Assert(ldBlock.OpCode == OpCodes.Ldloc && ldKey.OpCode == OpCodes.Ldloc); instrs.RemoveAt(i); instrs.RemoveAt(i - 1); instrs.RemoveAt(i - 2); instrs.InsertRange(i - 2, moduleCtx.ModeHandler.EmitDecrypt(moduleCtx.InitMethod, moduleCtx, (Local)ldBlock.Operand, (Local)ldKey.Operand)); } else if (method.DeclaringType.Name == "Lzma" && method.Name == "Decompress") { instr.Operand = decomp; } } } moduleCtx.InitMethod.Body.Instructions.Clear(); foreach (Instruction instr in instrs) moduleCtx.InitMethod.Body.Instructions.Add(instr); MutationHelper.ReplacePlaceholder(moduleCtx.InitMethod, arg => { var repl = new List<Instruction>(); repl.AddRange(arg); repl.Add(Instruction.Create(OpCodes.Dup)); repl.Add(Instruction.Create(OpCodes.Ldtoken, moduleCtx.DataField)); repl.Add(Instruction.Create(OpCodes.Call, moduleCtx.Module.Import( typeof(RuntimeHelpers).GetMethod("InitializeArray")))); return repl.ToArray(); }); moduleCtx.Context.Registry.GetService<IConstantService>().ExcludeMethod(moduleCtx.Context, moduleCtx.InitMethod); }
// Token: 0x06000196 RID: 406 RVA: 0x000616C4 File Offset: 0x0005F8C4 private void InjectHelpers(ConfuserContext context, ICompressionService compression, IRuntimeService rt, REContext moduleCtx) { string fullName = (context.Packer != null) ? "Confuser.Runtime.Resource_Packer" : "Confuser.Runtime.Resource"; IEnumerable <IDnlibDef> enumerable = InjectHelper.Inject(rt.GetRuntimeType(fullName), context.CurrentModule.GlobalType, context.CurrentModule); foreach (IDnlibDef dnlibDef in enumerable) { bool flag = dnlibDef.Name == "Initialize"; if (flag) { moduleCtx.InitMethod = (MethodDef)dnlibDef; } moduleCtx.Name.MarkHelper(dnlibDef, moduleCtx.Marker, (Protection)base.Parent); } TypeDefUser typeDefUser = new TypeDefUser("", moduleCtx.Name.RandomName(), context.CurrentModule.CorLibTypes.GetTypeRef("System", "ValueType")); typeDefUser.Layout = TypeAttributes.ExplicitLayout; typeDefUser.Visibility = TypeAttributes.NestedPrivate; typeDefUser.IsSealed = true; typeDefUser.ClassLayout = new ClassLayoutUser(1, 0u); moduleCtx.DataType = typeDefUser; context.CurrentModule.GlobalType.NestedTypes.Add(typeDefUser); moduleCtx.Name.MarkHelper(typeDefUser, moduleCtx.Marker, (Protection)base.Parent); moduleCtx.DataField = new FieldDefUser(moduleCtx.Name.RandomName(), new FieldSig(typeDefUser.ToTypeSig())) { IsStatic = true, HasFieldRVA = true, InitialValue = new byte[0], Access = FieldAttributes.PrivateScope }; context.CurrentModule.GlobalType.Fields.Add(moduleCtx.DataField); moduleCtx.Name.MarkHelper(moduleCtx.DataField, moduleCtx.Marker, (Protection)base.Parent); }