protected override void Execute(ConfuserContext 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));
                    }
                };
            }
Beispiel #2
0
            public void OnWriterEvent(ModuleWriterBase writer, ModuleWriterEvent evt)
            {
                if (evt == ModuleWriterEvent.MDBeginCreateTables)
                {
                    // Add key signature
                    uint sigBlob = writer.MetaData.BlobHeap.Add(ctx.KeySig);
                    uint sigRid  = writer.MetaData.TablesHeap.StandAloneSigTable.Add(new RawStandAloneSigRow(sigBlob));
                    Debug.Assert(sigRid == 1);
                    uint sigToken = 0x11000000 | sigRid;
                    ctx.KeyToken = sigToken;
                    MutationHelper.InjectKey(writer.Module.EntryPoint, 2, (int)sigToken);
                }
                else if (evt == ModuleWriterEvent.MDBeginAddResources)
                {
                    // Compute hash
                    byte[] hash     = SHA1.Create().ComputeHash(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));
                    uint impl = CodedToken.Implementation.Encode(new MDToken(Table.File, fileRid));

                    // Add resources
                    MDTable <RawManifestResourceRow> resTbl = writer.MetaData.TablesHeap.ManifestResourceTable;
                    foreach (var resource in ctx.ManifestResources)
                    {
                        resTbl.Add(new RawManifestResourceRow(resource.Item1, resource.Item2, writer.MetaData.StringsHeap.Add(resource.Item3), impl));
                    }
                }
            }
            void Randomize <T>(MDTable <T> table) where T : IRawRow
            {
                List <T> rows = table.ToList();

                random.Shuffle(rows);
                table.Reset();
                foreach (T row in rows)
                {
                    table.Add(row);
                }
            }
Beispiel #4
0
            private void Randomize <T>(MDTable <T> table) where T : IRawRow
            {
                List <T> list = table.ToList <T>();

                this.random.Shuffle <T>(list);
                table.Reset();
                foreach (T current in list)
                {
                    table.Add(current);
                }
            }
            public void OnWriterEvent(ModuleWriterBase writer, ModuleWriterEvent evt)
            {
                if (evt == ModuleWriterEvent.MDBeginCreateTables)
                {
                    // Add key signature
                    uint sigBlob = writer.Metadata.BlobHeap.Add(ctx.KeySig);
                    uint sigRid  = writer.Metadata.TablesHeap.StandAloneSigTable.Add(new RawStandAloneSigRow(sigBlob));
                    Debug.Assert(sigRid == 1);
                    uint sigToken = 0x11000000 | sigRid;
                    ctx.KeyToken = sigToken;
                    MutationHelper.InjectKey(writer.Module.EntryPoint, 2, (int)sigToken);
                }
                else if (evt == ModuleWriterEvent.MDBeginAddResources && !ctx.CompatMode)
                {
                    // Compute hash
                    byte[] hash     = SHA1.Create().ComputeHash(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));
                    uint impl = CodedToken.Implementation.Encode(new MDToken(Table.File, fileRid));

                    // Add resources
                    MDTable <RawManifestResourceRow> resTbl = writer.Metadata.TablesHeap.ManifestResourceTable;
                    foreach (var resource in ctx.ManifestResources)
                    {
                        resTbl.Add(new RawManifestResourceRow(resource.Offset, resource.Flags, writer.Metadata.StringsHeap.Add(resource.Value), impl));
                    }

                    // Add exported types
                    var exTbl = writer.Metadata.TablesHeap.ExportedTypeTable;
                    foreach (var type in ctx.OriginModuleDef.GetTypes())
                    {
                        if (!type.IsVisibleOutside())
                        {
                            continue;
                        }
                        exTbl.Add(new RawExportedTypeRow((uint)type.Attributes, 0,
                                                         writer.Metadata.StringsHeap.Add(type.Name),
                                                         writer.Metadata.StringsHeap.Add(type.Namespace), impl));
                    }
                }
            }