obfuz/Editor/Obfuscator.cs

225 lines
9.2 KiB
C#
Raw Normal View History

2025-04-05 19:02:50 +08:00
using dnlib.DotNet;
2025-05-10 11:25:07 +08:00
using dnlib.Protection;
using Obfuz.Data;
using Obfuz.Emit;
2025-05-11 12:53:24 +08:00
using Obfuz.EncryptionVM;
2025-05-04 19:55:10 +08:00
using Obfuz.ObfusPasses;
2025-05-10 18:25:43 +08:00
using Obfuz.ObfusPasses.CleanUp;
using Obfuz.ObfusPasses.SymbolObfus;
2025-05-04 19:24:14 +08:00
using Obfuz.Utils;
2025-04-05 19:02:50 +08:00
using System;
using System.Collections.Generic;
2025-04-05 21:47:28 +08:00
using System.IO;
2025-04-05 19:02:50 +08:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2025-04-05 21:47:28 +08:00
using UnityEngine;
2025-05-10 18:25:43 +08:00
using static UnityEditor.ObjectChangeEventStream;
2025-04-05 19:02:50 +08:00
namespace Obfuz
{
public class Obfuscator
{
2025-05-03 21:43:50 +08:00
private readonly string _obfuscatedAssemblyOutputDir;
2025-04-05 19:02:50 +08:00
2025-05-03 21:43:50 +08:00
private readonly List<string> _toObfuscatedAssemblyNames;
private readonly List<string> _notObfuscatedAssemblyNamesReferencingObfuscated;
private readonly List<string> _assemblySearchDirs;
2025-04-21 09:57:34 +08:00
private readonly Pipeline _pipeline1 = new Pipeline();
private readonly Pipeline _pipeline2 = new Pipeline();
private readonly byte[] _secret;
private readonly int _randomSeed;
private readonly string _encryptionVmGenerationSecret;
2025-05-11 17:36:58 +08:00
private readonly int _encryptionVmOpCodeCount;
private readonly string _encryptionVmCodeFile;
2025-04-21 09:57:34 +08:00
2025-05-04 19:24:14 +08:00
private ObfuscationPassContext _ctx;
2025-04-17 22:02:48 +08:00
2025-05-11 17:36:58 +08:00
public Obfuscator(ObfuscatorBuilder builder)
2025-04-05 19:02:50 +08:00
{
_secret = KeyGenerator.GenerateKey(builder.Secret, VirtualMachine.SecretKeyLength);
SaveKey(_secret, builder.SecretOutputPath);
_randomSeed = builder.RandomSeed;
_encryptionVmGenerationSecret = builder.EncryptionVmGenerationSecretKey;
2025-05-11 17:36:58 +08:00
_encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount;
_encryptionVmCodeFile = builder.EncryptionVmCodeFile;
2025-05-11 09:17:04 +08:00
2025-05-11 17:36:58 +08:00
_toObfuscatedAssemblyNames = builder.ToObfuscatedAssemblyNames;
_notObfuscatedAssemblyNamesReferencingObfuscated = builder.NotObfuscatedAssemblyNamesReferencingObfuscated;
_obfuscatedAssemblyOutputDir = builder.ObfuscatedAssemblyOutputDir;
_assemblySearchDirs = builder.AssemblySearchDirs;
2025-05-03 21:43:50 +08:00
2025-05-11 17:36:58 +08:00
foreach (var pass in builder.ObfuscationPasses)
2025-05-03 21:43:50 +08:00
{
if (pass is SymbolObfusPass symbolObfusPass)
{
_pipeline2.AddPass(pass);
}
else
{
_pipeline1.AddPass(pass);
}
2025-05-03 21:43:50 +08:00
}
_pipeline1.AddPass(new CleanUpInstructionPass());
2025-04-05 19:02:50 +08:00
}
public static void SaveKey(byte[] secret, string secretOutputPath)
{
FileUtil.CreateParentDir(secretOutputPath);
File.WriteAllBytes(secretOutputPath, secret);
Debug.Log($"Save secret key to {secretOutputPath}, secret length:{secret.Length}");
}
2025-04-16 23:03:41 +08:00
public void Run()
2025-05-03 21:43:50 +08:00
{
FileUtil.RecreateDir(_obfuscatedAssemblyOutputDir);
RunPipeline(_pipeline1);
_assemblySearchDirs.Insert(0, _obfuscatedAssemblyOutputDir);
RunPipeline(_pipeline2);
}
private void RunPipeline(Pipeline pipeline)
{
if (pipeline.Empty)
{
return;
}
OnPreObfuscation(pipeline);
DoObfuscation(pipeline);
OnPostObfuscation(pipeline);
2025-05-03 21:43:50 +08:00
}
2025-05-11 12:48:53 +08:00
private IEncryptor CreateEncryptionVirtualMachine()
{
var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecret);
2025-05-11 17:36:58 +08:00
var vm = vmCreator.CreateVirtualMachine(_encryptionVmOpCodeCount);
var vmGenerator = new VirtualMachineCodeGenerator(vm);
if (!File.Exists(_encryptionVmCodeFile))
{
throw new Exception($"EncryptionVm CodeFile:`{_encryptionVmCodeFile}` not exists! Please run `Obfuz/GenerateVm` to generate it!");
}
if (!vmGenerator.ValidateMatch(_encryptionVmCodeFile))
{
throw new Exception($"EncryptionVm CodeFile:`{_encryptionVmCodeFile}` not match with encryptionVM settings! Please run `Obfuz/GenerateVm` to update it!");
}
var vms = new VirtualMachineSimulator(vm, _secret);
var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies()
.Select(assembly => assembly.GetType("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine"))
.Where(type => type != null)
.ToList();
if (generatedVmTypes.Count == 0)
{
throw new Exception($"class Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine not found in any assembly! Please run `Obfuz/GenerateVm` to generate it!");
}
if (generatedVmTypes.Count > 1)
{
throw new Exception($"class Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine found in multiple assemblies! Please retain only one!");
}
var gvmInstance = (IEncryptor)Activator.CreateInstance(generatedVmTypes[0], new object[] { _secret } );
int testValue = 11223344;
for (int i = 0; i < vm.opCodes.Length; i++)
{
int encryptedValueOfVms = vms.Encrypt(testValue, i, i);
int decryptedValueOfVms = vms.Decrypt(encryptedValueOfVms, i, i);
if (decryptedValueOfVms != testValue)
{
throw new Exception($"VirtualMachineSimulator decrypt failed! opCode:{i}, originalValue:{testValue} decryptedValue:{decryptedValueOfVms}");
}
int encryptedValueOfGvm = gvmInstance.Encrypt(testValue, i, i);
int decryptedValueOfGvm = gvmInstance.Decrypt(encryptedValueOfGvm, i, i);
if (encryptedValueOfGvm != encryptedValueOfVms)
{
throw new Exception($"encryptedValue not match! opCode:{i}, originalValue:{testValue} encryptedValue VirtualMachineSimulator:{encryptedValueOfVms} GeneratedEncryptionVirtualMachine:{encryptedValueOfGvm}");
}
if (decryptedValueOfGvm != testValue)
{
throw new Exception($"GeneratedEncryptionVirtualMachine decrypt failed! opCode:{i}, originalValue:{testValue} decryptedValue:{decryptedValueOfGvm}");
}
}
return vms;
2025-05-11 12:48:53 +08:00
}
private void OnPreObfuscation(Pipeline pipeline)
2025-04-05 19:02:50 +08:00
{
AssemblyCache assemblyCache = new AssemblyCache(new PathAssemblyResolver(_assemblySearchDirs.ToArray()));
List<ModuleDef> toObfuscatedModules = new List<ModuleDef>();
List<ModuleDef> obfuscatedAndNotObfuscatedModules = new List<ModuleDef>();
GroupByModuleEntityManager.Reset();
LoadAssemblies(assemblyCache, toObfuscatedModules, obfuscatedAndNotObfuscatedModules);
2025-05-10 11:25:07 +08:00
var random = new RandomWithKey(_secret, _randomSeed);
2025-05-11 12:48:53 +08:00
var encryptor = CreateEncryptionVirtualMachine();
2025-05-10 11:25:07 +08:00
var rvaDataAllocator = new RvaDataAllocator(random, encryptor);
var constFieldAllocator = new ConstFieldAllocator(encryptor, random, rvaDataAllocator);
2025-05-04 19:24:14 +08:00
_ctx = new ObfuscationPassContext
{
assemblyCache = assemblyCache,
toObfuscatedModules = toObfuscatedModules,
obfuscatedAndNotObfuscatedModules = obfuscatedAndNotObfuscatedModules,
toObfuscatedAssemblyNames = _toObfuscatedAssemblyNames,
notObfuscatedAssemblyNamesReferencingObfuscated = _notObfuscatedAssemblyNamesReferencingObfuscated,
obfuscatedAssemblyOutputDir = _obfuscatedAssemblyOutputDir,
2025-05-10 11:25:07 +08:00
random = random,
encryptor = encryptor,
rvaDataAllocator = rvaDataAllocator,
constFieldAllocator = constFieldAllocator,
2025-05-12 18:03:39 +08:00
whiteList = new NotObfuscatedMethodWhiteList(),
};
pipeline.Start(_ctx);
2025-04-05 19:02:50 +08:00
}
private void LoadAssemblies(AssemblyCache assemblyCache, List<ModuleDef> toObfuscatedModules, List<ModuleDef> obfuscatedAndNotObfuscatedModules)
2025-04-05 19:02:50 +08:00
{
2025-05-03 23:23:16 +08:00
foreach (string assName in _toObfuscatedAssemblyNames.Concat(_notObfuscatedAssemblyNamesReferencingObfuscated))
2025-04-05 19:02:50 +08:00
{
ModuleDefMD mod = assemblyCache.TryLoadModule(assName);
2025-04-17 22:02:48 +08:00
if (mod == null)
{
Debug.Log($"assembly: {assName} not found! ignore.");
continue;
}
2025-05-03 23:23:16 +08:00
if (_toObfuscatedAssemblyNames.Contains(assName))
2025-04-05 19:02:50 +08:00
{
toObfuscatedModules.Add(mod);
2025-04-05 19:02:50 +08:00
}
obfuscatedAndNotObfuscatedModules.Add(mod);
2025-04-05 19:02:50 +08:00
}
}
private void WriteAssemblies()
2025-04-05 19:02:50 +08:00
{
foreach (ModuleDef mod in _ctx.obfuscatedAndNotObfuscatedModules)
2025-04-05 21:47:28 +08:00
{
2025-05-03 23:23:16 +08:00
string assNameWithExt = mod.Name;
string outputFile = $"{_obfuscatedAssemblyOutputDir}/{assNameWithExt}";
mod.Write(outputFile);
Debug.Log($"save module. name:{mod.Assembly.Name} output:{outputFile}");
2025-04-05 21:47:28 +08:00
}
}
private void DoObfuscation(Pipeline pipeline)
{
pipeline.Run(_ctx);
}
private void OnPostObfuscation(Pipeline pipeline)
{
pipeline.Stop(_ctx);
WriteAssemblies();
}
2025-04-05 19:02:50 +08:00
}
}