fix: 修复 ConstFieldAllocator和 RvaDataAllocator的encryption level固定取4导致EncryptionVirtualMachine的Opcode个数超过256时打印了大量"OpCode overflow"警告日志的问题

main
walon 2025-08-26 20:58:09 +08:00
parent 2576236960
commit 4ad3ed76dc
5 changed files with 14 additions and 5 deletions

View File

@ -172,7 +172,7 @@ namespace Obfuz.Data
{ {
ConstFieldInfo constInfo = _field2Fields[field]; ConstFieldInfo constInfo = _field2Fields[field];
IRandom localRandom = _randomCreator(HashUtil.ComputePrimitiveOrStringOrBytesHashCode(constInfo.value)); IRandom localRandom = _randomCreator(HashUtil.ComputePrimitiveOrStringOrBytesHashCode(constInfo.value));
int ops = EncryptionUtil.GenerateEncryptionOpCodes(localRandom, _encryptor, 4); int ops = EncryptionUtil.GenerateEncryptionOpCodes(localRandom, _encryptor, EncryptionScopeInfo.MaxEncryptionLevel, false);
int salt = localRandom.NextInt(); int salt = localRandom.NextInt();
switch (constInfo.value) switch (constInfo.value)
{ {

View File

@ -238,7 +238,7 @@ namespace Obfuz.Data
int verifyIntValue = 0x12345678; int verifyIntValue = 0x12345678;
EncryptionScopeInfo encryptionScope = this.EncryptionScope; EncryptionScopeInfo encryptionScope = this.EncryptionScope;
IRandom verifyRandom = encryptionScope.localRandomCreator(verifyIntValue); IRandom verifyRandom = encryptionScope.localRandomCreator(verifyIntValue);
int verifyOps = EncryptionUtil.GenerateEncryptionOpCodes(verifyRandom, encryptionScope.encryptor, 4); int verifyOps = EncryptionUtil.GenerateEncryptionOpCodes(verifyRandom, encryptionScope.encryptor, EncryptionScopeInfo.MaxEncryptionLevel, false);
int verifySalt = verifyRandom.NextInt(); int verifySalt = verifyRandom.NextInt();
int encryptedVerifyIntValue = encryptionScope.encryptor.Encrypt(verifyIntValue, verifyOps, verifySalt); int encryptedVerifyIntValue = encryptionScope.encryptor.Encrypt(verifyIntValue, verifyOps, verifySalt);

View File

@ -2,6 +2,7 @@
using dnlib.DotNet.Emit; using dnlib.DotNet.Emit;
using Obfuz.Data; using Obfuz.Data;
using Obfuz.Emit; using Obfuz.Emit;
using Obfuz.Settings;
using Obfuz.Utils; using Obfuz.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -44,6 +45,7 @@ namespace Obfuz.ObfusPasses.CallObfus
public MethodDef proxyMethod; public MethodDef proxyMethod;
} }
private readonly Dictionary<MethodKey, CallInfo> _callMethods = new Dictionary<MethodKey, CallInfo>(); private readonly Dictionary<MethodKey, CallInfo> _callMethods = new Dictionary<MethodKey, CallInfo>();
private CallObfuscationSettingsFacade _settings;
public DelegateProxyAllocator() public DelegateProxyAllocator()
{ {
@ -53,6 +55,7 @@ namespace Obfuz.ObfusPasses.CallObfus
public override void Init() public override void Init()
{ {
_delegateInstanceHolderType = CreateDelegateInstanceHolderTypeDef(); _delegateInstanceHolderType = CreateDelegateInstanceHolderTypeDef();
_settings = CallObfusPass.CurrentSettings;
} }
private string AllocateDelegateTypeName(MethodSig delegateInvokeSig) private string AllocateDelegateTypeName(MethodSig delegateInvokeSig)
@ -241,7 +244,7 @@ namespace Obfuz.ObfusPasses.CallObfus
ins.Add(Instruction.Create(OpCodes.Dup)); ins.Add(Instruction.Create(OpCodes.Dup));
IRandom localRandom = encryptionScope.localRandomCreator(HashUtil.ComputePrimitiveOrStringOrBytesHashCode(ci.key1)); IRandom localRandom = encryptionScope.localRandomCreator(HashUtil.ComputePrimitiveOrStringOrBytesHashCode(ci.key1));
int ops = EncryptionUtil.GenerateEncryptionOpCodes(localRandom, encryptionScope.encryptor, 4); int ops = EncryptionUtil.GenerateEncryptionOpCodes(localRandom, encryptionScope.encryptor, _settings.obfuscationLevel);
int salt = localRandom.NextInt(); int salt = localRandom.NextInt();
int encryptedValue = encryptionScope.encryptor.Encrypt(ci.index, ops, salt); int encryptedValue = encryptionScope.encryptor.Encrypt(ci.index, ops, salt);

View File

@ -9,6 +9,8 @@ namespace Obfuz
public class EncryptionScopeInfo public class EncryptionScopeInfo
{ {
public const int MaxEncryptionLevel = 4;
public readonly IEncryptor encryptor; public readonly IEncryptor encryptor;
public readonly RandomCreator localRandomCreator; public readonly RandomCreator localRandomCreator;

View File

@ -16,7 +16,7 @@ namespace Obfuz.Utils
return count; return count;
} }
public static int GenerateEncryptionOpCodes(IRandom random, IEncryptor encryptor, int encryptionLevel) public static int GenerateEncryptionOpCodes(IRandom random, IEncryptor encryptor, int encryptionLevel, bool logWarningWhenExceedMaxOps = true)
{ {
if (encryptionLevel <= 0 || encryptionLevel > 4) if (encryptionLevel <= 0 || encryptionLevel > 4)
{ {
@ -32,7 +32,11 @@ namespace Obfuz.Utils
newOps |= (uint)op; newOps |= (uint)op;
if (newOps > uint.MaxValue) if (newOps > uint.MaxValue)
{ {
Debug.LogWarning($"OpCode overflow. encryptionLevel:{encryptionLevel}, vmOpCodeCount:{vmOpCodeCount}"); if (logWarningWhenExceedMaxOps)
{
Debug.LogWarning($"OpCode overflow. encryptionLevel:{encryptionLevel}, vmOpCodeCount:{vmOpCodeCount}");
}
break;
} }
else else
{ {