修复ObfuzIgnoreAttribute实现的bug

before-split
walon 2025-05-26 19:22:55 +08:00
parent ad258e9c84
commit 78699d7959
3 changed files with 151 additions and 37 deletions

View File

@ -5,14 +5,24 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
public class SystemRenamePolicy : ObfuscationPolicyBase public class SystemRenamePolicy : ObfuscationPolicyBase
{ {
private bool IsFullIgnoreObfuscatedType(TypeDef typeDef)
{
return typeDef.FullName == "Obfuz.ObfuzIgnoreAttribute" || typeDef.FullName == "Obfuz.ObfuzScope";
}
public override bool NeedRename(TypeDef typeDef) public override bool NeedRename(TypeDef typeDef)
{ {
string name = typeDef.Name; string name = typeDef.Name;
if (name == "<Module>" || name == "ObfuzIgnoreAttribute") if (name == "<Module>")
{ {
return false; return false;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(typeDef, typeDef.DeclaringType, ObfuzScope.TypeName)) if (IsFullIgnoreObfuscatedType(typeDef))
{
return false;
}
if (MetaUtil.HasObfuzIgnoreScope(typeDef, ObfuzScope.TypeName) || MetaUtil.HasEnclosingObfuzIgnoreScope(typeDef.DeclaringType, ObfuzScope.TypeName))
{ {
return false; return false;
} }
@ -21,7 +31,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
public override bool NeedRename(MethodDef methodDef) public override bool NeedRename(MethodDef methodDef)
{ {
if (methodDef.DeclaringType.IsDelegate) if (methodDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(methodDef.DeclaringType))
{ {
return false; return false;
} }
@ -30,7 +40,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
return false; return false;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(methodDef, methodDef.DeclaringType, ObfuzScope.MethodName)) if (MetaUtil.HasSelfOrInheritPropertyOrEventOrOrTypeDefIgnoreMethodName(methodDef))
{ {
return false; return false;
} }
@ -39,7 +49,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
public override bool NeedRename(FieldDef fieldDef) public override bool NeedRename(FieldDef fieldDef)
{ {
if (fieldDef.DeclaringType.IsDelegate) if (fieldDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(fieldDef.DeclaringType))
{ {
return false; return false;
} }
@ -56,7 +66,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
public override bool NeedRename(PropertyDef propertyDef) public override bool NeedRename(PropertyDef propertyDef)
{ {
if (propertyDef.DeclaringType.IsDelegate) if (propertyDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(propertyDef.DeclaringType))
{ {
return false; return false;
} }
@ -69,7 +79,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
public override bool NeedRename(EventDef eventDef) public override bool NeedRename(EventDef eventDef)
{ {
if (eventDef.DeclaringType.IsDelegate) if (eventDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(eventDef.DeclaringType))
{ {
return false; return false;
} }

View File

@ -816,23 +816,23 @@ namespace Obfuz.Utils
return scope; return scope;
} }
public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef) //public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef)
{ //{
TypeDef cur = typeDef; // TypeDef cur = typeDef;
while (cur != null) // while (cur != null)
{ // {
var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); // var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
if (ca != null) // if (ca != null)
{ // {
var scope = (ObfuzScope)ca.ConstructorArguments[0].Value; // var scope = (ObfuzScope)ca.ConstructorArguments[0].Value;
CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToMembers", false); // CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToMembers", false);
bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value; // bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value;
return cur == typeDef || inheritByNestedTypes ? (ObfuzScope?) scope : null; // return inheritByNestedTypes ? (ObfuzScope?) scope : null;
} // }
cur = cur.DeclaringType; // cur = cur.DeclaringType;
} // }
return null; // return null;
} //}
public static bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope) public static bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope)
@ -845,16 +845,121 @@ namespace Obfuz.Utils
return objScope != null && (objScope & targetScope) != 0; return objScope != null && (objScope & targetScope) != 0;
} }
public static bool HasEnclosingObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope)
{
TypeDef cur = typeDef;
while (cur != null)
{
var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
if (ca != null)
{
var scope = (ObfuzScope)ca.ConstructorArguments[0].Value;
CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToNestedTypes", false);
bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value;
return inheritByNestedTypes && (scope & targetScope) != 0;
}
cur = cur.DeclaringType;
}
return false;
}
public static bool HasDeclaringOrEnclosingMemberObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope)
{
TypeDef cur = typeDef;
while (cur != null)
{
var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
if (ca != null)
{
var scope = (ObfuzScope)ca.ConstructorArguments[0].Value;
if (cur != typeDef)
{
CANamedArgument applyToNestedTypesArg = ca.GetNamedArgument(cur == typeDef ? "ApplyToMembers" : "ApplyToNestedTypes", false);
if (applyToNestedTypesArg != null && !(bool)applyToNestedTypesArg.Value)
{
return false;
}
}
CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToMembers", false);
bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value;
return inheritByNestedTypes && (scope & targetScope) != 0;
}
cur = cur.DeclaringType;
}
return false;
}
//public static bool HasSelfOrInheritObfuzIgnoreScope(TypeDef obj, ObfuzScope targetScope)
//{
// ObfuzScope? scope = GetSelfOrInheritObfuzIgnoreScope(obj);
// return scope != null && (scope & targetScope) != 0;
//}
public static bool HasSelfOrInheritObfuzIgnoreScope(IHasCustomAttribute obj, TypeDef declaringType, ObfuzScope targetScope) public static bool HasSelfOrInheritObfuzIgnoreScope(IHasCustomAttribute obj, TypeDef declaringType, ObfuzScope targetScope)
{ {
ObfuzScope? objScope = GetObfuzIgnoreScope(obj); return HasObfuzIgnoreScope(obj, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
if (objScope != null) }
public static bool HasSelfOrInheritPropertyOrEventOrOrTypeDefObfuzIgnoreScope(MethodDef obj, ObfuzScope targetScope)
{
if (HasObfuzIgnoreScope(obj, targetScope))
{ {
return (objScope & targetScope) != 0; return true;
} }
ObfuzScope? parentScope = GetSelfOrInheritObfuzIgnoreScope(declaringType); TypeDef declaringType = obj.DeclaringType;
return parentScope != null && (parentScope & targetScope) != 0;
foreach (var propertyDef in declaringType.Properties)
{
if (propertyDef.GetMethod == obj || propertyDef.SetMethod == obj)
{
return HasObfuzIgnoreScope(propertyDef, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
}
}
foreach (var eventDef in declaringType.Events)
{
if (eventDef.AddMethod == obj || eventDef.RemoveMethod == obj)
{
ObfuzScope? eventScope = GetObfuzIgnoreScope(eventDef);
if (eventScope != null && (eventScope & targetScope) != 0)
{
return true;
}
return HasObfuzIgnoreScope(eventDef, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
}
}
return HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
}
public static bool HasSelfOrInheritPropertyOrEventOrOrTypeDefIgnoreMethodName(MethodDef obj)
{
if (HasObfuzIgnoreScope(obj, ObfuzScope.MethodName))
{
return true;
}
TypeDef declaringType = obj.DeclaringType;
foreach (var propertyDef in declaringType.Properties)
{
if (propertyDef.GetMethod == obj || propertyDef.SetMethod == obj)
{
return HasSelfOrInheritObfuzIgnoreScope(propertyDef, declaringType, ObfuzScope.PropertyGetterSetterName | ObfuzScope.MethodName);
}
}
foreach (var eventDef in declaringType.Events)
{
if (eventDef.AddMethod == obj || eventDef.RemoveMethod == obj)
{
return HasSelfOrInheritObfuzIgnoreScope(eventDef, declaringType, ObfuzScope.EventAddRemoveFireName | ObfuzScope.MethodName);
}
}
return HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, ObfuzScope.MethodName);
} }
public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj) public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj)

View File

@ -16,15 +16,12 @@ namespace Obfuz
MethodParameter = 0x8, MethodParameter = 0x8,
MethodBody = 0x10, MethodBody = 0x10,
Method = MethodName | MethodParameter | MethodBody, Method = MethodName | MethodParameter | MethodBody,
PropertyName = 020, PropertyName = 0x20,
PropertyGetter = 0x40, PropertyGetterSetterName = 0x40,
PropertySetter = 0x80, Property = PropertyName | PropertyGetterSetterName,
Property = PropertyName | PropertyGetter | PropertySetter,
EventName = 0x100, EventName = 0x100,
EventAdd = 0x200, EventAddRemoveFireName = 0x200,
EventRemove = 0x400, Event = EventName | PropertyGetterSetterName,
EventFire = 0x800,
Event = EventName | EventAdd | EventRemove,
Module = 0x1000, Module = 0x1000,
All = TypeName | Field | Method | Property | Event, All = TypeName | Field | Method | Property | Event,
} }
@ -36,6 +33,8 @@ namespace Obfuz
public bool ApplyToMembers { get; set; } = true; public bool ApplyToMembers { get; set; } = true;
public bool ApplyToNestedTypes { get; set; } = true;
public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All) public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All)
{ {
this.Scope = scope; this.Scope = scope;