Compare commits
4 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
0b6b8a92c0 | |
|
|
aefec5c024 | |
|
|
67f3dd4cf3 | |
|
|
79c81e234c |
|
|
@ -124,7 +124,7 @@ namespace Obfuz.Emit
|
||||||
Assert.IsNotNull(_decryptFromRvaBytes);
|
Assert.IsNotNull(_decryptFromRvaBytes);
|
||||||
_decryptFromRvaString = mod.Import(encryptionServiceType.GetMethod("DecryptFromRvaString", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
|
_decryptFromRvaString = mod.Import(encryptionServiceType.GetMethod("DecryptFromRvaString", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
|
||||||
Assert.IsNotNull(_decryptFromRvaString);
|
Assert.IsNotNull(_decryptFromRvaString);
|
||||||
_decryptInitializeArray = mod.Import(encryptionServiceType.GetMethod("DecryptInitializeArray", new[] { typeof(System.Array), typeof(System.RuntimeFieldHandle), typeof(int), typeof(int), typeof(int) }));
|
_decryptInitializeArray = mod.Import(encryptionServiceType.GetMethod("DecryptInitializeArray", new[] { typeof(System.Array), typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
|
||||||
Assert.IsNotNull(_decryptInitializeArray);
|
Assert.IsNotNull(_decryptInitializeArray);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,9 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
{
|
{
|
||||||
// don't need cache for byte array obfuscation
|
// don't need cache for byte array obfuscation
|
||||||
needCache = false;
|
needCache = false;
|
||||||
|
// Drop ldtoken RuntimeFieldHandle; DecryptInitializeArray now loads from RvaDataAllocator byte[].
|
||||||
|
prevInst.OpCode = OpCodes.Nop;
|
||||||
|
prevInst.Operand = null;
|
||||||
_dataObfuscator.ObfuscateBytes(method, needCache, ravFieldDef, data, outputInstructions);
|
_dataObfuscator.ObfuscateBytes(method, needCache, ravFieldDef, data, outputInstructions);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -266,15 +266,15 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
|
|
||||||
class EncryptedRvaDataInfo
|
class EncryptedRvaDataInfo
|
||||||
{
|
{
|
||||||
public readonly FieldDef fieldDef;
|
public readonly RvaData rvaData;
|
||||||
public readonly byte[] originalBytes;
|
public readonly byte[] originalBytes;
|
||||||
public readonly byte[] encryptedBytes;
|
public readonly byte[] encryptedBytes;
|
||||||
public readonly int opts;
|
public readonly int opts;
|
||||||
public readonly int salt;
|
public readonly int salt;
|
||||||
|
|
||||||
public EncryptedRvaDataInfo(FieldDef fieldDef, byte[] originalBytes, byte[] encryptedBytes, int opts, int salt)
|
public EncryptedRvaDataInfo(RvaData rvaData, byte[] originalBytes, byte[] encryptedBytes, int opts, int salt)
|
||||||
{
|
{
|
||||||
this.fieldDef = fieldDef;
|
this.rvaData = rvaData;
|
||||||
this.originalBytes = originalBytes;
|
this.originalBytes = originalBytes;
|
||||||
this.encryptedBytes = encryptedBytes;
|
this.encryptedBytes = encryptedBytes;
|
||||||
this.opts = opts;
|
this.opts = opts;
|
||||||
|
|
@ -296,8 +296,11 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
byte[] encryptedBytes = (byte[])originalBytes.Clone();
|
byte[] encryptedBytes = (byte[])originalBytes.Clone();
|
||||||
encryptionScope.encryptor.EncryptBlock(encryptedBytes, ops, salt);
|
encryptionScope.encryptor.EncryptBlock(encryptedBytes, ops, salt);
|
||||||
Assert.AreNotEqual(originalBytes, encryptedBytes, "Original bytes should not be the same as encrypted bytes.");
|
Assert.AreNotEqual(originalBytes, encryptedBytes, "Original bytes should not be the same as encrypted bytes.");
|
||||||
encryptedRvaData = new EncryptedRvaDataInfo(fieldDef, originalBytes, encryptedBytes, ops, salt);
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(fieldDef.Module);
|
||||||
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedBytes);
|
||||||
|
encryptedRvaData = new EncryptedRvaDataInfo(rvaData, originalBytes, encryptedBytes, ops, salt);
|
||||||
_encryptedRvaFields.Add(fieldDef, encryptedRvaData);
|
_encryptedRvaFields.Add(fieldDef, encryptedRvaData);
|
||||||
|
// Keep ciphertext in the original RVA field so plaintext is not shipped; runtime loads via RvaDataAllocator.
|
||||||
fieldDef.InitialValue = encryptedBytes;
|
fieldDef.InitialValue = encryptedBytes;
|
||||||
byte[] decryptedBytes = (byte[])encryptedBytes.Clone();
|
byte[] decryptedBytes = (byte[])encryptedBytes.Clone();
|
||||||
encryptionScope.encryptor.DecryptBlock(decryptedBytes, ops, salt);
|
encryptionScope.encryptor.DecryptBlock(decryptedBytes, ops, salt);
|
||||||
|
|
@ -313,6 +316,8 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
Assert.AreEqual(value.Length, encryptedData.encryptedBytes.Length);
|
Assert.AreEqual(value.Length, encryptedData.encryptedBytes.Length);
|
||||||
|
|
||||||
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, encryptedData.rvaData.field));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.rvaData.offset));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.encryptedBytes.Length));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.encryptedBytes.Length));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.opts));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.opts));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.salt));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedData.salt));
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.NameMakers
|
||||||
|
|
||||||
public virtual string GetNewName(ParamDef param, string originalName)
|
public virtual string GetNewName(ParamDef param, string originalName)
|
||||||
{
|
{
|
||||||
return "1";
|
return $"_{param.Sequence}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetNewName(FieldDef fieldDef, string originalName)
|
public string GetNewName(FieldDef fieldDef, string originalName)
|
||||||
|
|
|
||||||
|
|
@ -146,6 +146,8 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
||||||
private readonly CachedDictionary<TypeDef, bool> _isSerializableCache;
|
private readonly CachedDictionary<TypeDef, bool> _isSerializableCache;
|
||||||
private readonly CachedDictionary<TypeDef, bool> _isInheritFromMonoBehaviourOrScriptableObjectCache;
|
private readonly CachedDictionary<TypeDef, bool> _isInheritFromMonoBehaviourOrScriptableObjectCache;
|
||||||
private readonly CachedDictionary<TypeDef, bool> _isScriptOrSerializableTypeCache;
|
private readonly CachedDictionary<TypeDef, bool> _isScriptOrSerializableTypeCache;
|
||||||
|
private readonly CachedDictionary<TypeDef, bool> _isAnyNestedTypeHasBurstCompileAttributeCache;
|
||||||
|
private readonly CachedDictionary<TypeDef, bool> _isInheritAnyNestingOrAnyNestedTypeHasBurstCompileAttributeCache;
|
||||||
|
|
||||||
public UnityRenamePolicy()
|
public UnityRenamePolicy()
|
||||||
{
|
{
|
||||||
|
|
@ -153,6 +155,8 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
||||||
_isSerializableCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsSerializableType);
|
_isSerializableCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsSerializableType);
|
||||||
_isInheritFromMonoBehaviourOrScriptableObjectCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsScriptType);
|
_isInheritFromMonoBehaviourOrScriptableObjectCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsScriptType);
|
||||||
_isScriptOrSerializableTypeCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsScriptOrSerializableType);
|
_isScriptOrSerializableTypeCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsScriptOrSerializableType);
|
||||||
|
_isAnyNestedTypeHasBurstCompileAttributeCache = new CachedDictionary<TypeDef, bool>(IsAnyNestedTypeHasBurstCompileAttribute);
|
||||||
|
_isInheritAnyNestingOrAnyNestedTypeHasBurstCompileAttributeCache = new CachedDictionary<TypeDef, bool>(IsAnyNestingOrNestedTypeHasBurstCompileAttribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsUnitySourceGeneratedAssemblyType(TypeDef typeDef)
|
private bool IsUnitySourceGeneratedAssemblyType(TypeDef typeDef)
|
||||||
|
|
@ -197,6 +201,30 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool IsInheritAnyNestingTypeHasBurstCompileAttribute(TypeDef typeDef)
|
||||||
|
{
|
||||||
|
if (typeDef.Module.IsCoreLibraryModule == true)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (MetaUtil.HasBurstCompileAttribute(typeDef))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
TypeDef parentTypeDef = typeDef.DeclaringType;
|
||||||
|
return parentTypeDef != null && IsInheritAnyNestingTypeHasBurstCompileAttribute(parentTypeDef);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsAnyNestedTypeHasBurstCompileAttribute(TypeDef typeDef)
|
||||||
|
{
|
||||||
|
return typeDef.NestedTypes.Any(nestedType => MetaUtil.HasBurstCompileAttribute(nestedType) || _isAnyNestedTypeHasBurstCompileAttributeCache.GetValue(nestedType));
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsAnyNestingOrNestedTypeHasBurstCompileAttribute(TypeDef typeDef)
|
||||||
|
{
|
||||||
|
return IsInheritAnyNestingTypeHasBurstCompileAttribute(typeDef) || _isAnyNestedTypeHasBurstCompileAttributeCache.GetValue(typeDef);
|
||||||
|
}
|
||||||
|
|
||||||
public override bool NeedRename(TypeDef typeDef)
|
public override bool NeedRename(TypeDef typeDef)
|
||||||
{
|
{
|
||||||
if (_isScriptOrSerializableTypeCache.GetValue(typeDef))
|
if (_isScriptOrSerializableTypeCache.GetValue(typeDef))
|
||||||
|
|
@ -207,7 +235,8 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (MetaUtil.HasBurstCompileAttribute(typeDef))
|
// if any declaring type or any nested type has BurstCompile attribute, we should not rename this type
|
||||||
|
if (_isInheritAnyNestingOrAnyNestedTypeHasBurstCompileAttributeCache.GetValue(typeDef))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -247,10 +247,10 @@ namespace Obfuz
|
||||||
{
|
{
|
||||||
builder.AddPass(new ControlFlowObfusPass(settings.controlFlowObfusSettings.ToFacade()));
|
builder.AddPass(new ControlFlowObfusPass(settings.controlFlowObfusSettings.ToFacade()));
|
||||||
}
|
}
|
||||||
if (obfuscationPasses.HasFlag(ObfuscationPassType.WaterMark))
|
// if (obfuscationPasses.HasFlag(ObfuscationPassType.WaterMark))
|
||||||
{
|
// {
|
||||||
builder.AddPass(new WatermarkPass(settings.watermarkSettings.ToFacade()));
|
// builder.AddPass(new WatermarkPass(settings.watermarkSettings.ToFacade()));
|
||||||
}
|
// }
|
||||||
if (obfuscationPasses.HasFlag(ObfuscationPassType.SymbolObfus))
|
if (obfuscationPasses.HasFlag(ObfuscationPassType.SymbolObfus))
|
||||||
{
|
{
|
||||||
builder.AddPass(new SymbolObfusPass(settings.symbolObfusSettings.ToFacade()));
|
builder.AddPass(new SymbolObfusPass(settings.symbolObfusSettings.ToFacade()));
|
||||||
|
|
|
||||||
|
|
@ -139,9 +139,9 @@ namespace Obfuz
|
||||||
return Decrypt(data, offset, bytesLength, ops, salt);
|
return Decrypt(data, offset, bytesLength, ops, salt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DecryptInitializeArray(System.Array arr, System.RuntimeFieldHandle field, int length, int ops, int salt)
|
public static void DecryptInitializeArray(System.Array arr, byte[] data, int offset, int length, int ops, int salt)
|
||||||
{
|
{
|
||||||
_encryptor.DecryptInitializeArray(arr, field, length, ops, salt);
|
_encryptor.DecryptInitializeArray(arr, data, offset, length, ops, salt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
// SOFTWARE.
|
// SOFTWARE.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Unity.Collections.LowLevel.Unsafe;
|
using Unity.Collections.LowLevel.Unsafe;
|
||||||
using UnityEngine.Assertions;
|
using UnityEngine.Assertions;
|
||||||
|
|
@ -291,10 +290,9 @@ namespace Obfuz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual unsafe void DecryptInitializeArray(System.Array arr, System.RuntimeFieldHandle field, int length, int ops, int salt)
|
public virtual unsafe void DecryptInitializeArray(System.Array arr, byte[] data, int offset, int length, int ops, int salt)
|
||||||
{
|
{
|
||||||
//Assert.AreEqual(Marshal.SizeOf(arr.GetType().GetElementType()), arr.Length);
|
Buffer.BlockCopy(data, offset, arr, 0, length);
|
||||||
RuntimeHelpers.InitializeArray(arr, field);
|
|
||||||
if (arr is byte[] byteArr)
|
if (arr is byte[] byteArr)
|
||||||
{
|
{
|
||||||
fixed (byte* dataPtr = &byteArr[0])
|
fixed (byte* dataPtr = &byteArr[0])
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,6 @@
|
||||||
byte[] Encrypt(string value, int ops, int salt);
|
byte[] Encrypt(string value, int ops, int salt);
|
||||||
string DecryptString(byte[] value, int offset, int stringBytesLength, int ops, int salt);
|
string DecryptString(byte[] value, int offset, int stringBytesLength, int ops, int salt);
|
||||||
|
|
||||||
void DecryptInitializeArray(System.Array arr, System.RuntimeFieldHandle field, int length, int ops, int salt);
|
void DecryptInitializeArray(System.Array arr, byte[] data, int offset, int length, int ops, int salt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue