backup
walon 2025-05-10 18:25:43 +08:00
parent 62845a5c35
commit 3c6cc385a4
18 changed files with 87 additions and 87 deletions

View File

@ -258,6 +258,28 @@ namespace Obfuz.ObfusPasses.CallObfus
}; };
} }
private bool IsSpecialNotObfuscatedMethod(TypeDef typeDef, IMethod method)
{
if (typeDef.IsDelegate || typeDef.IsEnum)
return true;
string methodName = method.Name;
// doesn't proxy call if the method is a constructor
if (methodName == ".ctor")
{
return true;
}
// special handle
// don't proxy call for List<T>.Enumerator GetEnumerator()
if (methodName == "GetEnumerator")
{
return true;
}
return false;
}
private bool ComputeIsInWhiteList(IMethod calledMethod) private bool ComputeIsInWhiteList(IMethod calledMethod)
{ {
ITypeDefOrRef declaringType = calledMethod.DeclaringType; ITypeDefOrRef declaringType = calledMethod.DeclaringType;
@ -283,25 +305,14 @@ namespace Obfuz.ObfusPasses.CallObfus
TypeDef typeDef = declaringType.ResolveTypeDef(); TypeDef typeDef = declaringType.ResolveTypeDef();
if (typeDef.IsDelegate || typeDef.IsEnum) if (IsSpecialNotObfuscatedMethod(typeDef, calledMethod))
{
return true; return true;
}
string assName = typeDef.Module.Assembly.Name; string assName = typeDef.Module.Assembly.Name;
string typeFullName = typeDef.FullName; string typeFullName = typeDef.FullName;
string methodName = calledMethod.Name; string methodName = calledMethod.Name;
// doesn't proxy call if the method is a constructor
if (methodName == ".ctor")
{
return true;
}
// special handle
// don't proxy call for List<T>.Enumerator GetEnumerator()
if (methodName == "GetEnumerator")
{
return true;
}
foreach (var ass in _whiteListAssemblies) foreach (var ass in _whiteListAssemblies)
{ {
if (!ass.nameMatcher.IsMatch(assName)) if (!ass.nameMatcher.IsMatch(assName))

View File

@ -22,4 +22,13 @@ namespace Obfuz.ObfusPasses.CallObfus
bool NeedObfuscateCalledMethod(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool currentInLoop); bool NeedObfuscateCalledMethod(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool currentInLoop);
} }
public abstract class ObfuscationPolicyBase : IObfuscationPolicy
{
public abstract bool NeedObfuscateCallInMethod(MethodDef method);
public abstract ObfuscationCachePolicy GetMethodObfuscationCachePolicy(MethodDef method);
public abstract bool NeedObfuscateCalledMethod(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool currentInLoop);
}
} }

View File

@ -14,4 +14,10 @@ namespace Obfuz.ObfusPasses.CallObfus
void Done(); void Done();
} }
public abstract class ObfuscatorBase : IObfuscator
{
public abstract void Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, bool needCacheCall, List<Instruction> obfuscatedInstructions);
public abstract void Done();
}
} }

View File

@ -1,13 +0,0 @@
using dnlib.DotNet;
namespace Obfuz.ObfusPasses.CallObfus
{
public abstract class ObfuscationPolicyBase : IObfuscationPolicy
{
public abstract bool NeedObfuscateCallInMethod(MethodDef method);
public abstract ObfuscationCachePolicy GetMethodObfuscationCachePolicy(MethodDef method);
public abstract bool NeedObfuscateCalledMethod(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool currentInLoop);
}
}

View File

@ -1,12 +0,0 @@
using dnlib.DotNet.Emit;
using dnlib.DotNet;
using System.Collections.Generic;
namespace Obfuz.ObfusPasses.CallObfus
{
public abstract class ObfuscatorBase : IObfuscator
{
public abstract void Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, bool needCacheCall, List<Instruction> obfuscatedInstructions);
public abstract void Done();
}
}

View File

@ -1,16 +0,0 @@
using dnlib.DotNet;
namespace Obfuz.ObfusPasses.ConstEncrypt
{
public abstract class EncryptPolicyBase : IEncryptPolicy
{
public abstract bool NeedObfuscateMethod(MethodDef method);
public abstract ConstCachePolicy GetMethodConstCachePolicy(MethodDef method);
public abstract bool NeedObfuscateDouble(MethodDef method, bool currentInLoop, double value);
public abstract bool NeedObfuscateFloat(MethodDef method, bool currentInLoop, float value);
public abstract bool NeedObfuscateInt(MethodDef method, bool currentInLoop, int value);
public abstract bool NeedObfuscateLong(MethodDef method, bool currentInLoop, long value);
public abstract bool NeedObfuscateString(MethodDef method, bool currentInLoop, string value);
public abstract bool NeedObfuscateArray(MethodDef method, bool currentInLoop, byte[] array);
}
}

View File

@ -21,4 +21,15 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
void Done(); void Done();
} }
public abstract class ConstEncryptorBase : IConstEncryptor
{
public abstract void ObfuscateBytes(MethodDef method, bool needCacheValue, byte[] value, List<Instruction> obfuscatedInstructions);
public abstract void ObfuscateDouble(MethodDef method, bool needCacheValue, double value, List<Instruction> obfuscatedInstructions);
public abstract void ObfuscateFloat(MethodDef method, bool needCacheValue, float value, List<Instruction> obfuscatedInstructions);
public abstract void ObfuscateInt(MethodDef method, bool needCacheValue, int value, List<Instruction> obfuscatedInstructions);
public abstract void ObfuscateLong(MethodDef method, bool needCacheValue, long value, List<Instruction> obfuscatedInstructions);
public abstract void ObfuscateString(MethodDef method, bool needCacheValue, string value, List<Instruction> obfuscatedInstructions);
public abstract void Done();
}
} }

View File

@ -33,4 +33,16 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
bool NeedObfuscateArray(MethodDef method, bool currentInLoop, byte[] array); bool NeedObfuscateArray(MethodDef method, bool currentInLoop, byte[] array);
} }
public abstract class EncryptPolicyBase : IEncryptPolicy
{
public abstract bool NeedObfuscateMethod(MethodDef method);
public abstract ConstCachePolicy GetMethodConstCachePolicy(MethodDef method);
public abstract bool NeedObfuscateDouble(MethodDef method, bool currentInLoop, double value);
public abstract bool NeedObfuscateFloat(MethodDef method, bool currentInLoop, float value);
public abstract bool NeedObfuscateInt(MethodDef method, bool currentInLoop, int value);
public abstract bool NeedObfuscateLong(MethodDef method, bool currentInLoop, long value);
public abstract bool NeedObfuscateString(MethodDef method, bool currentInLoop, string value);
public abstract bool NeedObfuscateArray(MethodDef method, bool currentInLoop, byte[] array);
}
} }

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Obfuz.ObfusPasses.ExprObfus namespace Obfuz.ObfusPasses.ExprObfus
{ {
public class ExprObfuscationPass : InstructionObfuscationPassBase public class ExprObfusPass : InstructionObfuscationPassBase
{ {

View File

@ -2,7 +2,7 @@
namespace Obfuz.ObfusPasses.MemEncrypt namespace Obfuz.ObfusPasses.MemEncrypt
{ {
public class ConfigEncryptionPolicy : EncryptionPolicyBase public class ConfigEncryptionPolicy : EncryptPolicyBase
{ {
private bool IsSupportedFieldType(TypeSig type) private bool IsSupportedFieldType(TypeSig type)

View File

@ -8,7 +8,7 @@ using UnityEngine.Assertions;
namespace Obfuz.ObfusPasses.MemEncrypt namespace Obfuz.ObfusPasses.MemEncrypt
{ {
public class DefaultMemoryEncryptor : MemoryEncryptorBase public class DefaultMemoryEncryptor : MemEncryptorBase
{ {
private class ModuleDefaultMemoryEncryptor private class ModuleDefaultMemoryEncryptor

View File

@ -1,9 +0,0 @@
using dnlib.DotNet;
namespace Obfuz.ObfusPasses.MemEncrypt
{
public abstract class EncryptionPolicyBase : IEncryptionPolicy
{
public abstract bool NeedEncrypt(FieldDef field);
}
}

View File

@ -7,8 +7,13 @@ using System.Threading.Tasks;
namespace Obfuz.ObfusPasses.MemEncrypt namespace Obfuz.ObfusPasses.MemEncrypt
{ {
public interface IEncryptionPolicy public interface IEncryptPolicy
{ {
bool NeedEncrypt(FieldDef field); bool NeedEncrypt(FieldDef field);
} }
public abstract class EncryptPolicyBase : IEncryptPolicy
{
public abstract bool NeedEncrypt(FieldDef field);
}
} }

View File

@ -14,10 +14,16 @@ namespace Obfuz.ObfusPasses.MemEncrypt
public Instruction currentInstruction; public Instruction currentInstruction;
} }
public interface IMemoryEncryptor public interface IMemEncryptor
{ {
void Encrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx); void Encrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx);
void Decrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx); void Decrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx);
} }
public abstract class MemEncryptorBase : IMemEncryptor
{
public abstract void Encrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx);
public abstract void Decrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx);
}
} }

View File

@ -9,10 +9,10 @@ using UnityEngine;
namespace Obfuz.ObfusPasses.MemEncrypt namespace Obfuz.ObfusPasses.MemEncrypt
{ {
public class MemoryEncryptionPass : InstructionObfuscationPassBase public class MemEncryptPass : InstructionObfuscationPassBase
{ {
private readonly IEncryptionPolicy _encryptionPolicy = new ConfigEncryptionPolicy(); private readonly IEncryptPolicy _encryptionPolicy = new ConfigEncryptionPolicy();
private readonly IMemoryEncryptor _memoryEncryptor = new DefaultMemoryEncryptor(); private readonly IMemEncryptor _memoryEncryptor = new DefaultMemoryEncryptor();
public override void Start(ObfuscationPassContext ctx) public override void Start(ObfuscationPassContext ctx)
{ {

View File

@ -1,12 +0,0 @@
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using System.Collections.Generic;
namespace Obfuz.ObfusPasses.MemEncrypt
{
public abstract class MemoryEncryptorBase : IMemoryEncryptor
{
public abstract void Encrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx);
public abstract void Decrypt(FieldDef field, List<Instruction> outputInstructions, MemoryEncryptionContext ctx);
}
}

View File

@ -3,6 +3,7 @@ using dnlib.Protection;
using Obfuz.Data; using Obfuz.Data;
using Obfuz.Emit; using Obfuz.Emit;
using Obfuz.ObfusPasses; using Obfuz.ObfusPasses;
using Obfuz.ObfusPasses.CleanUp;
using Obfuz.Utils; using Obfuz.Utils;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -11,6 +12,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using static UnityEditor.ObjectChangeEventStream;
namespace Obfuz namespace Obfuz
{ {
@ -46,6 +48,7 @@ namespace Obfuz
{ {
_pipeline.AddPass(pass); _pipeline.AddPass(pass);
} }
_pipeline.AddPass(new CleanUpInstructionPass());
} }
public void Run() public void Run()

View File

@ -77,7 +77,7 @@ namespace Obfuz
ObfuscationPassType obfuscationPasses = settings.enabledObfuscationPasses; ObfuscationPassType obfuscationPasses = settings.enabledObfuscationPasses;
if (obfuscationPasses.HasFlag(ObfuscationPassType.MemoryEncryption)) if (obfuscationPasses.HasFlag(ObfuscationPassType.MemoryEncryption))
{ {
builder.AddPass(new MemoryEncryptionPass()); builder.AddPass(new MemEncryptPass());
} }
if (obfuscationPasses.HasFlag(ObfuscationPassType.CallProxy)) if (obfuscationPasses.HasFlag(ObfuscationPassType.CallProxy))
{ {
@ -89,13 +89,12 @@ namespace Obfuz
} }
if (obfuscationPasses.HasFlag(ObfuscationPassType.ExprObfuscation)) if (obfuscationPasses.HasFlag(ObfuscationPassType.ExprObfuscation))
{ {
builder.AddPass(new ExprObfuscationPass()); builder.AddPass(new ExprObfusPass());
} }
if (obfuscationPasses.HasFlag(ObfuscationPassType.SymbolObfuscation)) if (obfuscationPasses.HasFlag(ObfuscationPassType.SymbolObfuscation))
{ {
builder.AddPass(new SymbolObfusPass(settings.symbolObfusSettings)); builder.AddPass(new SymbolObfusPass(settings.symbolObfusSettings));
} }
builder.AddPass(new CleanUpInstructionPass());
return builder; return builder;
} }
} }