-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.c
358 lines (292 loc) · 8.13 KB
/
main.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include<stdio.h>
#include<Windows.h>
#include <tchar.h>
#include <psapi.h>
#include <tlhelp32.h>
#include<shlwapi.h>
#include "crypto/pbkdf2_hmac.h"
#include "crypto/sha.h"
#include "crypto/aes.h"
#pragma comment(lib,"shlwapi.lib")
#undef _UNICODE
#define SQLITE_FILE_HEADER "SQLite format 3"
#define IV_SIZE 16
#define HMAC_SHA1_SIZE 20
#define KEY_SIZE 32
#define SL3SIGNLEN 20
#ifndef ANDROID_WECHAT
//4048数据 + 16IV + 20 HMAC + 12
#define DEFAULT_PAGESIZE 4096
#define DEFAULT_ITER 64000
#else
#define NO_USE_HMAC_SHA1
#define DEFAULT_PAGESIZE 1024
#define DEFAULT_ITER 4000
#endif
//for WeChat Version 2.8.0.121
//密钥指针相对于WeChatWin.dll基地址偏移
#define KEY_OFFSET 0x161cc50
#define WXID_OFFSET 0x161cc78
char dbfilename[32] = "ChatMsg.db";
BOOL Decryptdb(char *szFilePath, BYTE *pass);
DWORD FindProcessId(const char *processname)
{
HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
DWORD result = NULL;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hProcessSnap) return(FALSE);
pe32.dwSize = sizeof(PROCESSENTRY32); // <----- IMPORTANT
// Retrieve information about the first process,
// and exit if unsuccessful
if (!Process32First(hProcessSnap, &pe32))
{
CloseHandle(hProcessSnap); // clean the snapshot object
printf("!!! Failed to gather information on system processes! \n");
return(NULL);
}
do
{
//printf("Checking process %ls\n", pe32.szExeFile);
if (0 == strcmp(processname, pe32.szExeFile))
{
result = pe32.th32ProcessID;
break;
}
} while (Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
return result;
}
DWORD FindProcess(char *szProcName) {
DWORD pid;
while (TRUE) {
pid = FindProcessId(szProcName);
if (pid)
{
return pid;
}
Sleep(500);
}
}
HMODULE GetModule(DWORD processID, TCHAR * szModuleName)
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
// Get a handle to the process.
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
if (NULL == hProcess)
return NULL;
// Get a list of all the modules in this process.
if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
TCHAR szModName[MAX_PATH];
// Get the full path to the module's file.
if (GetModuleFileNameEx(hProcess, hMods[i], szModName,
sizeof(szModName) / sizeof(TCHAR)))
{
// Print the module name and handle value.
PathStripPath(szModName);
if (!_tcscmp(szModName, szModuleName)) {
//_tprintf(TEXT("\t%s (0x%08X)\n"), szModName, hMods[i]);
CloseHandle(hProcess);
return hMods[i];
}
}
}
}
// Release the handle to the process.
CloseHandle(hProcess);
return 0;
}
BOOL GetDatabaseKey(DWORD pid, LPVOID *buf) {
DWORD pKey = 0;
HMODULE pWeChatdll = GetModule(pid, "WeChatWin.dll");
if (!pWeChatdll)
{
printf("GetModule error!\n");
return 0;
}
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
ReadProcessMemory(hProcess, (DWORD)pWeChatdll + KEY_OFFSET, &pKey, 4, NULL);
ReadProcessMemory(hProcess, pKey, buf, 32, NULL);
CloseHandle(hProcess);
if (!pKey) {
return 0;
}
return 1;
}
BOOL GetWeChatID(DWORD pid, char *szWXID)
{
BYTE buffer[MAX_PATH];
HMODULE pWeChatdll = GetModule(pid, "WeChatWin.dll");
if (!pWeChatdll)
{
printf("GetModule error!\n");
return FALSE;
}
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
ReadProcessMemory(hProcess, (DWORD)pWeChatdll + WXID_OFFSET, &buffer, MAX_PATH, NULL);
size_t len = strlen(buffer);
if (len> MAX_PATH)
{
printf("get wechat id error!\n");
return 0;
}
strcpy_s(szWXID, MAX_PATH, buffer);
return TRUE;
}
int main(int argc, char* argv[])
{
printf("正在检测微信进程...\n");
DWORD pid = FindProcess("WeChat.exe");
printf("检测到微信运行!\n");
BOOL bRet;
char szwxid[MAX_PATH] ;
BOOL bRet = GetWeChatID(pid, szwxid);
if (!bRet)
{
getchar();
return 0;
}
printf("wxid:%s\n", szwxid);
char homePath[MAX_PATH] = { 0 };
DWORD dwPathSize = GetEnvironmentVariable("USERPROFILE", homePath, MAX_PATH);
if (dwPathSize == 0 || dwPathSize > MAX_PATH)
{
printf("get home path error!\n");
getchar();
return 0;
}
//printf("homepath:%s\n", homePath);
char szDBPath[MAX_PATH];
snprintf(szDBPath, MAX_PATH,
"%s\\Documents\\WeChat Files\\%s\\Msg\\%s", homePath, szwxid, dbfilename);
printf("DataBasePath:%s\n", szDBPath);
BYTE KeyBuffer[32];
bRet = GetDatabaseKey(pid, KeyBuffer);
if (!bRet)
{
printf("get key error!\n");
getchar();
return 0;
}
//for (int i = 0; i < 32; i++) {
// printf("%02x", KeyBuffer[i]);
//}
Decryptdb(szDBPath, KeyBuffer);
return 0;
}
//打开db文件
size_t OpenDBFile(char *szFilePath,BYTE ** pDbBuffer) {
DWORD RSize;
HANDLE hFile = CreateFile(szFilePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("open datebase file error! %d \n",GetLastError());
return 0;
}
DWORD dwFileSize = GetFileSize(hFile, NULL);
*pDbBuffer = (BYTE*)malloc(sizeof(BYTE)*dwFileSize);
ReadFile(hFile, *pDbBuffer, dwFileSize, &RSize, NULL);
if (dwFileSize != RSize)
{
printf("read file error!\n");
return 0;
}
CloseHandle(hFile);
return dwFileSize;
};
BOOL Decryptdb(char *szFilePath,BYTE *pass)
{
BYTE* pDbBuffer = NULL;
size_t nFileSize = OpenDBFile(szFilePath,&pDbBuffer);
if (!nFileSize) {
return 0;
}
//数据库文件前16字节为盐值
BYTE salt[16] = { 0 };
memcpy(salt, pDbBuffer, 16);
#ifndef NO_USE_HMAC_SHA1
//HMAC验证用的盐值需要异或0x3a
BYTE mac_salt[16] = { 0 };
memcpy(mac_salt, salt, 16);
for (int i = 0; i < sizeof(salt); i++)
mac_salt[i] ^= 0x3a;
#endif
//保留段长度,PC端每4KB有48B
int reserve = IV_SIZE;
#ifndef NO_USE_HMAC_SHA1
reserve += HMAC_SHA1_SIZE;
#endif
reserve = ((reserve % AES_BLOCK_SIZE) == 0) ? reserve : ((reserve / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
//密钥扩展,分别对应AES解密密钥和HMAC验证密钥
BYTE key[KEY_SIZE] = { 0 };
BYTE mac_key[KEY_SIZE] = { 0 };
PKCS5_PBKDF2_HMAC((const BYTE*)pass, 32,
salt, sizeof(salt), DEFAULT_ITER, sizeof(key), key);
#ifndef NO_USE_HMAC_SHA1
PKCS5_PBKDF2_HMAC((const BYTE*)key, sizeof(key),
mac_salt, sizeof(mac_salt), 2, sizeof(mac_key), mac_key);
#endif
BYTE* pTemp = pDbBuffer;
BYTE pDecryptPerPageBuffer[DEFAULT_PAGESIZE];
int nPage = 1;
int offset = 16;
printf("\n 开始解密 \n");
while (pTemp < pDbBuffer + nFileSize)
{
printf("解密数据页:%d/%d \n", nPage, nFileSize / DEFAULT_PAGESIZE);
//计算加密数据的HMAC,并与保留字段中的值进行比较
#ifndef NO_USE_HMAC_SHA1
BYTE hash_mac[HMAC_SHA1_SIZE] = { 0 };
sha1_context hctx;
SHA_Init(&hctx.ctx);
sha1_hmac_starts(&hctx, mac_key, sizeof(mac_key));
sha1_hmac_update(&hctx, pTemp + offset, DEFAULT_PAGESIZE - reserve - offset + IV_SIZE);//4096-48-16+16
sha1_hmac_update(&hctx, (const BYTE*)& nPage, sizeof(nPage));
sha1_hmac_finish(&hctx, hash_mac);
BYTE* pHMAC = pTemp + DEFAULT_PAGESIZE - reserve + IV_SIZE;
if (0 != memcmp(hash_mac, pHMAC, sizeof(hash_mac)))
{
printf("\n 哈希值错误! \n");
getchar();
return 0;
}
#endif
if (nPage == 1)
memcpy(pDecryptPerPageBuffer, SQLITE_FILE_HEADER, offset);
//AES解密操作
BYTE key_schedule[40];
aes_key_setup(key, key_schedule, 256);
aes_decrypt_cbc(
pTemp + offset, DEFAULT_PAGESIZE - reserve - offset,
pDecryptPerPageBuffer + offset,
key_schedule, 256,
pTemp + (DEFAULT_PAGESIZE - reserve)
);
memcpy(pDecryptPerPageBuffer + DEFAULT_PAGESIZE - reserve,
pTemp + DEFAULT_PAGESIZE - reserve,
reserve);
//将解密后的数据写入到文件中
char decFile[1024] = { 0 };
sprintf_s(decFile, 1024, "dec_%s", dbfilename);
FILE * fp;
fopen_s(&fp, decFile, "ab+");
fwrite(pDecryptPerPageBuffer, 1, DEFAULT_PAGESIZE, fp);
fclose(fp);
nPage++;
offset = 0;
pTemp += DEFAULT_PAGESIZE;
}
printf("\n 解密成功! \n");
printf("\n dec_%s文件可用navicat之类的软件读取! \n", dbfilename);
getchar();
return TRUE;
}