diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallObfusPass.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallObfusPass.cs index aed4f7b..5ad95b8 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallObfusPass.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallObfusPass.cs @@ -15,19 +15,15 @@ namespace Obfuz.ObfusPasses.CallObfus { public class CallObfusPass : BasicBlockObfuscationPassBase { - private readonly List _configFiles; - private readonly int _obfuscationLevel; - private readonly int _maxProxyMethodPerDispatchMethod; + private readonly CallObfuscationSettingsFacade _settings; private IObfuscator _dynamicProxyObfuscator; private IObfuscationPolicy _dynamicProxyPolicy; public override ObfuscationPassType Type => ObfuscationPassType.CallObfus; - public CallObfusPass(CallObfuscationSettings settings) + public CallObfusPass(CallObfuscationSettingsFacade settings) { - _configFiles = settings.ruleFiles.ToList(); - _obfuscationLevel = settings.obfuscationLevel; - _maxProxyMethodPerDispatchMethod = settings.maxProxyMethodCountPerDispatchMethod; + _settings = settings; } public override void Stop() @@ -38,8 +34,8 @@ namespace Obfuz.ObfusPasses.CallObfus public override void Start() { var ctx = ObfuscationPassContext.Current; - _dynamicProxyObfuscator = new DefaultCallProxyObfuscator(ctx.encryptionScopeProvider, ctx.constFieldAllocator, ctx.moduleEntityManager, _obfuscationLevel, _maxProxyMethodPerDispatchMethod); - _dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.assembliesToObfuscate, _configFiles); + _dynamicProxyObfuscator = new DefaultCallProxyObfuscator(ctx.encryptionScopeProvider, ctx.constFieldAllocator, ctx.moduleEntityManager, _settings); + _dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles); } protected override bool NeedObfuscateMethod(MethodDef method) diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallProxyAllocator.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallProxyAllocator.cs index 919054a..2c4e5c5 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallProxyAllocator.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/CallProxyAllocator.cs @@ -2,6 +2,7 @@ using dnlib.DotNet.Emit; using Obfuz.Editor; using Obfuz.Emit; +using Obfuz.Settings; using Obfuz.Utils; using System; using System.Collections.Generic; @@ -35,7 +36,7 @@ namespace Obfuz.ObfusPasses.CallObfus { private ModuleDef _module; private readonly EncryptionScopeProvider _encryptionScopeProvider; - private readonly int _encryptionLevel; + private readonly CallObfuscationSettingsFacade _settings; private EncryptionScopeInfo _encryptionScope; private bool _done; @@ -88,17 +89,15 @@ namespace Obfuz.ObfusPasses.CallObfus public List methods = new List(); } - private readonly int _maxProxyMethodPerDispatchMethod; private readonly Dictionary> _dispatchMethods = new Dictionary>(SignatureEqualityComparer.Instance); private TypeDef _proxyTypeDef; - public ModuleCallProxyAllocator(EncryptionScopeProvider encryptionScopeProvider, int encryptionLevel, int maxProxyMethodPerDispatchMethod) + public ModuleCallProxyAllocator(EncryptionScopeProvider encryptionScopeProvider, CallObfuscationSettingsFacade settings) { _encryptionScopeProvider = encryptionScopeProvider; - _encryptionLevel = encryptionLevel; - _maxProxyMethodPerDispatchMethod = maxProxyMethodPerDispatchMethod; + _settings = settings; } public void Init(ModuleDef mod) @@ -160,7 +159,7 @@ namespace Obfuz.ObfusPasses.CallObfus private int GenerateEncryptOps(IRandom random) { - return EncryptionUtil.GenerateEncryptionOpCodes(random, _encryptionScope.encryptor, _encryptionLevel); + return EncryptionUtil.GenerateEncryptionOpCodes(random, _encryptionScope.encryptor, _settings.obfuscationLevel); } private DispatchMethodInfo GetDispatchMethod(IMethod method) @@ -171,7 +170,7 @@ namespace Obfuz.ObfusPasses.CallObfus dispatchMethods = new List(); _dispatchMethods.Add(methodSig, dispatchMethods); } - if (dispatchMethods.Count == 0 || dispatchMethods.Last().methods.Count >= _maxProxyMethodPerDispatchMethod) + if (dispatchMethods.Count == 0 || dispatchMethods.Last().methods.Count >= _settings.maxProxyMethodCountPerDispatchMethod) { var newDispatchMethodInfo = new DispatchMethodInfo { @@ -261,20 +260,18 @@ namespace Obfuz.ObfusPasses.CallObfus { private readonly EncryptionScopeProvider _encryptionScopeProvider; private GroupByModuleEntityManager _moduleEntityManager; - private readonly int _encryptionLevel; - private readonly int _maxProxyMethodPerDispatchMethod; + private readonly CallObfuscationSettingsFacade _settings; - public CallProxyAllocator(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, int encryptionLevel, int maxProxyMethodPerDispatchMethod) + public CallProxyAllocator(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, CallObfuscationSettingsFacade settings) { _encryptionScopeProvider = encryptionScopeProvider; _moduleEntityManager = moduleEntityManager; - _encryptionLevel = encryptionLevel; - _maxProxyMethodPerDispatchMethod = maxProxyMethodPerDispatchMethod; + _settings = settings; } private ModuleCallProxyAllocator GetModuleAllocator(ModuleDef mod) { - return _moduleEntityManager.GetEntity(mod, () => new ModuleCallProxyAllocator(_encryptionScopeProvider, _encryptionLevel, _maxProxyMethodPerDispatchMethod)); + return _moduleEntityManager.GetEntity(mod, () => new ModuleCallProxyAllocator(_encryptionScopeProvider, _settings)); } public ProxyCallMethodData Allocate(ModuleDef mod, IMethod method, bool callVir) diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/DefaultCallProxyObfuscator.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/DefaultCallProxyObfuscator.cs index 65b15f6..cee61ac 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/DefaultCallProxyObfuscator.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/DefaultCallProxyObfuscator.cs @@ -5,6 +5,7 @@ using Obfuz.Utils; using Obfuz.Emit; using Obfuz.Data; using UnityEngine; +using Obfuz.Settings; namespace Obfuz.ObfusPasses.CallObfus { @@ -15,12 +16,12 @@ namespace Obfuz.ObfusPasses.CallObfus private readonly CallProxyAllocator _proxyCallAllocator; private readonly GroupByModuleEntityManager _moduleEntityManager; - public DefaultCallProxyObfuscator(EncryptionScopeProvider encryptionScopeProvider, ConstFieldAllocator constFieldAllocator, GroupByModuleEntityManager moduleEntityManager, int encryptionLevel, int maxProxyMethodPerDispatchMethod) + public DefaultCallProxyObfuscator(EncryptionScopeProvider encryptionScopeProvider, ConstFieldAllocator constFieldAllocator, GroupByModuleEntityManager moduleEntityManager, CallObfuscationSettingsFacade settings) { _encryptionScopeProvider = encryptionScopeProvider; _constFieldAllocator = constFieldAllocator; _moduleEntityManager = moduleEntityManager; - _proxyCallAllocator = new CallProxyAllocator(encryptionScopeProvider, moduleEntityManager, encryptionLevel, maxProxyMethodPerDispatchMethod); + _proxyCallAllocator = new CallProxyAllocator(encryptionScopeProvider, moduleEntityManager, settings); } public override void Done() diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs index b39d304..6b37b1e 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs @@ -15,23 +15,21 @@ namespace Obfuz.ObfusPasses.ConstEncrypt public class ConstEncryptPass : BasicBlockObfuscationPassBase { - private readonly List _configFiles; - private readonly int _encryptionLevel; + private readonly ConstEncryptionSettingsFacade _settings; private IEncryptPolicy _dataObfuscatorPolicy; private IConstEncryptor _dataObfuscator; public override ObfuscationPassType Type => ObfuscationPassType.ConstEncrypt; - public ConstEncryptPass(ConstEncryptionSettings settings) + public ConstEncryptPass(ConstEncryptionSettingsFacade settings) { - _configFiles = settings.ruleFiles.ToList(); - _encryptionLevel = settings.encryptionLevel; + _settings = settings; } public override void Start() { var ctx = ObfuscationPassContext.Current; - _dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.assembliesToObfuscate, _configFiles); - _dataObfuscator = new DefaultConstEncryptor(ctx.encryptionScopeProvider, ctx.rvaDataAllocator, ctx.constFieldAllocator, ctx.moduleEntityManager, _encryptionLevel); + _dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles); + _dataObfuscator = new DefaultConstEncryptor(ctx.encryptionScopeProvider, ctx.rvaDataAllocator, ctx.constFieldAllocator, ctx.moduleEntityManager, _settings); } public override void Stop() diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs index 75f98dc..9807026 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/DefaultConstEncryptor.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using NUnit.Framework; using System.Text; +using Obfuz.Settings; namespace Obfuz.ObfusPasses.ConstEncrypt { @@ -16,15 +17,15 @@ namespace Obfuz.ObfusPasses.ConstEncrypt private readonly RvaDataAllocator _rvaDataAllocator; private readonly ConstFieldAllocator _constFieldAllocator; private readonly GroupByModuleEntityManager _moduleEntityManager; - private readonly int _encryptionLevel; + private readonly ConstEncryptionSettingsFacade _settings; - public DefaultConstEncryptor(EncryptionScopeProvider encryptionScopeProvider, RvaDataAllocator rvaDataAllocator, ConstFieldAllocator constFieldAllocator, GroupByModuleEntityManager moduleEntityManager, int encryptionLevel) + public DefaultConstEncryptor(EncryptionScopeProvider encryptionScopeProvider, RvaDataAllocator rvaDataAllocator, ConstFieldAllocator constFieldAllocator, GroupByModuleEntityManager moduleEntityManager, ConstEncryptionSettingsFacade settings) { _encryptionScopeProvider = encryptionScopeProvider; _rvaDataAllocator = rvaDataAllocator; _constFieldAllocator = constFieldAllocator; _moduleEntityManager = moduleEntityManager; - _encryptionLevel = encryptionLevel; + _settings = settings; } private IRandom CreateRandomForValue(EncryptionScopeInfo encryptionScope, int value) @@ -34,7 +35,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt private int GenerateEncryptionOperations(EncryptionScopeInfo encryptionScope, IRandom random) { - return EncryptionUtil.GenerateEncryptionOpCodes(random, encryptionScope.encryptor, _encryptionLevel); + return EncryptionUtil.GenerateEncryptionOpCodes(random, encryptionScope.encryptor, _settings.encryptionLevel); } public int GenerateSalt(IRandom random) diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/DefaultFieldEncryptor.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/DefaultFieldEncryptor.cs index 05c9c63..8ff7fbd 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/DefaultFieldEncryptor.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/DefaultFieldEncryptor.cs @@ -1,6 +1,7 @@ using dnlib.DotNet; using dnlib.DotNet.Emit; using Obfuz.Emit; +using Obfuz.Settings; using Obfuz.Utils; using System; using System.Collections.Generic; @@ -14,13 +15,13 @@ namespace Obfuz.ObfusPasses.FieldEncrypt { private readonly EncryptionScopeProvider _encryptionScopeProvider; private readonly GroupByModuleEntityManager _moduleEntityManager; - private readonly int _encryptionLevel; + private readonly FieldEncryptionSettingsFacade _settings; - public DefaultFieldEncryptor(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, int encryptionLevel) + public DefaultFieldEncryptor(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, FieldEncryptionSettingsFacade settings) { _encryptionScopeProvider = encryptionScopeProvider; _moduleEntityManager = moduleEntityManager; - _encryptionLevel = encryptionLevel; + _settings = settings; } private DefaultMetadataImporter GetMetadataImporter(MethodDef method) @@ -64,7 +65,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt private int GenerateEncryptionOperations(IRandom random, IEncryptor encryptor) { - return EncryptionUtil.GenerateEncryptionOpCodes(random, encryptor, _encryptionLevel); + return EncryptionUtil.GenerateEncryptionOpCodes(random, encryptor, _settings.encryptionLevel); } public int GenerateSalt(IRandom random) diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs index 054044e..d9d5029 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/FieldEncryptPass.cs @@ -12,17 +12,15 @@ namespace Obfuz.ObfusPasses.FieldEncrypt public class FieldEncryptPass : InstructionObfuscationPassBase { - private readonly List _configFiles; - private readonly int _encryptionLevel; + private FieldEncryptionSettingsFacade _settings; private IEncryptPolicy _encryptionPolicy; private IFieldEncryptor _memoryEncryptor; public override ObfuscationPassType Type => ObfuscationPassType.FieldEncrypt; - public FieldEncryptPass(FieldEncryptionSettings settings) + public FieldEncryptPass(FieldEncryptionSettingsFacade settings) { - _configFiles = settings.ruleFiles.ToList(); - _encryptionLevel = settings.encryptionLevel; + _settings = settings; } protected override bool ForceProcessAllAssembliesAndIgnoreAllPolicy => true; @@ -30,8 +28,8 @@ namespace Obfuz.ObfusPasses.FieldEncrypt public override void Start() { var ctx = ObfuscationPassContext.Current; - _memoryEncryptor = new DefaultFieldEncryptor(ctx.encryptionScopeProvider, ctx.moduleEntityManager, _encryptionLevel); - _encryptionPolicy = new ConfigurableEncryptPolicy(ctx.assembliesToObfuscate, _configFiles); + _memoryEncryptor = new DefaultFieldEncryptor(ctx.encryptionScopeProvider, ctx.moduleEntityManager, _settings); + _encryptionPolicy = new ConfigurableEncryptPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles); } public override void Stop() diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolObfusPass.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolObfusPass.cs index 2501f1d..3bbdff3 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolObfusPass.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolObfusPass.cs @@ -13,7 +13,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus public override ObfuscationPassType Type => ObfuscationPassType.SymbolObfus; - public SymbolObfusPass(SymbolObfuscationSettings settings) + public SymbolObfusPass(SymbolObfuscationSettingsFacade settings) { _symbolRename = new SymbolRename(settings); } diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs index d65f8c8..b7f2105 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/SymbolRename.cs @@ -42,7 +42,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus public List namedArguments; } - public SymbolRename(SymbolObfuscationSettings settings) + public SymbolRename(SymbolObfuscationSettingsFacade settings) { _useConsistentNamespaceObfuscation = settings.useConsistentNamespaceObfuscation; _mappingXmlPath = settings.symbolMappingFile; @@ -59,7 +59,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus _toObfuscatedModules = ctx.modulesToObfuscate; _obfuscatedAndNotObfuscatedModules = ctx.allObfuscationRelativeModules; _toObfuscatedModuleSet = new HashSet(ctx.modulesToObfuscate); - var obfuscateRuleConfig = new ConfigurableRenamePolicy(ctx.assembliesToObfuscate, _obfuscationRuleFiles); + var obfuscateRuleConfig = new ConfigurableRenamePolicy(ctx.coreSettings.assembliesToObfuscate, _obfuscationRuleFiles); _renamePolicy = new CacheRenamePolicy(new CombineRenamePolicy(new SupportPassPolicy(ctx.passPolicy), new SystemRenamePolicy(), new UnityRenamePolicy(), obfuscateRuleConfig)); BuildCustomAttributeArguments(); } diff --git a/com.code-philosophy.obfuz/Editor/ObfuscationPassContext.cs b/com.code-philosophy.obfuz/Editor/ObfuscationPassContext.cs index f4b5c74..9367b00 100644 --- a/com.code-philosophy.obfuz/Editor/ObfuscationPassContext.cs +++ b/com.code-philosophy.obfuz/Editor/ObfuscationPassContext.cs @@ -61,19 +61,14 @@ namespace Obfuz { public static ObfuscationPassContext Current { get; set; } + public CoreSettingsFacade coreSettings; public GroupByModuleEntityManager moduleEntityManager; public AssemblyCache assemblyCache; - public List modulesToObfuscate; public List allObfuscationRelativeModules; - public List assembliesToObfuscate; - public List nonObfuscatedButReferencingObfuscatedAssemblies; - - public string obfuscatedAssemblyOutputPath; - public EncryptionScopeProvider encryptionScopeProvider; public ConstFieldAllocator constFieldAllocator; public RvaDataAllocator rvaDataAllocator; diff --git a/com.code-philosophy.obfuz/Editor/Obfuscator.cs b/com.code-philosophy.obfuz/Editor/Obfuscator.cs index a7b8242..cad0320 100644 --- a/com.code-philosophy.obfuz/Editor/Obfuscator.cs +++ b/com.code-philosophy.obfuz/Editor/Obfuscator.cs @@ -21,11 +21,9 @@ namespace Obfuz public class Obfuscator { - private readonly string _obfuscatedAssemblyTempOutputPath; - private readonly string _obfuscatedAssemblyOutputPath; - - private readonly List _assembliesToObfuscate; - private readonly List _nonObfuscatedButReferencingObfuscatedAssemblies; + private readonly CoreSettingsFacade _coreSettings; + private readonly List _allObfuscationRelativeAssemblyNames; + private readonly HashSet _assembliesUsingDynamicSecretKeys; private readonly CombinedAssemblyResolver _assemblyResolver; private readonly ConfigurablePassPolicy _passPolicy; @@ -33,38 +31,20 @@ namespace Obfuz private readonly Pipeline _pipeline1 = new Pipeline(); private readonly Pipeline _pipeline2 = new Pipeline(); - private readonly byte[] _defaultStaticByteSecretKey; - private readonly byte[] _defaultDynamicByteSecret; - private readonly HashSet _assembliesUsingDynamicSecretKeys; - - private readonly int _randomSeed; - private readonly string _encryptionVmGenerationSecretKey; - private readonly int _encryptionVmOpCodeCount; - private readonly string _encryptionVmCodeFile; - private ObfuscationPassContext _ctx; public Obfuscator(ObfuscatorBuilder builder) { - _defaultStaticByteSecretKey = KeyGenerator.GenerateKey(builder.DefaultStaticSecretKey, VirtualMachine.SecretKeyLength); - _defaultDynamicByteSecret = KeyGenerator.GenerateKey(builder.DefaultDynamicSecretKey, VirtualMachine.SecretKeyLength); - _assembliesUsingDynamicSecretKeys = new HashSet(builder.AssembliesUsingDynamicSecretKeys); + _coreSettings = builder.CoreSettingsFacade; + _allObfuscationRelativeAssemblyNames = _coreSettings.assembliesToObfuscate + .Concat(_coreSettings.nonObfuscatedButReferencingObfuscatedAssemblies) + .ToList(); + _assembliesUsingDynamicSecretKeys = new HashSet(_coreSettings.assembliesUsingDynamicSecretKeys); + _assemblyResolver = new CombinedAssemblyResolver(new PathAssemblyResolver(_coreSettings.assemblySearchPaths.ToArray()), new UnityProjectManagedAssemblyResolver(_coreSettings.buildTarget)); + _passPolicy = new ConfigurablePassPolicy(_coreSettings.assembliesToObfuscate, _coreSettings.enabledObfuscationPasses, _coreSettings.obfuscationPassRuleConfigFiles); - _randomSeed = builder.RandomSeed; - _encryptionVmGenerationSecretKey = builder.EncryptionVmGenerationSecretKey; - _encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount; - _encryptionVmCodeFile = builder.EncryptionVmCodeFile; - - _assembliesToObfuscate = builder.AssembliesToObfuscate; - _nonObfuscatedButReferencingObfuscatedAssemblies = builder.NonObfuscatedButReferencingObfuscatedAssemblies; - _obfuscatedAssemblyOutputPath = builder.ObfuscatedAssemblyOutputPath; - _obfuscatedAssemblyTempOutputPath = builder.ObfuscatedAssemblyTempOutputPath; - _assemblyResolver = new CombinedAssemblyResolver(new PathAssemblyResolver(builder.AssemblySearchPaths.ToArray()), new UnityProjectManagedAssemblyResolver(builder.BuildTarget)); - - _passPolicy = new ConfigurablePassPolicy(_assembliesToObfuscate, builder.EnableObfuscationPasses, builder.ObfuscationPassRuleConfigFiles); - - foreach (var pass in builder.ObfuscationPasses) + foreach (var pass in _coreSettings.obfuscationPasses) { if (pass is SymbolObfusPass symbolObfusPass) { @@ -82,12 +62,12 @@ namespace Obfuz public void Run() { Debug.Log($"Obfuscator Run. begin"); - FileUtil.RecreateDir(_obfuscatedAssemblyOutputPath); - FileUtil.RecreateDir(_obfuscatedAssemblyTempOutputPath); + FileUtil.RecreateDir(_coreSettings.obfuscatedAssemblyOutputPath); + FileUtil.RecreateDir(_coreSettings.obfuscatedAssemblyTempOutputPath); RunPipeline(_pipeline1); - _assemblyResolver.InsertFirst(new PathAssemblyResolver(_obfuscatedAssemblyTempOutputPath)); + _assemblyResolver.InsertFirst(new PathAssemblyResolver(_coreSettings.obfuscatedAssemblyTempOutputPath)); RunPipeline(_pipeline2); - FileUtil.CopyDir(_obfuscatedAssemblyTempOutputPath, _obfuscatedAssemblyOutputPath, true); + FileUtil.CopyDir(_coreSettings.obfuscatedAssemblyTempOutputPath, _coreSettings.obfuscatedAssemblyOutputPath, true); Debug.Log($"Obfuscator Run. end"); } @@ -104,17 +84,18 @@ namespace Obfuz private IEncryptor CreateEncryptionVirtualMachine(byte[] secretKey) { - var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecretKey); - var vm = vmCreator.CreateVirtualMachine(_encryptionVmOpCodeCount); + var vmCreator = new VirtualMachineCreator(_coreSettings.encryptionVmGenerationSecretKey); + var vm = vmCreator.CreateVirtualMachine(_coreSettings.encryptionVmOpCodeCount); var vmGenerator = new VirtualMachineCodeGenerator(vm); - if (!File.Exists(_encryptionVmCodeFile)) + string encryptionVmCodeFile = _coreSettings.encryptionVmCodeFile; + if (!File.Exists(encryptionVmCodeFile)) { - throw new Exception($"EncryptionVm CodeFile:`{_encryptionVmCodeFile}` not exists! Please run `Obfuz/GenerateVm` to generate it!"); + throw new Exception($"EncryptionVm CodeFile:`{encryptionVmCodeFile}` not exists! Please run `Obfuz/GenerateVm` to generate it!"); } - if (!vmGenerator.ValidateMatch(_encryptionVmCodeFile)) + if (!vmGenerator.ValidateMatch(encryptionVmCodeFile)) { - throw new Exception($"EncryptionVm CodeFile:`{_encryptionVmCodeFile}` not match with encryptionVM settings! Please run `Obfuz/GenerateVm` to update it!"); + throw new Exception($"EncryptionVm CodeFile:`{encryptionVmCodeFile}` not match with encryptionVM settings! Please run `Obfuz/GenerateVm` to update it!"); } var vms = new VirtualMachineSimulator(vm, secretKey); @@ -249,19 +230,19 @@ namespace Obfuz { int[] intSecretKey = KeyGenerator.ConvertToIntKey(byteSecret); IEncryptor encryption = CreateEncryptionVirtualMachine(byteSecret); - RandomCreator localRandomCreator = (seed) => new RandomWithKey(intSecretKey, _randomSeed ^ seed); + RandomCreator localRandomCreator = (seed) => new RandomWithKey(intSecretKey, _coreSettings.randomSeed ^ seed); return new EncryptionScopeInfo(encryption, localRandomCreator); } private EncryptionScopeProvider CreateEncryptionScopeProvider() { - var defaultStaticScope = CreateEncryptionScope(_defaultStaticByteSecretKey); - var defaultDynamicScope = CreateEncryptionScope(_defaultDynamicByteSecret); + var defaultStaticScope = CreateEncryptionScope(_coreSettings.defaultStaticSecretKey); + var defaultDynamicScope = CreateEncryptionScope(_coreSettings.defaultDynamicSecretKey); foreach (string dynamicAssName in _assembliesUsingDynamicSecretKeys) { - if (!_assembliesToObfuscate.Contains(dynamicAssName)) + if (!_coreSettings.assembliesToObfuscate.Contains(dynamicAssName)) { - throw new Exception($"Dynamic secret assembly `{dynamicAssName}` should be in the toObfuscatedAssemblyNames list!"); + throw new Exception($"Dynamic secret assembly `{dynamicAssName}` should be in the assembliesToObfuscate list!"); } } return new EncryptionScopeProvider(defaultStaticScope, defaultDynamicScope, _assembliesUsingDynamicSecretKeys); @@ -280,12 +261,10 @@ namespace Obfuz var constFieldAllocator = new ConstFieldAllocator(encryptionScopeProvider, rvaDataAllocator, moduleEntityManager); _ctx = new ObfuscationPassContext { + coreSettings = _coreSettings, assemblyCache = assemblyCache, modulesToObfuscate = modulesToObfuscate, allObfuscationRelativeModules = allObfuscationRelativeModules, - assembliesToObfuscate = _assembliesToObfuscate, - nonObfuscatedButReferencingObfuscatedAssemblies = _nonObfuscatedButReferencingObfuscatedAssemblies, - obfuscatedAssemblyOutputPath = _obfuscatedAssemblyOutputPath, moduleEntityManager = moduleEntityManager, encryptionScopeProvider = encryptionScopeProvider, @@ -301,7 +280,7 @@ namespace Obfuz private void LoadAssemblies(AssemblyCache assemblyCache, List modulesToObfuscate, List allObfuscationRelativeModules) { - foreach (string assName in _assembliesToObfuscate.Concat(_nonObfuscatedButReferencingObfuscatedAssemblies)) + foreach (string assName in _allObfuscationRelativeAssemblyNames) { ModuleDefMD mod = assemblyCache.TryLoadModule(assName); if (mod == null) @@ -309,7 +288,7 @@ namespace Obfuz Debug.Log($"assembly: {assName} not found! ignore."); continue; } - if (_assembliesToObfuscate.Contains(assName)) + if (_coreSettings.assembliesToObfuscate.Contains(assName)) { modulesToObfuscate.Add(mod); } @@ -322,7 +301,7 @@ namespace Obfuz foreach (ModuleDef mod in _ctx.allObfuscationRelativeModules) { string assNameWithExt = mod.Name; - string outputFile = $"{_obfuscatedAssemblyTempOutputPath}/{assNameWithExt}"; + string outputFile = $"{_coreSettings.obfuscatedAssemblyTempOutputPath}/{assNameWithExt}"; mod.Write(outputFile); Debug.Log($"save module. name:{mod.Assembly.Name} output:{outputFile}"); } diff --git a/com.code-philosophy.obfuz/Editor/ObfuscatorBuilder.cs b/com.code-philosophy.obfuz/Editor/ObfuscatorBuilder.cs index 16be3e9..d9d48f1 100644 --- a/com.code-philosophy.obfuz/Editor/ObfuscatorBuilder.cs +++ b/com.code-philosophy.obfuz/Editor/ObfuscatorBuilder.cs @@ -6,6 +6,7 @@ using Obfuz.ObfusPasses.ExprObfus; using Obfuz.ObfusPasses.FieldEncrypt; using Obfuz.ObfusPasses.SymbolObfus; using Obfuz.Settings; +using Obfuz.Utils; using System.Collections.Generic; using System.IO; using System.Linq; @@ -13,148 +14,45 @@ using UnityEditor; namespace Obfuz { + + public class CoreSettingsFacade + { + public BuildTarget buildTarget; + + public byte[] defaultStaticSecretKey; + public byte[] defaultDynamicSecretKey; + public List assembliesUsingDynamicSecretKeys; + public int randomSeed; + + public string encryptionVmGenerationSecretKey; + public int encryptionVmOpCodeCount; + public string encryptionVmCodeFile; + + public List assembliesToObfuscate; + public List nonObfuscatedButReferencingObfuscatedAssemblies; + public List assemblySearchPaths; + public string obfuscatedAssemblyOutputPath; + public string obfuscatedAssemblyTempOutputPath; + + public ObfuscationPassType enabledObfuscationPasses; + public List obfuscationPassRuleConfigFiles; + public List obfuscationPasses; + } + public class ObfuscatorBuilder { - private BuildTarget _buildTarget; + private CoreSettingsFacade _coreSettingsFacade; - private string _defaultStaticSecretKey; - private string _defaultStaticSecretKeyOutputPath; - private string _defaultDynamicSecretKey; - private string _defaultDynamicSecretKeyOutputPath; - private List _assembliesUsingDynamicSecretKeys = new List(); - - private int _randomSeed; - private string _encryptionVmGenerationSecretKey; - private int _encryptionVmOpCodeCount; - private string _encryptionVmCodeFile; - - private List _assembliesToObfuscate = new List(); - private List _nonObfuscatedButReferencingObfuscatedAssemblies = new List(); - private List _assemblySearchPaths = new List(); - - private string _obfuscatedAssemblyTempOutputPath; - private string _obfuscatedAssemblyOutputPath; - private List _obfuscationPassRuleConfigFiles; - - private ObfuscationPassType _enabledObfuscationPasses; - private List _obfuscationPasses = new List(); - - public BuildTarget BuildTarget - { - get => _buildTarget; - set => _buildTarget = value; - } - - public string DefaultStaticSecretKey - { - get => _defaultStaticSecretKey; - set => _defaultStaticSecretKey = value; - } - - public string DefaultStaticSecretKeyOutputPath - { - get => _defaultStaticSecretKeyOutputPath; - set => _defaultStaticSecretKeyOutputPath = value; - } - - public string DefaultDynamicSecretKey - { - get => _defaultDynamicSecretKey; - set => _defaultDynamicSecretKey = value; - } - - public string DefaultDynamicSecretKeyOutputPath - { - get => _defaultDynamicSecretKeyOutputPath; - set => _defaultDynamicSecretKeyOutputPath = value; - } - - public List AssembliesUsingDynamicSecretKeys - { - get => _assembliesUsingDynamicSecretKeys; - set => _assembliesUsingDynamicSecretKeys = value; - } - - public int RandomSeed - { - get => _randomSeed; - set => _randomSeed = value; - } - - public string EncryptionVmGenerationSecretKey - { - get => _encryptionVmGenerationSecretKey; - set => _encryptionVmGenerationSecretKey = value; - } - - public int EncryptionVmOpCodeCount - { - get => _encryptionVmOpCodeCount; - set => _encryptionVmOpCodeCount = value; - } - - public string EncryptionVmCodeFile - { - get => _encryptionVmCodeFile; - set => _encryptionVmCodeFile = value; - } - - public List AssembliesToObfuscate - { - get => _assembliesToObfuscate; - set => _assembliesToObfuscate = value; - } - - public List NonObfuscatedButReferencingObfuscatedAssemblies - { - get => _nonObfuscatedButReferencingObfuscatedAssemblies; - set => _nonObfuscatedButReferencingObfuscatedAssemblies = value; - } - - public List AssemblySearchPaths - { - get => _assemblySearchPaths; - set => _assemblySearchPaths = value; - } - - public string ObfuscatedAssemblyOutputPath - { - get => _obfuscatedAssemblyOutputPath; - set => _obfuscatedAssemblyOutputPath = value; - } - - public string ObfuscatedAssemblyTempOutputPath - { - get => _obfuscatedAssemblyTempOutputPath; - set => _obfuscatedAssemblyTempOutputPath = value; - } - - public ObfuscationPassType EnableObfuscationPasses - { - get => _enabledObfuscationPasses; - set => _enabledObfuscationPasses = value; - } - - public List ObfuscationPassRuleConfigFiles - { - get => _obfuscationPassRuleConfigFiles; - set => _obfuscationPassRuleConfigFiles = value; - } - - public List ObfuscationPasses - { - get => _obfuscationPasses; - set => _obfuscationPasses = value; - } + public CoreSettingsFacade CoreSettingsFacade => _coreSettingsFacade; public void InsertTopPriorityAssemblySearchPaths(List assemblySearchPaths) { - _assemblySearchPaths.InsertRange(0, assemblySearchPaths); + _coreSettingsFacade.assemblySearchPaths.InsertRange(0, assemblySearchPaths); } public ObfuscatorBuilder AddPass(IObfuscationPass pass) { - _obfuscationPasses.Add(pass); + _coreSettingsFacade.obfuscationPasses.Add(pass); return this; } @@ -209,8 +107,6 @@ namespace Obfuz #else #error "Unsupported platform, please report to us" #endif - - "Managed/UnityEngine", }; return searchPaths.Select(path => Path.Combine(applicationContentsPath, path)).ToList(); } @@ -222,36 +118,38 @@ namespace Obfuz : settings.assemblySettings.additionalAssemblySearchPaths.ToList(); var builder = new ObfuscatorBuilder { - _buildTarget = target, - _defaultStaticSecretKey = settings.secretSettings.defaultStaticSecretKey, - _defaultStaticSecretKeyOutputPath = settings.secretSettings.staticSecretKeyOutputPath, - _defaultDynamicSecretKey = settings.secretSettings.defaultDynamicSecretKey, - _defaultDynamicSecretKeyOutputPath = settings.secretSettings.dynamicSecretKeyOutputPath, - _assembliesUsingDynamicSecretKeys = settings.secretSettings.assembliesUsingDynamicSecretKeys.ToList(), - _randomSeed = settings.secretSettings.randomSeed, - _encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecretKey, - _encryptionVmOpCodeCount = settings.encryptionVMSettings.encryptionOpCodeCount, - _encryptionVmCodeFile = settings.encryptionVMSettings.codeOutputPath, - _assembliesToObfuscate = settings.assemblySettings.GetAssembliesToObfuscate(), - _nonObfuscatedButReferencingObfuscatedAssemblies = settings.assemblySettings.nonObfuscatedButReferencingObfuscatedAssemblies.ToList(), - _assemblySearchPaths = searchPaths, - _obfuscatedAssemblyOutputPath = settings.GetObfuscatedAssemblyOutputPath(target), - _obfuscatedAssemblyTempOutputPath = settings.GetObfuscatedAssemblyTempOutputPath(target), - _enabledObfuscationPasses = settings.obfuscationPassSettings.enabledPasses, - _obfuscationPassRuleConfigFiles = settings.obfuscationPassSettings.ruleFiles.ToList(), + _coreSettingsFacade = new CoreSettingsFacade() + { + buildTarget = target, + defaultStaticSecretKey = KeyGenerator.GenerateKey(settings.secretSettings.defaultStaticSecretKey, VirtualMachine.SecretKeyLength), + defaultDynamicSecretKey = KeyGenerator.GenerateKey(settings.secretSettings.defaultDynamicSecretKey, VirtualMachine.SecretKeyLength), + assembliesUsingDynamicSecretKeys = settings.secretSettings.assembliesUsingDynamicSecretKeys.ToList(), + randomSeed = settings.secretSettings.randomSeed, + encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecretKey, + encryptionVmOpCodeCount = settings.encryptionVMSettings.encryptionOpCodeCount, + encryptionVmCodeFile = settings.encryptionVMSettings.codeOutputPath, + assembliesToObfuscate = settings.assemblySettings.GetAssembliesToObfuscate(), + nonObfuscatedButReferencingObfuscatedAssemblies = settings.assemblySettings.nonObfuscatedButReferencingObfuscatedAssemblies.ToList(), + assemblySearchPaths = searchPaths, + obfuscatedAssemblyOutputPath = settings.GetObfuscatedAssemblyOutputPath(target), + obfuscatedAssemblyTempOutputPath = settings.GetObfuscatedAssemblyTempOutputPath(target), + enabledObfuscationPasses = settings.obfuscationPassSettings.enabledPasses, + obfuscationPassRuleConfigFiles = settings.obfuscationPassSettings.ruleFiles.ToList(), + obfuscationPasses = new List(), + }, }; ObfuscationPassType obfuscationPasses = settings.obfuscationPassSettings.enabledPasses; if (obfuscationPasses.HasFlag(ObfuscationPassType.ConstEncrypt)) { - builder.AddPass(new ConstEncryptPass(settings.constEncryptSettings)); + builder.AddPass(new ConstEncryptPass(settings.constEncryptSettings.ToFacade())); } if (obfuscationPasses.HasFlag(ObfuscationPassType.FieldEncrypt)) { - builder.AddPass(new FieldEncryptPass(settings.fieldEncryptSettings)); + builder.AddPass(new FieldEncryptPass(settings.fieldEncryptSettings.ToFacade())); } if (obfuscationPasses.HasFlag(ObfuscationPassType.CallObfus)) { - builder.AddPass(new CallObfusPass(settings.callObfusSettings)); + builder.AddPass(new CallObfusPass(settings.callObfusSettings.ToFacade())); } if (obfuscationPasses.HasFlag(ObfuscationPassType.ExprObfus)) { @@ -259,7 +157,7 @@ namespace Obfuz } if (obfuscationPasses.HasFlag(ObfuscationPassType.SymbolObfus)) { - builder.AddPass(new SymbolObfusPass(settings.symbolObfusSettings)); + builder.AddPass(new SymbolObfusPass(settings.symbolObfusSettings.ToFacade())); } return builder; } diff --git a/com.code-philosophy.obfuz/Editor/Settings/CallObfuscationSettings.cs b/com.code-philosophy.obfuz/Editor/Settings/CallObfuscationSettings.cs index 9ad91bc..04658da 100644 --- a/com.code-philosophy.obfuz/Editor/Settings/CallObfuscationSettings.cs +++ b/com.code-philosophy.obfuz/Editor/Settings/CallObfuscationSettings.cs @@ -7,6 +7,13 @@ using UnityEngine; namespace Obfuz.Settings { + public class CallObfuscationSettingsFacade + { + public List ruleFiles; + public int obfuscationLevel; + public int maxProxyMethodCountPerDispatchMethod; + } + [Serializable] public class CallObfuscationSettings { @@ -19,5 +26,15 @@ namespace Obfuz.Settings [Tooltip("rule config xml files")] public string[] ruleFiles; + + public CallObfuscationSettingsFacade ToFacade() + { + return new CallObfuscationSettingsFacade + { + ruleFiles = ruleFiles.ToList(), + obfuscationLevel = obfuscationLevel, + maxProxyMethodCountPerDispatchMethod = maxProxyMethodCountPerDispatchMethod, + }; + } } } diff --git a/com.code-philosophy.obfuz/Editor/Settings/ConstEncryptionSettings.cs b/com.code-philosophy.obfuz/Editor/Settings/ConstEncryptionSettings.cs index 794d685..53a21c9 100644 --- a/com.code-philosophy.obfuz/Editor/Settings/ConstEncryptionSettings.cs +++ b/com.code-philosophy.obfuz/Editor/Settings/ConstEncryptionSettings.cs @@ -7,6 +7,12 @@ using UnityEngine; namespace Obfuz.Settings { + public class ConstEncryptionSettingsFacade + { + public int encryptionLevel; + public List ruleFiles; + } + [Serializable] public class ConstEncryptionSettings { @@ -16,5 +22,14 @@ namespace Obfuz.Settings [Tooltip("config xml files")] public string[] ruleFiles; + + public ConstEncryptionSettingsFacade ToFacade() + { + return new ConstEncryptionSettingsFacade + { + ruleFiles = ruleFiles.ToList(), + encryptionLevel = encryptionLevel, + }; + } } } diff --git a/com.code-philosophy.obfuz/Editor/Settings/FieldEncryptionSettings.cs b/com.code-philosophy.obfuz/Editor/Settings/FieldEncryptionSettings.cs index 0b41afe..f17b05d 100644 --- a/com.code-philosophy.obfuz/Editor/Settings/FieldEncryptionSettings.cs +++ b/com.code-philosophy.obfuz/Editor/Settings/FieldEncryptionSettings.cs @@ -7,6 +7,12 @@ using UnityEngine; namespace Obfuz.Settings { + public class FieldEncryptionSettingsFacade + { + public int encryptionLevel; + public List ruleFiles; + } + [Serializable] public class FieldEncryptionSettings { @@ -16,5 +22,14 @@ namespace Obfuz.Settings [Tooltip("rule config xml files")] public string[] ruleFiles; + + public FieldEncryptionSettingsFacade ToFacade() + { + return new FieldEncryptionSettingsFacade + { + ruleFiles = ruleFiles.ToList(), + encryptionLevel = encryptionLevel, + }; + } } } diff --git a/com.code-philosophy.obfuz/Editor/Settings/ObfuzSettings.cs b/com.code-philosophy.obfuz/Editor/Settings/ObfuzSettings.cs index cd8379d..8759996 100644 --- a/com.code-philosophy.obfuz/Editor/Settings/ObfuzSettings.cs +++ b/com.code-philosophy.obfuz/Editor/Settings/ObfuzSettings.cs @@ -8,8 +8,6 @@ using UnityEngine; namespace Obfuz.Settings { - - public class ObfuzSettings : ScriptableObject { [Tooltip("enable Obfuz")] diff --git a/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs b/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs index 6e1d765..a5f27e5 100644 --- a/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs +++ b/com.code-philosophy.obfuz/Editor/Settings/SymbolObfuscationSettings.cs @@ -7,6 +7,15 @@ using UnityEngine; namespace Obfuz.Settings { + public class SymbolObfuscationSettingsFacade + { + public bool debug; + public string obfuscatedNamePrefix; + public bool useConsistentNamespaceObfuscation; + public string symbolMappingFile; + public List ruleFiles; + } + [Serializable] public class SymbolObfuscationSettings { @@ -23,5 +32,17 @@ namespace Obfuz.Settings [Tooltip("rule files")] public string[] ruleFiles; + + public SymbolObfuscationSettingsFacade ToFacade() + { + return new SymbolObfuscationSettingsFacade + { + debug = debug, + obfuscatedNamePrefix = obfuscatedNamePrefix, + useConsistentNamespaceObfuscation = useConsistentNamespaceObfuscation, + symbolMappingFile = symbolMappingFile, + ruleFiles = ruleFiles.ToList(), + }; + } } } diff --git a/com.code-philosophy.obfuz/Editor/Unity/ObfuscationProcess.cs b/com.code-philosophy.obfuz/Editor/Unity/ObfuscationProcess.cs index 0d64bb0..573d3cd 100644 --- a/com.code-philosophy.obfuz/Editor/Unity/ObfuscationProcess.cs +++ b/com.code-philosophy.obfuz/Editor/Unity/ObfuscationProcess.cs @@ -98,13 +98,13 @@ namespace Obfuz.Unity }; obfuscatorBuilder.InsertTopPriorityAssemblySearchPaths(assemblySearchDirs); - ValidateReferences(stagingAreaTempManagedDllDir, new HashSet(obfuscatorBuilder.AssembliesToObfuscate), obfuscationRelativeAssemblyNames); + ValidateReferences(stagingAreaTempManagedDllDir, new HashSet(obfuscatorBuilder.CoreSettingsFacade.assembliesToObfuscate), obfuscationRelativeAssemblyNames); OnObfuscationBegin?.Invoke(new ObfuscationBeginEventArgs { scriptAssembliesPath = stagingAreaTempManagedDllDir, - obfuscatedScriptAssembliesPath = obfuscatorBuilder.ObfuscatedAssemblyOutputPath, + obfuscatedScriptAssembliesPath = obfuscatorBuilder.CoreSettingsFacade.obfuscatedAssemblyOutputPath, }); bool succ = false; @@ -115,7 +115,7 @@ namespace Obfuz.Unity foreach (var dllName in obfuscationRelativeAssemblyNames) { - string src = $"{obfuscatorBuilder.ObfuscatedAssemblyOutputPath}/{dllName}.dll"; + string src = $"{obfuscatorBuilder.CoreSettingsFacade.obfuscatedAssemblyOutputPath}/{dllName}.dll"; string dst = $"{stagingAreaTempManagedDllDir}/{dllName}.dll"; if (!File.Exists(src))