Example #1
0
        private List <PackageInfo> ObtainAndFillMetadata(List <PackageInfo> infos)
        {
            foreach (var packageInfo in infos)
            {
                foreach (var instPair in packageInfo.VersionedInstances)
                {
                    foreach (var inst in instPair.Value)
                    {
                        var addr = new Uri(RepositoryAddress);

                        var address = RepositoryAddress + (RepositoryAddress.EndsWith("/")?"":"/") + packageInfo.PackageName + "/" + instPair.Key + "/" +
                                      (inst.VersionName == null ? "trunk" : "tags/" + inst.VersionName) +
                                      "/" + MetadataFileName;

                        string filePath = null;
                        Common.Utility.ExceptionablePlaceWrapper(() =>
                        {
                            filePath = Utility.DownloadFileFromSVN(address);
                        }, string.Format("Attempt to get metadata by Address: {0} failed", address), "", false);

                        if (filePath == null)
                        {
                            continue;
                        }

                        MetadataProcessor.Process(filePath, inst);
                    }
                }
            }
            return(infos);
        }
 private void ProcessMetaData(ResourceSessionDirector resourceDirector)
 {
     try
     {
         m_metadataProcessor.Process(resourceDirector);
     }
     catch (Exception e)
     {
         if (m_log.IsErrorEnabled)
         {
             m_log.Error("Error processing XMD metadata", e);
         }
         throw;
     }
 }
Example #3
0
        void ProcessMdPePhases(ModuleDefinition mod, IDictionary <IConfusion, NameValueCollection> globalParams, IEnumerable <Phase> phases, Stream stream, WriterParameters parameters)
        {
            MetadataProcessor psr    = new MetadataProcessor();
            double            total1 = (from i in phases where (i is MetadataPhase) select i).Count();
            int now1 = 1;

            psr.BeforeBuildModule += new MetadataProcessor.MetadataProcess(delegate(MetadataProcessor.MetadataAccessor accessor)
            {
                foreach (MetadataPhase i in from i in phases where (i is MetadataPhase) && i.PhaseID == 1 orderby i.Priority ascending select i)
                {
                    if (GetTargets(mod, i.Confusion).Count == 0)
                    {
                        continue;
                    }
                    param.Logger.Log("Executing " + i.Confusion.Name + " Phase 1...");
                    i.Confuser = this;
                    NameValueCollection globalParam;
                    if (globalParams.ContainsKey(i.Confusion))
                    {
                        globalParam = globalParams[i.Confusion];
                    }
                    else
                    {
                        globalParam = new NameValueCollection();
                    }
                    i.Process(globalParam, accessor);
                    param.Logger.Progress(now1 / total1); now1++;
                }
            });
            psr.BeforeWriteTables += new MetadataProcessor.MetadataProcess(delegate(MetadataProcessor.MetadataAccessor accessor)
            {
                foreach (MetadataPhase i in from i in phases where (i is MetadataPhase) && i.PhaseID == 2 orderby i.Priority ascending select i)
                {
                    if (GetTargets(mod, i.Confusion).Count == 0)
                    {
                        continue;
                    }
                    param.Logger.Log("Executing " + i.Confusion.Name + " Phase 2...");
                    i.Confuser = this;
                    NameValueCollection globalParam;
                    if (globalParams.ContainsKey(i.Confusion))
                    {
                        globalParam = globalParams[i.Confusion];
                    }
                    else
                    {
                        globalParam = new NameValueCollection();
                    }
                    i.Process(globalParam, accessor);
                    param.Logger.Progress(now1 / total1); now1++;
                }
            });
            psr.AfterWriteTables += new MetadataProcessor.MetadataProcess(delegate(MetadataProcessor.MetadataAccessor accessor)
            {
                foreach (MetadataPhase i in from i in phases where (i is MetadataPhase) && i.PhaseID == 3 orderby i.Priority ascending select i)
                {
                    if (GetTargets(mod, i.Confusion).Count == 0)
                    {
                        continue;
                    }
                    param.Logger.Log("Executing " + i.Confusion.Name + " Phase 3...");
                    i.Confuser = this;
                    NameValueCollection globalParam;
                    if (globalParams.ContainsKey(i.Confusion))
                    {
                        globalParam = globalParams[i.Confusion];
                    }
                    else
                    {
                        globalParam = new NameValueCollection();
                    }
                    i.Process(globalParam, accessor);
                    param.Logger.Progress(now1 / total1); now1++;
                }
            });
            psr.ProcessImage += new MetadataProcessor.ImageProcess(delegate(MetadataProcessor.ImageAccessor accessor)
            {
                param.Logger.StartPhase(4);
                param.Logger.Log(string.Format("Obfuscating Image of module {0}...", mod.Name));
                ImagePhase[] imgPhases = (from i in phases where (i is ImagePhase)orderby(int) i.Priority + i.PhaseID * 10 ascending select(ImagePhase) i).ToArray();
                for (int i = 0; i < imgPhases.Length; i++)
                {
                    if (GetTargets(mod, imgPhases[i].Confusion).Count == 0)
                    {
                        continue;
                    }
                    param.Logger.Log("Executing " + imgPhases[i].Confusion.Name + " Phase " + imgPhases[i].PhaseID + "...");
                    imgPhases[i].Confuser = this;
                    NameValueCollection globalParam;
                    if (globalParams.ContainsKey(imgPhases[i].Confusion))
                    {
                        globalParam = globalParams[imgPhases[i].Confusion];
                    }
                    else
                    {
                        globalParam = new NameValueCollection();
                    }
                    imgPhases[i].Process(globalParam, accessor);
                    param.Logger.Progress((double)i / imgPhases.Length);
                }
            });
            psr.ProcessPe += new MetadataProcessor.PeProcess(delegate(Stream str)
            {
                param.Logger.Log(string.Format("Obfuscating PE of module {0}...", mod.Name));
                PePhase[] pePhases = (from i in phases where (i is PePhase)orderby(int) i.Priority + i.PhaseID * 10 ascending select(PePhase) i).ToArray();
                for (int i = 0; i < pePhases.Length; i++)
                {
                    if (GetTargets(mod, pePhases[i].Confusion).Count == 0)
                    {
                        continue;
                    }
                    param.Logger.Log("Executing " + pePhases[i].Confusion.Name + " Phase " + pePhases[i].PhaseID + "...");
                    pePhases[i].Confuser = this;
                    NameValueCollection globalParam;
                    if (globalParams.ContainsKey(pePhases[i].Confusion))
                    {
                        globalParam = globalParams[pePhases[i].Confusion];
                    }
                    else
                    {
                        globalParam = new NameValueCollection();
                    }
                    pePhases[i].Process(globalParam, str);
                    param.Logger.Progress((double)i / pePhases.Length);
                }
            });
            param.Logger.Log(string.Format("Obfuscating metadata of module {0}...", mod.Name));
            psr.Process(mod, stream, parameters);
        }
 private void ProcessMetaData(ResourceSessionDirector resourceDirector)
 {
     m_metadataProcessor.Process(resourceDirector);
 }
Example #5
0
        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));
        }
Example #6
0
        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);
        }
Example #7
0
        public string[] Pack(ConfuserParameter crParam, PackerParameter param)
        {
            AssemblyDefinition asm;

            PackCore(out asm, param);

            string tmp = Path.GetTempPath() + "\\" + Path.GetRandomFileName() + "\\";

            Directory.CreateDirectory(tmp);
            MetadataProcessor psr     = new MetadataProcessor();
            Section           oldRsrc = null;

            foreach (Section s in param.Modules[0].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.PointerToRawData;
                    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 + asm.MainModule.Name);

            Confuser          cr  = new Confuser();
            ConfuserParameter par = new ConfuserParameter();

            par.SourceAssembly = tmp + asm.MainModule.Name;
            par.ReferencesPath = tmp;
            tmp = Path.GetTempPath() + "\\" + Path.GetRandomFileName() + "\\";
            par.DestinationPath   = tmp;
            par.Confusions        = crParam.Confusions;
            par.DefaultPreset     = crParam.DefaultPreset;
            par.StrongNameKeyPath = crParam.StrongNameKeyPath;
            par.Marker            = new PackerMarker(param.Modules[0]);
            cr.Confuse(par);

            return(Directory.GetFiles(tmp));
        }