EncryptionVM重构

backup
walon 2025-05-11 17:36:58 +08:00
parent fcb2cb1d89
commit a1f947416d
7 changed files with 80 additions and 36 deletions

View File

@ -4,12 +4,13 @@
{ {
public const int SecretKeyLength = 1024; public const int SecretKeyLength = 1024;
public readonly int[] secretKey; public readonly int version;
public readonly string codeGenerationSecretKey;
public readonly EncryptionInstructionWithOpCode[] opCodes; public readonly EncryptionInstructionWithOpCode[] opCodes;
public VirtualMachine(int[] secretKey, EncryptionInstructionWithOpCode[] opCodes) public VirtualMachine(int version, string codeGenerationSecretKey, EncryptionInstructionWithOpCode[] opCodes)
{ {
this.secretKey = secretKey; this.codeGenerationSecretKey = codeGenerationSecretKey;
this.opCodes = opCodes; this.opCodes = opCodes;
} }
} }

View File

@ -0,0 +1,27 @@
using Obfuz.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.EncryptionVM
{
public class VirtualMachineCodeGenerator
{
private readonly string _vmCodeGenerateSecretKey;
private readonly IRandom _random;
private VirtualMachine _vm;
public VirtualMachineCodeGenerator(string vmCodeGenerateSecretKey, int opCount)
{
_vmCodeGenerateSecretKey = vmCodeGenerateSecretKey;
_vm = new VirtualMachineCreator(_vmCodeGenerateSecretKey).CreateVirtualMachine(opCount);
}
public void Generate(string outputFile)
{
}
}
}

View File

@ -6,15 +6,18 @@ namespace Obfuz.EncryptionVM
{ {
public class VirtualMachineCreator public class VirtualMachineCreator
{ {
private readonly int[] _vmGenerationSecretKey; private readonly string _vmGenerationSecretKey;
private readonly IRandom _random; private readonly IRandom _random;
public const int CodeGenerationSecretKeyLength = 1024; public const int CodeGenerationSecretKeyLength = 1024;
public VirtualMachineCreator(string vmGenerationSecretKey, byte[] encryptionSecretKey) public const int VirtualMachineVersion = 1;
public VirtualMachineCreator(string vmGenerationSecretKey)
{ {
_vmGenerationSecretKey = KeyGenerator.ConvertToIntKey(KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength)); _vmGenerationSecretKey = vmGenerationSecretKey;
_random = new RandomWithKey(encryptionSecretKey, 0); byte[] byteGenerationSecretKey = KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength);
_random = new RandomWithKey(byteGenerationSecretKey, 0);
} }
private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength) private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength)
@ -35,20 +38,25 @@ namespace Obfuz.EncryptionVM
private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code) private EncryptionInstructionWithOpCode CreateEncryptOpCode(ushort code)
{ {
IEncryptionInstruction inst = CreateRandomInstruction(VirtualMachine.SecretKeyLength / sizeof(int)); IEncryptionInstruction inst = CreateRandomInstruction(VirtualMachine.SecretKeyLength / sizeof(int));
Assert.AreEqual(1234, inst.Decrypt(inst.Encrypt(1234, _vmGenerationSecretKey, 0x12345678), _vmGenerationSecretKey, 0x12345678));
return new EncryptionInstructionWithOpCode(code, inst); return new EncryptionInstructionWithOpCode(code, inst);
} }
public VirtualMachine CreateVirtualMachine(int opCodeCount) public VirtualMachine CreateVirtualMachine(int opCodeCount)
{ {
Assert.IsTrue(opCodeCount > 0); if (opCodeCount < 64)
Assert.AreEqual(0, opCodeCount & (opCodeCount - 1)); {
throw new System.Exception("OpCode count should be >= 64");
}
if ((opCodeCount & (opCodeCount - 1)) != 0)
{
throw new System.Exception("OpCode count should be power of 2");
}
var opCodes = new EncryptionInstructionWithOpCode[opCodeCount]; var opCodes = new EncryptionInstructionWithOpCode[opCodeCount];
for (int i = 0; i < opCodes.Length; i++) for (int i = 0; i < opCodes.Length; i++)
{ {
opCodes[i] = CreateEncryptOpCode((ushort)i); opCodes[i] = CreateEncryptOpCode((ushort)i);
} }
return new VirtualMachine(_vmGenerationSecretKey, opCodes); return new VirtualMachine(VirtualMachineVersion, _vmGenerationSecretKey, opCodes);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System; using Obfuz.Utils;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -14,10 +15,10 @@ namespace Obfuz.EncryptionVM
private readonly EncryptionInstructionWithOpCode[] _opCodes; private readonly EncryptionInstructionWithOpCode[] _opCodes;
private readonly int[] _secretKey; private readonly int[] _secretKey;
public VirtualMachineSimulator(VirtualMachine vm) public VirtualMachineSimulator(VirtualMachine vm, byte[] byteSecretKey)
{ {
_opCodes = vm.opCodes; _opCodes = vm.opCodes;
_secretKey = vm.secretKey; _secretKey = KeyGenerator.ConvertToIntKey(byteSecretKey);
VerifyInstructions(); VerifyInstructions();
} }

View File

@ -31,27 +31,25 @@ namespace Obfuz
private readonly Pipeline _pipeline = new Pipeline(); private readonly Pipeline _pipeline = new Pipeline();
private readonly byte[] _secretKey; private readonly byte[] _secretKey;
private readonly int _globalRandomSeed; private readonly int _globalRandomSeed;
private readonly string _encryptionVmSecretKey; private readonly string _encryptionVmGenerationSecretKey;
private readonly int _encryptionVmOpCodeCount;
private ObfuscationPassContext _ctx; private ObfuscationPassContext _ctx;
public Obfuscator(List<string> toObfuscatedAssemblyNames, public Obfuscator(ObfuscatorBuilder builder)
List<string> notObfuscatedAssemblyNamesReferencingObfuscated,
List<string> assemblySearchDirs,
string obfuscatedAssemblyOutputDir,
List<IObfuscationPass> obfuscationPasses, string rawSecretKey, int globalRandomSeed, string encryptionVmSecretKey)
{ {
_secretKey = KeyGenerator.GenerateKey(rawSecretKey, VirtualMachine.SecretKeyLength); _secretKey = KeyGenerator.GenerateKey(builder.SecretKey, VirtualMachine.SecretKeyLength);
_globalRandomSeed = globalRandomSeed; _globalRandomSeed = builder.GlobalRandomSeed;
_encryptionVmSecretKey = encryptionVmSecretKey; _encryptionVmGenerationSecretKey = builder.EncryptionVmGenerationSecretKey;
_encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount;
_toObfuscatedAssemblyNames = toObfuscatedAssemblyNames; _toObfuscatedAssemblyNames = builder.ToObfuscatedAssemblyNames;
_notObfuscatedAssemblyNamesReferencingObfuscated = notObfuscatedAssemblyNamesReferencingObfuscated; _notObfuscatedAssemblyNamesReferencingObfuscated = builder.NotObfuscatedAssemblyNamesReferencingObfuscated;
_obfuscatedAssemblyOutputDir = obfuscatedAssemblyOutputDir; _obfuscatedAssemblyOutputDir = builder.ObfuscatedAssemblyOutputDir;
GroupByModuleEntityManager.Reset(); GroupByModuleEntityManager.Reset();
_assemblyCache = new AssemblyCache(new PathAssemblyResolver(assemblySearchDirs.ToArray())); _assemblyCache = new AssemblyCache(new PathAssemblyResolver(builder.AssemblySearchDirs.ToArray()));
foreach (var pass in obfuscationPasses) foreach (var pass in builder.ObfuscationPasses)
{ {
_pipeline.AddPass(pass); _pipeline.AddPass(pass);
} }
@ -67,9 +65,9 @@ namespace Obfuz
private IEncryptor CreateEncryptionVirtualMachine() private IEncryptor CreateEncryptionVirtualMachine()
{ {
var vmCreator = new VirtualMachineCreator(_encryptionVmSecretKey, _secretKey); var vmCreator = new VirtualMachineCreator(_encryptionVmGenerationSecretKey);
var vm = vmCreator.CreateVirtualMachine(1); var vm = vmCreator.CreateVirtualMachine(_encryptionVmOpCodeCount);
return new VirtualMachineSimulator(vm); return new VirtualMachineSimulator(vm, _secretKey);
} }
private void OnPreObfuscation() private void OnPreObfuscation()

View File

@ -16,6 +16,7 @@ namespace Obfuz
private string _secretKey; private string _secretKey;
private int _globalRandomSeed; private int _globalRandomSeed;
private string _encryptionVmGenerationSecretKey; private string _encryptionVmGenerationSecretKey;
private int _encryptionVmOpCodeCount;
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>();
@ -41,6 +42,12 @@ namespace Obfuz
set => _encryptionVmGenerationSecretKey = value; set => _encryptionVmGenerationSecretKey = value;
} }
public int EncryptionVmOpCodeCount
{
get => _encryptionVmOpCodeCount;
set => _encryptionVmOpCodeCount = value;
}
public List<string> ToObfuscatedAssemblyNames public List<string> ToObfuscatedAssemblyNames
{ {
get => _toObfuscatedAssemblyNames; get => _toObfuscatedAssemblyNames;
@ -65,6 +72,8 @@ namespace Obfuz
set => _obfuscatedAssemblyOutputDir = value; set => _obfuscatedAssemblyOutputDir = value;
} }
public List<IObfuscationPass> ObfuscationPasses { get => _obfuscationPasses; set => _obfuscationPasses = value; }
public void InsertTopPriorityAssemblySearchDirs(List<string> assemblySearchDirs) public void InsertTopPriorityAssemblySearchDirs(List<string> assemblySearchDirs)
{ {
_assemblySearchDirs.InsertRange(0, assemblySearchDirs); _assemblySearchDirs.InsertRange(0, assemblySearchDirs);
@ -78,11 +87,7 @@ namespace Obfuz
public Obfuscator Build() public Obfuscator Build()
{ {
return new Obfuscator(_toObfuscatedAssemblyNames, return new Obfuscator(this);
_notObfuscatedAssemblyNamesReferencingObfuscated,
_assemblySearchDirs,
_obfuscatedAssemblyOutputDir,
_obfuscationPasses, _secretKey, _globalRandomSeed, _encryptionVmGenerationSecretKey);
} }
public static ObfuscatorBuilder FromObfuzSettings(ObfuzSettings settings, BuildTarget target) public static ObfuscatorBuilder FromObfuzSettings(ObfuzSettings settings, BuildTarget target)
@ -92,6 +97,7 @@ namespace Obfuz
_secretKey = settings.secretKey, _secretKey = settings.secretKey,
_globalRandomSeed = settings.globalRandomSeed, _globalRandomSeed = settings.globalRandomSeed,
_encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecretKey, _encryptionVmGenerationSecretKey = settings.encryptionVMSettings.codeGenerationSecretKey,
_encryptionVmOpCodeCount = settings.encryptionVMSettings.encryptionOpCodeCount,
_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

@ -13,6 +13,9 @@ namespace Obfuz.Settings
[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 codeGenerationSecretKey = "Obfuz";
[Tooltip("encryption OpCode count, should be power of 2 and >= 64")]
public int encryptionOpCodeCount = 256;
[Tooltip("encryption virtual machine source code output dir")] [Tooltip("encryption virtual machine source code output dir")]
public string codeOutputDir = "Assets/Obfuz"; public string codeOutputDir = "Assets/Obfuz";
} }