-
Notifications
You must be signed in to change notification settings - Fork 168
/
wechat_util.go
273 lines (256 loc) · 7.69 KB
/
wechat_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"unsafe"
"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows"
)
// 获取微信进程对象,包含进程ID、进程句柄和Module列表
func GetWeChatProcess() (windows.ProcessEntry32, error) {
var process windows.ProcessEntry32
process.Size = uint32(unsafe.Sizeof(process))
snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return process, err
}
defer windows.CloseHandle(snapshot)
for {
err = windows.Process32Next(snapshot, &process)
if err != nil {
return process, err
}
if windows.UTF16ToString(process.ExeFile[:]) == "WeChat.exe" {
return process, nil
}
}
}
// 获取微信进程的WeChatWin.dll模块对象,包含模块基址、模块大小和模块路径()
func GetWeChatWinModule(process windows.ProcessEntry32) (windows.ModuleEntry32, error) {
var module windows.ModuleEntry32
module.Size = uint32(unsafe.Sizeof(module))
snapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPMODULE, process.ProcessID)
if err != nil {
return module, err
}
defer windows.CloseHandle(snapshot)
for {
err = windows.Module32Next(snapshot, &module)
if err != nil {
return module, err
}
if windows.UTF16ToString(module.Module[:]) == "WeChatWin.dll" {
return module, nil
}
}
}
// 通过模块获取版本号 c#代码为:string FileVersion = processModule.FileVersionInfo.FileVersion;转成go代码如下
func GetVersion(module windows.ModuleEntry32) (string, error) {
image, imgErr := windows.LoadLibraryEx(windows.UTF16ToString(module.ExePath[:]), 0, windows.LOAD_LIBRARY_AS_DATAFILE)
if imgErr != nil {
return "", fmt.Errorf("LoadLibraryEx error: %v", imgErr)
}
resInfo, infoErr := windows.FindResource(image, windows.ResourceID(1), windows.RT_VERSION)
if infoErr != nil {
return "", fmt.Errorf("FindResource error: %v", infoErr)
}
resData, dataErr := windows.LoadResourceData(image, resInfo)
if dataErr != nil {
return "", fmt.Errorf("LoadResourceData error: %v", dataErr)
}
var info *windows.VS_FIXEDFILEINFO
size := uint32(unsafe.Sizeof(*info))
err := windows.VerQueryValue(unsafe.Pointer(&resData[0]), `\`, unsafe.Pointer(&info), &size)
if err != nil {
return "", fmt.Errorf("VerQueryValue error: %v", err)
}
// 从低位到高位,分别为主版本号、次版本号、修订号、编译号
version := fmt.Sprintf("%d.%d.%d.%d", info.FileVersionMS>>16, info.FileVersionMS&0xffff, info.FileVersionLS>>16, info.FileVersionLS&0xffff)
return version, nil
}
// 获取微信数据:入参为微信进程句柄,偏移地址,返回值为昵称和错误信息
func GetWeChatData(process windows.Handle, offset uintptr, nSize int) (string, error) {
var buffer = make([]byte, nSize)
err := windows.ReadProcessMemory(process, offset, &buffer[0], uintptr(nSize), nil)
if err != nil {
return "", err
}
// 声明一个字节数组,暂时为空
var textBytes []byte = nil
for _, v := range buffer {
if v == 0 {
break
}
textBytes = append(textBytes, v)
}
// 返回utf8编码的字符串
return string(textBytes), nil
}
// 获取微信key:入参为微信进程句柄,偏移地址,返回值为key和错误信息
func GetWeChatKey(process windows.Handle, offset uintptr) (string, error) {
var buffer = make([]byte, 4)
err := windows.ReadProcessMemory(process, offset, &buffer[0], 4, nil)
if err != nil {
return "", err
}
var num = 32
var buffer2 = make([]byte, num)
// c# 代码(IntPtr)(((int)array[3] << 24) + ((int)array[2] << 16) + ((int)array[1] << 8) + (int)array[0]);转成go代码如下
offset2 := uintptr((int(buffer[3]) << 24) + (int(buffer[2]) << 16) + (int(buffer[1]) << 8) + int(buffer[0]))
err = windows.ReadProcessMemory(process, offset2, &buffer2[0], uintptr(num), nil)
if err != nil {
return "", err
}
// 将byte数组转成hex字符串,并转成大写
key := hex.EncodeToString(buffer2)
key = strings.ToUpper(key)
return key, nil
}
func GetWeChatFromRegistry() (string, error) {
// 打开注册表的微信路径:HKEY_CURRENT_USER\Software\Tencent\WeChat\FileSavePath
key, err := registry.OpenKey(registry.CURRENT_USER, `Software\Tencent\WeChat`, registry.QUERY_VALUE)
if err != nil {
return "", err
}
defer key.Close()
//获取key的值
value, _, err := key.GetStringValue("FileSavePath")
if err != nil {
return "", err
}
return value, nil
}
func GetWeChatDir() (string, error) {
msgDir := ""
wDir, err := GetWeChatFromRegistry()
if err != nil {
return "", err
}
// 如果wDir为MyDocument:
if wDir == "MyDocument:" {
// 获取%USERPROFILE%/Documents目录
profile, err := os.UserHomeDir()
if err != nil {
return "", err
}
// 获取微信消息目录
msgDir = filepath.Join(profile, "Documents", "WeChat Files")
} else {
// 获取微信消息目录
msgDir = filepath.Join(wDir, "WeChat Files")
}
// 判断目录是否存在
_, err = os.Stat(msgDir)
if err != nil {
return "", err
}
return msgDir, nil
}
func IsSupportAutoGetData(version string) bool {
// 判断version是否在支持的版本列表中
for _, v := range SupportAutoGetDataVersionList {
if version == v {
return true
}
}
return false
}
// 获取微信消息目录下的所有用户目录,排除All Users目录和Applet目录,返回一个map,key用户id,value用户目录
func GetWeChatUserDir(wechatRoot string) (map[string]string, error) {
userDir := make(map[string]string)
// 获取微信消息目录下的所有用户目录
files, err := ioutil.ReadDir(wechatRoot)
if err != nil {
return nil, err
}
for _, file := range files {
// 排除All Users目录和Applet目录
if file.Name() == "All Users" || file.Name() == "Applet" || file.Name() == "WMPF" {
continue
}
userDir[file.Name()] = filepath.Join(wechatRoot, file.Name())
}
return userDir, nil
}
// 解密微信数据库
func DecryptDb(key string) error {
// 判断tmp目录是否存在
_, err := os.Stat(CurrentPath + "\\tmp")
if err != nil {
return err
}
// 判断decrypted目录是否存在
_, err = os.Stat(CurrentPath + "\\decrypted")
if err != nil {
return err
}
// 正则匹配,将所有MSG数字.db文件解密到decrypted目录,不扫描子目录
err = filepath.Walk(CurrentPath+"\\tmp", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if ok, _ := filepath.Match("*.db", info.Name()); ok {
err = Decrypt(key, path, CurrentPath+"\\decrypted\\"+info.Name())
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
return nil
}
// 复制微信的数据文件
func CopyMsgDb(dataDir string) error {
// 判断目录是否存在
_, err := os.Stat(dataDir)
if err != nil {
return err
}
// 判断运行目录是否存在tmp目录没有则创建
_, err = os.Stat(CurrentPath + "\\tmp")
if err != nil {
err = os.Mkdir(CurrentPath+"\\tmp", os.ModePerm)
if err != nil {
return err
}
}
// 正则匹配,将所有MSG数字.db文件拷贝到tmp目录,不扫描子目录
err = filepath.Walk(dataDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if ok, _ := filepath.Match("MSG*.db", info.Name()); ok {
err = CopyFile(path, CurrentPath+"\\tmp\\"+info.Name())
if err != nil {
return err
}
}
// 复制MicroMsg.db到tmp目录
if ok, _ := filepath.Match("MicroMsg.db", info.Name()); ok {
err = CopyFile(path, CurrentPath+"\\tmp\\"+info.Name())
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
// 如果不存在decrypted目录则创建
_, err = os.Stat(CurrentPath + "\\decrypted")
if err != nil {
err = os.Mkdir(CurrentPath+"\\decrypted", os.ModePerm)
if err != nil {
return err
}
}
return nil
}