2
0

CreateWork.xaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using JiebaNet.Segmenter.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using WechatBakTool.Helpers;
  18. using WechatBakTool.Model;
  19. using WechatBakTool.ViewModel;
  20. namespace WechatBakTool.Pages
  21. {
  22. /// <summary>
  23. /// CreateWork.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class CreateWork : Page
  26. {
  27. private CreateWorkViewModel ViewModel = new CreateWorkViewModel();
  28. public CreateWork()
  29. {
  30. DataContext = ViewModel;
  31. InitializeComponent();
  32. GetWechatProcessInfos();
  33. isManualProcess();
  34. }
  35. private void isManualProcess()
  36. {
  37. if(Main2.CurrentUserBakConfig!= null)
  38. {
  39. cb_manual.IsChecked = Main2.CurrentUserBakConfig.Manual;
  40. }
  41. }
  42. private void GetWechatProcessInfos()
  43. {
  44. ViewModel.ProcessInfos.Clear();
  45. Process[] processes = Process.GetProcessesByName("wechat");
  46. foreach (Process p in processes)
  47. {
  48. var lHandles = NativeAPIHelper.GetHandleInfoForPID((uint)p.Id);
  49. foreach (var h in lHandles)
  50. {
  51. string name = NativeAPIHelper.FindHandleName(h, p);
  52. if (name != "")
  53. {
  54. // 预留handle log
  55. if (File.Exists("handle.log"))
  56. {
  57. File.AppendAllText("handle.log", string.Format("{0}|{1}|{2}|{3}\n", p.Id, h.ObjectTypeIndex, h.HandleValue, name));
  58. }
  59. if (name.Contains("\\MicroMsg.db") && name.Substring(name.Length - 3, 3) == ".db")
  60. {
  61. ProcessInfo info = new ProcessInfo();
  62. info.ProcessId = p.Id.ToString();
  63. info.ProcessName = p.ProcessName;
  64. info.DBPath = DevicePathMapper.FromDevicePath(name)!;
  65. ViewModel.ProcessInfos.Add(info);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. private void list_process_SelectionChanged(object sender, SelectionChangedEventArgs e)
  72. {
  73. if (ViewModel.SelectProcess != null)
  74. {
  75. string[] name_raw = ViewModel.SelectProcess.DBPath.Split("\\");
  76. ViewModel.UserName = name_raw[name_raw.Length - 3];
  77. FileInfo fileInfo = new FileInfo(ViewModel.SelectProcess.DBPath);
  78. DirectoryInfo msgParent = fileInfo.Directory!.Parent!;
  79. DirectoryInfo[] accounts = msgParent.GetDirectories();
  80. DirectoryInfo? newUserName = null;
  81. foreach ( DirectoryInfo account in accounts )
  82. {
  83. if(account.Name.Contains("account_")) {
  84. if(newUserName == null)
  85. newUserName = account;
  86. else
  87. {
  88. if (newUserName.LastWriteTime < account.LastWriteTime)
  89. newUserName = account;
  90. }
  91. }
  92. }
  93. if(newUserName != null)
  94. {
  95. ViewModel.UserName = newUserName.Name.Split("_")[1];
  96. }
  97. }
  98. }
  99. private void btn_create_worksapce_Click(object sender, RoutedEventArgs e)
  100. {
  101. ViewModel.IsEnable = false;
  102. bool m = (bool)cb_manual.IsChecked!;
  103. Task.Run(() => {
  104. if (ViewModel.KeyType != -1 && !m)
  105. {
  106. if (ViewModel.SelectProcess != null)
  107. {
  108. ViewModel.LabelStatus = "数据准备";
  109. string path = ViewModel.SelectProcess.DBPath.Replace("\\Msg\\MicroMsg.db", "");
  110. try
  111. {
  112. ViewModel.LabelStatus = "准备创建工作区";
  113. //创建工作区
  114. WXWorkspace wXWorkspace = new WXWorkspace(path, ViewModel.UserName);
  115. //DB移动
  116. wXWorkspace.MoveDB(ViewModel);
  117. if(ViewModel.SelectProcess == null)
  118. return;
  119. //开始解密数据库
  120. try
  121. {
  122. ViewModel.LabelStatus = "开始解密数据库";
  123. wXWorkspace.DecryptDB(ViewModel.SelectProcess.ProcessId, ViewModel.KeyType,ViewModel);
  124. MessageBox.Show("创建工作区成功");
  125. Dispatcher.Invoke(() =>
  126. {
  127. ((Main2)Window.GetWindow(this)).LoadWorkspace();
  128. });
  129. }
  130. catch (Exception ex)
  131. {
  132. MessageBox.Show(ex.Message);
  133. ViewModel.IsEnable = true;
  134. }
  135. }
  136. catch (Exception)
  137. {
  138. MessageBox.Show("创建工作区失败,请检查路径是否正确");
  139. ViewModel.IsEnable = true;
  140. }
  141. }
  142. }
  143. else if (m)
  144. {
  145. WXWorkspace wXWorkspace = new WXWorkspace(Main2.CurrentUserBakConfig!);
  146. ViewModel.LabelStatus = "开始解密数据库";
  147. wXWorkspace.DecryptDB("", -1, ViewModel,Main2.CurrentUserBakConfig!.Key);
  148. Dispatcher.Invoke(() =>
  149. {
  150. MessageBox.Show("解密完成");
  151. ((Main2)Window.GetWindow(this)).LoadWorkspace();
  152. });
  153. }
  154. else
  155. {
  156. MessageBox.Show("请选择Key获取方式", "错误");
  157. }
  158. ViewModel.IsEnable = true;
  159. });
  160. }
  161. private void cb_manual_Checked(object sender, RoutedEventArgs e)
  162. {
  163. MessageBox.Show("该功能仅限用于网络安全研究用途使用,红队同学请在合规授权下进行相关操作","重要提醒!!!!!!!!!");
  164. if(Main2.CurrentUserBakConfig != null)
  165. {
  166. if (Main2.CurrentUserBakConfig.Manual)
  167. {
  168. return;
  169. }
  170. }
  171. if (MessageBox.Show("我确认获取到合规授权,仅用于网络安全用途使用", "信息确认", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  172. {
  173. if (File.Exists("auth.txt"))
  174. {
  175. string auth = File.ReadAllText("auth.txt");
  176. /*
  177. *
  178. * pwd:
  179. * 我已知晓手动模式可能潜在的法律及道德风险,我明白非法使用将要承担相关法律责任。
  180. * tips:
  181. * 请不要公开宣传手动模式,不提供任何使用解答,谢谢。
  182. * 不要编写任何关于手动模式的教程,避免非法传播使用。
  183. *
  184. */
  185. if (DecryptionHelper.GetMD5(auth) == "295f634af60d61dfa52a5f35849ac42b")
  186. {
  187. string genHash = DateTime.Now.ToString();
  188. string md5 = DecryptionHelper.GetMD5(genHash);
  189. UserBakConfig config = new UserBakConfig();
  190. config.Hash = md5;
  191. string workspacePath = Path.Combine(Directory.GetCurrentDirectory(), "workspace");
  192. config.UserWorkspacePath = Path.Combine(workspacePath, md5);
  193. WXWorkspace workspace = new WXWorkspace(config);
  194. workspace.ManualInit();
  195. MessageBox.Show("已经创建空的配置文件,请完善该配置文件后,点击开始解密","提示");
  196. }
  197. }
  198. else
  199. {
  200. MessageBox.Show("未完成声明文件,请先确认声明", "错误");
  201. }
  202. }
  203. else
  204. {
  205. cb_manual.IsChecked = false;
  206. }
  207. }
  208. }
  209. }