- 重构设置中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 Pipeline _pipeline = new Pipeline();
private readonly byte[] _secretKey;
private readonly int _globalRandomSeed;
private readonly string _encryptionVmGenerationSecretKey;
private readonly byte[] _secret;
private readonly int _randomSeed;
private readonly string _encryptionVmGenerationSecret;
private readonly int _encryptionVmOpCodeCount;
private readonly string _encryptionVmCodeFile;
@ -39,9 +39,10 @@ namespace Obfuz
public Obfuscator(ObfuscatorBuilder builder)
{
_secretKey = KeyGenerator.GenerateKey(builder.SecretKey, VirtualMachine.SecretKeyLength);
_globalRandomSeed = builder.GlobalRandomSeed;
_encryptionVmGenerationSecretKey = builder.EncryptionVmGenerationSecretKey;
_secret = KeyGenerator.GenerateKey(builder.Secret, VirtualMachine.SecretKeyLength);
SaveKey(_secret, builder.SecretOutputPath);
_randomSeed = builder.RandomSeed;
_encryptionVmGenerationSecret = builder.EncryptionVmGenerationSecretKey;
_encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount;
_encryptionVmCodeFile = builder.EncryptionVmCodeFile;
@ -58,6 +59,13 @@ namespace Obfuz
_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()
{
OnPreObfuscation();
@ -67,7 +75,7 @@ namespace Obfuz
private IEncryptor CreateEncryptionVirtualMachine()
{
var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecretKey);
var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecret);
var vm = vmCreator.CreateVirtualMachine(_encryptionVmOpCodeCount);
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!");
}
var vms = new VirtualMachineSimulator(vm, _secretKey);
var vms = new VirtualMachineSimulator(vm, _secret);
var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies()
.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!");
}
var gvmInstance = (IEncryptor)Activator.CreateInstance(generatedVmTypes[0], new object[] { _secretKey } );
var gvmInstance = (IEncryptor)Activator.CreateInstance(generatedVmTypes[0], new object[] { _secret } );
int testValue = 11223344;
for (int i = 0; i < vm.opCodes.Length; i++)
@ -125,7 +133,7 @@ namespace Obfuz
LoadAssemblies();
var random = new RandomWithKey(_secretKey, _globalRandomSeed);
var random = new RandomWithKey(_secret, _randomSeed);
var encryptor = CreateEncryptionVirtualMachine();
var rvaDataAllocator = new RvaDataAllocator(random, encryptor);
var constFieldAllocator = new ConstFieldAllocator(encryptor, random, rvaDataAllocator);

View File

@ -15,8 +15,9 @@ namespace Obfuz
{
public class ObfuscatorBuilder
{
private string _secretKey;
private int _globalRandomSeed;
private string _secret;
private string _secretOutputPath;
private int _randomSeed;
private string _encryptionVmGenerationSecretKey;
private int _encryptionVmOpCodeCount;
private string _encryptionVmCodeFile;
@ -28,16 +29,22 @@ namespace Obfuz
private string _obfuscatedAssemblyOutputDir;
private List<IObfuscationPass> _obfuscationPasses = new List<IObfuscationPass>();
public string SecretKey
public string Secret
{
get => _secretKey;
set => _secretKey = value;
get => _secret;
set => _secret = value;
}
public int GlobalRandomSeed
public string SecretOutputPath
{
get => _globalRandomSeed;
set => _globalRandomSeed = value;
get => _secretOutputPath;
set => _secretOutputPath = value;
}
public int RandomSeed
{
get => _randomSeed;
set => _randomSeed = value;
}
public string EncryptionVmGenerationSecretKey
@ -104,11 +111,12 @@ namespace Obfuz
{
var builder = new ObfuscatorBuilder
{
_secretKey = settings.secretKey,
_globalRandomSeed = settings.globalRandomSeed,
_encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecretKey,
_secret = settings.secret,
_secretOutputPath = settings.secretOutputPath,
_randomSeed = settings.randomSeed,
_encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecret,
_encryptionVmOpCodeCount = settings.encryptionVMSettings.encryptionOpCodeCount,
_encryptionVmCodeFile = settings.encryptionVMSettings.CodeOutputPath,
_encryptionVmCodeFile = settings.encryptionVMSettings.codeOutputPath,
_toObfuscatedAssemblyNames = settings.toObfuscatedAssemblyNames.ToList(),
_notObfuscatedAssemblyNamesReferencingObfuscated = settings.notObfuscatedAssemblyNamesReferencingObfuscated.ToList(),
_assemblySearchDirs = settings.extraAssemblySearchDirs.ToList(),

View File

@ -12,14 +12,12 @@ namespace Obfuz.Settings
public class EncryptionVMSettings
{
[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")]
public int encryptionOpCodeCount = 256;
[Tooltip("encryption virtual machine source code output dir")]
public string codeOutputDir = "Assets/Obfuz";
public string CodeOutputPath { get => Path.Combine(codeOutputDir, "GeneratedEncryptionVirtualMachine.cs"); }
[Tooltip("encryption virtual machine source code output path")]
public string codeOutputPath = "Assets/Obfuz/GeneratedEncryptionVirtualMachine.cs";
}
}

View File

@ -27,10 +27,13 @@ namespace Obfuz.Settings
public ObfuscationPassType enabledObfuscationPasses = ObfuscationPassType.All;
[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")]
public int globalRandomSeed = 0;
public int randomSeed = 0;
[Tooltip("encryption virtual machine settings")]
public EncryptionVMSettings encryptionVMSettings;

View File

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

View File

@ -1,7 +1,11 @@
using Obfuz.EncryptionVM;
using Obfuz.Settings;
using Obfuz.Utils;
using System.IO;
using UnityEditor;
using UnityEngine;
using static UnityEditor.ObjectChangeEventStream;
using FileUtil = Obfuz.Utils.FileUtil;
namespace Obfuz.Unity
{
@ -15,8 +19,17 @@ namespace Obfuz.Unity
public static void GenerateEncryptionVM()
{
EncryptionVMSettings settings = ObfuzSettings.Instance.encryptionVMSettings;
var generator = new VirtualMachineCodeGenerator(settings.codeGenerationSecretKey, settings.encryptionOpCodeCount);
generator.Generate(settings.CodeOutputPath);
var generator = new VirtualMachineCodeGenerator(settings.codeGenerationSecret, settings.encryptionOpCodeCount);
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")]

View File

@ -8,7 +8,13 @@ namespace Obfuz
{
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)
{