WcToken.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using System.Text.RegularExpressions;
  8. namespace SheepSheep
  9. {
  10. class WcToken
  11. {
  12. [DllImport("kernel32.dll")]
  13. private static extern uint GetLastError();
  14. [DllImport("kernel32.dll")]
  15. private static extern int OpenProcess(int dwDesiredAccess, int bInheritHandle, int dwProcessId);
  16. [DllImport("Kernel32.dll", SetLastError = true)]
  17. private static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);
  18. [DllImport("Kernel32.dll")]
  19. public static extern bool ReadProcessMemory(IntPtr handle, int address, byte[] data, int size, byte[] read);
  20. private struct MEMORY_BASIC_INFORMATION
  21. {
  22. public int BaseAddress;
  23. public int AllocationBase;
  24. public int AllocationProtect;
  25. public int RegionSize;
  26. public int State;
  27. public int Protect;
  28. public int lType;
  29. }
  30. const int PROCESS_ALL_ACCESS = 0x1F0FFF;
  31. private static byte[] GetBlankBytes(int Length)
  32. {
  33. byte[] AOB = new byte[Length];
  34. for (int i = 0; i < Length; i++)
  35. {
  36. AOB[i] = 0;
  37. }
  38. return AOB;
  39. }
  40. public static int IndexOf(byte[] array, byte[] pattern, int startOffset = 0)
  41. {
  42. int success = 0;
  43. for (int i = startOffset; i < array.Length; i++)
  44. {
  45. if (array[i] == pattern[success])
  46. {
  47. success++;
  48. }
  49. else
  50. {
  51. success = 0;
  52. }
  53. if (pattern.Length == success)
  54. {
  55. return i - pattern.Length + 1;
  56. }
  57. }
  58. return -1;
  59. }
  60. private static unsafe long IndexOf2(byte[] haystack, byte[] needle, long startOffset = 0)
  61. {
  62. fixed (byte* h = haystack) fixed (byte* n = needle)
  63. {
  64. for (byte* hNext = h + startOffset, hEnd = h + haystack.LongLength + 1 - needle.LongLength, nEnd = n + needle.LongLength; hNext < hEnd; hNext++)
  65. for (byte* hInc = hNext, nInc = n; *nInc == *hInc; hInc++)
  66. if (++nInc == nEnd)
  67. return hNext - h;
  68. return -1;
  69. }
  70. }
  71. private static string getSubString(string text, string start, string end)
  72. {
  73. Regex regex = new Regex("(?<=(" + start + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);
  74. return regex.Match(text).Value;
  75. }
  76. private static string ReadMemoryString(IntPtr HWND, int IpAddr, int length)
  77. {
  78. byte[] readByte = GetBlankBytes(length);
  79. byte[] readBytes = GetBlankBytes(length);
  80. ReadProcessMemory(HWND, IpAddr, readByte, length, readBytes);
  81. return System.Text.Encoding.UTF8.GetString(readByte);
  82. }
  83. private static List<int> MemorySearch(IntPtr HWND, byte[] content)
  84. {
  85. int IpAddr = 0x000000;
  86. List<int> foundList = new List<int>();
  87. MEMORY_BASIC_INFORMATION mbi = new MEMORY_BASIC_INFORMATION();
  88. while (VirtualQueryEx(HWND, (IntPtr)IpAddr, out mbi, 28) != 0)
  89. {
  90. if (mbi.Protect != 16 && mbi.Protect != 1 && mbi.Protect != 512)
  91. {
  92. byte[] readByte = GetBlankBytes(mbi.RegionSize);
  93. byte[] readBytes = GetBlankBytes(mbi.RegionSize);
  94. bool isRead = false;
  95. int position = 0;
  96. isRead = ReadProcessMemory(HWND, IpAddr, readByte, mbi.RegionSize, readBytes);
  97. while (isRead)
  98. {
  99. position = (int)IndexOf(readByte, content, position);
  100. if (position == -1)
  101. {
  102. break;
  103. }
  104. else
  105. {
  106. foundList.Add(IpAddr + position);
  107. }
  108. position = position + content.Length;
  109. }
  110. }
  111. IpAddr = IpAddr + mbi.RegionSize;
  112. }
  113. return foundList;
  114. }
  115. public static string GetTokenFromWechat()
  116. {
  117. Process[] processes = Process.GetProcesses();
  118. byte[] searchStr = System.Text.Encoding.UTF8.GetBytes("\",\"token\":\"");
  119. foreach (Process process in processes)
  120. {
  121. if (process.ProcessName.Equals("WeChatAppEx"))
  122. {
  123. IntPtr HWND = (IntPtr)OpenProcess(PROCESS_ALL_ACCESS, 0, process.Id);
  124. List<int> foundList = MemorySearch(HWND, searchStr);
  125. foreach (var item in foundList)
  126. {
  127. string ret = ReadMemoryString(HWND, item, 1024);
  128. string token = getSubString(ret, "\",\"token\":\"", "\",\"");
  129. if (!token.Equals("") && token.IndexOf("eyJ") != -1)
  130. {
  131. return token;
  132. }
  133. }
  134. }
  135. }
  136. return "false";
  137. }
  138. }
  139. }