new: 新增CompatibilitySettings,允许指定生成与mono或il2cpp兼容的混淆代码

main
walon 2025-09-10 11:15:05 +08:00
parent daff29ea94
commit e12a0e26dc
7 changed files with 55 additions and 6 deletions

View File

@ -20,7 +20,7 @@ namespace Obfuz.ObfusPasses.CallObfus
public static CallObfuscationSettingsFacade CurrentSettings { get; private set; }
private readonly CallObfuscationSettingsFacade _settings;
private readonly SpecialWhiteListMethodCalculator _specialWhiteListMethodCache;
private SpecialWhiteListMethodCalculator _specialWhiteListMethodCache;
private IObfuscator _dynamicProxyObfuscator;
private IObfuscationPolicy _dynamicProxyPolicy;
@ -31,8 +31,6 @@ namespace Obfuz.ObfusPasses.CallObfus
{
_settings = settings;
CurrentSettings = settings;
_specialWhiteListMethodCache = new SpecialWhiteListMethodCalculator(settings.obfuscateCallToMethodInMscorlib);
}
public override void Stop()
@ -43,6 +41,8 @@ namespace Obfuz.ObfusPasses.CallObfus
public override void Start()
{
var ctx = ObfuscationPassContext.Current;
_specialWhiteListMethodCache = new SpecialWhiteListMethodCalculator(ctx.coreSettings.targetRuntime, _settings.obfuscateCallToMethodInMscorlib);
_dynamicProxyObfuscator = CreateObfuscator(ctx, _settings.proxyMode);
_dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles);
}

View File

@ -1,4 +1,5 @@
using dnlib.DotNet;
using Obfuz.Settings;
using Obfuz.Utils;
using System.Collections.Generic;
@ -6,11 +7,13 @@ namespace Obfuz.ObfusPasses.CallObfus
{
class SpecialWhiteListMethodCalculator
{
private readonly RuntimeType _targetRuntime;
private readonly bool _obfuscateCallToMethodInMscorlib;
private readonly CachedDictionary<IMethod, bool> _specialWhiteListMethodCache;
public SpecialWhiteListMethodCalculator(bool obfuscateCallToMethodInMscorlib)
public SpecialWhiteListMethodCalculator(RuntimeType targetRuntime, bool obfuscateCallToMethodInMscorlib)
{
_targetRuntime = targetRuntime;
_obfuscateCallToMethodInMscorlib = obfuscateCallToMethodInMscorlib;
_specialWhiteListMethodCache = new CachedDictionary<IMethod, bool>(MethodEqualityComparer.CompareDeclaringTypes, this.ComputeIsInWhiteList);
}
@ -46,7 +49,7 @@ namespace Obfuz.ObfusPasses.CallObfus
{
MethodDef calledMethodDef = calledMethod.ResolveMethodDef();
// mono has more strict access control, calls non-public method will raise exception.
if (PlatformUtil.IsMonoBackend())
if (_targetRuntime == RuntimeType.Mono)
{
if (calledMethodDef != null && (!calledMethodDef.IsPublic || !IsTypeSelfAndParentPublic(calledMethodDef.DeclaringType)))
{

View File

@ -1,4 +1,5 @@
using Obfuz.EncryptionVM;
using dnlib.DotNet.Writer;
using Obfuz.EncryptionVM;
using Obfuz.ObfusPasses;
using Obfuz.ObfusPasses.CallObfus;
using Obfuz.ObfusPasses.ConstEncrypt;
@ -15,6 +16,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace Obfuz
{
@ -22,6 +24,7 @@ namespace Obfuz
public class CoreSettingsFacade
{
public BuildTarget buildTarget;
public RuntimeType targetRuntime;
public byte[] defaultStaticSecretKey;
public byte[] defaultDynamicSecretKey;
@ -152,11 +155,20 @@ namespace Obfuz
bool exists = Directory.Exists(path);
UnityEngine.Debug.Log($"search path:{path} exists:{exists}");
}
RuntimeType targetRuntime = settings.compatibilitySettings.targetRuntime != RuntimeType.ActivatedScriptingBackend ?
settings.compatibilitySettings.targetRuntime :
(PlatformUtil.IsMonoBackend() ? RuntimeType.Mono : RuntimeType.IL2CPP);
if (searchPathIncludeUnityEditorDll && targetRuntime != RuntimeType.Mono)
{
Debug.LogError($"obfuscate dll for editor, but ObfuzSettings.CompatibilitySettings.targetRuntime isn't RuntimeType.Mono. Use RuntimeType.Mono for targetRuntime automatically.");
targetRuntime = RuntimeType.Mono;
}
var builder = new ObfuscatorBuilder
{
_coreSettingsFacade = new CoreSettingsFacade()
{
buildTarget = target,
targetRuntime = targetRuntime,
defaultStaticSecretKey = KeyGenerator.GenerateKey(settings.secretSettings.defaultStaticSecretKey, VirtualMachine.SecretKeyLength),
defaultDynamicSecretKey = KeyGenerator.GenerateKey(settings.secretSettings.defaultDynamicSecretKey, VirtualMachine.SecretKeyLength),
assembliesUsingDynamicSecretKeys = settings.secretSettings.assembliesUsingDynamicSecretKeys.ToList(),

View File

@ -0,0 +1,17 @@
using System;
namespace Obfuz.Settings
{
public enum RuntimeType
{
ActivatedScriptingBackend,
IL2CPP,
Mono,
}
[Serializable]
public class CompatibilitySettings
{
public RuntimeType targetRuntime;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3a69af5ef930cf749bfc8e9941a0f4e8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -11,6 +11,9 @@ namespace Obfuz.Settings
[Tooltip("build pipeline settings")]
public BuildPipelineSettings buildPipelineSettings;
[Tooltip("compatibility settings")]
public CompatibilitySettings compatibilitySettings;
[Tooltip("assembly settings")]
public AssemblySettings assemblySettings;

View File

@ -25,6 +25,7 @@ namespace Obfuz.Settings
private SerializedObject _serializedObject;
private SerializedProperty _buildPipelineSettings;
private SerializedProperty _compatibilitySettings;
private SerializedProperty _assemblySettings;
private SerializedProperty _obfuscationPassSettings;
@ -66,6 +67,7 @@ namespace Obfuz.Settings
_serializedObject?.Dispose();
_serializedObject = new SerializedObject(setting);
_buildPipelineSettings = _serializedObject.FindProperty("buildPipelineSettings");
_compatibilitySettings = _serializedObject.FindProperty("compatibilitySettings");
_assemblySettings = _serializedObject.FindProperty("assemblySettings");
_obfuscationPassSettings = _serializedObject.FindProperty("obfuscationPassSettings");
@ -98,6 +100,7 @@ namespace Obfuz.Settings
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(_buildPipelineSettings);
EditorGUILayout.PropertyField(_compatibilitySettings);
EditorGUILayout.PropertyField(_assemblySettings);
EditorGUILayout.PropertyField(_obfuscationPassSettings);