DecryptionHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Text.Json.Serialization;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using WechatPCMsgBakTool.Model;
  14. namespace WechatPCMsgBakTool.Helpers
  15. {
  16. public class DecryptionHelper
  17. {
  18. const int IV_SIZE = 16;
  19. const int HMAC_SHA1_SIZE = 20;
  20. const int KEY_SIZE = 32;
  21. const int AES_BLOCK_SIZE = 16;
  22. const int DEFAULT_ITER = 64000;
  23. const int DEFAULT_PAGESIZE = 4096; //4048数据 + 16IV + 20 HMAC + 12
  24. const string SQLITE_HEADER = "SQLite format 3";
  25. public static byte[]? GetWechatKey(bool mem_find_key,string account)
  26. {
  27. Process? process = ProcessHelper.GetProcess("WeChat");
  28. if (process == null)
  29. {
  30. return null;
  31. }
  32. ProcessModule? module = ProcessHelper.FindProcessModule(process.Id, "WeChatWin.dll");
  33. if (module == null)
  34. {
  35. return null;
  36. }
  37. string? version = module.FileVersionInfo.FileVersion;
  38. if (version == null)
  39. {
  40. return null;
  41. }
  42. if (!mem_find_key)
  43. {
  44. List<VersionInfo>? info = null;
  45. string json = File.ReadAllText("version.json");
  46. info = JsonConvert.DeserializeObject<List<VersionInfo>?>(json);
  47. if (info == null)
  48. return null;
  49. if (info.Count == 0)
  50. return null;
  51. VersionInfo? cur = info.Find(x => x.Version == version);
  52. if (cur == null)
  53. return null;
  54. //这里加的是版本偏移量,兼容不同版本把这个加给改了
  55. long baseAddress = (long)module.BaseAddress + cur.BaseAddr;
  56. byte[]? bytes = ProcessHelper.ReadMemoryDate(process.Handle, (IntPtr)baseAddress, 8);
  57. if (bytes != null)
  58. {
  59. IntPtr baseAddress2 = (IntPtr)(((long)bytes[7] << 56) + ((long)bytes[6] << 48) + ((long)bytes[5] << 40) + ((long)bytes[4] << 32) + ((long)bytes[3] << 24) + ((long)bytes[2] << 16) + ((long)bytes[1] << 8) + (long)bytes[0]);
  60. byte[]? twoGet = ProcessHelper.ReadMemoryDate(process.Handle, baseAddress2, 32);
  61. if (twoGet != null)
  62. {
  63. string key = BytesToHex(twoGet);
  64. return twoGet;
  65. }
  66. }
  67. }
  68. else
  69. {
  70. List<int> read = ProcessHelper.FindProcessMemory(process.Handle, module, account);
  71. if(read.Count >= 2)
  72. {
  73. byte[] buffer = new byte[8];
  74. int key_offset = read[1] - 64;
  75. if (ProcessHelper.ReadProcessMemory(process.Handle, module.BaseAddress + key_offset, buffer, buffer.Length, out _))
  76. {
  77. ulong addr = BitConverter.ToUInt64(buffer, 0);
  78. byte[] key_bytes = new byte[32];
  79. if(ProcessHelper.ReadProcessMemory(process.Handle, (IntPtr)addr, key_bytes, key_bytes.Length, out _))
  80. {
  81. return key_bytes;
  82. }
  83. }
  84. }
  85. else
  86. {
  87. MessageBox.Show("搜索不到微信账号,请确认用户名是否正确,如错误请重新新建工作区,务必确认账号是否正确", "错误");
  88. }
  89. }
  90. return null;
  91. }
  92. public static byte[] DecryptDB(byte[] db_file_bytes, byte[] password_bytes)
  93. {
  94. //数据库头16字节是盐值
  95. var salt = db_file_bytes.Take(16).ToArray();
  96. //HMAC验证时用的盐值需要亦或0x3a
  97. byte[] hmac_salt = new byte[16];
  98. for (int i = 0; i < salt.Length; i++)
  99. {
  100. hmac_salt[i] = (byte)(salt[i] ^ 0x3a);
  101. }
  102. //计算保留段长度
  103. int reserved = IV_SIZE;
  104. reserved += HMAC_SHA1_SIZE;
  105. reserved = ((reserved % AES_BLOCK_SIZE) == 0) ? reserved : ((reserved / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
  106. //密钥扩展,分别对应AES解密密钥和HMAC验证密钥
  107. byte[] key = new byte[KEY_SIZE];
  108. byte[] hmac_key = new byte[KEY_SIZE];
  109. OpenSSLInterop.PKCS5_PBKDF2_HMAC_SHA1(password_bytes, password_bytes.Length, salt, salt.Length, DEFAULT_ITER, key.Length, key);
  110. OpenSSLInterop.PKCS5_PBKDF2_HMAC_SHA1(key, key.Length, hmac_salt, hmac_salt.Length, 2, hmac_key.Length, hmac_key);
  111. int page_no = 0;
  112. int offset = 16;
  113. Console.WriteLine("开始解密...");
  114. var hmac_sha1 = HMAC.Create("HMACSHA1");
  115. hmac_sha1!.Key = hmac_key;
  116. List<byte> decrypted_file_bytes = new List<byte>();
  117. while (page_no < db_file_bytes.Length / DEFAULT_PAGESIZE)
  118. {
  119. byte[] decryped_page_bytes = new byte[DEFAULT_PAGESIZE];
  120. byte[] going_to_hashed = new byte[DEFAULT_PAGESIZE - reserved - offset + IV_SIZE + 4];
  121. db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + offset).Take(DEFAULT_PAGESIZE - reserved - offset + IV_SIZE).ToArray().CopyTo(going_to_hashed, 0);
  122. var page_bytes = BitConverter.GetBytes(page_no + 1);
  123. page_bytes.CopyTo(going_to_hashed, DEFAULT_PAGESIZE - reserved - offset + IV_SIZE);
  124. //计算分页的Hash
  125. var hash_mac_compute = hmac_sha1.ComputeHash(going_to_hashed, 0, going_to_hashed.Count());
  126. //取出分页中存储的Hash
  127. var hash_mac_cached = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + DEFAULT_PAGESIZE - reserved + IV_SIZE).Take(hash_mac_compute.Length).ToArray();
  128. //对比两个Hash
  129. if (!hash_mac_compute.SequenceEqual(hash_mac_cached))
  130. {
  131. Console.WriteLine("Hash错误...");
  132. return decrypted_file_bytes.ToArray();
  133. }
  134. else
  135. {
  136. Console.WriteLine($"解密第[{page_no + 1}]页");
  137. if (page_no == 0)
  138. {
  139. var header_bytes = Encoding.ASCII.GetBytes(SQLITE_HEADER);
  140. header_bytes.CopyTo(decryped_page_bytes, 0);
  141. }
  142. var encrypted_content = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + offset).Take(DEFAULT_PAGESIZE - reserved - offset).ToArray();
  143. var iv = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + (DEFAULT_PAGESIZE - reserved)).Take(16).ToArray();
  144. var decrypted_content = DecryptionHelper.AESDecrypt(encrypted_content, key, iv);
  145. decrypted_content.CopyTo(decryped_page_bytes, offset);
  146. var reserved_bytes = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + DEFAULT_PAGESIZE - reserved).Take(reserved).ToArray();
  147. reserved_bytes.CopyTo(decryped_page_bytes, DEFAULT_PAGESIZE - reserved);
  148. }
  149. page_no++;
  150. offset = 0;
  151. foreach (var item in decryped_page_bytes)
  152. {
  153. decrypted_file_bytes.Add(item);
  154. }
  155. }
  156. return decrypted_file_bytes.ToArray();
  157. }
  158. public static byte[] AESDecrypt(byte[] content, byte[] key, byte[] iv)
  159. {
  160. Aes rijndaelCipher = Aes.Create();
  161. rijndaelCipher.Mode = CipherMode.CBC;
  162. rijndaelCipher.Padding = PaddingMode.None;
  163. rijndaelCipher.KeySize = 256;
  164. rijndaelCipher.BlockSize = 128;
  165. rijndaelCipher.Key = key;
  166. rijndaelCipher.IV = iv;
  167. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  168. byte[] plain_bytes = transform.TransformFinalBlock(content, 0, content.Length);
  169. return plain_bytes;
  170. }
  171. private static string BytesToHex(byte[] bytes)
  172. {
  173. return BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower().ToUpper();
  174. }
  175. public static byte[] DecImage(string source)
  176. {
  177. //读取数据
  178. byte[] fileBytes = File.ReadAllBytes(source);
  179. //算差异转换
  180. byte key = GetImgKey(fileBytes);
  181. fileBytes = ConvertData(fileBytes, key);
  182. return fileBytes;
  183. }
  184. public static string CheckFileType(byte[] data)
  185. {
  186. switch (data[0])
  187. {
  188. case 0XFF: //byte[] jpg = new byte[] { 0xFF, 0xD8, 0xFF };
  189. {
  190. if (data[1] == 0xD8 && data[2] == 0xFF)
  191. {
  192. return ".jpg";
  193. }
  194. break;
  195. }
  196. case 0x89: //byte[] png = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
  197. {
  198. if (data[1] == 0x50 && data[2] == 0x4E && data[7] == 0x0A)
  199. {
  200. return ".png";
  201. }
  202. break;
  203. }
  204. case 0x42: //byte[] bmp = new byte[] { 0x42, 0x4D };
  205. {
  206. if (data[1] == 0X4D)
  207. {
  208. return ".bmp";
  209. }
  210. break;
  211. }
  212. case 0x47: //byte[] gif = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39(0x37), 0x61 };
  213. {
  214. if (data[1] == 0x49 && data[2] == 0x46 && data[3] == 0x38 && data[5] == 0x61)
  215. {
  216. return ".gif";
  217. }
  218. break;
  219. }
  220. case 0x49: // byte[] tif = new byte[] { 0x49, 0x49, 0x2A, 0x00 };
  221. {
  222. if (data[1] == 0x49 && data[2] == 0x2A && data[3] == 0x00)
  223. {
  224. return ".tif";
  225. }
  226. break;
  227. }
  228. case 0x4D: //byte[] tif = new byte[] { 0x4D, 0x4D, 0x2A, 0x00 };
  229. {
  230. if (data[1] == 0x4D && data[2] == 0x2A && data[3] == 0x00)
  231. {
  232. return ".tif";
  233. }
  234. break;
  235. }
  236. }
  237. return ".dat";
  238. }
  239. private static byte GetImgKey(byte[] fileRaw)
  240. {
  241. byte[] raw = new byte[8];
  242. for (int i = 0; i < 8; i++)
  243. {
  244. raw[i] = fileRaw[i];
  245. }
  246. for (byte key = 0x01; key < 0xFF; key++)
  247. {
  248. byte[] buf = new byte[8];
  249. raw.CopyTo(buf, 0);
  250. if (CheckFileType(ConvertData(buf, key)) != ".dat")
  251. {
  252. return key;
  253. }
  254. }
  255. return 0x00;
  256. }
  257. private static byte[] ConvertData(byte[] data, byte key)
  258. {
  259. for (int i = 0; i < data.Length; i++)
  260. {
  261. data[i] ^= key;
  262. }
  263. return data;
  264. }
  265. public static string SaveDecImage(byte[] fileRaw,string source,string to_dir,string type)
  266. {
  267. FileInfo fileInfo = new FileInfo(source);
  268. string fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
  269. string saveFilePath = Path.Combine(to_dir, fileName + type);
  270. using (FileStream fileStream = File.OpenWrite(saveFilePath))
  271. {
  272. fileStream.Write(fileRaw, 0, fileRaw.Length);
  273. fileStream.Flush();
  274. }
  275. return saveFilePath;
  276. }
  277. }
  278. }