From 839a74c9d2655edd136e6a46d35428c175c39276 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Sun, 15 Mar 2026 11:26:28 +0100 Subject: [PATCH] Work to make netstandard2.0 For now only GetHINSTANCE implemented, but no DynamicMethod/DynamicILInfo available --- Confuser.Runtime/AntiDump.cs | 9 +++++++-- Confuser.Runtime/AntiTamper.Anti.cs | 7 ++++++- Confuser.Runtime/AntiTamper.JIT.cs | 7 ++++++- Confuser.Runtime/AntiTamper.Normal.cs | 7 ++++++- Confuser.Runtime/Confuser.Runtime.csproj | 2 +- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Confuser.Runtime/AntiDump.cs b/Confuser.Runtime/AntiDump.cs index 798707c3d..581190a87 100644 --- a/Confuser.Runtime/AntiDump.cs +++ b/Confuser.Runtime/AntiDump.cs @@ -10,7 +10,7 @@ internal static class AntiDump { static unsafe void Initialize() { uint old; Module module = typeof(AntiDump).Module; - var bas = (byte*)Marshal.GetHINSTANCE(module); + var bas = (byte*)GetHINSTANCE(module); byte* ptr = bas + 0x3c; byte* ptr2; ptr = ptr2 = bas + *(uint*)ptr; @@ -234,5 +234,10 @@ static unsafe void Initialize() { } } } + + public static IntPtr GetHINSTANCE(Module module) { + var method = typeof(Marshal).GetMember("GetHINSTANCE", BindingFlags.Public | BindingFlags.Static)[0] as MethodInfo; + return (IntPtr)method.Invoke(null, new object[] { module }); + } } -} \ No newline at end of file +} diff --git a/Confuser.Runtime/AntiTamper.Anti.cs b/Confuser.Runtime/AntiTamper.Anti.cs index f1be244cd..b1ceef66b 100644 --- a/Confuser.Runtime/AntiTamper.Anti.cs +++ b/Confuser.Runtime/AntiTamper.Anti.cs @@ -16,7 +16,7 @@ static unsafe void Initialize() { Module m = typeof(AntiTamperAnti).Module; string n = m.FullyQualifiedName; bool f = n.Length > 0 && n[0] == '<'; - var b = (byte*)Marshal.GetHINSTANCE(m); + var b = (byte*)GetHINSTANCE(m); byte* p = b + *(uint*)(b + 0x3c); ushort s = *(ushort*)(p + 0x6); ushort o = *(ushort*)(p + 0x14); @@ -88,5 +88,10 @@ static unsafe void Initialize() { h++; } } + + public static IntPtr GetHINSTANCE(Module module) { + var method = typeof(Marshal).GetMember("GetHINSTANCE", BindingFlags.Public | BindingFlags.Static)[0] as MethodInfo; + return (IntPtr)method.Invoke(null, new object[] { module }); + } } } diff --git a/Confuser.Runtime/AntiTamper.JIT.cs b/Confuser.Runtime/AntiTamper.JIT.cs index ca0535fbb..fd07bc28c 100644 --- a/Confuser.Runtime/AntiTamper.JIT.cs +++ b/Confuser.Runtime/AntiTamper.JIT.cs @@ -19,7 +19,7 @@ public static void Initialize() { Module m = typeof(AntiTamperNormal).Module; string n = m.FullyQualifiedName; bool f = n.Length > 0 && n[0] == '<'; - var b = (byte*)Marshal.GetHINSTANCE(m); + var b = (byte*)GetHINSTANCE(m); byte* p = b + *(uint*)(b + 0x3c); ushort s = *(ushort*)(p + 0x6); ushort o = *(ushort*)(p + 0x14); @@ -86,6 +86,11 @@ public static void Initialize() { Hook(); } + public static IntPtr GetHINSTANCE(Module module) { + var method = typeof(Marshal).GetMember("GetHINSTANCE", BindingFlags.Public | BindingFlags.Static)[0] as MethodInfo; + return (IntPtr)method.Invoke(null, new object[] { module }); + } + static object GetFieldValue(object obj, string fieldName) { var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (field == null) diff --git a/Confuser.Runtime/AntiTamper.Normal.cs b/Confuser.Runtime/AntiTamper.Normal.cs index 6fc882ff9..2abb75c61 100644 --- a/Confuser.Runtime/AntiTamper.Normal.cs +++ b/Confuser.Runtime/AntiTamper.Normal.cs @@ -11,7 +11,7 @@ static unsafe void Initialize() { Module m = typeof(AntiTamperNormal).Module; string n = m.FullyQualifiedName; bool f = n.Length > 0 && n[0] == '<'; - var b = (byte*)Marshal.GetHINSTANCE(m); + var b = (byte*)GetHINSTANCE(m); byte* p = b + *(uint*)(b + 0x3c); ushort s = *(ushort*)(p + 0x6); ushort o = *(ushort*)(p + 0x14); @@ -64,5 +64,10 @@ static unsafe void Initialize() { h++; } } + + public static IntPtr GetHINSTANCE(Module module) { + var method = typeof(Marshal).GetMember("GetHINSTANCE", BindingFlags.Public | BindingFlags.Static)[0] as MethodInfo; + return (IntPtr)method.Invoke(null, new object[] { module }); + } } } diff --git a/Confuser.Runtime/Confuser.Runtime.csproj b/Confuser.Runtime/Confuser.Runtime.csproj index be1448112..8ac37535c 100644 --- a/Confuser.Runtime/Confuser.Runtime.csproj +++ b/Confuser.Runtime/Confuser.Runtime.csproj @@ -4,7 +4,7 @@ - net472;net8.0 + netstandard2.0 true