DecryptionHelper.cs 11 KB

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