新增 secretKey和globalRandomSeed
parent
dafa1ddb32
commit
69e6068dd0
|
@ -28,6 +28,8 @@ namespace Obfuz
|
|||
private readonly List<ModuleDef> _obfuscatedAndNotObfuscatedModules = new List<ModuleDef>();
|
||||
|
||||
private readonly Pipeline _pipeline = new Pipeline();
|
||||
private readonly byte[] _secretKey;
|
||||
private readonly int _globalRandomSeed;
|
||||
|
||||
private ObfuscationPassContext _ctx;
|
||||
|
||||
|
@ -35,8 +37,11 @@ namespace Obfuz
|
|||
List<string> notObfuscatedAssemblyNamesReferencingObfuscated,
|
||||
List<string> assemblySearchDirs,
|
||||
string obfuscatedAssemblyOutputDir,
|
||||
List<IObfuscationPass> obfuscationPasses)
|
||||
List<IObfuscationPass> 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
|
||||
|
|
|
@ -13,6 +13,8 @@ namespace Obfuz
|
|||
{
|
||||
public class ObfuscatorBuilder
|
||||
{
|
||||
private string _secretKey;
|
||||
private int _globalRandomSeed;
|
||||
private List<string> _toObfuscatedAssemblyNames = new List<string>();
|
||||
private List<string> _notObfuscatedAssemblyNamesReferencingObfuscated = new List<string>();
|
||||
private List<string> _assemblySearchDirs = new List<string>();
|
||||
|
@ -20,6 +22,18 @@ namespace Obfuz
|
|||
private string _obfuscatedAssemblyOutputDir;
|
||||
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
|
||||
{
|
||||
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(),
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue