change: ObfuzIgnoreAttribute support ObfuzScope
parent
d258738262
commit
c260cc2379
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
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 HasObfuzIgnoreAttribute(IHasCustomAttribute obj)
|
||||
//{
|
||||
// return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
|
||||
//}
|
||||
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;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue