Beispiel #1
0
        public virtual AssemblyDefinition[] GetAssemblies(string src, Preset preset, Confuser cr, EventHandler <LogEventArgs> err)
        {
            this.cr = cr;
            Settings setting = new Settings();

            FillPreset(preset, setting.CurrentConfusions);
            Dictionary <string, AssemblyDefinition> ret = new Dictionary <string, AssemblyDefinition>();

            AssemblyDefinition main = AssemblyDefinition.ReadAssembly(src);

            MarkAssembly(main, setting);
            ret.Add(main.FullName, main);
            foreach (ModuleDefinition mod in main.Modules)
            {
                mod.FullLoad();
                foreach (AssemblyNameReference refer in mod.AssemblyReferences)
                {
                    if (!FrameworkAssemblies.Contains(string.Format("{0}/{1}", refer.Name, BitConverter.ToString(refer.PublicKeyToken ?? new byte[0]))) && !ret.ContainsKey(refer.FullName) && !IsExcludedDependency(main, refer))
                    {
                        AssemblyDefinition asm = GlobalAssemblyResolver.Instance.Resolve(refer);
                        if (asm == null)
                        {
                            err(this, new LogEventArgs(string.Format("WARNING : Cannot load dependency '" + refer.FullName + ".")));
                        }
                        else
                        {
                            MarkAssembly(asm, setting);
                            ret.Add(refer.FullName, asm);
                            GetAssemblies(asm, setting, ret, err);
                        }
                    }
                }
            }
            return(ret.Values.ToArray());
        }
Beispiel #2
0
            protected override void MarkModule(ModuleDefinition mod, IDictionary <IConfusion, NameValueCollection> current, Confuser cr)
            {
                IAnnotationProvider m = mod;

                m.Annotations.Clear();
                IAnnotationProvider src = (IAnnotationProvider)origin;

                foreach (object key in src.Annotations.Keys)
                {
                    m.Annotations.Add(key, src.Annotations[src]);
                }

                var dict = (IDictionary <IConfusion, NameValueCollection>)src.Annotations["ConfusionSets"];

                current.Clear();
                foreach (var i in dict)
                {
                    current.Add(i.Key, i.Value);
                }
            }
Beispiel #3
0
            protected override void MarkAssembly(AssemblyDefinition asm, IDictionary <IConfusion, NameValueCollection> current, Confuser cr)
            {
                IAnnotationProvider m = asm;

                m.Annotations.Clear();
                IAnnotationProvider src = (IAnnotationProvider)origin.Assembly;

                current.Clear();
                foreach (var i in (IDictionary <IConfusion, NameValueCollection>)src.Annotations["ConfusionSets"])
                {
                    current.Add(i.Key, i.Value);
                }
                foreach (object key in src.Annotations.Keys)
                {
                    if (key.ToString() == "Packer" || key.ToString() == "PackerParams")
                    {
                        continue;
                    }
                    m.Annotations[key] = src.Annotations[key];
                }
            }
Beispiel #4
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));
        }
Beispiel #5
0
 protected override void MarkAssembly(AssemblyDefinition asm, IDictionary <IConfusion, NameValueCollection> current, Confuser cr)
 {
     current.Clear();
     foreach (var i in (IDictionary <IConfusion, NameValueCollection>)((IAnnotationProvider)origin).Annotations["ConfusionSets"])
     {
         if (i.Key != exclude)
         {
             current.Add(i);
         }
     }
 }
Beispiel #6
0
 protected virtual void MarkMember(IMemberDefinition mem, IDictionary <IConfusion, NameValueCollection> current, Confuser cr)
 {
 }
Beispiel #7
0
 protected virtual void MarkType(TypeDefinition type, IDictionary <IConfusion, NameValueCollection> current, Confuser cr)
 {
 }
Beispiel #8
0
 protected virtual void MarkModule(ModuleDefinition mod, IDictionary <IConfusion, NameValueCollection> current, Confuser cr)
 {
 }
Beispiel #9
0
 protected virtual void MarkAssembly(AssemblyDefinition asm, IDictionary <IConfusion, NameValueCollection> current, Confuser cr)
 {
 }