新增 函数体混淆白名单功能

backup
walon 2025-05-12 18:03:39 +08:00
parent bf6112a4ab
commit ac15ef8ebc
6 changed files with 62 additions and 38 deletions

View File

@ -12,19 +12,24 @@ namespace Obfuz.ObfusPasses
public override void Process(ObfuscationPassContext ctx) public override void Process(ObfuscationPassContext ctx)
{ {
NotObfuscatedMethodWhiteList whiteList = ctx.whiteList;
foreach (ModuleDef mod in ctx.toObfuscatedModules) foreach (ModuleDef mod in ctx.toObfuscatedModules)
{ {
if (whiteList.IsInWhiteList(mod))
{
continue;
}
// ToArray to avoid modify list exception // ToArray to avoid modify list exception
foreach (TypeDef type in mod.GetTypes().ToArray()) foreach (TypeDef type in mod.GetTypes().ToArray())
{ {
if (type.Name.StartsWith("$Obfuz$")) if (whiteList.IsInWhiteList(type))
{ {
continue; continue;
} }
// ToArray to avoid modify list exception // ToArray to avoid modify list exception
foreach (MethodDef method in type.Methods.ToArray()) foreach (MethodDef method in type.Methods.ToArray())
{ {
if (!method.HasBody || method.Name.StartsWith("$Obfuz$") || !NeedObfuscateMethod(method)) if (!method.HasBody || ctx.whiteList.IsInWhiteList(method) || !NeedObfuscateMethod(method))
{ {
continue; continue;
} }

View File

@ -14,19 +14,24 @@ namespace Obfuz.ObfusPasses
public override void Process(ObfuscationPassContext ctx) public override void Process(ObfuscationPassContext ctx)
{ {
var modules = NeedProcessNotObfuscatedAssembly ? ctx.obfuscatedAndNotObfuscatedModules : ctx.toObfuscatedModules; var modules = NeedProcessNotObfuscatedAssembly ? ctx.obfuscatedAndNotObfuscatedModules : ctx.toObfuscatedModules;
NotObfuscatedMethodWhiteList whiteList = ctx.whiteList;
foreach (ModuleDef mod in modules) foreach (ModuleDef mod in modules)
{ {
if (whiteList.IsInWhiteList(mod))
{
continue;
}
// ToArray to avoid modify list exception // ToArray to avoid modify list exception
foreach (TypeDef type in mod.GetTypes().ToArray()) foreach (TypeDef type in mod.GetTypes().ToArray())
{ {
if (type.Name.StartsWith("$Obfuz$")) if (whiteList.IsInWhiteList(type))
{ {
continue; continue;
} }
// ToArray to avoid modify list exception // ToArray to avoid modify list exception
foreach (MethodDef method in type.Methods.ToArray()) foreach (MethodDef method in type.Methods.ToArray())
{ {
if (!method.HasBody || method.Name.StartsWith("$Obfuz$") || !NeedObfuscateMethod(method)) if (!method.HasBody || ctx.whiteList.IsInWhiteList(method) || !NeedObfuscateMethod(method))
{ {
continue; continue;
} }

View File

@ -0,0 +1,45 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.ObfusPasses
{
public class NotObfuscatedMethodWhiteList
{
public bool IsInWhiteList(ModuleDef module)
{
string modName = module.Assembly.Name;
if (modName == "Obfuz.Runtime")
{
return true;
}
return false;
}
public bool IsInWhiteList(MethodDef method)
{
if (IsInWhiteList(method.DeclaringType))
{
return true;
}
return false;
}
public bool IsInWhiteList(TypeDef type)
{
//if (type.Name.StartsWith("$Obfuz$"))
//{
// continue;
//}
if (IsInWhiteList(type.Module))
{
return true;
}
return false;
}
}
}

View File

@ -38,7 +38,6 @@ namespace Obfuz.ObfusPasses.SymbolObfus
private List<ModuleDef> _toObfuscatedModules; private List<ModuleDef> _toObfuscatedModules;
private List<ModuleDef> _obfuscatedAndNotObfuscatedModules; private List<ModuleDef> _obfuscatedAndNotObfuscatedModules;
private List<AssemblyReferenceInfo> _obfuzAssemblies;
private HashSet<ModuleDef> _toObfuscatedModuleSet; private HashSet<ModuleDef> _toObfuscatedModuleSet;
private IObfuscationPolicy _renamePolicy; private IObfuscationPolicy _renamePolicy;
private INameMaker _nameMaker; private INameMaker _nameMaker;
@ -70,44 +69,11 @@ namespace Obfuz.ObfusPasses.SymbolObfus
_toObfuscatedModules = ctx.toObfuscatedModules; _toObfuscatedModules = ctx.toObfuscatedModules;
_obfuscatedAndNotObfuscatedModules = ctx.obfuscatedAndNotObfuscatedModules; _obfuscatedAndNotObfuscatedModules = ctx.obfuscatedAndNotObfuscatedModules;
_toObfuscatedModuleSet = ctx.toObfuscatedModules.ToHashSet(); _toObfuscatedModuleSet = ctx.toObfuscatedModules.ToHashSet();
_obfuzAssemblies = BuildAssemblyReferenceInfos(ctx);
var obfuscateRuleConfig = new ConfigurableRenamePolicy(ctx.toObfuscatedAssemblyNames, _obfuscationRuleFiles); var obfuscateRuleConfig = new ConfigurableRenamePolicy(ctx.toObfuscatedAssemblyNames, _obfuscationRuleFiles);
_renamePolicy = new CacheRenamePolicy(new CombineRenamePolicy(new SystemRenamePolicy(), new UnityRenamePolicy(), obfuscateRuleConfig)); _renamePolicy = new CacheRenamePolicy(new CombineRenamePolicy(new SystemRenamePolicy(), new UnityRenamePolicy(), obfuscateRuleConfig));
BuildCustomAttributeArguments(); BuildCustomAttributeArguments();
} }
private static List<AssemblyReferenceInfo> BuildAssemblyReferenceInfos(ObfuscationPassContext ctx)
{
var obfuzAssemblies = new List<AssemblyReferenceInfo>();
foreach (ModuleDef mod in ctx.obfuscatedAndNotObfuscatedModules)
{
var obfuzAsm = new AssemblyReferenceInfo
{
name = mod.Assembly.Name,
needObfuscated = ctx.toObfuscatedModules.Contains(mod),
module = mod,
referenceMeAssemblies = new List<AssemblyReferenceInfo>(),
};
obfuzAsm.referenceMeAssemblies.Add(obfuzAsm);
obfuzAssemblies.Add(obfuzAsm);
}
var assByName = obfuzAssemblies.ToDictionary(x => x.name);
foreach (var ass in obfuzAssemblies)
{
foreach (var refAss in ass.module.GetAssemblyRefs())
{
string refAssName = refAss.Name;
if (assByName.TryGetValue(refAssName, out var refAssembly))
{
//UnityEngine.Debug.Log($"assembly:{ass.name} reference to {refAssName}");
refAssembly.referenceMeAssemblies.Add(ass);
}
}
}
return obfuzAssemblies;
}
private void CollectCArgumentWithTypeOf(IHasCustomAttribute meta, List<CustomAttributeInfo> customAttributes) private void CollectCArgumentWithTypeOf(IHasCustomAttribute meta, List<CustomAttributeInfo> customAttributes)
{ {
int index = 0; int index = 0;

View File

@ -1,5 +1,6 @@
using dnlib.DotNet; using dnlib.DotNet;
using Obfuz.Data; using Obfuz.Data;
using Obfuz.ObfusPasses;
using Obfuz.ObfusPasses.SymbolObfus; using Obfuz.ObfusPasses.SymbolObfus;
using Obfuz.Utils; using Obfuz.Utils;
using System; using System;
@ -27,5 +28,6 @@ namespace Obfuz
public IEncryptor encryptor; public IEncryptor encryptor;
public ConstFieldAllocator constFieldAllocator; public ConstFieldAllocator constFieldAllocator;
public RvaDataAllocator rvaDataAllocator; public RvaDataAllocator rvaDataAllocator;
public NotObfuscatedMethodWhiteList whiteList;
} }
} }

View File

@ -175,6 +175,7 @@ namespace Obfuz
encryptor = encryptor, encryptor = encryptor,
rvaDataAllocator = rvaDataAllocator, rvaDataAllocator = rvaDataAllocator,
constFieldAllocator = constFieldAllocator, constFieldAllocator = constFieldAllocator,
whiteList = new NotObfuscatedMethodWhiteList(),
}; };
pipeline.Start(_ctx); pipeline.Start(_ctx);
} }