添加ObfuzIgnoreAttribute

backup
walon 2025-05-13 11:37:07 +08:00
parent c17397eac0
commit abecc20a4a
5 changed files with 46 additions and 6 deletions

View File

@ -1,4 +1,5 @@
using dnlib.DotNet;
using Obfuz.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,6 +10,10 @@ namespace Obfuz
{
public class NotObfuscatedMethodWhiteList
{
private bool ShouldBeIgnoredByCustomAttribute(IHasCustomAttribute obj)
{
return MetaUtil.HasObfuzIgnoreAttribute(obj) || MetaUtil.HasCompilerGeneratedAttribute(obj);
}
public bool IsInWhiteList(ModuleDef module)
{
@ -17,6 +22,10 @@ namespace Obfuz
{
return true;
}
if (ShouldBeIgnoredByCustomAttribute(module))
{
return true;
}
return false;
}
@ -26,6 +35,10 @@ namespace Obfuz
{
return true;
}
if (ShouldBeIgnoredByCustomAttribute(method))
{
return true;
}
return false;
}
@ -39,6 +52,10 @@ namespace Obfuz
{
return true;
}
if (ShouldBeIgnoredByCustomAttribute(type))
{
return true;
}
if (type.FullName == "Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine")
{
return true;

View File

@ -25,7 +25,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
_encryptionLevel = settings.encryptionLevel;
}
protected override bool NeedProcessNotObfuscatedAssembly => true;
protected override bool ForceProcessAllAssembliesAndIgnoreAllPolicy => true;
public override void Start()
{

View File

@ -7,33 +7,33 @@ namespace Obfuz.ObfusPasses
{
public abstract class InstructionObfuscationPassBase : ObfuscationPassBase
{
protected virtual bool NeedProcessNotObfuscatedAssembly => false;
protected virtual bool ForceProcessAllAssembliesAndIgnoreAllPolicy => false;
protected abstract bool NeedObfuscateMethod(MethodDef method);
public override void Process()
{
var ctx = ObfuscationPassContext.Current;
var modules = NeedProcessNotObfuscatedAssembly ? ctx.obfuscatedAndNotObfuscatedModules : ctx.toObfuscatedModules;
var modules = ForceProcessAllAssembliesAndIgnoreAllPolicy ? ctx.obfuscatedAndNotObfuscatedModules : ctx.toObfuscatedModules;
NotObfuscatedMethodWhiteList whiteList = ctx.whiteList;
ConfigurablePassPolicy passPolicy = ctx.passPolicy;
foreach (ModuleDef mod in modules)
{
if (whiteList.IsInWhiteList(mod) || !Support(passPolicy.GetAssemblyObfuscationPasses(mod)))
if (!ForceProcessAllAssembliesAndIgnoreAllPolicy && (whiteList.IsInWhiteList(mod) || !Support(passPolicy.GetAssemblyObfuscationPasses(mod))))
{
continue;
}
// ToArray to avoid modify list exception
foreach (TypeDef type in mod.GetTypes().ToArray())
{
if (whiteList.IsInWhiteList(type) || !Support(passPolicy.GetTypeObfuscationPasses(type)))
if (!ForceProcessAllAssembliesAndIgnoreAllPolicy && (whiteList.IsInWhiteList(type) || !Support(passPolicy.GetTypeObfuscationPasses(type))))
{
continue;
}
// ToArray to avoid modify list exception
foreach (MethodDef method in type.Methods.ToArray())
{
if (!method.HasBody || ctx.whiteList.IsInWhiteList(method) || !Support(passPolicy.GetMethodObfuscationPasses(method)) || !NeedObfuscateMethod(method))
if (!method.HasBody || (!ForceProcessAllAssembliesAndIgnoreAllPolicy && (ctx.whiteList.IsInWhiteList(method) || !Support(passPolicy.GetMethodObfuscationPasses(method)) || !NeedObfuscateMethod(method))))
{
continue;
}

View File

@ -778,5 +778,15 @@ namespace Obfuz.Utils
result.Append(")");
return result.ToString();
}
public static bool HasObfuzIgnoreAttribute(IHasCustomAttribute obj)
{
return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
}
public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj)
{
return obj.CustomAttributes.Any(ca => ca.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute");
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz
{
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)]
public class ObfuzIgnoreAttribute : Attribute
{
}
}