private void PackModules(ConfuserContext context, CompressorContext compCtx, ModuleDef stubModule, ICompressionService comp, RandomGenerator random) { int maxLen = 0; var modules = new Dictionary <string, byte[]>(); for (int i = 0; i < context.OutputModules.Count; i++) { if (i == compCtx.ModuleIndex) { continue; } string fullName = context.Modules[i].Assembly.FullName; modules.Add(fullName, context.OutputModules[i]); int strLen = Encoding.UTF8.GetByteCount(fullName); if (strLen > maxLen) { maxLen = strLen; } } byte[] key = random.NextBytes(4 + maxLen); key[0] = (byte)(compCtx.EntryPointToken >> 0); key[1] = (byte)(compCtx.EntryPointToken >> 8); key[2] = (byte)(compCtx.EntryPointToken >> 16); key[3] = (byte)(compCtx.EntryPointToken >> 24); for (int i = 4; i < key.Length; i++) // no zero bytes { key[i] |= 1; } compCtx.KeySig = key; int moduleIndex = 0; foreach (var entry in modules) { byte[] name = Encoding.UTF8.GetBytes(entry.Key); for (int i = 0; i < name.Length; i++) { name[i] *= key[i + 4]; } uint state = 0x6fff61; foreach (byte chr in name) { state = state * 0x5e3f1f + chr; } byte[] encrypted = compCtx.Encrypt(comp, entry.Value, state, progress => { progress = (progress + moduleIndex) / modules.Count; context.Logger.Progress((int)(progress * 10000), 10000); }); context.CheckCancellation(); var resource = new EmbeddedResource(Convert.ToBase64String(name), encrypted, ManifestResourceAttributes.Private); stubModule.Resources.Add(resource); moduleIndex++; } context.Logger.EndProgress(); }
void InjectStub(ConfuserContext context, CompressorContext compCtx, ProtectionParameters parameters, ModuleDef stubModule) { var rt = context.Registry.GetService <IRuntimeService>(); RandomGenerator random = context.Registry.GetService <IRandomService>().GetRandomGenerator(Id); var comp = context.Registry.GetService <ICompressionService>(); var rtType = rt.GetRuntimeType(compCtx.CompatMode ? "Confuser.Runtime.CompressorCompat" : "Confuser.Runtime.Compressor"); IEnumerable <IDnlibDef> defs = InjectHelper.Inject(rtType, stubModule.GlobalType, stubModule); switch (parameters.GetParameter(context, context.CurrentModule, "key", Mode.Dynamic)) { case Mode.Normal: compCtx.Deriver = new NormalDeriver(); break; case Mode.Dynamic: compCtx.Deriver = new DynamicDeriver(); break; default: throw new UnreachableException(); } compCtx.Deriver.Init(context, random); context.Logger.Debug("Encrypting modules..."); // Main MethodDef entryPoint = defs.OfType <MethodDef>().Single(method => method.Name == "Main"); stubModule.EntryPoint = entryPoint; if (compCtx.EntryPoint.HasAttribute("System.STAThreadAttribute")) { var attrType = stubModule.CorLibTypes.GetTypeRef("System", "STAThreadAttribute"); var ctorSig = MethodSig.CreateInstance(stubModule.CorLibTypes.Void); entryPoint.CustomAttributes.Add(new CustomAttribute( new MemberRefUser(stubModule, ".ctor", ctorSig, attrType))); } else if (compCtx.EntryPoint.HasAttribute("System.MTAThreadAttribute")) { var attrType = stubModule.CorLibTypes.GetTypeRef("System", "MTAThreadAttribute"); var ctorSig = MethodSig.CreateInstance(stubModule.CorLibTypes.Void); entryPoint.CustomAttributes.Add(new CustomAttribute( new MemberRefUser(stubModule, ".ctor", ctorSig, attrType))); } uint seed = random.NextUInt32(); compCtx.OriginModule = context.OutputModules[compCtx.ModuleIndex]; byte[] encryptedModule = compCtx.Encrypt(comp, compCtx.OriginModule, seed, progress => context.Logger.Progress((int)(progress * 10000), 10000)); context.Logger.EndProgress(); context.CheckCancellation(); compCtx.EncryptedModule = encryptedModule; MutationHelper.InjectKeys(entryPoint, new[] { 0, 1 }, new[] { encryptedModule.Length >> 2, (int)seed }); InjectData(stubModule, entryPoint, encryptedModule); // Decrypt MethodDef decrypter = defs.OfType <MethodDef>().Single(method => method.Name == "Decrypt"); decrypter.Body.SimplifyMacros(decrypter.Parameters); List <Instruction> instrs = decrypter.Body.Instructions.ToList(); for (int i = 0; i < instrs.Count; i++) { Instruction instr = instrs[i]; if (instr.OpCode == OpCodes.Call) { var method = (IMethod)instr.Operand; if (method.DeclaringType.Name == "Mutation" && method.Name == "Crypt") { Instruction ldDst = instrs[i - 2]; Instruction ldSrc = instrs[i - 1]; Debug.Assert(ldDst.OpCode == OpCodes.Ldloc && ldSrc.OpCode == OpCodes.Ldloc); instrs.RemoveAt(i); instrs.RemoveAt(i - 1); instrs.RemoveAt(i - 2); instrs.InsertRange(i - 2, compCtx.Deriver.EmitDerivation(decrypter, context, (Local)ldDst.Operand, (Local)ldSrc.Operand)); } else if (method.DeclaringType.Name == "Lzma" && method.Name == "Decompress") { MethodDef decomp = comp.GetRuntimeDecompressor(stubModule, member => { }); instr.Operand = decomp; } } } decrypter.Body.Instructions.Clear(); foreach (Instruction instr in instrs) { decrypter.Body.Instructions.Add(instr); } // Pack modules PackModules(context, compCtx, stubModule, comp, random); }