Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

130 lignes
8.7 KiB

  1. //-------------------------------------------------------------
  2. // ModifyInMemory_Spire.cs
  3. //
  4. // 23 May 2017 Updated
  5. //-------------------------------------------------------------
  6. using System;
  7. using System.Reflection;
  8. using System.Runtime.CompilerServices;
  9. namespace LicenseHelper1
  10. {
  11. public static class ModifyInMemory_Spire
  12. {
  13. public static void ActivateMemoryPatching()
  14. {
  15. Assembly[] arr = AppDomain.CurrentDomain.GetAssemblies();
  16. foreach (Assembly assembly in arr)
  17. {
  18. if (assembly.FullName.StartsWith("Spire.License"))
  19. ActivateForAssembly(assembly);
  20. }
  21. AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(ActivateOnLoad);
  22. }
  23. private static void ActivateOnLoad(object sender, AssemblyLoadEventArgs e)
  24. {
  25. string Name = e.LoadedAssembly.FullName;
  26. if (Name.IndexOf("Spire.License") != -1)
  27. ActivateForAssembly(e.LoadedAssembly);
  28. }
  29. private static void ActivateForAssembly(Assembly assembly)
  30. {
  31. bool isFound = false;
  32. Type[] arrType = null;
  33. try
  34. {
  35. arrType = assembly.GetTypes();
  36. }
  37. catch (ReflectionTypeLoadException err)
  38. {
  39. arrType = err.Types;
  40. }
  41. MethodInfo miLicensed = typeof(ModifyInMemory_Spire).GetMethod("InvokeMe", BindingFlags.NonPublic | BindingFlags.Static);
  42. MethodInfo miEvaluation = null;
  43. foreach (Type type in arrType)
  44. {
  45. if (isFound) break;
  46. if (type == null) continue;
  47. MethodInfo[] arrMInfo = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
  48. foreach (MethodInfo info in arrMInfo)
  49. {
  50. if (isFound) break;
  51. try
  52. {
  53. string strMethod = info.ToString();
  54. if ((strMethod.StartsWith("Byte[]")) && (strMethod.IndexOf("(System.String)") > 0))
  55. {
  56. miEvaluation = info;
  57. MemoryPatching(miEvaluation, miLicensed);
  58. isFound = true;
  59. break;
  60. }
  61. }
  62. catch
  63. {
  64. throw new InvalidOperationException("MemoryPatching for " + assembly.FullName + " failed !");
  65. }
  66. }
  67. }
  68. string LData = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8TGljZW5zZSBLZXk9ImNyOERaN1hKMkR5MUs2UUJBTkRPSVRLdlpjTzZkelVod2lsSHBnVlluQ3k0cXlHV2V6TFZubFJGeFAxNU1mSWZnUmdNWm1XaEdOQWRFNFRqZWZnQ1ovbFR2b1BkSXRIbDZXdDVBNWk1TVhFbnFkQnVPMUthRnovRFFzYUdWTGhzdjlySG1ybnRxSElFRGxJeGRxYUpNcGtLb0Frd1A3d1N6T01KMVkrbUNmVTVVRmV6REwvTjd1enJ4M0Y0d2I1SGErd0E2VFQ5VFJ3SzAzejlFS01aRmwzU1lSL3o0YVU3TE0wZFNYWTlqU0ZKZ2dqZlZzRFVLaUJyVm5td1ljaXVyOUVrYmw5Q3RaWTAzdG1yZm01QlplKzZnaHRFTm4wb2gzMzh0WlJleWpjcjc0QWs3MWhnWWtuTE9CQzE1VllmalhzcXVBVW13MlI2TWNWMlBPT2JyY1RSYlhBZ3pvUWJPeWQ4U2JFWmN3aE43NktQd1dzUVFTMUowdGlZSFVLeE9tMnQ0ZkJWMGhQVmhhOUI4Y0swNHFKUVp0MDBaMWNKRGEwd2I4VWx6RWs5QkhVVzJlbk9mVDE0UnlIQ2krWUdlbVBLY2RDUXJoMXpyWVRGN0ltb0x4N3h1NGV2RFRZc2xzV0JrbFFJb3g4NnJWckVVa1N0dXErQUNTWS9xVTM5L1Zhd3Y5S0FmUjVUZUVicGt3RGhTYjBOQkFqVDhBeXRsRFZkR2ZpZzBxS0czVllpVHBYRnc1cHRMVmgrYmtkK2RnN3Z4dHZyNDVaVVdKZXlyekdOR0g3YUZZZDZwLzJNRy9YSlRsR3ov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tPC9Vcmw+CiAgICA8L0lzc3Vlcj4KPC9MaWNlbnNlPg==";
  69. //Spire.License.LicenseProvider.SetLicenseKey(LData);
  70. Type type2 = assembly.GetType("Spire.License.LicenseProvider");
  71. MethodInfo mi = type2.GetMethod("SetLicenseKey", new Type[] { typeof(String) });
  72. mi.Invoke(Activator.CreateInstance(type2, null), new String[] { LData });
  73. }
  74. private static byte[] InvokeMe(String string_0)
  75. {
  76. return Convert.FromBase64String("PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8TGljZW5zZSBLZXk9IngiIFZlcnNpb249IjkuOSI+CiAgICA8VHlwZT5SdW50aW1lPC9UeXBlPgogICAgPFVzZXJuYW1lPlVzZXJOYW1lPC9Vc2VybmFtZT4KICAgIDxFbWFpbD5lTWFpbEBob3N0LmNvbTwvRW1haWw+CiAgICA8T3JnYW5pemF0aW9uPk9yZ2FuaXphdGlvbjwvT3JnYW5pemF0aW9uPgogICAgPExpY2Vuc2VkRGF0ZT4yMDE2LTAxLTAxVDEyOjAwOjAwWjwvTGljZW5zZWREYXRlPgogICAgPEV4cGlyZWREYXRlPjIwOTktMTItMzFUMTI6MDA6MDBaPC9FeHBpcmVkRGF0ZT4KICAgIDxQcm9kdWN0cz4KICAgICAgICA8UHJvZHVjdD4KICAgICAgICAgICAgPE5hbWU+U3BpcmUuT2ZmaWNlIFBsYXRpbnVtPC9OYW1lPgogICAgICAgICAgICA8VmVyc2lvbj45Ljk5PC9WZXJzaW9uPgogICAgICAgICAgICA8U3Vic2NyaXB0aW9uPgogICAgICAgICAgICAgICAgPE51bWJlck9mUGVybWl0dGVkRGV2ZWxvcGVyPjk5OTk5PC9OdW1iZXJPZlBlcm1pdHRlZERldmVsb3Blcj4KICAgICAgICAgICAgICAgIDxOdW1iZXJPZlBlcm1pdHRlZFNpdGU+OTk5OTk8L051bWJlck9mUGVybWl0dGVkU2l0ZT4KICAgICAgICAgICAgPC9TdWJzY3JpcHRpb24+CiAgICAgICAgPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxJc3N1ZXI+CiAgICAgICAgPE5hbWU+SXNzdWVyPC9OYW1lPgogICAgICAgIDxFbWFpbD5pc3N1ZXJAaXNzdWVyLmNvbTwvRW1haWw+CiAgICAgICAgPFVybD5odHRwOi8vd3d3Lmlzc3Vlci5jb208L1VybD4KICAgIDwvSXNzdWVyPgo8L0xpY2Vuc2U+Cg==");
  77. }
  78. private static unsafe void MemoryPatching(MethodBase miEvaluation, MethodBase miLicensed)
  79. {
  80. IntPtr IntPtrEval = GetMemoryAddress(miEvaluation);
  81. IntPtr IntPtrLicensed = GetMemoryAddress(miLicensed);
  82. if (IntPtr.Size == 8)
  83. *((long*)IntPtrEval.ToPointer()) = *((long*)IntPtrLicensed.ToPointer());
  84. else
  85. *((int*)IntPtrEval.ToPointer()) = *((int*)IntPtrLicensed.ToPointer());
  86. }
  87. private static unsafe IntPtr GetMemoryAddress(MethodBase mb)
  88. {
  89. RuntimeHelpers.PrepareMethod(mb.MethodHandle);
  90. if ((Environment.Version.Major >= 4) || ((Environment.Version.Major == 2) && (Environment.Version.MinorRevision >= 3053)))
  91. {
  92. return new IntPtr(((int*)mb.MethodHandle.Value.ToPointer() + 2));
  93. }
  94. UInt64* location = (UInt64*)(mb.MethodHandle.Value.ToPointer());
  95. int index = (int)(((*location) >> 32) & 0xFF);
  96. if (IntPtr.Size == 8)
  97. {
  98. ulong* classStart = (ulong*)mb.DeclaringType.TypeHandle.Value.ToPointer();
  99. ulong* address = classStart + index + 10;
  100. return new IntPtr(address);
  101. }
  102. else
  103. {
  104. uint* classStart = (uint*)mb.DeclaringType.TypeHandle.Value.ToPointer();
  105. uint* address = classStart + index + 10;
  106. return new IntPtr(address);
  107. }
  108. }
  109. }
  110. }