新增 secretKey和globalRandomSeed

backup
walon 2025-05-11 09:17:04 +08:00
parent dafa1ddb32
commit 69e6068dd0
6 changed files with 76 additions and 7 deletions

View File

@ -28,6 +28,8 @@ namespace Obfuz
private readonly List<ModuleDef> _obfuscatedAndNotObfuscatedModules = new List<ModuleDef>(); private readonly List<ModuleDef> _obfuscatedAndNotObfuscatedModules = new List<ModuleDef>();
private readonly Pipeline _pipeline = new Pipeline(); private readonly Pipeline _pipeline = new Pipeline();
private readonly byte[] _secretKey;
private readonly int _globalRandomSeed;
private ObfuscationPassContext _ctx; private ObfuscationPassContext _ctx;
@ -35,8 +37,11 @@ namespace Obfuz
List<string> notObfuscatedAssemblyNamesReferencingObfuscated, List<string> notObfuscatedAssemblyNamesReferencingObfuscated,
List<string> assemblySearchDirs, List<string> assemblySearchDirs,
string obfuscatedAssemblyOutputDir, string obfuscatedAssemblyOutputDir,
List<IObfuscationPass> obfuscationPasses) List<IObfuscationPass> obfuscationPasses, string rawSecretKey, int globalRandomSeed)
{ {
_secretKey = KeyGenerator.GenerateKey(rawSecretKey);
_globalRandomSeed = globalRandomSeed;
_toObfuscatedAssemblyNames = toObfuscatedAssemblyNames; _toObfuscatedAssemblyNames = toObfuscatedAssemblyNames;
_notObfuscatedAssemblyNamesReferencingObfuscated = notObfuscatedAssemblyNamesReferencingObfuscated; _notObfuscatedAssemblyNamesReferencingObfuscated = notObfuscatedAssemblyNamesReferencingObfuscated;
_obfuscatedAssemblyOutputDir = obfuscatedAssemblyOutputDir; _obfuscatedAssemblyOutputDir = obfuscatedAssemblyOutputDir;
@ -62,8 +67,8 @@ namespace Obfuz
LoadAssemblies(); LoadAssemblies();
var random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5); var random = new RandomWithKey(_secretKey, _globalRandomSeed);
var encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D }); var encryptor = new DefaultEncryptor(_secretKey);
var rvaDataAllocator = new RvaDataAllocator(random, encryptor); var rvaDataAllocator = new RvaDataAllocator(random, encryptor);
var constFieldAllocator = new ConstFieldAllocator(encryptor, random, rvaDataAllocator); var constFieldAllocator = new ConstFieldAllocator(encryptor, random, rvaDataAllocator);
_ctx = new ObfuscationPassContext _ctx = new ObfuscationPassContext

View File

@ -13,6 +13,8 @@ namespace Obfuz
{ {
public class ObfuscatorBuilder public class ObfuscatorBuilder
{ {
private string _secretKey;
private int _globalRandomSeed;
private List<string> _toObfuscatedAssemblyNames = new List<string>(); private List<string> _toObfuscatedAssemblyNames = new List<string>();
private List<string> _notObfuscatedAssemblyNamesReferencingObfuscated = new List<string>(); private List<string> _notObfuscatedAssemblyNamesReferencingObfuscated = new List<string>();
private List<string> _assemblySearchDirs = new List<string>(); private List<string> _assemblySearchDirs = new List<string>();
@ -20,6 +22,18 @@ namespace Obfuz
private string _obfuscatedAssemblyOutputDir; private string _obfuscatedAssemblyOutputDir;
private List<IObfuscationPass> _obfuscationPasses = new List<IObfuscationPass>(); private List<IObfuscationPass> _obfuscationPasses = new List<IObfuscationPass>();
public string SecretKey
{
get => _secretKey;
set => _secretKey = value;
}
public int GlobalRandomSeed
{
get => _globalRandomSeed;
set => _globalRandomSeed = value;
}
public List<string> ToObfuscatedAssemblyNames public List<string> ToObfuscatedAssemblyNames
{ {
get => _toObfuscatedAssemblyNames; get => _toObfuscatedAssemblyNames;
@ -61,13 +75,15 @@ namespace Obfuz
_notObfuscatedAssemblyNamesReferencingObfuscated, _notObfuscatedAssemblyNamesReferencingObfuscated,
_assemblySearchDirs, _assemblySearchDirs,
_obfuscatedAssemblyOutputDir, _obfuscatedAssemblyOutputDir,
_obfuscationPasses); _obfuscationPasses, _secretKey, _globalRandomSeed);
} }
public static ObfuscatorBuilder FromObfuzSettings(ObfuzSettings settings, BuildTarget target) public static ObfuscatorBuilder FromObfuzSettings(ObfuzSettings settings, BuildTarget target)
{ {
var builder = new ObfuscatorBuilder var builder = new ObfuscatorBuilder
{ {
_secretKey = settings.secretKey,
_globalRandomSeed = settings.globalRandomSeed,
_toObfuscatedAssemblyNames = settings.toObfuscatedAssemblyNames.ToList(), _toObfuscatedAssemblyNames = settings.toObfuscatedAssemblyNames.ToList(),
_notObfuscatedAssemblyNamesReferencingObfuscated = settings.notObfuscatedAssemblyNamesReferencingObfuscated.ToList(), _notObfuscatedAssemblyNamesReferencingObfuscated = settings.notObfuscatedAssemblyNamesReferencingObfuscated.ToList(),
_assemblySearchDirs = settings.extraAssemblySearchDirs.ToList(), _assemblySearchDirs = settings.extraAssemblySearchDirs.ToList(),

View File

@ -26,6 +26,12 @@ namespace Obfuz.Settings
[Tooltip("enable obfuscation pass")] [Tooltip("enable obfuscation pass")]
public ObfuscationPassType enabledObfuscationPasses = ObfuscationPassType.All; 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")] [Tooltip("symbol obfuscation settings")]
public SymbolObfusSettings symbolObfusSettings; public SymbolObfusSettings symbolObfusSettings;

View File

@ -34,6 +34,8 @@ namespace Obfuz.Settings
private SerializedProperty _extraAssemblySearchDirs; private SerializedProperty _extraAssemblySearchDirs;
private SerializedProperty _enabledObfuscationPasses; private SerializedProperty _enabledObfuscationPasses;
private SerializedProperty _secretKey;
private SerializedProperty _globalRandomSeed;
private SerializedProperty _symbolObfusSettings; private SerializedProperty _symbolObfusSettings;
private SerializedProperty _constEncryptSettings; private SerializedProperty _constEncryptSettings;
@ -64,7 +66,10 @@ namespace Obfuz.Settings
_toObfuscatedAssemblyNames = _serializedObject.FindProperty("toObfuscatedAssemblyNames"); _toObfuscatedAssemblyNames = _serializedObject.FindProperty("toObfuscatedAssemblyNames");
_notObfuscatedAssemblyNamesReferencingObfuscated = _serializedObject.FindProperty("notObfuscatedAssemblyNamesReferencingObfuscated"); _notObfuscatedAssemblyNamesReferencingObfuscated = _serializedObject.FindProperty("notObfuscatedAssemblyNamesReferencingObfuscated");
_extraAssemblySearchDirs = _serializedObject.FindProperty("extraAssemblySearchDirs"); _extraAssemblySearchDirs = _serializedObject.FindProperty("extraAssemblySearchDirs");
_enabledObfuscationPasses = _serializedObject.FindProperty("enabledObfuscationPasses"); _enabledObfuscationPasses = _serializedObject.FindProperty("enabledObfuscationPasses");
_secretKey = _serializedObject.FindProperty("secretKey");
_globalRandomSeed = _serializedObject.FindProperty("globalRandomSeed");
_symbolObfusSettings = _serializedObject.FindProperty("symbolObfusSettings"); _symbolObfusSettings = _serializedObject.FindProperty("symbolObfusSettings");
_constEncryptSettings = _serializedObject.FindProperty("constEncryptSettings"); _constEncryptSettings = _serializedObject.FindProperty("constEncryptSettings");
@ -85,7 +90,10 @@ namespace Obfuz.Settings
EditorGUILayout.PropertyField(_toObfuscatedAssemblyNames); EditorGUILayout.PropertyField(_toObfuscatedAssemblyNames);
EditorGUILayout.PropertyField(_notObfuscatedAssemblyNamesReferencingObfuscated); EditorGUILayout.PropertyField(_notObfuscatedAssemblyNamesReferencingObfuscated);
EditorGUILayout.PropertyField(_extraAssemblySearchDirs); EditorGUILayout.PropertyField(_extraAssemblySearchDirs);
EditorGUILayout.PropertyField(_enabledObfuscationPasses); EditorGUILayout.PropertyField(_enabledObfuscationPasses);
EditorGUILayout.PropertyField(_secretKey);
EditorGUILayout.PropertyField(_globalRandomSeed);
EditorGUILayout.PropertyField(_symbolObfusSettings); EditorGUILayout.PropertyField(_symbolObfusSettings);
EditorGUILayout.PropertyField(_constEncryptSettings); EditorGUILayout.PropertyField(_constEncryptSettings);

View File

@ -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;
}
}
}

View File

@ -9,7 +9,6 @@ namespace Obfuz.Utils
{ {
public class RandomWithKey : IRandom public class RandomWithKey : IRandom
{ {
// LCG 参数(使用经典的数值)
private const long a = 1664525; private const long a = 1664525;
private const long c = 1013904223; private const long c = 1013904223;
private const long m = 4294967296; // 2^32 private const long m = 4294967296; // 2^32
@ -45,7 +44,7 @@ namespace Obfuz.Utils
return (int)((uint)NextInt() % (uint)max); return (int)((uint)NextInt() % (uint)max);
} }
private int GetNextKeyByte() private int GetNextSalt()
{ {
if (_nextIndex >= _key.Length) if (_nextIndex >= _key.Length)
{ {
@ -57,7 +56,7 @@ namespace Obfuz.Utils
public int NextInt() public int NextInt()
{ {
_seed = (int)((a * _seed + c) % m); _seed = (int)((a * _seed + c) % m);
return _seed ^ GetNextKeyByte(); return _seed ^ GetNextSalt();
} }
public long NextLong() public long NextLong()