2
0

DecryptionHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 WechatBakTool.Model;
  14. namespace WechatBakTool.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(string pid, bool mem_find_key, string account)
  26. {
  27. Process process = Process.GetProcessById(int.Parse(pid));
  28. ProcessModule? module = ProcessHelper.FindProcessModule(process.Id, "WeChatWin.dll");
  29. if (module == null)
  30. {
  31. return null;
  32. }
  33. string? version = module.FileVersionInfo.FileVersion;
  34. if (version == null)
  35. {
  36. return null;
  37. }
  38. if (!mem_find_key)
  39. {
  40. List<VersionInfo>? info = null;
  41. string json = File.ReadAllText("version.json");
  42. 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. }
  64. else
  65. {
  66. List<int> read = ProcessHelper.FindProcessMemory(process.Handle, module, account);
  67. if (read.Count >= 2)
  68. {
  69. byte[] buffer = new byte[8];
  70. int key_offset = read[1] - 64;
  71. if (ProcessHelper.ReadProcessMemory(process.Handle, module.BaseAddress + key_offset, buffer, buffer.Length, out _))
  72. {
  73. ulong addr = BitConverter.ToUInt64(buffer, 0);
  74. byte[] key_bytes = new byte[32];
  75. if (ProcessHelper.ReadProcessMemory(process.Handle, (IntPtr)addr, key_bytes, key_bytes.Length, out _))
  76. {
  77. return key_bytes;
  78. }
  79. }
  80. }
  81. else
  82. {
  83. throw new Exception("搜索不到微信账号,请确认用户名是否正确,如错误请重新新建工作区,务必确认账号是否正确");
  84. }
  85. }
  86. return null;
  87. }
  88. public static byte[] DecryptDB(byte[] db_file_bytes, byte[] password_bytes)
  89. {
  90. //数据库头16字节是盐值
  91. var salt = db_file_bytes.Take(16).ToArray();
  92. //HMAC验证时用的盐值需要亦或0x3a
  93. byte[] hmac_salt = new byte[16];
  94. for (int i = 0; i < salt.Length; i++)
  95. {
  96. hmac_salt[i] = (byte)(salt[i] ^ 0x3a);
  97. }
  98. //计算保留段长度
  99. int reserved = IV_SIZE;
  100. reserved += HMAC_SHA1_SIZE;
  101. reserved = ((reserved % AES_BLOCK_SIZE) == 0) ? reserved : ((reserved / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
  102. //密钥扩展,分别对应AES解密密钥和HMAC验证密钥
  103. byte[] key = new byte[KEY_SIZE];
  104. byte[] hmac_key = new byte[KEY_SIZE];
  105. OpenSSLInterop.PKCS5_PBKDF2_HMAC_SHA1(password_bytes, password_bytes.Length, salt, salt.Length, DEFAULT_ITER, key.Length, key);
  106. OpenSSLInterop.PKCS5_PBKDF2_HMAC_SHA1(key, key.Length, hmac_salt, hmac_salt.Length, 2, hmac_key.Length, hmac_key);
  107. int page_no = 0;
  108. int offset = 16;
  109. Console.WriteLine("开始解密...");
  110. var hmac_sha1 = HMAC.Create("HMACSHA1");
  111. hmac_sha1!.Key = hmac_key;
  112. List<byte> decrypted_file_bytes = new List<byte>();
  113. while (page_no < db_file_bytes.Length / DEFAULT_PAGESIZE)
  114. {
  115. byte[] decryped_page_bytes = new byte[DEFAULT_PAGESIZE];
  116. byte[] going_to_hashed = new byte[DEFAULT_PAGESIZE - reserved - offset + IV_SIZE + 4];
  117. db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + offset).Take(DEFAULT_PAGESIZE - reserved - offset + IV_SIZE).ToArray().CopyTo(going_to_hashed, 0);
  118. var page_bytes = BitConverter.GetBytes(page_no + 1);
  119. page_bytes.CopyTo(going_to_hashed, DEFAULT_PAGESIZE - reserved - offset + IV_SIZE);
  120. //计算分页的Hash
  121. var hash_mac_compute = hmac_sha1.ComputeHash(going_to_hashed, 0, going_to_hashed.Count());
  122. //取出分页中存储的Hash
  123. var hash_mac_cached = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + DEFAULT_PAGESIZE - reserved + IV_SIZE).Take(hash_mac_compute.Length).ToArray();
  124. //对比两个Hash
  125. if (!hash_mac_compute.SequenceEqual(hash_mac_cached))
  126. {
  127. Console.WriteLine("Hash错误...");
  128. return decrypted_file_bytes.ToArray();
  129. }
  130. else
  131. {
  132. Console.WriteLine($"解密第[{page_no + 1}]页");
  133. if (page_no == 0)
  134. {
  135. var header_bytes = Encoding.ASCII.GetBytes(SQLITE_HEADER);
  136. header_bytes.CopyTo(decryped_page_bytes, 0);
  137. }
  138. var encrypted_content = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + offset).Take(DEFAULT_PAGESIZE - reserved - offset).ToArray();
  139. var iv = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + (DEFAULT_PAGESIZE - reserved)).Take(16).ToArray();
  140. var decrypted_content = DecryptionHelper.AESDecrypt(encrypted_content, key, iv);
  141. decrypted_content.CopyTo(decryped_page_bytes, offset);
  142. var reserved_bytes = db_file_bytes.Skip((page_no * DEFAULT_PAGESIZE) + DEFAULT_PAGESIZE - reserved).Take(reserved).ToArray();
  143. reserved_bytes.CopyTo(decryped_page_bytes, DEFAULT_PAGESIZE - reserved);
  144. }
  145. page_no++;
  146. offset = 0;
  147. foreach (var item in decryped_page_bytes)
  148. {
  149. decrypted_file_bytes.Add(item);
  150. }
  151. }
  152. return decrypted_file_bytes.ToArray();
  153. }
  154. public static byte[] AESDecrypt(byte[] content, byte[] key, byte[] iv)
  155. {
  156. Aes rijndaelCipher = Aes.Create();
  157. rijndaelCipher.Mode = CipherMode.CBC;
  158. rijndaelCipher.Padding = PaddingMode.None;
  159. rijndaelCipher.KeySize = 256;
  160. rijndaelCipher.BlockSize = 128;
  161. rijndaelCipher.Key = key;
  162. rijndaelCipher.IV = iv;
  163. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  164. byte[] plain_bytes = transform.TransformFinalBlock(content, 0, content.Length);
  165. return plain_bytes;
  166. }
  167. private static string BytesToHex(byte[] bytes)
  168. {
  169. return BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower().ToUpper();
  170. }
  171. public static byte[] DecImage(string source)
  172. {
  173. //读取数据
  174. byte[] fileBytes = File.ReadAllBytes(source);
  175. //算差异转换
  176. byte key = GetImgKey(fileBytes);
  177. fileBytes = ConvertData(fileBytes, key);
  178. return fileBytes;
  179. }
  180. public static string CheckFileType(byte[] data)
  181. {
  182. switch (data[0])
  183. {
  184. case 0XFF: //byte[] jpg = new byte[] { 0xFF, 0xD8, 0xFF };
  185. {
  186. if (data[1] == 0xD8 && data[2] == 0xFF)
  187. {
  188. return ".jpg";
  189. }
  190. break;
  191. }
  192. case 0x89: //byte[] png = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
  193. {
  194. if (data[1] == 0x50 && data[2] == 0x4E && data[7] == 0x0A)
  195. {
  196. return ".png";
  197. }
  198. break;
  199. }
  200. case 0x42: //byte[] bmp = new byte[] { 0x42, 0x4D };
  201. {
  202. if (data[1] == 0X4D)
  203. {
  204. return ".bmp";
  205. }
  206. break;
  207. }
  208. case 0x47: //byte[] gif = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39(0x37), 0x61 };
  209. {
  210. if (data[1] == 0x49 && data[2] == 0x46 && data[3] == 0x38 && data[5] == 0x61)
  211. {
  212. return ".gif";
  213. }
  214. break;
  215. }
  216. case 0x49: // byte[] tif = new byte[] { 0x49, 0x49, 0x2A, 0x00 };
  217. {
  218. if (data[1] == 0x49 && data[2] == 0x2A && data[3] == 0x00)
  219. {
  220. return ".tif";
  221. }
  222. break;
  223. }
  224. case 0x4D: //byte[] tif = new byte[] { 0x4D, 0x4D, 0x2A, 0x00 };
  225. {
  226. if (data[1] == 0x4D && data[2] == 0x2A && data[3] == 0x00)
  227. {
  228. return ".tif";
  229. }
  230. break;
  231. }
  232. }
  233. return ".dat";
  234. }
  235. private static byte GetImgKey(byte[] fileRaw)
  236. {
  237. byte[] raw = new byte[8];
  238. for (int i = 0; i < 8; i++)
  239. {
  240. raw[i] = fileRaw[i];
  241. }
  242. for (byte key = 0x01; key < 0xFF; key++)
  243. {
  244. byte[] buf = new byte[8];
  245. raw.CopyTo(buf, 0);
  246. if (CheckFileType(ConvertData(buf, key)) != ".dat")
  247. {
  248. return key;
  249. }
  250. }
  251. return 0x00;
  252. }
  253. private static byte[] ConvertData(byte[] data, byte key)
  254. {
  255. for (int i = 0; i < data.Length; i++)
  256. {
  257. data[i] ^= key;
  258. }
  259. return data;
  260. }
  261. public static string SaveDecImage(byte[] fileRaw,string source,string to_dir,string type)
  262. {
  263. FileInfo fileInfo = new FileInfo(source);
  264. string fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
  265. string saveFilePath = Path.Combine(to_dir, fileName + type);
  266. using (FileStream fileStream = File.OpenWrite(saveFilePath))
  267. {
  268. fileStream.Write(fileRaw, 0, fileRaw.Length);
  269. fileStream.Flush();
  270. }
  271. return saveFilePath;
  272. }
  273. public static void DecryUserData(byte[] key, string source, string to)
  274. {
  275. string dbPath = source;
  276. string decPath = to;
  277. if (!Directory.Exists(decPath))
  278. Directory.CreateDirectory(decPath);
  279. string[] filePath = Directory.GetFiles(dbPath);
  280. foreach (string file in filePath)
  281. {
  282. FileInfo info = new FileInfo(file);
  283. var db_bytes = File.ReadAllBytes(file);
  284. var decrypted_file_bytes = DecryptDB(db_bytes, key);
  285. if (decrypted_file_bytes == null || decrypted_file_bytes.Length == 0)
  286. {
  287. Console.WriteLine("解密后的数组为空");
  288. }
  289. else
  290. {
  291. File.WriteAllBytes(Path.Combine(decPath, info.Name), decrypted_file_bytes);
  292. }
  293. }
  294. }
  295. }
  296. }