示例#1
0
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            if (context.Packer == null)
                return;

            if (context.Annotations.Get<CompressorContext>(context, Compressor.ContextKey) != null)
                return;

            var mainModule = parameters.GetParameter<string>(context, null, "main");
            if (context.CurrentModule.Name == mainModule) {
                var ctx = new CompressorContext {
                    ModuleIndex = context.CurrentModuleIndex,
                    Assembly = context.CurrentModule.Assembly
                };
                context.Annotations.Set(context, Compressor.ContextKey, ctx);

                ctx.ModuleName = context.CurrentModule.Name;
                context.CurrentModule.Name = "koi";

                ctx.EntryPoint = context.CurrentModule.EntryPoint;
                context.CurrentModule.EntryPoint = null;

                ctx.Kind = context.CurrentModule.Kind;
                context.CurrentModule.Kind = ModuleKind.NetModule;

                context.CurrentModule.Assembly.Modules.Remove(context.CurrentModule);

                context.CurrentModuleWriterListener.OnWriterEvent += new ResourceRecorder(ctx, context.CurrentModule).OnWriterEvent;
            }
        }
示例#2
0
        protected override void Execute(ConfuserContext context, ProtectionParameters parameters)
        {
            if (context.Packer == null)
                return;

            bool isExe = context.CurrentModule.Kind == ModuleKind.Windows ||
                         context.CurrentModule.Kind == ModuleKind.Console;

            if (context.Annotations.Get<CompressorContext>(context, Compressor.ContextKey) != null) {
                if (isExe) {
                    context.Logger.Error("Too many executable modules!");
                    throw new ConfuserException(null);
                }
                return;
            }

            if (isExe) {
                var ctx = new CompressorContext {
                    ModuleIndex = context.CurrentModuleIndex,
                    Assembly = context.CurrentModule.Assembly
                };
                context.Annotations.Set(context, Compressor.ContextKey, ctx);

                ctx.ModuleName = context.CurrentModule.Name;
                context.CurrentModule.Name = "koi";

                ctx.EntryPoint = context.CurrentModule.EntryPoint;
                context.CurrentModule.EntryPoint = null;

                ctx.Kind = context.CurrentModule.Kind;
                context.CurrentModule.Kind = ModuleKind.NetModule;

                context.CurrentModuleWriterListener.OnWriterEvent += new ResourceRecorder(ctx, context.CurrentModule).OnWriterEvent;
            }
        }
示例#3
0
 internal StubProtection(CompressorContext ctx)
 {
     this.ctx = ctx;
 }
示例#4
0
 public KeyInjector(CompressorContext ctx)
 {
     this.ctx = ctx;
 }
示例#5
0
        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 name = context.Modules[i].Assembly.Name.ToUpperInvariant();
                modules.Add(name, context.OutputModules[i]);

                int strLen = Encoding.UTF8.GetByteCount(name);
                if (strLen > maxLen)
                    maxLen = strLen;
            }
            foreach (var extModule in context.ExternalModules) {
                var name = GetName(extModule).ToUpperInvariant();
                modules.Add(name, extModule);

                int strLen = Encoding.UTF8.GetByteCount(name);
                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();
        }
示例#6
0
        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>();
            IEnumerable<IDnlibDef> defs = InjectHelper.Inject(rt.GetRuntimeType("Confuser.Runtime.Compressor"), stubModule.GlobalType, stubModule);

            switch (parameters.GetParameter(context, context.CurrentModule, "key", Mode.Normal)) {
                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);
        }
示例#7
0
 public ResourceRecorder(CompressorContext ctx, ModuleDef module)
 {
     this.ctx = ctx;
     targetModule = module;
 }
 internal StubProtection(CompressorContext ctx, ModuleDef originModule)
 {
     this.ctx = ctx;
     this.originModule = originModule;
 }
示例#9
0
 internal StubProtection(CompressorContext ctx, ModuleDef originModule)
 {
     this.ctx          = ctx;
     this.originModule = originModule;
 }
示例#10
0
 public ResourceRecorder(CompressorContext ctx, ModuleDef module)
 {
     this.ctx     = ctx;
     targetModule = module;
 }
示例#11
0
 internal StubProtection(CompressorContext ctx)
 {
     this.ctx = ctx;
 }