From 69e6068dd093044f4305c2bba33b73b57da1201a Mon Sep 17 00:00:00 2001 From: walon Date: Sun, 11 May 2025 09:17:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20secretKey=E5=92=8CglobalRa?= =?UTF-8?q?ndomSeed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/Obfuscator.cs | 11 ++++++-- Editor/ObfuscatorBuilder.cs | 18 +++++++++++- Editor/Settings/ObfuzSettings.cs | 6 ++++ Editor/Settings/ObfuzSettingsProvider.cs | 8 ++++++ Editor/Utils/KeyGenerator.cs | 35 ++++++++++++++++++++++++ Editor/Utils/RandomWithKey.cs | 5 ++-- 6 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 Editor/Utils/KeyGenerator.cs diff --git a/Editor/Obfuscator.cs b/Editor/Obfuscator.cs index 5bd699e..ef2ac26 100644 --- a/Editor/Obfuscator.cs +++ b/Editor/Obfuscator.cs @@ -28,6 +28,8 @@ namespace Obfuz private readonly List _obfuscatedAndNotObfuscatedModules = new List(); private readonly Pipeline _pipeline = new Pipeline(); + private readonly byte[] _secretKey; + private readonly int _globalRandomSeed; private ObfuscationPassContext _ctx; @@ -35,8 +37,11 @@ namespace Obfuz List notObfuscatedAssemblyNamesReferencingObfuscated, List assemblySearchDirs, string obfuscatedAssemblyOutputDir, - List obfuscationPasses) + List obfuscationPasses, string rawSecretKey, int globalRandomSeed) { + _secretKey = KeyGenerator.GenerateKey(rawSecretKey); + _globalRandomSeed = globalRandomSeed; + _toObfuscatedAssemblyNames = toObfuscatedAssemblyNames; _notObfuscatedAssemblyNamesReferencingObfuscated = notObfuscatedAssemblyNamesReferencingObfuscated; _obfuscatedAssemblyOutputDir = obfuscatedAssemblyOutputDir; @@ -62,8 +67,8 @@ namespace Obfuz LoadAssemblies(); - var random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5); - var encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D }); + var random = new RandomWithKey(_secretKey, _globalRandomSeed); + var encryptor = new DefaultEncryptor(_secretKey); var rvaDataAllocator = new RvaDataAllocator(random, encryptor); var constFieldAllocator = new ConstFieldAllocator(encryptor, random, rvaDataAllocator); _ctx = new ObfuscationPassContext diff --git a/Editor/ObfuscatorBuilder.cs b/Editor/ObfuscatorBuilder.cs index 84c90ff..04ffcd4 100644 --- a/Editor/ObfuscatorBuilder.cs +++ b/Editor/ObfuscatorBuilder.cs @@ -13,6 +13,8 @@ namespace Obfuz { public class ObfuscatorBuilder { + private string _secretKey; + private int _globalRandomSeed; private List _toObfuscatedAssemblyNames = new List(); private List _notObfuscatedAssemblyNamesReferencingObfuscated = new List(); private List _assemblySearchDirs = new List(); @@ -20,6 +22,18 @@ namespace Obfuz private string _obfuscatedAssemblyOutputDir; private List _obfuscationPasses = new List(); + public string SecretKey + { + get => _secretKey; + set => _secretKey = value; + } + + public int GlobalRandomSeed + { + get => _globalRandomSeed; + set => _globalRandomSeed = value; + } + public List ToObfuscatedAssemblyNames { get => _toObfuscatedAssemblyNames; @@ -61,13 +75,15 @@ namespace Obfuz _notObfuscatedAssemblyNamesReferencingObfuscated, _assemblySearchDirs, _obfuscatedAssemblyOutputDir, - _obfuscationPasses); + _obfuscationPasses, _secretKey, _globalRandomSeed); } public static ObfuscatorBuilder FromObfuzSettings(ObfuzSettings settings, BuildTarget target) { var builder = new ObfuscatorBuilder { + _secretKey = settings.secretKey, + _globalRandomSeed = settings.globalRandomSeed, _toObfuscatedAssemblyNames = settings.toObfuscatedAssemblyNames.ToList(), _notObfuscatedAssemblyNamesReferencingObfuscated = settings.notObfuscatedAssemblyNamesReferencingObfuscated.ToList(), _assemblySearchDirs = settings.extraAssemblySearchDirs.ToList(), diff --git a/Editor/Settings/ObfuzSettings.cs b/Editor/Settings/ObfuzSettings.cs index 7d76a95..2683011 100644 --- a/Editor/Settings/ObfuzSettings.cs +++ b/Editor/Settings/ObfuzSettings.cs @@ -26,6 +26,12 @@ namespace Obfuz.Settings [Tooltip("enable obfuscation pass")] public ObfuscationPassType enabledObfuscationPasses = ObfuscationPassType.All; + [Tooltip("secret key")] + public string secretKey = "Code Philosophy"; + + [Tooltip("global random seed")] + public int globalRandomSeed = 0; + [Tooltip("symbol obfuscation settings")] public SymbolObfusSettings symbolObfusSettings; diff --git a/Editor/Settings/ObfuzSettingsProvider.cs b/Editor/Settings/ObfuzSettingsProvider.cs index 0285964..b5311b0 100644 --- a/Editor/Settings/ObfuzSettingsProvider.cs +++ b/Editor/Settings/ObfuzSettingsProvider.cs @@ -34,6 +34,8 @@ namespace Obfuz.Settings private SerializedProperty _extraAssemblySearchDirs; private SerializedProperty _enabledObfuscationPasses; + private SerializedProperty _secretKey; + private SerializedProperty _globalRandomSeed; private SerializedProperty _symbolObfusSettings; private SerializedProperty _constEncryptSettings; @@ -64,7 +66,10 @@ namespace Obfuz.Settings _toObfuscatedAssemblyNames = _serializedObject.FindProperty("toObfuscatedAssemblyNames"); _notObfuscatedAssemblyNamesReferencingObfuscated = _serializedObject.FindProperty("notObfuscatedAssemblyNamesReferencingObfuscated"); _extraAssemblySearchDirs = _serializedObject.FindProperty("extraAssemblySearchDirs"); + _enabledObfuscationPasses = _serializedObject.FindProperty("enabledObfuscationPasses"); + _secretKey = _serializedObject.FindProperty("secretKey"); + _globalRandomSeed = _serializedObject.FindProperty("globalRandomSeed"); _symbolObfusSettings = _serializedObject.FindProperty("symbolObfusSettings"); _constEncryptSettings = _serializedObject.FindProperty("constEncryptSettings"); @@ -85,7 +90,10 @@ namespace Obfuz.Settings EditorGUILayout.PropertyField(_toObfuscatedAssemblyNames); EditorGUILayout.PropertyField(_notObfuscatedAssemblyNamesReferencingObfuscated); EditorGUILayout.PropertyField(_extraAssemblySearchDirs); + EditorGUILayout.PropertyField(_enabledObfuscationPasses); + EditorGUILayout.PropertyField(_secretKey); + EditorGUILayout.PropertyField(_globalRandomSeed); EditorGUILayout.PropertyField(_symbolObfusSettings); EditorGUILayout.PropertyField(_constEncryptSettings); diff --git a/Editor/Utils/KeyGenerator.cs b/Editor/Utils/KeyGenerator.cs new file mode 100644 index 0000000..e83ccb8 --- /dev/null +++ b/Editor/Utils/KeyGenerator.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz.Utils +{ + public static class KeyGenerator + { + public const int KeyLength = 1024; + + public static byte[] GenerateKey(string initialString) + { + byte[] initialBytes = Encoding.UTF8.GetBytes(initialString); + using var sha512 = SHA512.Create(); + byte[] hash = sha512.ComputeHash(initialBytes); + byte[] key = new byte[KeyLength]; + int bytesCopied = 0; + while (bytesCopied < key.Length) + { + if (bytesCopied > 0) + { + // 再次哈希之前的哈希值以生成更多数据 + hash = sha512.ComputeHash(hash); + } + int bytesToCopy = Math.Min(hash.Length, key.Length - bytesCopied); + Buffer.BlockCopy(hash, 0, key, bytesCopied, bytesToCopy); + bytesCopied += bytesToCopy; + } + return key; + } + } +} diff --git a/Editor/Utils/RandomWithKey.cs b/Editor/Utils/RandomWithKey.cs index 9d767c5..8a0292e 100644 --- a/Editor/Utils/RandomWithKey.cs +++ b/Editor/Utils/RandomWithKey.cs @@ -9,7 +9,6 @@ namespace Obfuz.Utils { public class RandomWithKey : IRandom { - // LCG 参数(使用经典的数值) private const long a = 1664525; private const long c = 1013904223; private const long m = 4294967296; // 2^32 @@ -45,7 +44,7 @@ namespace Obfuz.Utils return (int)((uint)NextInt() % (uint)max); } - private int GetNextKeyByte() + private int GetNextSalt() { if (_nextIndex >= _key.Length) { @@ -57,7 +56,7 @@ namespace Obfuz.Utils public int NextInt() { _seed = (int)((a * _seed + c) % m); - return _seed ^ GetNextKeyByte(); + return _seed ^ GetNextSalt(); } public long NextLong()