void Begin() { var parameter = new ConfuserParameter(); parameter.Project = host.Project.ToCrProj(); parameter.Logger.BeginAssembly += Logger_BeginAssembly; parameter.Logger.EndAssembly += Logger_EndAssembly; parameter.Logger.Phase += Logger_Phase; parameter.Logger.Log += Logger_Log; parameter.Logger.Progress += Logger_Progress; parameter.Logger.Fault += Logger_Fault; parameter.Logger.End += Logger_End; cr = new Confuser.Core.Confuser(); thread = cr.ConfuseAsync(parameter); host.EnabledNavigation = false; btn.IsEnabled = true; p = 0; Action check = null; check = new Action(() => { progress.Value = p; if (p != -1) Dispatcher.BeginInvoke(check, System.Windows.Threading.DispatcherPriority.Background); }); check(); }
static int Main(string[] args) { ConsoleColor color = System.Console.ForegroundColor; System.Console.ForegroundColor = ConsoleColor.White; WriteLine("Confuser Version v" + typeof(Core.Confuser).Assembly.GetName().Version); WriteLine(); #if DEBUG for (int i = 0; i < 3; i++) { System.Console.Write('.'); System.Threading.Thread.Sleep(1000); } WriteLine(); #endif try { if (args.Length == 0 || args[0] == "-help") { PrintUsage(); return(0); } ConfuserProject proj; int error = ParseCommandLine(args, out proj); if (error != 0) { return(error); } Core.Confuser cr = new Core.Confuser(); ConfuserParameter param = new ConfuserParameter(); param.Project = proj; ConsoleLogger.Initalize(param.Logger); WriteLine("Start working."); WriteLine(new string('*', 15)); cr.Confuse(param); return(ConsoleLogger.ReturnValue); } finally { System.Console.ForegroundColor = color; } }
static int Main(string[] args) { ConsoleColor color = System.Console.ForegroundColor; System.Console.ForegroundColor = ConsoleColor.White; WriteLine("Confuser Version v" + typeof(Core.Confuser).Assembly.GetName().Version); WriteLine(); #if DEBUG for (int i = 0; i < 3; i++) { System.Console.Write('.'); System.Threading.Thread.Sleep(1000); } WriteLine(); #endif try { if (args.Length == 0 || args[0] == "-help") { PrintUsage(); return 0; } ConfuserProject proj; int error = ParseCommandLine(args, out proj); if (error != 0) { return error; } Core.Confuser cr = new Core.Confuser(); ConfuserParameter param = new ConfuserParameter(); param.Project = proj; ConsoleLogger.Initalize(param.Logger); WriteLine("Start working."); WriteLine(new string('*', 15)); cr.Confuse(param); return ConsoleLogger.ReturnValue; } finally { System.Console.ForegroundColor = color; } }
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)); }
public abstract string[] Pack(ConfuserParameter crParam, PackerParameter param);
public abstract string[] Pack(ConfuserParameter crParam, PackerParameter param);
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); }
public override bool Execute() { Log.LogMessage(MessageImportance.Low, "Confuser Version v{0}\n", typeof(Core.Confuser).Assembly.GetName().Version); string crproj = Path.Combine( Path.GetDirectoryName(BuildEngine.ProjectFileOfTaskNode), CrProj); if (!File.Exists(crproj)) { Log.LogError("Confuser", "CR001", "Project", "", 0, 0, 0, 0, string.Format("Error: Crproj file '{0}' not exist!", crproj)); return false; } XmlDocument xmlDoc = new XmlDocument(); ConfuserProject proj = new ConfuserProject(); bool err = false; try { XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(ConfuserProject.Schema); settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += (sender, e) => { Log.LogError("Confuser", "CR002", "Project", crproj, e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.LineNumber, e.Exception.LinePosition, e.Message); err = true; }; var rdr = XmlReader.Create(crproj, settings); xmlDoc.Load(rdr); rdr.Close(); if (err) return false; proj.Load(xmlDoc); } catch (Exception ex) { Log.LogError("Confuser", "CR002", "Project", crproj, 0, 0, 0, 0, ex.Message); return false; } proj.BasePath = BasePath; Core.Confuser cr = new Core.Confuser(); ConfuserParameter param = new ConfuserParameter(); param.Project = proj; var logger = new MSBuildLogger(Log); logger.Initalize(param.Logger); Log.LogMessage(MessageImportance.Low, "Start working."); Log.LogMessage(MessageImportance.Low, "***************"); cr.Confuse(param); return logger.ReturnValue; }
public override bool Execute() { Log.LogMessage(MessageImportance.Low, "Confuser Version v{0}\n", typeof(Core.Confuser).Assembly.GetName().Version); string crproj = Path.Combine( Path.GetDirectoryName(BuildEngine.ProjectFileOfTaskNode), CrProj); if (!File.Exists(crproj)) { Log.LogError("Confuser", "CR001", "Project", "", 0, 0, 0, 0, string.Format("Error: Crproj file '{0}' not exist!", crproj)); return(false); } XmlDocument xmlDoc = new XmlDocument(); ConfuserProject proj = new ConfuserProject(); bool err = false; try { XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(ConfuserProject.Schema); settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += (sender, e) => { Log.LogError("Confuser", "CR002", "Project", crproj, e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.LineNumber, e.Exception.LinePosition, e.Message); err = true; }; var rdr = XmlReader.Create(crproj, settings); xmlDoc.Load(rdr); rdr.Close(); if (err) { return(false); } proj.Load(xmlDoc); } catch (Exception ex) { Log.LogError("Confuser", "CR002", "Project", crproj, 0, 0, 0, 0, ex.Message); return(false); } proj.BasePath = BasePath; Core.Confuser cr = new Core.Confuser(); ConfuserParameter param = new ConfuserParameter(); param.Project = proj; var logger = new MSBuildLogger(Log); logger.Initalize(param.Logger); Log.LogMessage(MessageImportance.Low, "Start working."); Log.LogMessage(MessageImportance.Low, "***************"); cr.Confuse(param); return(logger.ReturnValue); }
public override string[] Pack(ConfuserParameter crParam, PackerParameter param) { ModuleDefinition originMain = param.Assemblies.Single(_ => _.IsMain).Assembly.MainModule; int originIndex = Array.IndexOf(param.Modules, originMain); var asm = AssemblyDefinition.CreateAssembly(originMain.Assembly.Name, modName, new ModuleParameters() { Architecture = originMain.Architecture, Kind = oKind, Runtime = originMain.Runtime }); ModuleDefinition mod = asm.MainModule; mod.Attributes |= (originMain.Attributes & ModuleAttributes.Required32Bit); // added -- christallire - to prevent BadImageFormatException, Stub assembly need to set ModuleAttribute.Required32Bit if oringinMain has one. hash = new ByteBuffer(SHA1Managed.Create().ComputeHash(param.PEs[originIndex])); int key0 = Random.Next(0, 0xff); int key1 = Random.Next(0, 0xff); int key2 = Random.Next(0, 0xff); Database.AddEntry("Compressor", "Key0", key0); Database.AddEntry("Compressor", "Key1", key1); Database.AddEntry("Compressor", "Key2", key2); ulong e = 0x47; ulong p = (ulong)Random.Next(0x1000, 0x10000); while (!isPrime(p) || (p - 1) % e == 0) { p = (ulong)Random.Next(0x1000, 0x10000); } ulong q = (ulong)Random.Next(0x1000, 0x10000); while (!isPrime(q) || (q - 1) % e == 0) { q = (ulong)Random.Next(0x1000, 0x10000); } ulong n = p * q; ulong n_ = (p - 1) * (q - 1); ulong d = modInv(e, n_); Database.AddEntry("Compressor", "p", p); Database.AddEntry("Compressor", "q", q); Database.AddEntry("Compressor", "n", n); Database.AddEntry("Compressor", "d", d); EmbeddedResource res = new EmbeddedResource(ObfuscationHelper.GetRandomName(), ManifestResourceAttributes.Private, Encrypt(param.PEs[originIndex], key0)); mod.Resources.Add(res); for (int i = 1; i < param.Modules.Length; i++) { if (param.Modules[i].IsMain) { mod.Resources.Add(new EmbeddedResource(GetNewName(param.Modules[i].Assembly.Name.FullName, key2), ManifestResourceAttributes.Private, Encrypt(param.PEs[i], key0))); } else { mod.Resources.Add(new EmbeddedResource(GetNewName(param.Modules[i].Name, key2), ManifestResourceAttributes.Private, Encrypt(param.PEs[i], key1))); //TODO: Support for multi-module asssembly } } AssemblyDefinition ldrC = AssemblyDefinition.ReadAssembly(typeof(Iid).Assembly.Location); ldrC.MainModule.ReadSymbols(); TypeDefinition t = CecilHelper.Inject(mod, ldrC.MainModule.GetType("CompressShell")); Mutator mutator = new Mutator(); mutator.IntKeys = new int[] { key0, key1, key2 }; mutator.LongKeys = new long[] { (long)modPow(entryPoint, d, n), (long)n }; mutator.StringKeys = new string[] { res.Name }; mutator.Mutate(Random, t, mod); t.Namespace = ""; t.DeclaringType = null; t.IsNestedPrivate = false; t.IsNotPublic = true; mod.Types.Add(t); //MethodDefinition cctor = new MethodDefinition(".cctor", MethodAttributes.Private | MethodAttributes.HideBySig | // MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | // MethodAttributes.Static, mod.TypeSystem.Void); //mod.GetType("<Module>").Methods.Add(cctor); //MethodBody bdy = cctor.Body = new MethodBody(cctor); //ILProcessor psr = bdy.GetILProcessor(); //psr.Emit(OpCodes.Call, mod.Import(typeof(AppDomain).GetProperty("CurrentDomain").GetGetMethod())); //psr.Emit(OpCodes.Ldnull); //psr.Emit(OpCodes.Ldftn, t.Methods.FirstOrDefault(mtd => mtd.Name == "DecryptAsm")); //psr.Emit(OpCodes.Newobj, mod.Import(typeof(ResolveEventHandler).GetConstructor(new Type[] { typeof(object), typeof(IntPtr) }))); //psr.Emit(OpCodes.Callvirt, mod.Import(typeof(AppDomain).GetEvent("AssemblyResolve").GetAddMethod())); //psr.Emit(OpCodes.Ret); MethodDefinition main = t.Methods.FirstOrDefault(mtd => mtd.Name == "Main"); mod.EntryPoint = main; return(ProtectStub(asm)); }