diff --git a/Editor/ObfusPasses/Instinct.meta b/Editor/ObfusPasses/Instinct.meta new file mode 100644 index 0000000..97e300c --- /dev/null +++ b/Editor/ObfusPasses/Instinct.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bb4f71e54c6a07341883ba0c642505c1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/ObfusPasses/Instinct/InstinctPass.cs b/Editor/ObfusPasses/Instinct/InstinctPass.cs new file mode 100644 index 0000000..d894ee8 --- /dev/null +++ b/Editor/ObfusPasses/Instinct/InstinctPass.cs @@ -0,0 +1,129 @@ +using dnlib.DotNet; +using dnlib.DotNet.Emit; +using Obfuz.Editor; +using Obfuz.Emit; +using Obfuz.Settings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Security; +using System.Text; +using UnityEngine.Assertions; + +namespace Obfuz.ObfusPasses.Instinct +{ + + public class InstinctPass : InstructionObfuscationPassBase + { + public override ObfuscationPassType Type => ObfuscationPassType.None; + + protected override bool ForceProcessAllAssembliesAndIgnoreAllPolicy => true; + + public InstinctPass() + { + } + + public override void Start() + { + var ctx = ObfuscationPassContext.Current; + } + + public override void Stop() + { + + } + + protected override bool NeedObfuscateMethod(MethodDef method) + { + return true; + } + + private string GetTypeName(TypeSig type) + { + type = type.RemovePinnedAndModifiers(); + switch(type.ElementType) + { + case ElementType.Class: + case ElementType.ValueType: + { + return type.ReflectionName; + } + case ElementType.GenericInst: + { + type = ((GenericInstSig)type).GenericType; + return type.ReflectionName; + } + default: return type.ReflectionName; + } + } + + private string GetTypeFullName(TypeSig type) + { + type = type.RemovePinnedAndModifiers(); + + switch (type.ElementType) + { + case ElementType.Class: + case ElementType.ValueType: + { + return type.ReflectionFullName; + } + case ElementType.GenericInst: + { + GenericInstSig genericInstSig = (GenericInstSig)type; + var typeName = new StringBuilder(genericInstSig.GenericType.ReflectionFullName); + typeName.Append("<").Append(string.Join(",", genericInstSig.GenericArguments.Select(GetTypeFullName))).Append(">"); + return typeName.ToString(); + } + default: return type.ReflectionFullName; + } + } + + protected override bool TryObfuscateInstruction(MethodDef callingMethod, Instruction inst, IList instructions, int instructionIndex, List outputInstructions, List totalFinalInstructions) + { + Code code = inst.OpCode.Code; + if (!(inst.Operand is IMethod method) || !method.IsMethod) + { + return false; + } + MethodDef methodDef = method.ResolveMethodDef(); + if (methodDef == null || methodDef.DeclaringType.FullName != "Obfuz.ObfuscationInstincts" || methodDef.DeclaringType.DefinitionAssembly.Name != ConstValues.ObfuzRuntimeAssemblyName) + { + return false; + } + + string methodName = methodDef.Name; + switch (methodName) + { + case "FullNameOf": + case "NameOf": + { + MethodSpec methodSpec = (MethodSpec)method; + GenericInstMethodSig gims = methodSpec.GenericInstMethodSig; + Assert.AreEqual(1, gims.GenericArguments.Count, "FullNameOf should have exactly one generic argument"); + TypeSig type = gims.GenericArguments[0]; + switch (methodName) + { + case "FullNameOf": + { + string typeFullName = GetTypeFullName(type); + outputInstructions.Add(Instruction.Create(OpCodes.Ldstr, typeFullName)); + break; + } + case "NameOf": + { + string typeName = GetTypeName(type); + outputInstructions.Add(Instruction.Create(OpCodes.Ldstr, typeName)); + break; + } + default: throw new NotSupportedException($"Unsupported instinct method: {methodDef.FullName}"); + } + break; + } + default: throw new NotSupportedException($"Unsupported instinct method: {methodDef.FullName}"); + } + //Debug.Log($"memory encrypt field: {field}"); + return true; + } + } +} diff --git a/Editor/ObfusPasses/Instinct/InstinctPass.cs.meta b/Editor/ObfusPasses/Instinct/InstinctPass.cs.meta new file mode 100644 index 0000000..ca21ae4 --- /dev/null +++ b/Editor/ObfusPasses/Instinct/InstinctPass.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 08027d16e09664c40b561715ef9326fc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Obfuscator.cs b/Editor/Obfuscator.cs index 5bc5703..b326096 100644 --- a/Editor/Obfuscator.cs +++ b/Editor/Obfuscator.cs @@ -3,6 +3,7 @@ using Obfuz.Data; using Obfuz.Emit; using Obfuz.EncryptionVM; using Obfuz.ObfusPasses.CleanUp; +using Obfuz.ObfusPasses.Instinct; using Obfuz.ObfusPasses.SymbolObfus; using Obfuz.Unity; using Obfuz.Utils; @@ -42,6 +43,7 @@ namespace Obfuz _assemblyResolver = new CombinedAssemblyResolver(new PathAssemblyResolver(_coreSettings.assemblySearchPaths.ToArray()), new UnityProjectManagedAssemblyResolver(_coreSettings.buildTarget)); _passPolicy = new ConfigurablePassPolicy(_coreSettings.assembliesToObfuscate, _coreSettings.enabledObfuscationPasses, _coreSettings.obfuscationPassRuleConfigFiles); + _pipeline1.AddPass(new InstinctPass()); foreach (var pass in _coreSettings.obfuscationPasses) { if (pass is SymbolObfusPass symbolObfusPass) diff --git a/Runtime/ObfuscationInstincts.cs b/Runtime/ObfuscationInstincts.cs new file mode 100644 index 0000000..96e5087 --- /dev/null +++ b/Runtime/ObfuscationInstincts.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Obfuz +{ + public static class ObfuscationInstincts + { + /// + /// Returns the original full name before obfuscated of the type T + /// + /// + /// + public static string FullNameOf() + { + return typeof(T).FullName; + } + + /// + /// Returns the original name before obfuscated of the type T + /// + /// + /// + public static string NameOf() + { + return typeof(T).Name; + } + } +} diff --git a/Runtime/ObfuscationInstincts.cs.meta b/Runtime/ObfuscationInstincts.cs.meta new file mode 100644 index 0000000..48d653f --- /dev/null +++ b/Runtime/ObfuscationInstincts.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 84320ab4adc4cbc49bf5e8f4009b4a96 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: