支持custom rename policy
parent
ceb92fba40
commit
620d695880
|
@ -33,6 +33,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
private readonly Dictionary<ModuleDef, List<CustomAttributeInfo>> _customAttributeArgumentsWithTypeByMods = new Dictionary<ModuleDef, List<CustomAttributeInfo>>();
|
private readonly Dictionary<ModuleDef, List<CustomAttributeInfo>> _customAttributeArgumentsWithTypeByMods = new Dictionary<ModuleDef, List<CustomAttributeInfo>>();
|
||||||
private readonly RenameRecordMap _renameRecordMap;
|
private readonly RenameRecordMap _renameRecordMap;
|
||||||
private readonly VirtualMethodGroupCalculator _virtualMethodGroupCalculator;
|
private readonly VirtualMethodGroupCalculator _virtualMethodGroupCalculator;
|
||||||
|
private readonly List<IObfuscationPolicy> _customPolicies = new List<IObfuscationPolicy>();
|
||||||
|
|
||||||
class CustomAttributeInfo
|
class CustomAttributeInfo
|
||||||
{
|
{
|
||||||
|
@ -50,6 +51,18 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
_renameRecordMap = new RenameRecordMap(settings.debug ? null : settings.symbolMappingFile);
|
_renameRecordMap = new RenameRecordMap(settings.debug ? null : settings.symbolMappingFile);
|
||||||
_virtualMethodGroupCalculator = new VirtualMethodGroupCalculator();
|
_virtualMethodGroupCalculator = new VirtualMethodGroupCalculator();
|
||||||
_nameMaker = settings.debug ? NameMakerFactory.CreateDebugNameMaker() : NameMakerFactory.CreateNameMakerBaseASCIICharSet(settings.obfuscatedNamePrefix);
|
_nameMaker = settings.debug ? NameMakerFactory.CreateDebugNameMaker() : NameMakerFactory.CreateNameMakerBaseASCIICharSet(settings.obfuscatedNamePrefix);
|
||||||
|
|
||||||
|
foreach (var customPolicyType in settings.customRenamePolicyTypes)
|
||||||
|
{
|
||||||
|
if (Activator.CreateInstance(customPolicyType, new object[] { this }) is IObfuscationPolicy customPolicy)
|
||||||
|
{
|
||||||
|
_customPolicies.Add(customPolicy);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogWarning($"Custom rename policy type {customPolicyType} is not a valid IObfuscationPolicy");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init()
|
public void Init()
|
||||||
|
@ -59,8 +72,18 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
_toObfuscatedModules = ctx.modulesToObfuscate;
|
_toObfuscatedModules = ctx.modulesToObfuscate;
|
||||||
_obfuscatedAndNotObfuscatedModules = ctx.allObfuscationRelativeModules;
|
_obfuscatedAndNotObfuscatedModules = ctx.allObfuscationRelativeModules;
|
||||||
_toObfuscatedModuleSet = new HashSet<ModuleDef>(ctx.modulesToObfuscate);
|
_toObfuscatedModuleSet = new HashSet<ModuleDef>(ctx.modulesToObfuscate);
|
||||||
|
|
||||||
var obfuscateRuleConfig = new ConfigurableRenamePolicy(ctx.coreSettings.assembliesToObfuscate, ctx.modulesToObfuscate, _obfuscationRuleFiles);
|
var obfuscateRuleConfig = new ConfigurableRenamePolicy(ctx.coreSettings.assembliesToObfuscate, ctx.modulesToObfuscate, _obfuscationRuleFiles);
|
||||||
_renamePolicy = new CacheRenamePolicy(new CombineRenamePolicy(new SupportPassPolicy(ctx.passPolicy), new SystemRenamePolicy(), new UnityRenamePolicy(), obfuscateRuleConfig));
|
var totalRenamePolicies = new List<IObfuscationPolicy>
|
||||||
|
{
|
||||||
|
new SupportPassPolicy(ctx.passPolicy),
|
||||||
|
new SystemRenamePolicy(),
|
||||||
|
new UnityRenamePolicy(),
|
||||||
|
obfuscateRuleConfig,
|
||||||
|
};
|
||||||
|
totalRenamePolicies.AddRange(_customPolicies);
|
||||||
|
|
||||||
|
_renamePolicy = new CacheRenamePolicy(new CombineRenamePolicy(totalRenamePolicies.ToArray()));
|
||||||
BuildCustomAttributeArguments();
|
BuildCustomAttributeArguments();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,10 +128,7 @@ namespace Obfuz
|
||||||
}
|
}
|
||||||
var vms = new VirtualMachineSimulator(vm, secretKey);
|
var vms = new VirtualMachineSimulator(vm, secretKey);
|
||||||
|
|
||||||
var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies()
|
var generatedVmTypes = ReflectionUtil.FindTypesInCurrentAppDomain("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine");
|
||||||
.Select(assembly => assembly.GetType("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine"))
|
|
||||||
.Where(type => type != null)
|
|
||||||
.ToList();
|
|
||||||
if (generatedVmTypes.Count == 0)
|
if (generatedVmTypes.Count == 0)
|
||||||
{
|
{
|
||||||
throw new Exception($"class Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine not found in any assembly! Please run `Obfuz/GenerateVm` to generate it!");
|
throw new Exception($"class Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine not found in any assembly! Please run `Obfuz/GenerateVm` to generate it!");
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
using System;
|
using Obfuz.ObfusPasses.SymbolObfus;
|
||||||
|
using Obfuz.Utils;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
@ -14,6 +16,7 @@ namespace Obfuz.Settings
|
||||||
public bool useConsistentNamespaceObfuscation;
|
public bool useConsistentNamespaceObfuscation;
|
||||||
public string symbolMappingFile;
|
public string symbolMappingFile;
|
||||||
public List<string> ruleFiles;
|
public List<string> ruleFiles;
|
||||||
|
public List<Type> customRenamePolicyTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
|
@ -33,6 +36,9 @@ namespace Obfuz.Settings
|
||||||
[Tooltip("rule files")]
|
[Tooltip("rule files")]
|
||||||
public string[] ruleFiles;
|
public string[] ruleFiles;
|
||||||
|
|
||||||
|
[Tooltip("custom rename policy types")]
|
||||||
|
public string[] customRenamePolicyTypes;
|
||||||
|
|
||||||
public SymbolObfuscationSettingsFacade ToFacade()
|
public SymbolObfuscationSettingsFacade ToFacade()
|
||||||
{
|
{
|
||||||
return new SymbolObfuscationSettingsFacade
|
return new SymbolObfuscationSettingsFacade
|
||||||
|
@ -42,6 +48,7 @@ namespace Obfuz.Settings
|
||||||
useConsistentNamespaceObfuscation = useConsistentNamespaceObfuscation,
|
useConsistentNamespaceObfuscation = useConsistentNamespaceObfuscation,
|
||||||
symbolMappingFile = symbolMappingFile,
|
symbolMappingFile = symbolMappingFile,
|
||||||
ruleFiles = ruleFiles.ToList(),
|
ruleFiles = ruleFiles.ToList(),
|
||||||
|
customRenamePolicyTypes = customRenamePolicyTypes.Select(typeName => ReflectionUtil.FindUniqueTypeInCurrentAppDomain(typeName)).ToList(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Obfuz.Utils
|
||||||
|
{
|
||||||
|
public static class ReflectionUtil
|
||||||
|
{
|
||||||
|
public static List<Type> FindTypesInCurrentAppDomain(string fullName)
|
||||||
|
{
|
||||||
|
return AppDomain.CurrentDomain.GetAssemblies()
|
||||||
|
.Select(assembly => assembly.GetType(fullName))
|
||||||
|
.Where(type => type != null)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Type FindUniqueTypeInCurrentAppDomain(string fullName)
|
||||||
|
{
|
||||||
|
var foundTypes = FindTypesInCurrentAppDomain(fullName);
|
||||||
|
if (foundTypes.Count == 0)
|
||||||
|
{
|
||||||
|
throw new Exception($"class {fullName} not found in any assembly!");
|
||||||
|
}
|
||||||
|
if (foundTypes.Count > 1)
|
||||||
|
{
|
||||||
|
throw new Exception($"class {fullName} found in multiple assemblies! Please retain only one!");
|
||||||
|
}
|
||||||
|
return foundTypes[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6fac8216afeffb746b1b67d1f16883b8
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue