From 8ab823ee704c572c79c9ac59dbcabdfe4b7e53d2 Mon Sep 17 00:00:00 2001 From: Nova <69091361+qzxtu@users.noreply.github.com> Date: Wed, 31 May 2023 12:04:19 -0500 Subject: [PATCH] Update Program.cs --- AntiDe4dot/Program.cs | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/AntiDe4dot/Program.cs b/AntiDe4dot/Program.cs index b7120bb..d786791 100644 --- a/AntiDe4dot/Program.cs +++ b/AntiDe4dot/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Security.Cryptography; @@ -7,20 +7,20 @@ namespace AntiDe4dot { - internal class Program + internal abstract class Program { - public static void RenameTypes(Context context, int numTypes) + private static void RenameTypes(Context context, int numTypes) { - using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) + using (var rng = RandomNumberGenerator.Create()) { - byte[] buffer = new byte[8]; - foreach (ModuleDef module in context.Assembly.Modules) + var buffer = new byte[8]; + foreach (var module in context.Assembly.Modules) { InterfaceImpl globalItem = new InterfaceImplUser(module.GlobalType); - for (int i = 0; i < numTypes; i++) + for (var i = 0; i < numTypes; i++) { rng.GetBytes(buffer); - string typeName = $"Type_{BitConverter.ToString(buffer).Replace("-", "").Substring(0, 16)}_Qzxtu"; + var typeName = $"Type_{BitConverter.ToString(buffer).Replace("-", "").Substring(0, 16)}_Qzxtu"; TypeDef newTypeDef = new TypeDefUser(string.Empty, typeName, module.CorLibTypes.GetTypeRef("System", "Attribute")); InterfaceImpl newInterfaceImpl = new InterfaceImplUser(newTypeDef); module.Types.Add(newTypeDef); @@ -32,15 +32,18 @@ public static void RenameTypes(Context context, int numTypes) if (assemblyCompanyAttr != null) { assemblyCompanyAttr.ConstructorArguments.Clear(); - assemblyCompanyAttr.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, "Qzxtu Anti-D4dot")); + assemblyCompanyAttr.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, + "Qzxtu Anti-D4dot")); } else { - var assemblyCompanyRef = module.CorLibTypes.GetTypeRef("System.Reflection", "AssemblyCompanyAttribute"); + var assemblyCompanyRef = + module.CorLibTypes.GetTypeRef("System.Reflection", "AssemblyCompanyAttribute"); var assemblyCompanyTypeDef = assemblyCompanyRef.ResolveTypeDef(); var assemblyCompanyCtor = assemblyCompanyTypeDef.FindConstructors().Single(); var assemblyCompanyAttrType = new CustomAttribute(assemblyCompanyCtor); - assemblyCompanyAttrType.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, "Qzxtu Anti-D4dot")); + assemblyCompanyAttrType.ConstructorArguments.Add(new CAArgument(module.CorLibTypes.String, + "Qzxtu Anti-D4dot")); module.Assembly.CustomAttributes.Add(assemblyCompanyAttrType); } } @@ -49,11 +52,11 @@ public static void RenameTypes(Context context, int numTypes) private static void Main(string[] args) { - string inputFilePath = args.FirstOrDefault(); + var inputFilePath = args.FirstOrDefault(); if (string.IsNullOrEmpty(inputFilePath)) { Console.WriteLine("Please enter the path to the file you want to protect:"); - inputFilePath = Console.ReadLine().Trim(); + inputFilePath = Console.ReadLine()?.Trim(); if (!File.Exists(inputFilePath)) { @@ -62,9 +65,9 @@ private static void Main(string[] args) } } - string outputFilePath = Path.Combine(Path.GetDirectoryName(inputFilePath), Path.GetFileNameWithoutExtension(inputFilePath) + "-Anti_D4dot.exe"); + var outputFilePath = Path.Combine(Path.GetDirectoryName(inputFilePath) ?? string.Empty, Path.GetFileNameWithoutExtension(inputFilePath) + "-Anti_D4dot.exe"); - AssemblyDef assemblyDef = null; + AssemblyDef assemblyDef; try { assemblyDef = AssemblyDef.Load(inputFilePath); @@ -75,11 +78,11 @@ private static void Main(string[] args) return; } - Context context = new Context(assemblyDef); - int numTypes = 100; + var context = new Context(assemblyDef); + const int numTypes = 100; RenameTypes(context, numTypes); - ModuleWriterOptions moduleWriterOptions = new ModuleWriterOptions(context.ManifestModule) + var moduleWriterOptions = new ModuleWriterOptions(context.ManifestModule) { Logger = null }; @@ -96,7 +99,7 @@ private static void Main(string[] args) PrintMessage("Press any key to exit."); Console.ReadKey(); - } + } //Message Printer Function private static void PrintMessage(string message, ConsoleColor color = ConsoleColor.White) @@ -106,4 +109,4 @@ private static void PrintMessage(string message, ConsoleColor color = ConsoleCol Console.ResetColor(); } } -} \ No newline at end of file +}