obfuz/Editor/Virtualization/DataVirtualizationPass.cs

141 lines
5.9 KiB
C#
Raw Normal View History

2025-04-21 21:02:47 +08:00
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using System;
2025-04-21 09:57:34 +08:00
using System.Collections.Generic;
using System.Linq;
2025-04-21 21:02:47 +08:00
using System.Runtime.CompilerServices;
2025-04-21 09:57:34 +08:00
using System.Text;
using System.Threading.Tasks;
2025-04-23 13:46:50 +08:00
using UnityEngine.Assertions;
2025-04-21 09:57:34 +08:00
namespace Obfuz.Virtualization
{
2025-04-21 21:02:47 +08:00
public class DataVirtualizationPass : MethodBodyObfuscationPassBase
2025-04-21 09:57:34 +08:00
{
2025-04-21 21:02:47 +08:00
private IDataObfuscationPolicy _dataObfuscatorPolicy;
private IDataObfuscator _dataObfuscator;
public override void Start(ObfuscatorContext ctx)
{
_dataObfuscatorPolicy = new ConfigDataObfuscationPolicy();
_dataObfuscator = new DefaultDataObfuscator();
}
public override void Stop(ObfuscatorContext ctx)
{
2025-04-28 11:37:48 +08:00
_dataObfuscator.Done();
2025-04-21 21:02:47 +08:00
}
protected override bool NeedObfuscateMethod(MethodDef method)
2025-04-21 09:57:34 +08:00
{
return _dataObfuscatorPolicy.NeedObfuscateMethod(method);
2025-04-21 21:02:47 +08:00
}
2025-04-21 09:57:34 +08:00
protected override bool TryObfuscateInstruction(MethodDef method, Instruction inst, IList<Instruction> instructions, int instructionIndex,
List<Instruction> outputInstructions, List<Instruction> totalFinalInstructions)
2025-04-21 21:02:47 +08:00
{
switch (inst.OpCode.OperandType)
2025-04-21 21:02:47 +08:00
{
case OperandType.InlineI:
case OperandType.InlineI8:
case OperandType.ShortInlineI:
case OperandType.ShortInlineR:
case OperandType.InlineR:
2025-04-21 21:02:47 +08:00
{
object operand = inst.Operand;
if (operand is int)
2025-04-21 21:02:47 +08:00
{
int value = (int)operand;
if (_dataObfuscatorPolicy.NeedObfuscateInt(method, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateInt(method, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is sbyte)
{
int value = (sbyte)operand;
if (_dataObfuscatorPolicy.NeedObfuscateInt(method, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateInt(method, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is byte)
{
int value = (byte)operand;
if (_dataObfuscatorPolicy.NeedObfuscateInt(method, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateInt(method, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is long)
{
long value = (long)operand;
if (_dataObfuscatorPolicy.NeedObfuscateLong(method, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateLong(method, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is float)
2025-04-21 21:02:47 +08:00
{
float value = (float)operand;
if (_dataObfuscatorPolicy.NeedObfuscateFloat(method, value))
2025-04-21 21:02:47 +08:00
{
_dataObfuscator.ObfuscateFloat(method, value, outputInstructions);
return true;
2025-04-21 21:02:47 +08:00
}
}
else if (operand is double)
2025-04-23 13:46:50 +08:00
{
double value = (double)operand;
if (_dataObfuscatorPolicy.NeedObfuscateDouble(method, value))
2025-04-23 13:46:50 +08:00
{
_dataObfuscator.ObfuscateDouble(method, value, outputInstructions);
return true;
2025-04-23 13:46:50 +08:00
}
}
return false;
2025-04-21 21:02:47 +08:00
}
case OperandType.InlineString:
2025-04-21 21:02:47 +08:00
{
//RuntimeHelpers.InitializeArray
string value = (string)inst.Operand;
if (_dataObfuscatorPolicy.NeedObfuscateString(method, value))
{
_dataObfuscator.ObfuscateString(method, value, outputInstructions);
return true;
}
return false;
2025-04-21 21:02:47 +08:00
}
case OperandType.InlineMethod:
{
if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)")
{
Instruction prevInst = instructions[instructionIndex - 1];
if (prevInst.OpCode.Code == Code.Ldtoken)
{
IField rvaField = (IField)prevInst.Operand;
FieldDef ravFieldDef = rvaField.ResolveFieldDefThrow();
byte[] data = ravFieldDef.InitialValue;
if (data != null && _dataObfuscatorPolicy.NeedObfuscateArray(method, data))
{
// remove prev ldtoken instruction
Assert.AreEqual(Code.Ldtoken, totalFinalInstructions[totalFinalInstructions.Count - 1].OpCode.Code);
totalFinalInstructions.RemoveAt(totalFinalInstructions.Count - 1);
_dataObfuscator.ObfuscateBytes(method, data, outputInstructions);
return true;
}
}
}
return false;
}
default: return false;
2025-04-22 08:13:58 +08:00
}
2025-04-21 09:57:34 +08:00
}
}
}