修复ObfuzIgnoreAttribute实现的bug
parent
ad258e9c84
commit
78699d7959
|
@ -5,14 +5,24 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
|||
{
|
||||
public class SystemRenamePolicy : ObfuscationPolicyBase
|
||||
{
|
||||
private bool IsFullIgnoreObfuscatedType(TypeDef typeDef)
|
||||
{
|
||||
return typeDef.FullName == "Obfuz.ObfuzIgnoreAttribute" || typeDef.FullName == "Obfuz.ObfuzScope";
|
||||
}
|
||||
|
||||
public override bool NeedRename(TypeDef typeDef)
|
||||
{
|
||||
string name = typeDef.Name;
|
||||
if (name == "<Module>" || name == "ObfuzIgnoreAttribute")
|
||||
if (name == "<Module>")
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
@ -21,7 +31,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
|||
|
||||
public override bool NeedRename(MethodDef methodDef)
|
||||
{
|
||||
if (methodDef.DeclaringType.IsDelegate)
|
||||
if (methodDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(methodDef.DeclaringType))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -30,7 +40,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
|||
return false;
|
||||
}
|
||||
|
||||
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(methodDef, methodDef.DeclaringType, ObfuzScope.MethodName))
|
||||
if (MetaUtil.HasSelfOrInheritPropertyOrEventOrOrTypeDefIgnoreMethodName(methodDef))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -39,7 +49,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
|||
|
||||
public override bool NeedRename(FieldDef fieldDef)
|
||||
{
|
||||
if (fieldDef.DeclaringType.IsDelegate)
|
||||
if (fieldDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(fieldDef.DeclaringType))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -56,7 +66,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
|||
|
||||
public override bool NeedRename(PropertyDef propertyDef)
|
||||
{
|
||||
if (propertyDef.DeclaringType.IsDelegate)
|
||||
if (propertyDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(propertyDef.DeclaringType))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -69,7 +79,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
|||
|
||||
public override bool NeedRename(EventDef eventDef)
|
||||
{
|
||||
if (eventDef.DeclaringType.IsDelegate)
|
||||
if (eventDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(eventDef.DeclaringType))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -816,23 +816,23 @@ namespace Obfuz.Utils
|
|||
return scope;
|
||||
}
|
||||
|
||||
public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef)
|
||||
{
|
||||
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("ApplyToMembers", false);
|
||||
bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value;
|
||||
return cur == typeDef || inheritByNestedTypes ? (ObfuzScope?) scope : null;
|
||||
}
|
||||
cur = cur.DeclaringType;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
//public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef)
|
||||
//{
|
||||
// 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("ApplyToMembers", false);
|
||||
// bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value;
|
||||
// return inheritByNestedTypes ? (ObfuzScope?) scope : null;
|
||||
// }
|
||||
// cur = cur.DeclaringType;
|
||||
// }
|
||||
// return null;
|
||||
//}
|
||||
|
||||
|
||||
public static bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope)
|
||||
|
@ -845,16 +845,121 @@ namespace Obfuz.Utils
|
|||
return objScope != null && (objScope & targetScope) != 0;
|
||||
}
|
||||
|
||||
public static bool HasSelfOrInheritObfuzIgnoreScope(IHasCustomAttribute obj, TypeDef declaringType, ObfuzScope targetScope)
|
||||
public static bool HasEnclosingObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope)
|
||||
{
|
||||
ObfuzScope? objScope = GetObfuzIgnoreScope(obj);
|
||||
if (objScope != null)
|
||||
TypeDef cur = typeDef;
|
||||
while (cur != null)
|
||||
{
|
||||
return (objScope & targetScope) != 0;
|
||||
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;
|
||||
}
|
||||
|
||||
ObfuzScope? parentScope = GetSelfOrInheritObfuzIgnoreScope(declaringType);
|
||||
return parentScope != null && (parentScope & targetScope) != 0;
|
||||
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)
|
||||
{
|
||||
return HasObfuzIgnoreScope(obj, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
|
||||
}
|
||||
|
||||
public static bool HasSelfOrInheritPropertyOrEventOrOrTypeDefObfuzIgnoreScope(MethodDef obj, ObfuzScope targetScope)
|
||||
{
|
||||
if (HasObfuzIgnoreScope(obj, targetScope))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
TypeDef declaringType = obj.DeclaringType;
|
||||
|
||||
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)
|
||||
|
|
|
@ -16,15 +16,12 @@ namespace Obfuz
|
|||
MethodParameter = 0x8,
|
||||
MethodBody = 0x10,
|
||||
Method = MethodName | MethodParameter | MethodBody,
|
||||
PropertyName = 020,
|
||||
PropertyGetter = 0x40,
|
||||
PropertySetter = 0x80,
|
||||
Property = PropertyName | PropertyGetter | PropertySetter,
|
||||
PropertyName = 0x20,
|
||||
PropertyGetterSetterName = 0x40,
|
||||
Property = PropertyName | PropertyGetterSetterName,
|
||||
EventName = 0x100,
|
||||
EventAdd = 0x200,
|
||||
EventRemove = 0x400,
|
||||
EventFire = 0x800,
|
||||
Event = EventName | EventAdd | EventRemove,
|
||||
EventAddRemoveFireName = 0x200,
|
||||
Event = EventName | PropertyGetterSetterName,
|
||||
Module = 0x1000,
|
||||
All = TypeName | Field | Method | Property | Event,
|
||||
}
|
||||
|
@ -36,6 +33,8 @@ namespace Obfuz
|
|||
|
||||
public bool ApplyToMembers { get; set; } = true;
|
||||
|
||||
public bool ApplyToNestedTypes { get; set; } = true;
|
||||
|
||||
public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All)
|
||||
{
|
||||
this.Scope = scope;
|
||||
|
|
Loading…
Reference in New Issue