DecryptionHelper.cs 11 KB

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