Beispiel #1
0
        public void Confuse(ConfuserParameter param)
        {
            try
            {
                this.param = param;
                param.Logger.StartPhase(1);
                param.Logger.Log("Started at " + DateTime.Now.ToShortTimeString() + ".");
                param.Logger.Log("Loading...");

                System.Reflection.StrongNameKeyPair sn;
                Initialize(out sn);

                List <byte[]>           pes  = new List <byte[]>();
                List <ModuleDefinition> mods = new List <ModuleDefinition>();
                for (int i = 0; i < assemblies.Count; i++)
                {
                    param.Logger.StartPhase(2);
                    param.Logger.Log(string.Format("Obfuscating assembly {0}...", assemblies[i].FullName));

                    IDictionary <IConfusion, NameValueCollection> globalParams = GetGlobalParams(assemblies[i]);
                    List <Phase> phases = new List <Phase>();
                    foreach (IConfusion cion in param.Confusions)
                    {
                        foreach (Phase phase in cion.Phases)
                        {
                            phases.Add(phase);
                        }
                    }

                    foreach (ModuleDefinition mod in assemblies[i].Modules)
                    {
                        if (sn != null && mod.Assembly != null)
                        {
                            if (mod.Assembly.MainModule == mod)
                            {
                                mod.Assembly.Name.PublicKey = sn.PublicKey;
                            }
                            mod.Attributes |= ModuleAttributes.StrongNameSigned;
                        }
                        else
                        {
                            if (mod.Assembly != null && mod.Assembly.MainModule == mod)
                            {
                                mod.Assembly.Name.PublicKey = null;
                            }
                            mod.Attributes &= ~ModuleAttributes.StrongNameSigned;
                        }
                        param.Logger.Log(string.Format("Obfuscating structure of module {0}...", mod.Name));

                        helpers.Clear();
                        //global cctor which used in many confusion
                        if (mod.GetType("<Module>").GetStaticConstructor() == null)
                        {
                            MethodDefinition cctor = new MethodDefinition(".cctor", MethodAttributes.Private | MethodAttributes.HideBySig |
                                                                          MethodAttributes.SpecialName | MethodAttributes.RTSpecialName |
                                                                          MethodAttributes.Static, mod.TypeSystem.Void);
                            cctor.Body = new MethodBody(cctor);
                            cctor.Body.GetILProcessor().Emit(OpCodes.Ret);
                            mod.GetType("<Module>").Methods.Add(cctor);
                        }
                        else
                        {
                            MethodDefinition cctor = mod.GetType("<Module>").GetStaticConstructor();
                            ((IAnnotationProvider)cctor).Annotations.Clear();
                        }
                        helpers.Add(mod.GetType("<Module>").GetStaticConstructor(), HelperAttribute.NoEncrypt);

                        ProcessStructuralPhases(mod, globalParams, phases);

                        param.Logger.StartPhase(3);

                        MemoryStream final = new MemoryStream();
                        ProcessMdPePhases(mod, globalParams, phases, final, new WriterParameters()
                        {
                            StrongNameKeyPair = (mod.Attributes & ModuleAttributes.StrongNameSigned) != 0 ? sn : null
                        });

                        pes.Add(final.ToArray());
                        mods.Add(mod);

                        param.Logger.Log("Module " + mod.Name + " Done.");
                    }
                }

                Finalize(mods.ToArray(), pes.ToArray());

                param.Logger.Finish("Ended at " + DateTime.Now.ToShortTimeString() + ".");
            }
            catch (Exception ex)
            {
                param.Logger.Fatal(ex);
            }
            finally
            {
                GlobalAssemblyResolver.Instance.AssemblyCache.Clear();
                GC.Collect();
            }
        }
Beispiel #2
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));
        }