コード例 #1
0
ファイル: Packer.cs プロジェクト: n017/Confuser
        protected string[] ProtectStub(AssemblyDefinition asm)
        {
            string tmp = Path.GetTempPath() + "\\" + Path.GetRandomFileName() + "\\";
            Directory.CreateDirectory(tmp);
            ModuleDefinition modDef = this.cr.settings.Single(_ => _.IsMain).Assembly.MainModule;
            asm.MainModule.TimeStamp = modDef.TimeStamp;
            byte[] mvid = new byte[0x10];
            Random.NextBytes(mvid);
            asm.MainModule.Mvid = new Guid(mvid);
            MetadataProcessor psr = new MetadataProcessor();
            Section oldRsrc = null;
            foreach (Section s in modDef.GetSections())
                if (s.Name == ".rsrc") { oldRsrc = s; break; }
            if (oldRsrc != null)
            {
                psr.ProcessImage += accessor =>
                {
                    Section sect = null;
                    foreach (Section s in accessor.Sections)
                        if (s.Name == ".rsrc") { sect = s; break; }
                    if (sect == null)
                    {
                        sect = new Section()
                        {
                            Name = ".rsrc",
                            Characteristics = 0x40000040
                        };
                        foreach (Section s in accessor.Sections)
                            if (s.Name == ".text") { accessor.Sections.Insert(accessor.Sections.IndexOf(s) + 1, sect); break; }
                    }
                    sect.VirtualSize = oldRsrc.VirtualSize;
                    sect.SizeOfRawData = oldRsrc.SizeOfRawData;
                    int idx = accessor.Sections.IndexOf(sect);
                    sect.VirtualAddress = accessor.Sections[idx - 1].VirtualAddress + ((accessor.Sections[idx - 1].VirtualSize + 0x2000U - 1) & ~(0x2000U - 1));
                    sect.PointerToRawData = accessor.Sections[idx - 1].PointerToRawData + accessor.Sections[idx - 1].SizeOfRawData;
                    for (int i = idx + 1; i < accessor.Sections.Count; i++)
                    {
                        accessor.Sections[i].VirtualAddress = accessor.Sections[i - 1].VirtualAddress + ((accessor.Sections[i - 1].VirtualSize + 0x2000U - 1) & ~(0x2000U - 1));
                        accessor.Sections[i].PointerToRawData = accessor.Sections[i - 1].PointerToRawData + accessor.Sections[i - 1].SizeOfRawData;
                    }
                    ByteBuffer buff = new ByteBuffer(oldRsrc.Data);
                    PatchResourceDirectoryTable(buff, oldRsrc, sect);
                    sect.Data = buff.GetBuffer();
                };
            }
            psr.Process(asm.MainModule, tmp + Path.GetFileName(modDef.FullyQualifiedName), new WriterParameters()
            {
                StrongNameKeyPair = this.cr.sn,
                WriteSymbols = this.cr.param.Project.Debug
            });

            Confuser cr = new Confuser();

            ConfuserProject proj = new ConfuserProject();
            proj.Seed = Random.Next().ToString();
            proj.Debug = this.cr.param.Project.Debug;
            foreach (var i in this.cr.param.Project.Rules)
                proj.Rules.Add(i);
            proj.Add(new ProjectAssembly()
            {
                Path = tmp + Path.GetFileName(modDef.FullyQualifiedName)
            });
            proj.OutputPath = tmp;
            foreach (var i in this.cr.param.Project.Plugins) proj.Plugins.Add(i);
            proj.SNKeyPath = this.cr.param.Project.SNKeyPath;

            ConfuserParameter par = new ConfuserParameter();
            par.Project = proj;
            par.ProcessMetadata = PostProcessMetadata;
            par.ProcessImage = PostProcessImage;
            cr.Confuse(par);

            return Directory.GetFiles(tmp);
        }