//------------------------------------------------------------- // ModifyInMemory_Spire.cs // // 23 May 2017 Updated //------------------------------------------------------------- using System; using System.Reflection; using System.Runtime.CompilerServices; namespace LicenseHelper1 { public static class ModifyInMemory_Spire { public static void ActivateMemoryPatching() { Assembly[] arr = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in arr) { if (assembly.FullName.StartsWith("Spire.License")) ActivateForAssembly(assembly); } AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(ActivateOnLoad); } private static void ActivateOnLoad(object sender, AssemblyLoadEventArgs e) { string Name = e.LoadedAssembly.FullName; if (Name.IndexOf("Spire.License") != -1) ActivateForAssembly(e.LoadedAssembly); } private static void ActivateForAssembly(Assembly assembly) { bool isFound = false; Type[] arrType = null; try { arrType = assembly.GetTypes(); } catch (ReflectionTypeLoadException err) { arrType = err.Types; } MethodInfo miLicensed = typeof(ModifyInMemory_Spire).GetMethod("InvokeMe", BindingFlags.NonPublic | BindingFlags.Static); MethodInfo miEvaluation = null; foreach (Type type in arrType) { if (isFound) break; if (type == null) continue; MethodInfo[] arrMInfo = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static); foreach (MethodInfo info in arrMInfo) { if (isFound) break; try { string strMethod = info.ToString(); if ((strMethod.StartsWith("Byte[]")) && (strMethod.IndexOf("(System.String)") > 0)) { miEvaluation = info; MemoryPatching(miEvaluation, miLicensed); isFound = true; break; } } catch { throw new InvalidOperationException("MemoryPatching for " + assembly.FullName + " failed !"); } } } string LData = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8TGljZW5zZSBLZXk9ImNyOERaN1hKMkR5MUs2UUJBTkRPSVRLdlpjTzZkelVod2lsSHBnVlluQ3k0cXlHV2V6TFZubFJGeFAxNU1mSWZnUmdNWm1XaEdOQWRFNFRqZWZnQ1ovbFR2b1BkSXRIbDZXdDVBNWk1TVhFbnFkQnVPMUthRnovRFFzYUdWTG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ICAgIDxFeHBpcmVkRGF0ZT4yMDk5LTEyLTMxVDEyOjAwOjAwWjwvRXhwaXJlZERhdGU+CiAgICA8UHJvZHVjdHM+CiAgICAgICAgPFByb2R1Y3Q+CiAgICAgICAgICAgIDxOYW1lPlNwaXJlLk9mZmljZSBQbGF0aW51bTwvTmFtZT4KICAgICAgICAgICAgPFZlcnNpb24+OS45OTwvVmVyc2lvbj4KICAgICAgICAgICAgPFN1YnNjcmlwdGlvbj4KICAgICAgICAgICAgICAgIDxOdW1iZXJPZlBlcm1pdHRlZERldmVsb3Blcj45OTk5OTwvTnVtYmVyT2ZQZXJtaXR0ZWREZXZlbG9wZXI+CiAgICAgICAgICAgICAgICA8TnVtYmVyT2ZQZXJtaXR0ZWRTaXRlPjk5OTk5PC9OdW1iZXJPZlBlcm1pdHRlZFNpdGU+CiAgICAgICAgICAgIDwvU3Vic2NyaXB0aW9uPgogICAgICAgIDwvUHJvZHVjdD4KICAgIDwvUHJvZHVjdHM+CiAgICA8SXNzdWVyPgogICAgICAgIDxOYW1lPklzc3VlcjwvTmFtZT4KICAgICAgICA8RW1haWw+aXNzdWVyQGlzc3Vlci5jb208L0VtYWlsPgogICAgICAgIDxVcmw+aHR0cDovL3d3dy5pc3N1ZXIuY29tPC9Vcmw+CiAgICA8L0lzc3Vlcj4KPC9MaWNlbnNlPg=="; //Spire.License.LicenseProvider.SetLicenseKey(LData); Type type2 = assembly.GetType("Spire.License.LicenseProvider"); MethodInfo mi = type2.GetMethod("SetLicenseKey", new Type[] { typeof(String) }); mi.Invoke(Activator.CreateInstance(type2, null), new String[] { LData }); } private static byte[] InvokeMe(String string_0) { return Convert.FromBase64String("PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8TGljZW5zZSBLZXk9IngiIFZlcnNpb249IjkuOSI+CiAgICA8VHlwZT5SdW50aW1lPC9UeXBlPgogICAgPFVzZXJuYW1lPlVzZXJOYW1lPC9Vc2VybmFtZT4KICAgIDxFbWFpbD5lTWFpbEBob3N0LmNvbTwvRW1haWw+CiAgICA8T3JnYW5pemF0aW9uPk9yZ2FuaXphdGlvbjwvT3JnYW5pemF0aW9uPgogICAgPExpY2Vuc2VkRGF0ZT4yMDE2LTAxLTAxVDEyOjAwOjAwWjwvTGljZW5zZWREYXRlPgogICAgPEV4cGlyZWREYXRlPjIwOTktMTItMzFUMTI6MDA6MDBaPC9FeHBpcmVkRGF0ZT4KICAgIDxQcm9kdWN0cz4KICAgICAgICA8UHJvZHVjdD4KICAgICAgICAgICAgPE5hbWU+U3BpcmUuT2ZmaWNlIFBsYXRpbnVtPC9OYW1lPgogICAgICAgICAgICA8VmVyc2lvbj45Ljk5PC9WZXJzaW9uPgogICAgICAgICAgICA8U3Vic2NyaXB0aW9uPgogICAgICAgICAgICAgICAgPE51bWJlck9mUGVybWl0dGVkRGV2ZWxvcGVyPjk5OTk5PC9OdW1iZXJPZlBlcm1pdHRlZERldmVsb3Blcj4KICAgICAgICAgICAgICAgIDxOdW1iZXJPZlBlcm1pdHRlZFNpdGU+OTk5OTk8L051bWJlck9mUGVybWl0dGVkU2l0ZT4KICAgICAgICAgICAgPC9TdWJzY3JpcHRpb24+CiAgICAgICAgPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxJc3N1ZXI+CiAgICAgICAgPE5hbWU+SXNzdWVyPC9OYW1lPgogICAgICAgIDxFbWFpbD5pc3N1ZXJAaXNzdWVyLmNvbTwvRW1haWw+CiAgICAgICAgPFVybD5odHRwOi8vd3d3Lmlzc3Vlci5jb208L1VybD4KICAgIDwvSXNzdWVyPgo8L0xpY2Vuc2U+Cg=="); } private static unsafe void MemoryPatching(MethodBase miEvaluation, MethodBase miLicensed) { IntPtr IntPtrEval = GetMemoryAddress(miEvaluation); IntPtr IntPtrLicensed = GetMemoryAddress(miLicensed); if (IntPtr.Size == 8) *((long*)IntPtrEval.ToPointer()) = *((long*)IntPtrLicensed.ToPointer()); else *((int*)IntPtrEval.ToPointer()) = *((int*)IntPtrLicensed.ToPointer()); } private static unsafe IntPtr GetMemoryAddress(MethodBase mb) { RuntimeHelpers.PrepareMethod(mb.MethodHandle); if ((Environment.Version.Major >= 4) || ((Environment.Version.Major == 2) && (Environment.Version.MinorRevision >= 3053))) { return new IntPtr(((int*)mb.MethodHandle.Value.ToPointer() + 2)); } UInt64* location = (UInt64*)(mb.MethodHandle.Value.ToPointer()); int index = (int)(((*location) >> 32) & 0xFF); if (IntPtr.Size == 8) { ulong* classStart = (ulong*)mb.DeclaringType.TypeHandle.Value.ToPointer(); ulong* address = classStart + index + 10; return new IntPtr(address); } else { uint* classStart = (uint*)mb.DeclaringType.TypeHandle.Value.ToPointer(); uint* address = classStart + index + 10; return new IntPtr(address); } } } }