Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
- using DNLib.DotNet;
- public class StrObfuscator
- {
- private static readonly Random rnd = new Random();
- private static readonly Dictionary<string, string> obfStrs = new Dictionary<string, string>();
- private const string key = "ChaveSecreta";
- public static void Obfuscate(string peFile)
- {
- try
- {
- var mod = ModuleDefMD.Load(peFile);
- foreach (var type in mod.GetTypes())
- {
- foreach (var method in type.Methods)
- {
- foreach (var instr in method.Body.Instructions)
- {
- if (instr.OpCode == OpCode.Ldstr)
- {
- string lit = (string)instr.Operand;
- string obfName = GenRandomName(type);
- var field = new FieldDefUser(obfName, FieldAttributes.Static | FieldAttributes.Public, mod.ImportAsType(typeof(string)));
- type.Fields.Add(field);
- field.SetValue(mod, Encrypt(lit, key));
- instr.Operand = obfName;
- }
- }
- }
- }
- mod.Write(peFile);
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Erro: {ex.Message}");
- }
- }
- private static string GenRandomName(TypeDef type)
- {
- string name;
- do
- {
- var sb = new StringBuilder();
- for (int i = 0; i < 10; i++)
- {
- sb.Append((char)rnd.Next(97, 123));
- }
- name = sb.ToString();
- } while (type.Fields.Any(f => f.Name == name)); // Garantir unicidade
- return name;
- }
- private static byte[] Encrypt(string str, string key)
- {
- using (Aes aes = Aes.Create())
- {
- aes.Key = Encoding.UTF8.GetBytes(key);
- aes.GenerateIV();
- byte[] iv = aes.IV;
- using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, iv))
- {
- byte[] encBytes = encryptor.TransformFinalBlock(Encoding.UTF8.GetBytes(str), 0, str.Length);
- byte[] result = new byte[iv.Length + encBytes.Length];
- Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
- Buffer.BlockCopy(encBytes, 0, result, iv.Length, encBytes.Length);
- return result;
- }
- }
- }
- public static string Deobfuscate(byte[] obfBytes)
- {
- byte[] iv = new byte[16];
- Buffer.BlockCopy(obfBytes, 0, iv, 0, iv.Length);
- byte[] encBytes = new byte[obfBytes.Length - iv.Length];
- Buffer.BlockCopy(obfBytes, iv.Length, encBytes, 0, encBytes.Length);
- return Decrypt(encBytes, key, iv);
- }
- private static string Decrypt(byte[] bytes, string key, byte[] iv)
- {
- using (Aes aes = Aes.Create())
- {
- aes.Key = Encoding.UTF8.GetBytes(key);
- aes.IV = iv;
- using (ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV))
- {
- byte[] decBytes = decryptor.TransformFinalBlock(bytes, 0, bytes.Length);
- return Encoding.UTF8.GetString(decBytes);
- }
- }
- }
- }
- class Program
- {
- [STAThread]
- static void Main(string[] args)
- {
- StrObfuscator.Obfuscate("MyAssembly.exe");
- string origStr = "Hello, World!";
- byte[] obfBytes = StrObfuscator.Encrypt(origStr, StrObfuscator.key);
- string deobfStr = StrObfuscator.Deobfuscate(obfBytes);
- Console.WriteLine(deobfStr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment