【混淆】尝试增加多态dll机制

main
刘涛 2025-10-31 15:07:07 +08:00
parent f7c194b0c4
commit 68b94b68f6
3 changed files with 64 additions and 2 deletions

View File

@ -92,6 +92,62 @@ public static class HotUpadeEditorHelper
Debug.Log("转移补充元数据dll成功!");
}
private static void _HandleConvertDllToPolymorphic(string dllPath)
{
// 把路径下的所有dll转换成多态dll形势
// 1. 读取所有*.bytes文件转存到临时目录,去掉.bytes后缀
// 2. 调用Obfuz4HybridCLR的转换接口进行转换,输出到另外一个临时目录
// 3. 把转换后的dll覆盖回原来的.bytes文件
// 临时目录设置在 Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls1" 和 Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls2"
var tempDir1 = Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls1/";
var tempDir2 = Application.dataPath + "/../Temp/Obfuz4HybridCLR/CacheDlls2/";
if (Directory.Exists(tempDir1))
{
Directory.Delete(tempDir1, true);
}
if (Directory.Exists(tempDir2))
{
Directory.Delete(tempDir2, true);
}
Directory.CreateDirectory(tempDir1);
Directory.CreateDirectory(tempDir2);
var dir = new DirectoryInfo(dllPath);
var files = dir.GetFiles();
// 步骤1: 复制.bytes文件到临时目录1去掉.bytes后缀
foreach (var file in files)
{
var dllFileName = Path.GetFileNameWithoutExtension(file.Name); // 去掉.bytes
var destPath = Path.Combine(tempDir1, dllFileName);
File.Copy(file.FullName, destPath, true);
Debug.Log($"[ConvertDll] Copy {file.Name} to {destPath}");
}
// 步骤2: 调用Obfuz4HybridCLR转换接口
ObfuscateUtil.GeneratePolymorphicDll(tempDir1, tempDir2);
// 步骤3: 把转换后的dll覆盖回原来的.bytes文件
var convertedFiles = new DirectoryInfo(tempDir2).GetFiles("*.dll");
foreach (var file in convertedFiles)
{
var originalBytesPath = Path.Combine(dllPath, file.Name + ".bytes");
if (File.Exists(originalBytesPath))
{
File.Copy(file.FullName, originalBytesPath, true);
Debug.Log($"[ConvertDll] Override {originalBytesPath} with converted dll");
}
}
}
public static void ConvertOriginalDllToPolymorphicDll()
{
// Assets/HotUpdateDlls/HotUpdateDll
var hotUpdateDestinationPath = Application.dataPath + HOT_UPDATE_DESTINATION_PATH;
_HandleConvertDllToPolymorphic(hotUpdateDestinationPath);
// Assets/HotUpdateDlls/MetaDataDll
var metaDataDestinationPath = Application.dataPath + META_DATA_DESTINATION_PATH;
_HandleConvertDllToPolymorphic(metaDataDestinationPath);
}
//之所以采用读取C#文件的方式是因为如果直接读取代码中的列表会出现打包时更改了AOTGenericReferences.cs但Unity没编译导致
//AOTGenericReferences中PatchedAOTAssemblyList还是代码修改前的数据的问题是因为Unity没有reload domain
//https://docs.unity.cn/2023.2/Documentation/Manual/DomainReloading.html

View File

@ -91,6 +91,12 @@ class BuildLauncher
}
HotUpadeEditorHelper.CopyMetaDataDll();
if (ObfuzSettings.Instance.polymorphicDllSettings.enable)
{
// 使用多态dll
HotUpadeEditorHelper.ConvertOriginalDllToPolymorphicDll();
}
}
/// <summary>

View File

@ -91,5 +91,5 @@ MonoBehaviour:
signatureLength: 256
polymorphicDllSettings:
enable: 1
codeGenerationSecretKey: obfuz-polymorphic-key
disableLoadStandardDll: 0
codeGenerationSecretKey: obfuz-phxh-polymorphic-check-dll
disableLoadStandardDll: 1