2025-05-21 09:23:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using Obfuz.Editor;
|
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
using System.Linq;
|
2025-05-23 09:25:44 +08:00
|
|
|
|
using UnityEngine;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
|
|
|
|
namespace Obfuz
|
|
|
|
|
{
|
|
|
|
|
public class ObfuscationMethodWhitelist
|
|
|
|
|
{
|
2025-05-29 22:20:27 +08:00
|
|
|
|
private readonly ObfuzIgnoreScopeComputeCache _obfuzComputeCache;
|
2025-07-14 11:45:15 +08:00
|
|
|
|
private readonly BurstCompileComputeCache _burstCompileComputeCache;
|
2025-05-29 22:20:27 +08:00
|
|
|
|
|
2025-07-14 11:45:15 +08:00
|
|
|
|
public ObfuscationMethodWhitelist(ObfuzIgnoreScopeComputeCache obfuzComputeCache, BurstCompileComputeCache burstCompileComputeCache)
|
2025-05-29 22:20:27 +08:00
|
|
|
|
{
|
|
|
|
|
_obfuzComputeCache = obfuzComputeCache;
|
2025-07-14 11:45:15 +08:00
|
|
|
|
_burstCompileComputeCache = burstCompileComputeCache;
|
2025-05-29 22:20:27 +08:00
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
|
|
|
|
public bool IsInWhiteList(ModuleDef module)
|
|
|
|
|
{
|
|
|
|
|
string modName = module.Assembly.Name;
|
2025-05-23 09:25:44 +08:00
|
|
|
|
if (modName == ConstValues.ObfuzRuntimeAssemblyName)
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-05-21 16:24:26 +08:00
|
|
|
|
//if (MetaUtil.HasObfuzIgnoreScope(module))
|
|
|
|
|
//{
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-23 11:11:08 +08:00
|
|
|
|
private bool DoesMethodContainsRuntimeInitializeOnLoadMethodAttributeAndLoadTypeGreaterEqualAfterAssembliesLoaded(MethodDef method)
|
|
|
|
|
{
|
2025-07-14 11:45:15 +08:00
|
|
|
|
CustomAttribute ca = method.CustomAttributes.Find(ConstValues.RuntimeInitializedOnLoadMethodAttributeFullName);
|
2025-05-23 11:11:08 +08:00
|
|
|
|
if (ca != null && ca.ConstructorArguments.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
RuntimeInitializeLoadType loadType = (RuntimeInitializeLoadType)ca.ConstructorArguments[0].Value;
|
|
|
|
|
if (loadType >= RuntimeInitializeLoadType.AfterAssembliesLoaded)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
public bool IsInWhiteList(MethodDef method)
|
|
|
|
|
{
|
2025-05-23 11:11:08 +08:00
|
|
|
|
TypeDef typeDef = method.DeclaringType;
|
2025-07-14 11:45:15 +08:00
|
|
|
|
//if (IsInWhiteList(typeDef))
|
|
|
|
|
//{
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
if (method.Name.StartsWith(ConstValues.ObfuzInternalSymbolNamePrefix))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-05-30 08:15:49 +08:00
|
|
|
|
if (_obfuzComputeCache.HasSelfOrDeclaringOrEnclosingOrInheritObfuzIgnoreScope(method, typeDef, ObfuzScope.MethodBody))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-07-14 11:45:15 +08:00
|
|
|
|
CustomAttribute ca = method.CustomAttributes.Find(ConstValues.RuntimeInitializedOnLoadMethodAttributeFullName);
|
2025-05-23 11:11:08 +08:00
|
|
|
|
if (DoesMethodContainsRuntimeInitializeOnLoadMethodAttributeAndLoadTypeGreaterEqualAfterAssembliesLoaded(method))
|
2025-05-23 09:25:44 +08:00
|
|
|
|
{
|
2025-05-23 11:11:08 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-08-06 18:27:15 +08:00
|
|
|
|
if (MetaUtil.HasBurstCompileAttribute(method) || _burstCompileComputeCache.IsBurstCompileMethodOrReferencedByBurstCompileMethod(method))
|
2025-07-11 18:03:25 +08:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-05-23 11:11:08 +08:00
|
|
|
|
|
|
|
|
|
// don't obfuscate cctor when it has RuntimeInitializeOnLoadMethodAttribute with load type AfterAssembliesLoaded
|
|
|
|
|
if (method.IsStatic && method.Name == ".cctor" && typeDef.Methods.Any(m => DoesMethodContainsRuntimeInitializeOnLoadMethodAttributeAndLoadTypeGreaterEqualAfterAssembliesLoaded(m)))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
2025-05-23 09:25:44 +08:00
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsInWhiteList(TypeDef type)
|
|
|
|
|
{
|
|
|
|
|
if (type.Name.StartsWith(ConstValues.ObfuzInternalSymbolNamePrefix))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (IsInWhiteList(type.Module))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-08-06 18:27:15 +08:00
|
|
|
|
if (MetaUtil.HasBurstCompileAttribute(type))
|
2025-07-11 18:03:25 +08:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-05-30 08:15:49 +08:00
|
|
|
|
if (_obfuzComputeCache.HasSelfOrDeclaringOrEnclosingOrInheritObfuzIgnoreScope(type, type.DeclaringType, ObfuzScope.TypeName))
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-05-21 16:24:26 +08:00
|
|
|
|
//if (type.DeclaringType != null && IsInWhiteList(type.DeclaringType))
|
|
|
|
|
//{
|
|
|
|
|
// return true;
|
|
|
|
|
//}
|
2025-07-14 11:45:15 +08:00
|
|
|
|
if (type.FullName == ConstValues.GeneratedEncryptionVirtualMachineFullName)
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|