Переглянути джерело

迁移解密至WXWorkspace
解密方式选择切换MVVM
完善新界面功能

Suxue 1 рік тому
батько
коміт
50c3b22c74

+ 3 - 7
Helpers/DecryptionHelper.cs

@@ -23,13 +23,9 @@ namespace WechatBakTool.Helpers
         const int DEFAULT_ITER = 64000;
         const int DEFAULT_PAGESIZE = 4096; //4048数据 + 16IV + 20 HMAC + 12
         const string SQLITE_HEADER = "SQLite format 3";
-        public static byte[]? GetWechatKey(bool mem_find_key,string account)
+        public static byte[]? GetWechatKey(string pid,bool mem_find_key,string account)
         {
-            Process? process = ProcessHelper.GetProcess("WeChat");
-            if (process == null)
-            {
-                return null;
-            }
+            Process process = Process.GetProcessById(int.Parse(pid));
             ProcessModule? module = ProcessHelper.FindProcessModule(process.Id, "WeChatWin.dll");
             if (module == null)
             {
@@ -90,7 +86,7 @@ namespace WechatBakTool.Helpers
                 }
                 else
                 {
-                    MessageBox.Show("搜索不到微信账号,请确认用户名是否正确,如错误请重新新建工作区,务必确认账号是否正确", "错误");
+                    throw new Exception("搜索不到微信账号,请确认用户名是否正确,如错误请重新新建工作区,务必确认账号是否正确");
                 }
             }
             return null;

+ 2 - 2
Main.xaml.cs

@@ -98,10 +98,10 @@ namespace WechatBakTool
                     byte[]? key = null;
                     try
                     {
-                        key = DecryptionHelper.GetWechatKey((bool)mem_find_key,CurrentUserBakConfig.Account);
+                        //key = DecryptionHelper.GetWechatKey((bool)mem_find_key,CurrentUserBakConfig.Account);
                         if (key == null)
                             MessageBox.Show("获取到的秘钥为空");
-                        File.AppendAllText("debug.log", BitConverter.ToString(key, 0));
+                        //File.AppendAllText("debug.log", BitConverter.ToString(key, 0));
                     }
                     catch (Exception ex)
                     {

+ 10 - 6
Main2.xaml.cs

@@ -37,7 +37,7 @@ namespace WechatBakTool
             Application.Current.Shutdown();
         }
 
-        private void LoadWorkspace()
+        public void LoadWorkspace()
         {
             userBakConfigs.Clear();
             string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "workspace");
@@ -72,16 +72,20 @@ namespace WechatBakTool
         private void list_workspace_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             UserBakConfig? config = list_workspace.SelectedItem as UserBakConfig;
-            if(config != null)
+            if(config == null)
             {
-                CurrentUserBakConfig = config;
-                MainFrame.Navigate(new Uri("pack://application:,,,/Pages/Workspace.xaml?datatime=" + DateTime.Now.Ticks));
+                MessageBox.Show("该工作区可能有异常,请确认配置文件是否正确");
+                return;
             }
-            else
+            if (!config.Decrypt)
             {
-                MessageBox.Show("工作区配置文件异常,请确认工作区配置是否正常", "错误", MessageBoxButton.OK);
+                MessageBox.Show("请先到创建工作区进行解密");
+                MainFrame.Navigate(new Uri("pack://application:,,,/Pages/CreateWork.xaml?datatime=" + DateTime.Now.Ticks));
                 return;
             }
+
+            CurrentUserBakConfig = config;
+            MainFrame.Navigate(new Uri("pack://application:,,,/Pages/Workspace.xaml?datatime=" + DateTime.Now.Ticks));
         }
 
         private void new_workspace_fill_MouseDown(object sender, MouseButtonEventArgs e)

+ 6 - 4
Pages/CreateWork.xaml

@@ -3,11 +3,13 @@
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-      xmlns:local="clr-namespace:WechatBakTool.Pages"
+      xmlns:local="clr-namespace:WechatBakTool.ViewModel"
       mc:Ignorable="d" 
       d:DesignHeight="550" d:DesignWidth="800"
       Title="Welcome" Background="White">
-
+    <Page.Resources>
+        <local:GetKeyConverter x:Key="getKeyConverterKey" />
+    </Page.Resources>
     <Grid>
         <Label FontSize="20" Margin="30,15" Content="新建工作区" HorizontalAlignment="Left" VerticalAlignment="Top" />
         <Label Margin="30,55,0,0" Content="请选择要创建工作区的微信,可以通过微信路径判断是哪一个微信哦!" HorizontalAlignment="Left" VerticalAlignment="Top" />
@@ -24,8 +26,8 @@
         <TextBox x:Name="txt_username" Margin="35,300,0,0" Width="280" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="0,0,0,1" Text="{Binding UserName}" />
 
         <Label Margin="30,350,0,0" Content="请选择解密方式:" FontWeight="Bold" HorizontalAlignment="Left" VerticalAlignment="Top"/>
-        <RadioButton Margin="35,380,0,0" Content="固定地址查找" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="rb_find_key" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
-        <RadioButton Margin="35,405,0,0" Content="用户名推断查找" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="rb_find_key" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
+        <RadioButton Margin="35,380,0,0" Content="固定地址查找" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="rb_find_key" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" IsChecked="{Binding KeyType, Converter={StaticResource ResourceKey=getKeyConverterKey}, ConverterParameter=1}" />
+        <RadioButton Margin="35,405,0,0" Content="用户名推断查找" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="rb_find_key" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" IsChecked="{Binding KeyType, Converter={StaticResource ResourceKey=getKeyConverterKey}, ConverterParameter=2}"/>
 
         <Button Name="btn_create_worksapce" Margin="0,0,35,50" Height="60" Width="100" HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="创建工作区" BorderThickness="0" Background="#2775b6" Foreground="White" Click="btn_create_worksapce_Click">
             <Button.Resources>

+ 33 - 3
Pages/CreateWork.xaml.cs

@@ -92,13 +92,43 @@ namespace WechatBakTool.Pages
                     ViewModel.UserName = newUserName.Name.Split("_")[1];
                 }
             }
-
-            
         }
 
         private void btn_create_worksapce_Click(object sender, RoutedEventArgs e)
         {
-
+            if(ViewModel.KeyType != -1)
+            {
+                if (ViewModel.SelectProcess != null)
+                {
+                    string path = ViewModel.SelectProcess.DBPath.Replace("\\Msg\\MicroMsg.db", "");
+                    try
+                    {
+                        //创建工作区
+                        WXWorkspace wXWorkspace = new WXWorkspace(path, ViewModel.UserName);
+                        //DB移动
+                        wXWorkspace.MoveDB();
+                        //开始解密数据库
+                        try
+                        {
+                            wXWorkspace.DecryptDB(ViewModel.SelectProcess.ProcessId, ViewModel.KeyType);
+                            MessageBox.Show("创建工作区成功");
+                            ((Main2)Window.GetWindow(this)).LoadWorkspace();
+                        }
+                        catch (Exception ex)
+                        {
+                            MessageBox.Show(ex.Message);
+                        }
+                    }
+                    catch (Exception)
+                    {
+                        MessageBox.Show("创建工作区失败,请检查路径是否正确");
+                    }
+                }
+            }
+            else
+            {
+                MessageBox.Show("请选择Key获取方式", "错误");
+            }
         }
     }
 }

+ 1 - 1
Pages/Welcome.xaml

@@ -5,7 +5,7 @@
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:local="clr-namespace:WechatBakTool.Pages"
       mc:Ignorable="d" 
-      d:DesignHeight="450" d:DesignWidth="800"
+      d:DesignHeight="550" d:DesignWidth="800"
       Title="Welcome" Background="White">
 
     <Grid>

+ 4 - 2
README.md

@@ -25,6 +25,8 @@
 > 本分支为项目开发分支,变动较为频繁且可能不可用<br/>
 > 如果你希望观察作者的开发动态,可以参考这个分支。<br/>
 
+<br/>
+
 ### 免责声明
 
 **本项目仅供学习使用,严禁商业使用**<br/>
@@ -40,10 +42,10 @@ C# + .NET6.0 + WPF MVVM(目前MVVM不是特别完全!莫喷!) <br/>
 - [ ] 性能优化
 - [ ] 打包资源文件夹
 - [ ] 手动模式(合适离线分析)
-
+<br/>
 
 ### 使用说明
-**本说明为新版本说明,即将发版**<br/><br/>
+**本说明为新版本说明,即将发版**<br/>
 0.安装.NET Desktop Runtime(如已经安装忽略)<br/>
 1.打开微信,并登录。<br/>
 2.在软件左侧下方点击**新建工作区**,<br/>

+ 17 - 0
ViewModel/CreateWorkViewModel.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows.Data;
 using WechatBakTool.Model;
 
 namespace WechatBakTool.ViewModel
@@ -18,5 +19,21 @@ namespace WechatBakTool.ViewModel
 
         [ObservableProperty]
         private string userName = "";
+
+        [ObservableProperty]
+        private int keyType = -1;
+    }
+
+    public class GetKeyConverter : IValueConverter
+    {
+        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
+        {
+            return (int.Parse(parameter.ToString()!) == (int)value);
+        }
+
+        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
+        {
+            return (bool)value ? parameter : Binding.DoNothing;
+        }
     }
 }

+ 3 - 1
WXUserReader.cs

@@ -17,6 +17,7 @@ using System.Xml;
 using System.Xml.Linq;
 using WechatBakTool.Helpers;
 using WechatBakTool.Model;
+using System.Windows;
 
 namespace WechatBakTool
 {
@@ -70,6 +71,7 @@ namespace WechatBakTool
                     UserNameCache.Add(contact.UserName, contact);
             }
         }
+
         public byte[]? GetHeadImgCahce(string username)
         {
             if (HeadImgCache.ContainsKey(username))
@@ -182,7 +184,7 @@ namespace WechatBakTool
                             WXContact? contact = UserNameCache[w.StrTalker] as WXContact;
                             if (contact != null)
                             {
-                                if (contact.Remark != null)
+                                if (contact.Remark != "")
                                     w.NickName = contact.Remark;
                                 else
                                     w.NickName = contact.NickName;

+ 32 - 1
WXWorkspace.cs

@@ -1,12 +1,14 @@
 using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.IO;
 using System.Linq;
 using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
+using WechatBakTool.Helpers;
 using WechatBakTool.Model;
 
 namespace WechatBakTool
@@ -24,6 +26,32 @@ namespace WechatBakTool
         {
             UserBakConfig = userBakConfig;
         }
+
+        public void DecryptDB(string pid,int type)
+        {
+            if (UserBakConfig == null)
+            {
+                throw new Exception("没有工作区文件,无法解密");
+            }
+
+            if (!UserBakConfig.Decrypt)
+            {
+                byte[]? key = null;
+                key = DecryptionHelper.GetWechatKey(pid, type == 2, UserBakConfig.Account);
+                if (key == null)
+                {
+                    throw new Exception("获取到的密钥为空,获取失败");
+                }
+                string key_string = BitConverter.ToString(key, 0).Replace("-", string.Empty).ToLower().ToUpper();
+                string source = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
+                string to = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
+
+                WechatDBHelper.DecryUserData(key, source, to);
+                UserBakConfig.Decrypt = true;
+                SaveConfig(UserBakConfig);
+            }
+        }
+
         public void MoveDB()
         {
             string sourceBase = Path.Combine(UserBakConfig.UserResPath, "Msg");
@@ -50,7 +78,10 @@ namespace WechatBakTool
                 }
             }
         }
-
+        public UserBakConfig ReturnConfig()
+        {
+            return UserBakConfig;
+        }
         public static void SaveConfig(UserBakConfig userBakConfig)
         {
             if(userBakConfig.UserWorkspacePath != "")