From c260cc2379382a487959a58331b5a36c4d743b23 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 21 May 2025 16:24:26 +0800 Subject: [PATCH] change: ObfuzIgnoreAttribute support ObfuzScope --- .../FieldEncrypt/ConfigurableEncryptPolicy.cs | 2 +- .../Policies/SystemRenamePolicy.cs | 10 ++-- .../Editor/ObfuscationMethodWhitelist.cs | 29 ++++----- .../Editor/Utils/MetaUtil.cs | 60 ++++++++++--------- .../Runtime/ObfuzIgnoreAttribute.cs | 20 ++----- 5 files changed, 52 insertions(+), 69 deletions(-) diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/ConfigurableEncryptPolicy.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/ConfigurableEncryptPolicy.cs index 15ce929..20ff574 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/ConfigurableEncryptPolicy.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/FieldEncrypt/ConfigurableEncryptPolicy.cs @@ -33,7 +33,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt { return true; } - if (MetaUtil.HasObfuzIgnoreAttribute(field) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(field.DeclaringType)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(field, field.DeclaringType, ObfuzScope.Field)) { return false; } diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs index de0854d..aac2747 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs @@ -12,7 +12,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies { return false; } - if (MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(typeDef)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(typeDef, typeDef.DeclaringType, ObfuzScope.TypeName)) { return false; } @@ -30,7 +30,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies return false; } - if (MetaUtil.HasObfuzIgnoreAttribute(methodDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(methodDef.DeclaringType)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(methodDef, methodDef.DeclaringType, ObfuzScope.MethodName)) { return false; } @@ -43,7 +43,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies { return false; } - if (MetaUtil.HasObfuzIgnoreAttribute(fieldDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(fieldDef.DeclaringType)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(fieldDef, fieldDef.DeclaringType, ObfuzScope.Field)) { return false; } @@ -60,7 +60,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies { return false; } - if (MetaUtil.HasObfuzIgnoreAttribute(propertyDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(propertyDef.DeclaringType)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(propertyDef, propertyDef.DeclaringType, ObfuzScope.PropertyName)) { return false; } @@ -73,7 +73,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies { return false; } - if (MetaUtil.HasObfuzIgnoreAttribute(eventDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(eventDef.DeclaringType)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(eventDef, eventDef.DeclaringType, ObfuzScope.EventName)) { return false; } diff --git a/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs b/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs index a712604..771221e 100644 --- a/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs +++ b/com.code-philosophy.obfuz/Editor/ObfuscationMethodWhitelist.cs @@ -11,15 +11,6 @@ namespace Obfuz { public class ObfuscationMethodWhitelist { - private bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope) - { - ObfuzScope? objScope = MetaUtil.GetObfuzIgnoreScope(obj); - if (objScope == null) - { - return false; - } - return (objScope & targetScope) != 0; - } public bool IsInWhiteList(ModuleDef module) { @@ -28,10 +19,10 @@ namespace Obfuz { return true; } - if (HasObfuzIgnoreScope(module, ObfuzScope.Self)) - { - return true; - } + //if (MetaUtil.HasObfuzIgnoreScope(module)) + //{ + // return true; + //} return false; } @@ -45,7 +36,7 @@ namespace Obfuz { return true; } - if (HasObfuzIgnoreScope(method, ObfuzScope.Self)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(method, method.DeclaringType, ObfuzScope.MethodBody)) { return true; } @@ -62,14 +53,14 @@ namespace Obfuz { return true; } - if (MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(type)) - { - return true; - } - if (type.DeclaringType != null && IsInWhiteList(type.DeclaringType)) + if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(type, type.DeclaringType, ObfuzScope.TypeName)) { return true; } + //if (type.DeclaringType != null && IsInWhiteList(type.DeclaringType)) + //{ + // return true; + //} if (type.FullName == "Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine") { return true; diff --git a/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs b/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs index 154f714..c94e0c3 100644 --- a/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs +++ b/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs @@ -790,28 +790,46 @@ namespace Obfuz.Utils return scope; } - public static ObfuzScope? GetObfuzIgnoreScopeOfSelfOrDeclaringType(TypeDef typeDef) + public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef) { TypeDef cur = typeDef; - while (true) + while (cur != null) { - ObfuzScope? scope = GetObfuzIgnoreScope(cur); - if (scope != null) + var ca = cur.CustomAttributes?.FirstOrDefault(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); + if (ca != null) { - return scope; + var scope = (ObfuzScope)ca.ConstructorArguments[0].Value; + CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("InheritByNestedTypes", false); + bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value; + return cur == typeDef || inheritByNestedTypes ? scope : null; } cur = cur.DeclaringType; - if (cur == null) - { - return null; - } } + return null; } - //public static bool HasObfuzIgnoreAttribute(IHasCustomAttribute obj) - //{ - // return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); - //} + + public static bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope) + { + ObfuzScope? objScope = GetObfuzIgnoreScope(obj); + if (objScope == null) + { + return false; + } + return objScope != null && (objScope & targetScope) != 0; + } + + public static bool HasSelfOrInheritObfuzIgnoreScope(IHasCustomAttribute obj, TypeDef declaringType, ObfuzScope targetScope) + { + ObfuzScope? objScope = GetObfuzIgnoreScope(obj); + if (objScope != null) + { + return (objScope & targetScope) != 0; + } + + ObfuzScope? parentScope = GetSelfOrInheritObfuzIgnoreScope(declaringType); + return parentScope != null && (parentScope & targetScope) != 0; + } public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj) { @@ -822,21 +840,5 @@ namespace Obfuz.Utils { return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.EncryptFieldAttribute"); } - - //public static bool HasObfuzIgnoreAttributeInSelfOrParent(TypeDef typeDef) - //{ - // while (true) - // { - // if (HasObfuzIgnoreAttribute(typeDef)) - // { - // return true; - // } - // typeDef = typeDef.DeclaringType; - // if (typeDef == null) - // { - // return false; - // } - // } - //} } } diff --git a/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs b/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs index 4736ce2..08278ba 100644 --- a/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs +++ b/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs @@ -9,7 +9,7 @@ namespace Obfuz public enum ObfuzScope { None = 0x0, - Self = 0x1, + TypeName = 0x1, Field = 0x2, MethodName = 0x4, MethodParameter = 0x8, @@ -24,20 +24,8 @@ namespace Obfuz EventRemove = 0x400, EventFire = 0x800, Event = EventName | EventAdd | EventRemove, - - NestedTypeSelf = 0x1000, - NestedTypeField = 0x2000, - NestedTypeMethod = 0x4000, - NestedTypeProperty = 0x8000, - NestedTypeEvent = 0x10000, - - NestedTypeAll = NestedTypeSelf | NestedTypeField | NestedTypeMethod | NestedTypeProperty | NestedTypeEvent, - - Member = Field | Method | Property | Event, - MemberAndNestedTypeSelf = Member | NestedTypeSelf, - MemberAndNestedTypeAll = Member | NestedTypeAll, - SelfAndNestedTypeSelf = Self | NestedTypeSelf, - All = Self | MemberAndNestedTypeAll, + Module = 0x1000, + All = TypeName | Field | Method | Property | Event, } [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] @@ -45,6 +33,8 @@ namespace Obfuz { public ObfuzScope Scope { get; set; } + public bool InheritByNestedTypes { get; set; } = true; + public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All) { this.Scope = scope;