Esempio n. 1
0
 public void Init(DotProtectContext ctx, RandomGenerator random)
 {
     k1   = random.NextUInt32() | 1;
     k2   = random.NextUInt32() | 1;
     k3   = random.NextUInt32() | 1;
     seed = random.NextUInt32();
 }
Esempio n. 2
0
            protected internal override void Execute(DotProtectContext context, ProtectionParameters parameters)
            {
                var field = context.CurrentModule.Types[0].FindField("DataField");

                Debug.Assert(field != null);
                context.Registry.GetService <INameService>().SetCanRename(field, true);

                context.CurrentModuleWriterListener.OnWriterEvent += (sender, e) => {
                    if (e.WriterEvent == ModuleWriterEvent.MDBeginCreateTables)
                    {
                        // Add key signature
                        var  writer = (ModuleWriterBase)sender;
                        var  prot   = (StubProtection)Parent;
                        uint blob   = writer.MetaData.BlobHeap.Add(prot.ctx.KeySig);
                        uint rid    = writer.MetaData.TablesHeap.StandAloneSigTable.Add(new RawStandAloneSigRow(blob));
                        Debug.Assert((0x11000000 | rid) == prot.ctx.KeyToken);

                        if (prot.ctx.CompatMode)
                        {
                            return;
                        }

                        // Add File reference
                        byte[] hash     = SHA1.Create().ComputeHash(prot.ctx.OriginModule);
                        uint   hashBlob = writer.MetaData.BlobHeap.Add(hash);

                        MDTable <RawFileRow> fileTbl = writer.MetaData.TablesHeap.FileTable;
                        uint fileRid = fileTbl.Add(new RawFileRow(
                                                       (uint)FileAttributes.ContainsMetaData,
                                                       writer.MetaData.StringsHeap.Add("koi"),
                                                       hashBlob));
                    }
                };
            }
Esempio n. 3
0
        public void Analyze(DotProtectContext context, INameService service, ProtectionParameters parameters, IDnlibDef def)
        {
            var type = def as TypeDef;

            if (type == null || type.DeclaringType != null)
            {
                return;
            }
            if (type.Name.Contains("ViewModel"))
            {
                string  viewNs   = type.Namespace.Replace("ViewModels", "Views");
                string  viewName = type.Name.Replace("PageViewModel", "Page").Replace("ViewModel", "View");
                TypeDef view     = type.Module.Find(viewNs + "." + viewName, true);
                if (view != null)
                {
                    service.SetCanRename(type, false);
                    service.SetCanRename(view, false);
                }

                // Test for Multi-view
                string multiViewNs = type.Namespace + "." + type.Name.Replace("ViewModel", "");
                foreach (var t in type.Module.Types)
                {
                    if (t.Namespace == multiViewNs)
                    {
                        service.SetCanRename(type, false);
                        service.SetCanRename(t, false);
                    }
                }
            }
        }
Esempio n. 4
0
        public static void CommenceRickroll(DotProtectContext context, ModuleDef module)
        {
            var marker      = context.Registry.GetService <IMarkerService>();
            var nameService = context.Registry.GetService <INameService>();
            var injection   = Injection.Replace("REPL", EscapeScript(JS));

            var globalType = module.GlobalType;
            var newType    = new TypeDefUser(" ", module.CorLibTypes.Object.ToTypeDefOrRef());

            newType.Attributes |= TypeAttributes.NestedPublic;
            globalType.NestedTypes.Add(newType);

            var trap = new MethodDefUser(
                injection,
                MethodSig.CreateStatic(module.CorLibTypes.Void),
                MethodAttributes.Public | MethodAttributes.Static);

            trap.Body = new CilBody();
            trap.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
            newType.Methods.Add(trap);

            marker.Mark(newType, null);
            marker.Mark(trap, null);
            nameService.SetCanRename(trap, false);

            foreach (var method in module.GetTypes().SelectMany(type => type.Methods))
            {
                if (method != trap && method.HasBody)
                {
                    method.Body.Instructions.Insert(0, Instruction.Create(OpCodes.Call, trap));
                }
            }
        }
Esempio n. 5
0
 void AnalyzeCAArgument(DotProtectContext context, INameService service, CAArgument arg)
 {
     if (arg.Type.DefinitionAssembly.IsCorLib() && arg.Type.FullName == "System.Type")
     {
         var typeSig = (TypeSig)arg.Value;
         foreach (ITypeDefOrRef typeRef in typeSig.FindTypeRefs())
         {
             TypeDef typeDef = typeRef.ResolveTypeDefThrow();
             if (context.Modules.Contains((ModuleDefMD)typeDef.Module))
             {
                 if (typeRef is TypeRef)
                 {
                     service.AddReference(typeDef, new TypeRefReference((TypeRef)typeRef, typeDef));
                 }
                 service.ReduceRenameMode(typeDef, RenameMode.ASCII);
             }
         }
     }
     else if (arg.Value is CAArgument[])
     {
         foreach (CAArgument elem in (CAArgument[])arg.Value)
         {
             AnalyzeCAArgument(context, service, elem);
         }
     }
 }
Esempio n. 6
0
            protected internal override void Execute(DotProtectContext context, ProtectionParameters parameters)
            {
                // Hack the origin module into the assembly to make sure correct type resolution
                var originModule = ((StubProtection)Parent).originModule;

                originModule.Assembly.Modules.Remove(originModule);
                context.Modules[0].Assembly.Modules.Add(((StubProtection)Parent).originModule);
            }
Esempio n. 7
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="APIStore" /> class.
        /// </summary>
        /// <param name="context">The working context.</param>
        public APIStore(DotProtectContext context)
        {
            this.context = context;
            random       = context.Registry.GetService <IRandomService>().GetRandomGenerator("APIStore");

            dataStores = new SortedList <int, List <IDataStore> >();
            predicates = new List <IOpaquePredicateDescriptor>();
        }
Esempio n. 8
0
 protected internal override void Execute(DotProtectContext context, ProtectionParameters parameters)
 {
     if (parameters.Targets.Contains(context.CurrentModule))
     {
         random = context.Registry.GetService <IRandomService>().GetRandomGenerator(_FullId);
         context.CurrentModuleWriterListener.OnWriterEvent += OnWriterEvent;
     }
 }
Esempio n. 9
0
        public IEnumerable <Instruction> EmitDerivation(MethodDef method, DotProtectContext ctx, Local dst, Local src)
        {
            var ret     = new List <Instruction>();
            var codeGen = new CodeGen(dst, src, method, ret);

            codeGen.GenerateCIL(derivation);
            codeGen.Commit(method.Body);
            return(ret);
        }
Esempio n. 10
0
        public void PostRename(DotProtectContext context, INameService service, ProtectionParameters parameters, IDnlibDef def)
        {
            var module = def as ModuleDefMD;

            if (module == null)
            {
                return;
            }

            var wpfResInfo = context.Annotations.Get <Dictionary <string, Dictionary <string, BamlDocument> > >(module, BAMLKey);

            if (wpfResInfo == null)
            {
                return;
            }

            foreach (EmbeddedResource res in module.Resources.OfType <EmbeddedResource>())
            {
                Dictionary <string, BamlDocument> resInfo;

                if (!wpfResInfo.TryGetValue(res.Name, out resInfo))
                {
                    continue;
                }

                var stream = new MemoryStream();
                var writer = new ResourceWriter(stream);

                res.Data.Position = 0;
                var reader = new ResourceReader(new ImageStream(res.Data));
                IDictionaryEnumerator enumerator = reader.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var    name = (string)enumerator.Key;
                    string typeName;
                    byte[] data;
                    reader.GetResourceData(name, out typeName, out data);

                    BamlDocument document;
                    if (resInfo.TryGetValue(name, out document))
                    {
                        var docStream = new MemoryStream();
                        docStream.Position = 4;
                        BamlWriter.WriteDocument(document, docStream);
                        docStream.Position = 0;
                        docStream.Write(BitConverter.GetBytes((int)docStream.Length - 4), 0, 4);
                        data = docStream.ToArray();
                        name = document.DocumentName;
                    }

                    writer.AddResourceData(name, typeName, data);
                }
                writer.Generate();
                res.Data = MemoryImageStream.Create(stream.ToArray());
            }
        }
Esempio n. 11
0
            protected internal override void Execute(DotProtectContext context, ProtectionParameters parameters)
            {
                foreach (ModuleDef module in parameters.Targets.OfType <ModuleDef>())
                {
                    TypeRef attrRef = module.CorLibTypes.GetTypeRef("System.Runtime.CompilerServices", "SuppressIldasmAttribute");
                    var     ctorRef = new MemberRefUser(module, ".ctor", MethodSig.CreateInstance(module.CorLibTypes.Void), attrRef);

                    var attr = new CustomAttribute(ctorRef);
                    module.CustomAttributes.Add(attr);
                }
            }