feature: 新增 RemoveConstField pass
parent
1a240c47ac
commit
ddb144eac8
|
@ -16,6 +16,8 @@ namespace Obfuz.ObfusPasses
|
||||||
ControlFlowObfus = 0x800,
|
ControlFlowObfus = 0x800,
|
||||||
EvalStackObfus = 0x1000,
|
EvalStackObfus = 0x1000,
|
||||||
|
|
||||||
|
RemoveConstField = 0x100000,
|
||||||
|
|
||||||
AllObfus = SymbolObfus | CallObfus | ExprObfus | ControlFlowObfus | EvalStackObfus,
|
AllObfus = SymbolObfus | CallObfus | ExprObfus | ControlFlowObfus | EvalStackObfus,
|
||||||
AllEncrypt = ConstEncrypt | FieldEncrypt,
|
AllEncrypt = ConstEncrypt | FieldEncrypt,
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d2c28a04d2997bc4d91a4c7693983d12
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,35 @@
|
||||||
|
using dnlib.DotNet;
|
||||||
|
using Obfuz.Conf;
|
||||||
|
using Obfuz.Utils;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
|
namespace Obfuz.ObfusPasses.RemoveConstField
|
||||||
|
{
|
||||||
|
public class ConfigurableRemoveConstFieldPolicy : RemoveConstFieldBase
|
||||||
|
{
|
||||||
|
class ObfuscationRule
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly XmlFieldRuleParser<ObfuscationRule> _configParser;
|
||||||
|
|
||||||
|
public ConfigurableRemoveConstFieldPolicy(List<string> toObfuscatedAssemblyNames, List<string> configFiles)
|
||||||
|
{
|
||||||
|
_configParser = new XmlFieldRuleParser<ObfuscationRule>(toObfuscatedAssemblyNames, ParseRule, null);
|
||||||
|
_configParser.LoadConfigs(configFiles);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ObfuscationRule ParseRule(string configFile, XmlElement ele)
|
||||||
|
{
|
||||||
|
return new ObfuscationRule();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool NeedPreserved(FieldDef field)
|
||||||
|
{
|
||||||
|
var rule = _configParser.GetFieldRule(field);
|
||||||
|
return rule != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e3b708559fbf755419d7daf4ddce72f2
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,14 @@
|
||||||
|
using dnlib.DotNet;
|
||||||
|
|
||||||
|
namespace Obfuz.ObfusPasses.RemoveConstField
|
||||||
|
{
|
||||||
|
public interface IRemoveConstFieldPolicy
|
||||||
|
{
|
||||||
|
bool NeedPreserved(FieldDef field);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class RemoveConstFieldBase : IRemoveConstFieldPolicy
|
||||||
|
{
|
||||||
|
public abstract bool NeedPreserved(FieldDef field);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 5fad0b7ef9225b24cacc94f8dcaee26d
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -0,0 +1,76 @@
|
||||||
|
using dnlib.DotNet;
|
||||||
|
using dnlib.DotNet.Emit;
|
||||||
|
using Obfuz.Settings;
|
||||||
|
using Obfuz.Utils;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Obfuz.ObfusPasses.RemoveConstField
|
||||||
|
{
|
||||||
|
|
||||||
|
public class RemoveConstFieldPass : ObfuscationPassBase
|
||||||
|
{
|
||||||
|
private RemoveConstFieldSettingsFacade _settings;
|
||||||
|
private ObfuzIgnoreScopeComputeCache _obfuzIgnoreScopeComputeCache;
|
||||||
|
private IRemoveConstFieldPolicy _removeConstFieldPolicy;
|
||||||
|
|
||||||
|
public override ObfuscationPassType Type => ObfuscationPassType.RemoveConstField;
|
||||||
|
|
||||||
|
public RemoveConstFieldPass(RemoveConstFieldSettingsFacade settings)
|
||||||
|
{
|
||||||
|
_settings = settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Start()
|
||||||
|
{
|
||||||
|
var ctx = ObfuscationPassContext.Current;
|
||||||
|
_obfuzIgnoreScopeComputeCache = ctx.obfuzIgnoreScopeComputeCache;
|
||||||
|
_removeConstFieldPolicy = new ConfigurableRemoveConstFieldPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Stop()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Process()
|
||||||
|
{
|
||||||
|
var ctx = ObfuscationPassContext.Current;
|
||||||
|
var modules = ctx.modulesToObfuscate;
|
||||||
|
ConfigurablePassPolicy passPolicy = ctx.passPolicy;
|
||||||
|
foreach (ModuleDef mod in modules)
|
||||||
|
{
|
||||||
|
// ToArray to avoid modify list exception
|
||||||
|
foreach (TypeDef type in mod.GetTypes())
|
||||||
|
{
|
||||||
|
if (type.IsEnum)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
foreach (FieldDef field in type.Fields.ToArray())
|
||||||
|
{
|
||||||
|
if (!field.IsLiteral)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!Support(passPolicy.GetFieldObfuscationPasses(field)))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (_obfuzIgnoreScopeComputeCache.HasSelfOrDeclaringOrEnclosingOrInheritObfuzIgnoreScope(field, field.DeclaringType, ObfuzScope.Field))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (_removeConstFieldPolicy.NeedPreserved(field))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
field.DeclaringType = null;
|
||||||
|
//Debug.Log($"Remove const field {field.FullName} in type {type.FullName} in module {mod.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3188de094ab4cdd47b55c2f622251cf5
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -6,6 +6,7 @@ using Obfuz.ObfusPasses.ControlFlowObfus;
|
||||||
using Obfuz.ObfusPasses.EvalStackObfus;
|
using Obfuz.ObfusPasses.EvalStackObfus;
|
||||||
using Obfuz.ObfusPasses.ExprObfus;
|
using Obfuz.ObfusPasses.ExprObfus;
|
||||||
using Obfuz.ObfusPasses.FieldEncrypt;
|
using Obfuz.ObfusPasses.FieldEncrypt;
|
||||||
|
using Obfuz.ObfusPasses.RemoveConstField;
|
||||||
using Obfuz.ObfusPasses.SymbolObfus;
|
using Obfuz.ObfusPasses.SymbolObfus;
|
||||||
using Obfuz.Settings;
|
using Obfuz.Settings;
|
||||||
using Obfuz.Utils;
|
using Obfuz.Utils;
|
||||||
|
@ -177,6 +178,10 @@ namespace Obfuz
|
||||||
{
|
{
|
||||||
builder.AddPass(new ConstEncryptPass(settings.constEncryptSettings.ToFacade()));
|
builder.AddPass(new ConstEncryptPass(settings.constEncryptSettings.ToFacade()));
|
||||||
}
|
}
|
||||||
|
if (obfuscationPasses.HasFlag(ObfuscationPassType.RemoveConstField))
|
||||||
|
{
|
||||||
|
builder.AddPass(new RemoveConstFieldPass(settings.removeConstFieldSettings.ToFacade()));
|
||||||
|
}
|
||||||
if (obfuscationPasses.HasFlag(ObfuscationPassType.ExprObfus))
|
if (obfuscationPasses.HasFlag(ObfuscationPassType.ExprObfus))
|
||||||
{
|
{
|
||||||
builder.AddPass(new ExprObfusPass(settings.exprObfusSettings.ToFacade()));
|
builder.AddPass(new ExprObfusPass(settings.exprObfusSettings.ToFacade()));
|
||||||
|
|
|
@ -29,6 +29,9 @@ namespace Obfuz.Settings
|
||||||
[Tooltip("const encryption settings")]
|
[Tooltip("const encryption settings")]
|
||||||
public ConstEncryptionSettings constEncryptSettings;
|
public ConstEncryptionSettings constEncryptSettings;
|
||||||
|
|
||||||
|
[Tooltip("remove const field settings")]
|
||||||
|
public RemoveConstFieldSettings removeConstFieldSettings;
|
||||||
|
|
||||||
[Tooltip("eval stack obfuscation settings")]
|
[Tooltip("eval stack obfuscation settings")]
|
||||||
public EvalStackObfuscationSettings evalStackObfusSettings;
|
public EvalStackObfuscationSettings evalStackObfusSettings;
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ namespace Obfuz.Settings
|
||||||
|
|
||||||
private SerializedProperty _symbolObfusSettings;
|
private SerializedProperty _symbolObfusSettings;
|
||||||
private SerializedProperty _constEncryptSettings;
|
private SerializedProperty _constEncryptSettings;
|
||||||
|
private SerializedProperty _removeConstFieldSettings;
|
||||||
private SerializedProperty _evalStackObfusSettings;
|
private SerializedProperty _evalStackObfusSettings;
|
||||||
private SerializedProperty _fieldEncryptSettings;
|
private SerializedProperty _fieldEncryptSettings;
|
||||||
private SerializedProperty _callObfusSettings;
|
private SerializedProperty _callObfusSettings;
|
||||||
|
@ -73,6 +74,7 @@ namespace Obfuz.Settings
|
||||||
|
|
||||||
_symbolObfusSettings = _serializedObject.FindProperty("symbolObfusSettings");
|
_symbolObfusSettings = _serializedObject.FindProperty("symbolObfusSettings");
|
||||||
_constEncryptSettings = _serializedObject.FindProperty("constEncryptSettings");
|
_constEncryptSettings = _serializedObject.FindProperty("constEncryptSettings");
|
||||||
|
_removeConstFieldSettings = _serializedObject.FindProperty("removeConstFieldSettings");
|
||||||
_evalStackObfusSettings = _serializedObject.FindProperty("evalStackObfusSettings");
|
_evalStackObfusSettings = _serializedObject.FindProperty("evalStackObfusSettings");
|
||||||
_exprObfusSettings = _serializedObject.FindProperty("exprObfusSettings");
|
_exprObfusSettings = _serializedObject.FindProperty("exprObfusSettings");
|
||||||
_fieldEncryptSettings = _serializedObject.FindProperty("fieldEncryptSettings");
|
_fieldEncryptSettings = _serializedObject.FindProperty("fieldEncryptSettings");
|
||||||
|
@ -103,6 +105,7 @@ namespace Obfuz.Settings
|
||||||
|
|
||||||
EditorGUILayout.PropertyField(_symbolObfusSettings);
|
EditorGUILayout.PropertyField(_symbolObfusSettings);
|
||||||
EditorGUILayout.PropertyField(_constEncryptSettings);
|
EditorGUILayout.PropertyField(_constEncryptSettings);
|
||||||
|
EditorGUILayout.PropertyField(_removeConstFieldSettings);
|
||||||
EditorGUILayout.PropertyField(_evalStackObfusSettings);
|
EditorGUILayout.PropertyField(_evalStackObfusSettings);
|
||||||
EditorGUILayout.PropertyField(_exprObfusSettings);
|
EditorGUILayout.PropertyField(_exprObfusSettings);
|
||||||
EditorGUILayout.PropertyField(_fieldEncryptSettings);
|
EditorGUILayout.PropertyField(_fieldEncryptSettings);
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Obfuz.Settings
|
||||||
|
{
|
||||||
|
public class RemoveConstFieldSettingsFacade
|
||||||
|
{
|
||||||
|
public List<string> ruleFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class RemoveConstFieldSettings
|
||||||
|
{
|
||||||
|
[Tooltip("rule config xml files")]
|
||||||
|
public string[] ruleFiles;
|
||||||
|
|
||||||
|
public RemoveConstFieldSettingsFacade ToFacade()
|
||||||
|
{
|
||||||
|
return new RemoveConstFieldSettingsFacade
|
||||||
|
{
|
||||||
|
ruleFiles = ruleFiles?.ToList() ?? new List<string>(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: a8b8eef10eaba5844a0ad39b3c79a51c
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
Reference in New Issue