LookPlays

String Obfuscator with AES Encryption for .NET Assemblies

Oct 31st, 2024
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | Cybersecurity | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using DNLib.DotNet;
  6.  
  7. public class StrObfuscator
  8. {
  9.     private static readonly Random rnd = new Random();
  10.     private static readonly Dictionary<string, string> obfStrs = new Dictionary<string, string>();
  11.     private const string key = "ChaveSecreta";
  12.  
  13.     public static void Obfuscate(string peFile)
  14.     {
  15.         try
  16.         {
  17.             var mod = ModuleDefMD.Load(peFile);
  18.             foreach (var type in mod.GetTypes())
  19.             {
  20.                 foreach (var method in type.Methods)
  21.                 {
  22.                     foreach (var instr in method.Body.Instructions)
  23.                     {
  24.                         if (instr.OpCode == OpCode.Ldstr)
  25.                         {
  26.                             string lit = (string)instr.Operand;
  27.                             string obfName = GenRandomName(type);
  28.                             var field = new FieldDefUser(obfName, FieldAttributes.Static | FieldAttributes.Public, mod.ImportAsType(typeof(string)));
  29.                             type.Fields.Add(field);
  30.                             field.SetValue(mod, Encrypt(lit, key));
  31.                             instr.Operand = obfName;
  32.                         }
  33.                     }
  34.                 }
  35.             }
  36.             mod.Write(peFile);
  37.         }
  38.         catch (Exception ex)
  39.         {
  40.             Console.WriteLine($"Erro: {ex.Message}");
  41.         }
  42.     }
  43.  
  44.     private static string GenRandomName(TypeDef type)
  45.     {
  46.         string name;
  47.         do
  48.         {
  49.             var sb = new StringBuilder();
  50.             for (int i = 0; i < 10; i++)
  51.             {
  52.                 sb.Append((char)rnd.Next(97, 123));
  53.             }
  54.             name = sb.ToString();
  55.         } while (type.Fields.Any(f => f.Name == name)); // Garantir unicidade
  56.         return name;
  57.     }
  58.  
  59.     private static byte[] Encrypt(string str, string key)
  60.     {
  61.         using (Aes aes = Aes.Create())
  62.         {
  63.             aes.Key = Encoding.UTF8.GetBytes(key);
  64.             aes.GenerateIV();
  65.             byte[] iv = aes.IV;
  66.  
  67.             using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, iv))
  68.             {
  69.                 byte[] encBytes = encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(str), 0, str.Length);
  70.                 byte[] result = new byte[iv.Length + encBytes.Length];
  71.                 Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
  72.                 Buffer.BlockCopy(encBytes, 0, result, iv.Length, encBytes.Length);
  73.                 return result;
  74.             }
  75.         }
  76.     }
  77.  
  78.     public static string Deobfuscate(byte[] obfBytes)
  79.     {
  80.         byte[] iv = new byte[16];
  81.         Buffer.BlockCopy(obfBytes, 0, iv, 0, iv.Length);
  82.         byte[] encBytes = new byte[obfBytes.Length - iv.Length];
  83.         Buffer.BlockCopy(obfBytes, iv.Length, encBytes, 0, encBytes.Length);
  84.         return Decrypt(encBytes, key, iv);
  85.     }
  86.  
  87.     private static string Decrypt(byte[] bytes, string key, byte[] iv)
  88.     {
  89.         using (Aes aes = Aes.Create())
  90.         {
  91.             aes.Key = Encoding.UTF8.GetBytes(key);
  92.             aes.IV = iv;
  93.  
  94.             using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
  95.             {
  96.                 byte[] decBytes = decryptor.TransformFinalBlock(bytes, 0, bytes.Length);
  97.                 return Encoding.UTF8.GetString(decBytes);
  98.             }
  99.         }
  100.     }
  101. }
  102.  
  103. class Program
  104. {
  105.     [STAThread]
  106.     static void Main(string[] args)
  107.     {
  108.         StrObfuscator.Obfuscate("MyAssembly.exe");
  109.         string origStr = "Hello, World!";
  110.         byte[] obfBytes = StrObfuscator.Encrypt(origStr, StrObfuscator.key);
  111.         string deobfStr = StrObfuscator.Deobfuscate(obfBytes);
  112.         Console.WriteLine(deobfStr);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment