新增局部种子生成器

backup
walon 2025-05-13 08:56:19 +08:00
parent b4a0414f25
commit 62cabf939c
7 changed files with 20 additions and 18 deletions

View File

@ -17,7 +17,8 @@ namespace Obfuz.EncryptionVM
{ {
_vmGenerationSecretKey = vmGenerationSecretKey; _vmGenerationSecretKey = vmGenerationSecretKey;
byte[] byteGenerationSecretKey = KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength); byte[] byteGenerationSecretKey = KeyGenerator.GenerateKey(vmGenerationSecretKey, CodeGenerationSecretKeyLength);
_random = new RandomWithKey(byteGenerationSecretKey, 0); int[] intGenerationSecretKey = KeyGenerator.ConvertToIntKey(byteGenerationSecretKey);
_random = new RandomWithKey(intGenerationSecretKey, 0);
} }
private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength) private IEncryptionInstruction CreateRandomInstruction(int intSecretKeyLength)

View File

@ -38,7 +38,7 @@ namespace Obfuz.ObfusPasses.CallObfus
public override void Start() public override void Start()
{ {
var ctx = ObfuscationPassContext.Current; var ctx = ObfuscationPassContext.Current;
_random = ctx.random; _random = ctx.globalRandom;
_encryptor = ctx.encryptor; _encryptor = ctx.encryptor;
_dynamicProxyObfuscator = new DefaultCallProxyObfuscator(_random, _encryptor, ctx.constFieldAllocator, ctx.moduleEntityManager, _obfuscationLevel); _dynamicProxyObfuscator = new DefaultCallProxyObfuscator(_random, _encryptor, ctx.constFieldAllocator, ctx.moduleEntityManager, _obfuscationLevel);
_dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.toObfuscatedAssemblyNames, _configFiles); _dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.toObfuscatedAssemblyNames, _configFiles);

View File

@ -31,7 +31,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
{ {
var ctx = ObfuscationPassContext.Current; var ctx = ObfuscationPassContext.Current;
_dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFiles); _dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFiles);
_dataObfuscator = new DefaultConstEncryptor(ctx.random, ctx.encryptor, ctx.rvaDataAllocator, ctx.constFieldAllocator, ctx.moduleEntityManager, _encryptionLevel); _dataObfuscator = new DefaultConstEncryptor(ctx.globalRandom, ctx.encryptor, ctx.rvaDataAllocator, ctx.constFieldAllocator, ctx.moduleEntityManager, _encryptionLevel);
} }
public override void Stop() public override void Stop()

View File

@ -30,7 +30,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
public override void Start() public override void Start()
{ {
var ctx = ObfuscationPassContext.Current; var ctx = ObfuscationPassContext.Current;
_memoryEncryptor = new DefaultFieldEncryptor(ctx.random, ctx.encryptor, ctx.moduleEntityManager, _encryptionLevel); _memoryEncryptor = new DefaultFieldEncryptor(ctx.globalRandom, ctx.encryptor, ctx.moduleEntityManager, _encryptionLevel);
_encryptionPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFiles); _encryptionPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFiles);
} }

View File

@ -30,7 +30,9 @@ namespace Obfuz
public string obfuscatedAssemblyOutputDir; public string obfuscatedAssemblyOutputDir;
public IRandom random; public IRandom globalRandom;
public Func<int, IRandom> localScopeRandomCreator;
public IEncryptor encryptor; public IEncryptor encryptor;
public ConstFieldAllocator constFieldAllocator; public ConstFieldAllocator constFieldAllocator;
public RvaDataAllocator rvaDataAllocator; public RvaDataAllocator rvaDataAllocator;

View File

@ -31,7 +31,8 @@ namespace Obfuz
private readonly Pipeline _pipeline1 = new Pipeline(); private readonly Pipeline _pipeline1 = new Pipeline();
private readonly Pipeline _pipeline2 = new Pipeline(); private readonly Pipeline _pipeline2 = new Pipeline();
private readonly byte[] _secret; private readonly byte[] _byteSecret;
private readonly int[] _intSecret;
private readonly int _randomSeed; private readonly int _randomSeed;
private readonly string _encryptionVmGenerationSecret; private readonly string _encryptionVmGenerationSecret;
private readonly int _encryptionVmOpCodeCount; private readonly int _encryptionVmOpCodeCount;
@ -41,8 +42,9 @@ namespace Obfuz
public Obfuscator(ObfuscatorBuilder builder) public Obfuscator(ObfuscatorBuilder builder)
{ {
_secret = KeyGenerator.GenerateKey(builder.Secret, VirtualMachine.SecretKeyLength); _byteSecret = KeyGenerator.GenerateKey(builder.Secret, VirtualMachine.SecretKeyLength);
SaveKey(_secret, builder.SecretOutputPath); _intSecret = KeyGenerator.ConvertToIntKey(_byteSecret);
SaveKey(_byteSecret, builder.SecretOutputPath);
_randomSeed = builder.RandomSeed; _randomSeed = builder.RandomSeed;
_encryptionVmGenerationSecret = builder.EncryptionVmGenerationSecretKey; _encryptionVmGenerationSecret = builder.EncryptionVmGenerationSecretKey;
_encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount; _encryptionVmOpCodeCount = builder.EncryptionVmOpCodeCount;
@ -109,7 +111,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, _secret); var vms = new VirtualMachineSimulator(vm, _byteSecret);
var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies() var generatedVmTypes = AppDomain.CurrentDomain.GetAssemblies()
.Select(assembly => assembly.GetType("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine")) .Select(assembly => assembly.GetType("Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine"))
@ -124,7 +126,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[] { _secret } ); var gvmInstance = (IEncryptor)Activator.CreateInstance(generatedVmTypes[0], new object[] { _byteSecret } );
int testValue = 11223344; int testValue = 11223344;
for (int i = 0; i < vm.opCodes.Length; i++) for (int i = 0; i < vm.opCodes.Length; i++)
@ -152,16 +154,12 @@ namespace Obfuz
private void OnPreObfuscation(Pipeline pipeline) private void OnPreObfuscation(Pipeline pipeline)
{ {
AssemblyCache assemblyCache = new AssemblyCache(new PathAssemblyResolver(_assemblySearchDirs.ToArray())); AssemblyCache assemblyCache = new AssemblyCache(new PathAssemblyResolver(_assemblySearchDirs.ToArray()));
List<ModuleDef> toObfuscatedModules = new List<ModuleDef>(); List<ModuleDef> toObfuscatedModules = new List<ModuleDef>();
List<ModuleDef> obfuscatedAndNotObfuscatedModules = new List<ModuleDef>(); List<ModuleDef> obfuscatedAndNotObfuscatedModules = new List<ModuleDef>();
LoadAssemblies(assemblyCache, toObfuscatedModules, obfuscatedAndNotObfuscatedModules); LoadAssemblies(assemblyCache, toObfuscatedModules, obfuscatedAndNotObfuscatedModules);
var random = new RandomWithKey(_intSecret, _randomSeed);
var random = new RandomWithKey(_secret, _randomSeed);
var encryptor = CreateEncryptionVirtualMachine(); var encryptor = CreateEncryptionVirtualMachine();
var moduleEntityManager = new GroupByModuleEntityManager(); var moduleEntityManager = new GroupByModuleEntityManager();
var rvaDataAllocator = new RvaDataAllocator(random, encryptor, moduleEntityManager); var rvaDataAllocator = new RvaDataAllocator(random, encryptor, moduleEntityManager);
@ -176,7 +174,8 @@ namespace Obfuz
obfuscatedAssemblyOutputDir = _obfuscatedAssemblyOutputDir, obfuscatedAssemblyOutputDir = _obfuscatedAssemblyOutputDir,
moduleEntityManager = moduleEntityManager, moduleEntityManager = moduleEntityManager,
random = random, globalRandom = random,
localScopeRandomCreator = (seed) => new RandomWithKey(_intSecret, _randomSeed ^ seed),
encryptor = encryptor, encryptor = encryptor,
rvaDataAllocator = rvaDataAllocator, rvaDataAllocator = rvaDataAllocator,
constFieldAllocator = constFieldAllocator, constFieldAllocator = constFieldAllocator,

View File

@ -19,9 +19,9 @@ namespace Obfuz.Utils
private int _seed; private int _seed;
public RandomWithKey(byte[] key, int seed) public RandomWithKey(int[] key, int seed)
{ {
_key = KeyGenerator.ConvertToIntKey(key); _key = key;
_seed = seed; _seed = seed;
} }