- 重构设置中secretKey为secret,重构globalRandomSeed为randomSeed。

- 保存 secret文件
backup
walon 2025-05-12 08:13:01 +08:00
parent db86070fdb
commit ce535949aa
7 changed files with 77 additions and 38 deletions

View File

@ -29,9 +29,9 @@ 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 byte[] _secret;
private readonly int _globalRandomSeed; private readonly int _randomSeed;
private readonly string _encryptionVmGenerationSecretKey; private readonly string _encryptionVmGenerationSecret;
private readonly int _encryptionVmOpCodeCount; private readonly int _encryptionVmOpCodeCount;
private readonly string _encryptionVmCodeFile; private readonly string _encryptionVmCodeFile;
@ -39,9 +39,10 @@ namespace Obfuz
public Obfuscator(ObfuscatorBuilder builder) public Obfuscator(ObfuscatorBuilder builder)
{ {
_secretKey = KeyGenerator.GenerateKey(builder.SecretKey, VirtualMachine.SecretKeyLength); _secret = KeyGenerator.GenerateKey(builder.Secret, VirtualMachine.SecretKeyLength);
_globalRandomSeed = builder.GlobalRandomSeed; SaveKey(_secret, builder.SecretOutputPath);
_encryptionVmGenerationSecretKey = builder.EncryptionVmGenerationSecretKey; _randomSeed = builder.RandomSeed;
_encryptionVmGenerationSecret = builder.EncryptionVmGenerationSecretKey;
_encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount; _encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount;
_encryptionVmCodeFile = builder.EncryptionVmCodeFile; _encryptionVmCodeFile = builder.EncryptionVmCodeFile;
@ -58,6 +59,13 @@ namespace Obfuz
_pipeline.AddPass(new CleanUpInstructionPass()); _pipeline.AddPass(new CleanUpInstructionPass());
} }
public static void SaveKey(byte[] secret, string secretOutputPath)
{
FileUtil.CreateParentDir(secretOutputPath);
File.WriteAllBytes(secretOutputPath, secret);
Debug.Log($"Save secret key to {secretOutputPath}, secret length:{secret.Length}");
}
public void Run() public void Run()
{ {
OnPreObfuscation(); OnPreObfuscation();
@ -67,7 +75,7 @@ namespace Obfuz
private IEncryptor CreateEncryptionVirtualMachine() private IEncryptor CreateEncryptionVirtualMachine()
{ {
var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecretKey); var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecret);
var vm = vmCreator.CreateVirtualMachine(_encryptionVmOpCodeCount); var vm = vmCreator.CreateVirtualMachine(_encryptionVmOpCodeCount);
var vmGenerator = new VirtualMachineCodeGenerator(vm); var vmGenerator = new VirtualMachineCodeGenerator(vm);
@ -79,7 +87,7 @@ namespace Obfuz
{ {
throw new Exception($"EncryptionVm CodeFile:`{_encryptionVmCodeFile}` not match with encryptionVM settings! Please run `Obfuz/GenerateVm` to update it!"); throw new Exception($"EncryptionVm CodeFile:`{_encryptionVmCodeFile}` not match with encryptionVM settings! Please run `Obfuz/GenerateVm` to update it!");
} }
var vms = new VirtualMachineSimulator(vm, _secretKey); var vms = new VirtualMachineSimulator(vm, _secret);
var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies() var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies()
.Select(assembly => assembly.GetType("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine")) .Select(assembly => assembly.GetType("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine"))
@ -94,7 +102,7 @@ namespace Obfuz
throw new Exception($"class Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine found in multiple assemblies! Please retain only one!"); throw new Exception($"class Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine found in multiple assemblies! Please retain only one!");
} }
var gvmInstance = (IEncryptor)Activator.CreateInstance(generatedVmTypes[0], new object[] { _secretKey } ); var gvmInstance = (IEncryptor)Activator.CreateInstance(generatedVmTypes[0], new object[] { _secret } );
int testValue = 11223344; int testValue = 11223344;
for (int i = 0; i < vm.opCodes.Length; i++) for (int i = 0; i < vm.opCodes.Length; i++)
@ -125,7 +133,7 @@ namespace Obfuz
LoadAssemblies(); LoadAssemblies();
var random = new RandomWithKey(_secretKey, _globalRandomSeed); var random = new RandomWithKey(_secret, _randomSeed);
var encryptor = CreateEncryptionVirtualMachine(); var encryptor = CreateEncryptionVirtualMachine();
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);

View File

@ -15,8 +15,9 @@ namespace Obfuz
{ {
public class ObfuscatorBuilder public class ObfuscatorBuilder
{ {
private string _secretKey; private string _secret;
private int _globalRandomSeed; private string _secretOutputPath;
private int _randomSeed;
private string _encryptionVmGenerationSecretKey; private string _encryptionVmGenerationSecretKey;
private int _encryptionVmOpCodeCount; private int _encryptionVmOpCodeCount;
private string _encryptionVmCodeFile; private string _encryptionVmCodeFile;
@ -28,16 +29,22 @@ 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 public string Secret
{ {
get => _secretKey; get => _secret;
set => _secretKey = value; set => _secret = value;
} }
public int GlobalRandomSeed public string SecretOutputPath
{ {
get => _globalRandomSeed; get => _secretOutputPath;
set => _globalRandomSeed = value; set => _secretOutputPath = value;
}
public int RandomSeed
{
get => _randomSeed;
set => _randomSeed = value;
} }
public string EncryptionVmGenerationSecretKey public string EncryptionVmGenerationSecretKey
@ -104,11 +111,12 @@ namespace Obfuz
{ {
var builder = new ObfuscatorBuilder var builder = new ObfuscatorBuilder
{ {
_secretKey = settings.secretKey, _secret = settings.secret,
_globalRandomSeed = settings.globalRandomSeed, _secretOutputPath = settings.secretOutputPath,
_encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecretKey, _randomSeed = settings.randomSeed,
_encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecret,
_encryptionVmOpCodeCount = settings.encryptionVMSettings.encryptionOpCodeCount, _encryptionVmOpCodeCount = settings.encryptionVMSettings.encryptionOpCodeCount,
_encryptionVmCodeFile = settings.encryptionVMSettings.CodeOutputPath, _encryptionVmCodeFile = settings.encryptionVMSettings.codeOutputPath,
_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

@ -12,14 +12,12 @@ namespace Obfuz.Settings
public class EncryptionVMSettings public class EncryptionVMSettings
{ {
[Tooltip("secret key for generating encryption virtual machine source code")] [Tooltip("secret key for generating encryption virtual machine source code")]
public string codeGenerationSecretKey = "Obfuz"; public string codeGenerationSecret = "Obfuz";
[Tooltip("encryption OpCode count, should be power of 2 and >= 64")] [Tooltip("encryption OpCode count, should be power of 2 and >= 64")]
public int encryptionOpCodeCount = 256; public int encryptionOpCodeCount = 256;
[Tooltip("encryption virtual machine source code output dir")] [Tooltip("encryption virtual machine source code output path")]
public string codeOutputDir = "Assets/Obfuz"; public string codeOutputPath = "Assets/Obfuz/GeneratedEncryptionVirtualMachine.cs";
public string CodeOutputPath { get => Path.Combine(codeOutputDir, "GeneratedEncryptionVirtualMachine.cs"); }
} }
} }

View File

@ -27,10 +27,13 @@ namespace Obfuz.Settings
public ObfuscationPassType enabledObfuscationPasses = ObfuscationPassType.All; public ObfuscationPassType enabledObfuscationPasses = ObfuscationPassType.All;
[Tooltip("secret key")] [Tooltip("secret key")]
public string secretKey = "Code Philosophy"; public string secret = "Code Philosophy";
[Tooltip("secret key save path")]
public string secretOutputPath = $"Assets/Obfuz/secret.bytes";
[Tooltip("global random seed")] [Tooltip("global random seed")]
public int globalRandomSeed = 0; public int randomSeed = 0;
[Tooltip("encryption virtual machine settings")] [Tooltip("encryption virtual machine settings")]
public EncryptionVMSettings encryptionVMSettings; public EncryptionVMSettings encryptionVMSettings;

View File

@ -34,8 +34,9 @@ namespace Obfuz.Settings
private SerializedProperty _extraAssemblySearchDirs; private SerializedProperty _extraAssemblySearchDirs;
private SerializedProperty _enabledObfuscationPasses; private SerializedProperty _enabledObfuscationPasses;
private SerializedProperty _secretKey; private SerializedProperty _secret;
private SerializedProperty _globalRandomSeed; private SerializedProperty _secretOutputPath;
private SerializedProperty _randomSeed;
private SerializedProperty _encryptionVMSettings; private SerializedProperty _encryptionVMSettings;
@ -70,8 +71,9 @@ namespace Obfuz.Settings
_extraAssemblySearchDirs = _serializedObject.FindProperty("extraAssemblySearchDirs"); _extraAssemblySearchDirs = _serializedObject.FindProperty("extraAssemblySearchDirs");
_enabledObfuscationPasses = _serializedObject.FindProperty("enabledObfuscationPasses"); _enabledObfuscationPasses = _serializedObject.FindProperty("enabledObfuscationPasses");
_secretKey = _serializedObject.FindProperty("secretKey"); _secret = _serializedObject.FindProperty("secret");
_globalRandomSeed = _serializedObject.FindProperty("globalRandomSeed"); _secretOutputPath = _serializedObject.FindProperty("secretOutputPath");
_randomSeed = _serializedObject.FindProperty("randomSeed");
_encryptionVMSettings = _serializedObject.FindProperty("encryptionVMSettings"); _encryptionVMSettings = _serializedObject.FindProperty("encryptionVMSettings");
@ -96,8 +98,9 @@ namespace Obfuz.Settings
EditorGUILayout.PropertyField(_extraAssemblySearchDirs); EditorGUILayout.PropertyField(_extraAssemblySearchDirs);
EditorGUILayout.PropertyField(_enabledObfuscationPasses); EditorGUILayout.PropertyField(_enabledObfuscationPasses);
EditorGUILayout.PropertyField(_secretKey); EditorGUILayout.PropertyField(_secret);
EditorGUILayout.PropertyField(_globalRandomSeed); EditorGUILayout.PropertyField(_secretOutputPath);
EditorGUILayout.PropertyField(_randomSeed);
EditorGUILayout.PropertyField(_encryptionVMSettings); EditorGUILayout.PropertyField(_encryptionVMSettings);

View File

@ -1,7 +1,11 @@
using Obfuz.EncryptionVM; using Obfuz.EncryptionVM;
using Obfuz.Settings; using Obfuz.Settings;
using Obfuz.Utils;
using System.IO;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using static UnityEditor.ObjectChangeEventStream;
using FileUtil = Obfuz.Utils.FileUtil;
namespace Obfuz.Unity namespace Obfuz.Unity
{ {
@ -15,8 +19,17 @@ namespace Obfuz.Unity
public static void GenerateEncryptionVM() public static void GenerateEncryptionVM()
{ {
EncryptionVMSettings settings = ObfuzSettings.Instance.encryptionVMSettings; EncryptionVMSettings settings = ObfuzSettings.Instance.encryptionVMSettings;
var generator = new VirtualMachineCodeGenerator(settings.codeGenerationSecretKey, settings.encryptionOpCodeCount); var generator = new VirtualMachineCodeGenerator(settings.codeGenerationSecret, settings.encryptionOpCodeCount);
generator.Generate(settings.CodeOutputPath); generator.Generate(settings.codeOutputPath);
}
[MenuItem("Obfuz/SaveSecretFile", priority = 63)]
public static void SaveSecretFile()
{
ObfuzSettings settings = ObfuzSettings.Instance;
var secretBytes = KeyGenerator.GenerateKey(settings.secret, VirtualMachine.SecretKeyLength);
Obfuscator.SaveKey(secretBytes, settings.secretOutputPath);
} }
[MenuItem("Obfuz/Documents/Quick Start")] [MenuItem("Obfuz/Documents/Quick Start")]

View File

@ -8,7 +8,13 @@ namespace Obfuz
{ {
public static class EncryptionService public static class EncryptionService
{ {
private static readonly IEncryptor _encryptor = new NullEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D }); private static IEncryptor _encryptor;
public static IEncryptor Encryptor
{
get => _encryptor;
set { _encryptor = value; }
}
public static void EncryptBlock(byte[] data, int ops, int salt) public static void EncryptBlock(byte[] data, int ops, int salt)
{ {