change: ObfuzIgnoreAttribute support ObfuzScope

before-split
walon 2025-05-21 16:24:26 +08:00
parent d258738262
commit c260cc2379
5 changed files with 52 additions and 69 deletions

View File

@ -33,7 +33,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
{ {
return true; return true;
} }
if (MetaUtil.HasObfuzIgnoreAttribute(field) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(field.DeclaringType)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(field, field.DeclaringType, ObfuzScope.Field))
{ {
return false; return false;
} }

View File

@ -12,7 +12,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
return false; return false;
} }
if (MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(typeDef)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(typeDef, typeDef.DeclaringType, ObfuzScope.TypeName))
{ {
return false; return false;
} }
@ -30,7 +30,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
return false; return false;
} }
if (MetaUtil.HasObfuzIgnoreAttribute(methodDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(methodDef.DeclaringType)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(methodDef, methodDef.DeclaringType, ObfuzScope.MethodName))
{ {
return false; return false;
} }
@ -43,7 +43,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
return false; return false;
} }
if (MetaUtil.HasObfuzIgnoreAttribute(fieldDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(fieldDef.DeclaringType)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(fieldDef, fieldDef.DeclaringType, ObfuzScope.Field))
{ {
return false; return false;
} }
@ -60,7 +60,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
return false; return false;
} }
if (MetaUtil.HasObfuzIgnoreAttribute(propertyDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(propertyDef.DeclaringType)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(propertyDef, propertyDef.DeclaringType, ObfuzScope.PropertyName))
{ {
return false; return false;
} }
@ -73,7 +73,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
return false; return false;
} }
if (MetaUtil.HasObfuzIgnoreAttribute(eventDef) || MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(eventDef.DeclaringType)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(eventDef, eventDef.DeclaringType, ObfuzScope.EventName))
{ {
return false; return false;
} }

View File

@ -11,15 +11,6 @@ namespace Obfuz
{ {
public class ObfuscationMethodWhitelist 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) public bool IsInWhiteList(ModuleDef module)
{ {
@ -28,10 +19,10 @@ namespace Obfuz
{ {
return true; return true;
} }
if (HasObfuzIgnoreScope(module, ObfuzScope.Self)) //if (MetaUtil.HasObfuzIgnoreScope(module))
{ //{
return true; // return true;
} //}
return false; return false;
} }
@ -45,7 +36,7 @@ namespace Obfuz
{ {
return true; return true;
} }
if (HasObfuzIgnoreScope(method, ObfuzScope.Self)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(method, method.DeclaringType, ObfuzScope.MethodBody))
{ {
return true; return true;
} }
@ -62,14 +53,14 @@ namespace Obfuz
{ {
return true; return true;
} }
if (MetaUtil.HasObfuzIgnoreAttributeInSelfOrParent(type)) if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(type, type.DeclaringType, ObfuzScope.TypeName))
{
return true;
}
if (type.DeclaringType != null && IsInWhiteList(type.DeclaringType))
{ {
return true; return true;
} }
//if (type.DeclaringType != null && IsInWhiteList(type.DeclaringType))
//{
// return true;
//}
if (type.FullName == "Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine") if (type.FullName == "Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine")
{ {
return true; return true;

View File

@ -790,28 +790,46 @@ namespace Obfuz.Utils
return scope; return scope;
} }
public static ObfuzScope? GetObfuzIgnoreScopeOfSelfOrDeclaringType(TypeDef typeDef) public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef)
{ {
TypeDef cur = typeDef; TypeDef cur = typeDef;
while (true) while (cur != null)
{ {
ObfuzScope? scope = GetObfuzIgnoreScope(cur); var ca = cur.CustomAttributes?.FirstOrDefault(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
if (scope != null) 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; cur = cur.DeclaringType;
if (cur == null)
{
return null;
}
} }
return null;
} }
//public static bool HasObfuzIgnoreAttribute(IHasCustomAttribute obj)
//{ public static bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope)
// return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); {
//} 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) public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj)
{ {
@ -822,21 +840,5 @@ namespace Obfuz.Utils
{ {
return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.EncryptFieldAttribute"); 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;
// }
// }
//}
} }
} }

View File

@ -9,7 +9,7 @@ namespace Obfuz
public enum ObfuzScope public enum ObfuzScope
{ {
None = 0x0, None = 0x0,
Self = 0x1, TypeName = 0x1,
Field = 0x2, Field = 0x2,
MethodName = 0x4, MethodName = 0x4,
MethodParameter = 0x8, MethodParameter = 0x8,
@ -24,20 +24,8 @@ namespace Obfuz
EventRemove = 0x400, EventRemove = 0x400,
EventFire = 0x800, EventFire = 0x800,
Event = EventName | EventAdd | EventRemove, Event = EventName | EventAdd | EventRemove,
Module = 0x1000,
NestedTypeSelf = 0x1000, All = TypeName | Field | Method | Property | Event,
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,
} }
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
@ -45,6 +33,8 @@ namespace Obfuz
{ {
public ObfuzScope Scope { get; set; } public ObfuzScope Scope { get; set; }
public bool InheritByNestedTypes { get; set; } = true;
public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All) public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All)
{ {
this.Scope = scope; this.Scope = scope;