before-split
walon 2025-05-21 13:33:03 +08:00
parent 10eef16d78
commit d258738262
3 changed files with 95 additions and 21 deletions

View File

@ -11,9 +11,14 @@ namespace Obfuz
{ {
public class ObfuscationMethodWhitelist public class ObfuscationMethodWhitelist
{ {
private bool ShouldBeIgnoredByCustomAttribute(IHasCustomAttribute obj) private bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope)
{ {
return MetaUtil.HasObfuzIgnoreAttribute(obj); ObfuzScope? objScope = MetaUtil.GetObfuzIgnoreScope(obj);
if (objScope == null)
{
return false;
}
return (objScope & targetScope) != 0;
} }
public bool IsInWhiteList(ModuleDef module) public bool IsInWhiteList(ModuleDef module)
@ -23,7 +28,7 @@ namespace Obfuz
{ {
return true; return true;
} }
if (ShouldBeIgnoredByCustomAttribute(module)) if (HasObfuzIgnoreScope(module, ObfuzScope.Self))
{ {
return true; return true;
} }
@ -40,7 +45,7 @@ namespace Obfuz
{ {
return true; return true;
} }
if (ShouldBeIgnoredByCustomAttribute(method)) if (HasObfuzIgnoreScope(method, ObfuzScope.Self))
{ {
return true; return true;
} }

View File

@ -779,10 +779,39 @@ namespace Obfuz.Utils
return result.ToString(); return result.ToString();
} }
public static bool HasObfuzIgnoreAttribute(IHasCustomAttribute obj) public static ObfuzScope? GetObfuzIgnoreScope(IHasCustomAttribute obj)
{ {
return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); var ca = obj.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
if (ca == null)
{
return null;
} }
var scope = (ObfuzScope)ca.ConstructorArguments[0].Value;
return scope;
}
public static ObfuzScope? GetObfuzIgnoreScopeOfSelfOrDeclaringType(TypeDef typeDef)
{
TypeDef cur = typeDef;
while (true)
{
ObfuzScope? scope = GetObfuzIgnoreScope(cur);
if (scope != null)
{
return scope;
}
cur = cur.DeclaringType;
if (cur == null)
{
return null;
}
}
}
//public static bool HasObfuzIgnoreAttribute(IHasCustomAttribute obj)
//{
// return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
//}
public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj) public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj)
{ {
@ -794,20 +823,20 @@ 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) //public static bool HasObfuzIgnoreAttributeInSelfOrParent(TypeDef typeDef)
{ //{
while (true) // while (true)
{ // {
if (HasObfuzIgnoreAttribute(typeDef)) // if (HasObfuzIgnoreAttribute(typeDef))
{ // {
return true; // return true;
} // }
typeDef = typeDef.DeclaringType; // typeDef = typeDef.DeclaringType;
if (typeDef == null) // if (typeDef == null)
{ // {
return false; // return false;
} // }
} // }
} //}
} }
} }

View File

@ -6,8 +6,48 @@ using System.Threading.Tasks;
namespace Obfuz namespace Obfuz
{ {
public enum ObfuzScope
{
None = 0x0,
Self = 0x1,
Field = 0x2,
MethodName = 0x4,
MethodParameter = 0x8,
MethodBody = 0x10,
Method = MethodName | MethodParameter | MethodBody,
PropertyName = 020,
PropertyGetter = 0x40,
PropertySetter = 0x80,
Property = PropertyName | PropertyGetter | PropertySetter,
EventName = 0x100,
EventAdd = 0x200,
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,
}
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
public class ObfuzIgnoreAttribute : Attribute public class ObfuzIgnoreAttribute : Attribute
{ {
public ObfuzScope Scope { get; set; }
public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All)
{
this.Scope = scope;
}
} }
} }