解密PC版微信数据库ChatMsg.db

本文详述了解密PC版微信数据库ChatMsg.db的过程,包括获取加密密钥的步骤,如何利用sqlcipher解密,以及最终通过C++代码成功解密的挑战与注意事项,最终实现了生成聊天记录词云图的目标。

最近发现用Python生成词云图挺有意思的,于是想着生成微信和某个人的聊天记录的词云应该挺好玩。

在网上找了好多解密微信数据库的文章,但大多数都是解密Android版的,好不容易找到些解密PC版的还写得不够详细。

不过最后终于解密成功,其中遇到好多大坑小坑,听我一一道来。

1,获取数据库加密密钥

解密PC版微信数据库

照着这篇文章做就好了,有两个注意的地方

①如果你是64位系统,就下载64位的OllyDbg 否则无法运行。

②附加微信进程的时候可能会报错忽略就可以了,不影响后面操作。

③附加时候有时候会特别慢,重新关了软件再来了次就可以了。

这里终于出现了大坑!

坑一:文章的第14写“用鼠标框选前四行内容”错误,是前两行。

PC版微信的密钥是32位,4行就变成64位了,一开始一直用前四行试,永远无法成功。

2,利用sqlcipher解密数据库

使用微信聊天记录统计信息

文章是个好文章,可是无论如何也无法解密,去Google了好多其它方法,也无果而终。

联系了文章作者,说是针对Android版微信,不过之前看过其它地方说PC和Android版加密方法是一样的…

最后这种方法宣告失败,没能解密成功,于是用了下面的方法。

坑二:无法找到合适的sqlcipher解密参数。

为什么说是一个坑,因为要在linux上编译通过sqlcipher也不是一件容易的事情,安装各种各校的包不说,好多还不能直接通过apt或者yum安

#include "common.h" int sockfd = -1; atomic_bool bRunning = ATOMIC_VAR_INIT(true); atomic_bool bClientShutdown = ATOMIC_VAR_INIT(false); pthread_t readThread, writeThread; char sClientName[MAX_NAME_LEN] = {'\0'}; /*! \fn void cleanup_client() \brief Clean up client resources */ void cleanup_client() { TEST_PRINT(EN_TRACE, "in"); TEST_PRINT(EN_MSG, "Cleaning up client resources"); atomic_store_explicit(&bRunning, false, memory_order_release); atomic_store_explicit(&bClientShutdown, true, memory_order_release); shutdown(sockfd, SHUT_RDWR); if (0 <= sockfd) { close(sockfd); sockfd = -1; } TEST_PRINT(EN_TRACE, "out"); return; } /*! \fn void handle_signal(int sig) \brief signal processing function \param sig: Signal value */ void handle_signal(int sig) { TEST_PRINT(EN_TRACE, "in"); TEST_PRINT(EN_MSG, "Received signal %d, exiting...", sig); cleanup_client(); TEST_PRINT(EN_TRACE, "out"); } /*! \fn void* read_thread(void* arg) \brief Receive server messages \param arg: unused \return NULL */ void* read_thread(void* arg) { TEST_PRINT(EN_TRACE, "in"); ChatMessage chatMsg = {0}; int recvLen = 0; while (atomic_load_explicit(&bRunning, memory_order_acquire) && !atomic_load_explicit(&bClientShutdown, memory_order_acquire)) { pthread_testcancel(); recvLen = recv(sockfd, &chatMsg, sizeof(ChatMessage), 0); if (0 < recvLen) { TEST_PRINT(EN_MSG, "\n[%s]:\n%s", chatMsg.sender,chatMsg.content); fflush(stdout); } else if (0 == recvLen) { TEST_PRINT(EN_MSG, "Server disconnected"); atomic_store_explicit(&bRunning, false, memory_order_release); goto end; } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { TEST_PRINT(EN_ERROR, "Recv error: %s", strerror(errno)); atomic_store_explicit(&bRunning, false, memory_order_release); goto end; } } } end: TEST_PRINT(EN_TRACE, "out"); return NULL; } /*! \fn void* write_thread(void* arg) \brief Read user input \param arg: unused \return NULL */ void* write_thread(void* arg) { TEST_PRINT(EN_TRACE, "in"); int iChoice = 0; ChatMessage chatMsg = {0}; while (atomic_load_explicit(&bRunning, memory_order_acquire) && !atomic_load_explicit(&bClientShutdown, memory_order_acquire)) { pthread_testcancel(); TEST_PRINT(EN_MSG, "\n==== Chat menu ====\n"); TEST_PRINT(EN_MSG, "\n1. Private chat\n2. Group chat\n3. Online users\n4. Exit"); fflush(stdout); scanf("%d", &iChoice); getchar(); switch (iChoice) { case 1: chatMsg.type = MSG_TEXT; strncpy(chatMsg.sender, sClientName, MAX_NAME_LEN); chatMsg.sender[MAX_NAME_LEN - 1] = '\0'; TEST_PRINT(EN_MSG, "Please enter the recipient's username: "); fgets(chatMsg.receiver, MAX_NAME_LEN, stdin); chatMsg.receiver[strcspn(chatMsg.receiver, "\n")] = '\0'; TEST_PRINT(EN_MSG, "Input message: "); fgets(chatMsg.content, MAX_MSG_LEN, stdin); chatMsg.content[strcspn(chatMsg.content, "\n")] = '\0'; chatMsg.type = MSG_TEXT; break; case 2: chatMsg.type = MSG_GROUP; strncpy(chatMsg.sender, sClientName, MAX_NAME_LEN); chatMsg.sender[MAX_NAME_LEN - 1] = '\0'; TEST_PRINT(EN_MSG, "Enter group messaging: "); fgets(chatMsg.content, MAX_MSG_LEN, stdin); chatMsg.content[strcspn(chatMsg.content, "\n")] = '\0'; strncpy(chatMsg.receiver, "ALL", MAX_NAME_LEN); chatMsg.receiver[MAX_NAME_LEN - 1] = '\0'; break; case 3: chatMsg.type = MSG_LIST; break; case 4: atomic_store_explicit(&bRunning, false, memory_order_release); goto end; default: TEST_PRINT(EN_ERROR, "invalid Choice"); } TEST_PRINT(EN_DEBUG, "send msg"); if (0 > send(sockfd, &chatMsg, sizeof(ChatMessage), 0)) { TEST_PRINT(EN_ERROR, "Send failed: %s", strerror(errno)); goto end; } } end: TEST_PRINT(EN_TRACE, "out"); return NULL; } bool client_login() { int iChoice = 0; ChatMessage chatMsg = {0}; bool bRes = false; while (!atomic_load_explicit(&bClientShutdown, memory_order_acquire)) { start: TEST_PRINT(EN_MSG, "\n1. Login in\n2. Register\n3. Exit"); scanf("%d", &iChoice); getchar(); switch (iChoice) { case 1: chatMsg.type = MSG_LOGIN; TEST_PRINT(EN_MSG, "Enter user name"); fgets(chatMsg.sender, MAX_NAME_LEN, stdin); chatMsg.sender[strcspn(chatMsg.sender, "\n")] = '\0'; TEST_PRINT(EN_DEBUG, "User name %s", chatMsg.sender); TEST_PRINT(EN_MSG, "Enter user password"); fgets(chatMsg.content, MAX_PASSWORD_LEN, stdin); send(sockfd, &chatMsg, sizeof(ChatMessage), 0); break; case 2: chatMsg.type = MSG_REGIST; TEST_PRINT(EN_MSG, "Enter user name"); fgets(chatMsg.sender, MAX_NAME_LEN, stdin); chatMsg.sender[strcspn(chatMsg.sender, "\n")] = '\0'; TEST_PRINT(EN_MSG, "Enter user password"); fgets(chatMsg.content, MAX_PASSWORD_LEN, stdin); send(sockfd, &chatMsg, sizeof(ChatMessage), 0); break; case 3: bRes = false; goto end; default: TEST_PRINT(EN_ERROR, "invalid Choice"); goto start; } int recvLen = recv(sockfd, &chatMsg, sizeof(ChatMessage), 0); if (0 < recvLen) { TEST_PRINT(EN_MSG, "\n[%s]:\n%s", chatMsg.sender,chatMsg.content); fflush(stdout); } else if (0 == recvLen) { TEST_PRINT(EN_MSG, "Server disconnected"); bRes = false; goto end; } else { if (errno != EAGAIN && errno != EWOULDBLOCK) { TEST_PRINT(EN_ERROR, "Recv error: %s", strerror(errno)); bRes = false; goto end; } } if (0 == strncmp(chatMsg.content, "Login success", MAX_MSG_LEN)) { strncpy(sClientName, chatMsg.receiver, MAX_NAME_LEN); sClientName[MAX_NAME_LEN - 1] = '\0'; bRes = true; break; } } end: TEST_PRINT(EN_TRACE, "out"); return bRes; } int main(int argc, char* argv[]) { TEST_PRINT(EN_TRACE, "in"); struct timeval tv = {0}; struct sockaddr_in servAddr = {0}; int iChoice = 0; ChatMessage chatMsg = {0}; signal(SIGINT, handle_signal); signal(SIGTSTP, handle_signal); if (2 != argc) { TEST_PRINT(EN_ERROR, "Usage: %s <server_ip>\n", argv[0]); return EXIT_FAILURE; } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (0 > sockfd) { TEST_PRINT(EN_ERROR, "Socket creation failed"); return EXIT_FAILURE; } tv.tv_sec = TIMEOUT_SEC; tv.tv_usec = TIMEOUT_USEC; setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); servAddr.sin_family = AF_INET; servAddr.sin_port = htons(SERVER_PORT); if (0 >= inet_pton(AF_INET, argv[1], &servAddr.sin_addr)) { TEST_PRINT(EN_ERROR, "Invalid address"); goto end; } if (0 > connect(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr))) { TEST_PRINT(EN_ERROR, "Connection failed: %s", strerror(errno)); goto end; } TEST_PRINT(EN_MSG, "Connected to %s:%d", argv[1], SERVER_PORT); if (!client_login()) { TEST_PRINT(EN_ERROR, "Client login failed"); goto end; } if (0 != pthread_create(&readThread, NULL, read_thread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to create read thread"); goto end; } if (0 != pthread_create(&writeThread, NULL, write_thread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to create write thread"); goto end; } if (0 != pthread_join(readThread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to join read thread"); goto end; } if (0 != pthread_join(writeThread, NULL)) { TEST_PRINT(EN_ERROR, "Failed to join write thread"); goto end; } end: cleanup_client(); TEST_PRINT(EN_TRACE, "out"); return EXIT_SUCCESS; } 在写线程中,fgets会阻塞,导致读线程退出后,写线程无法退出
08-21
这是一个非常经典的问题:在多线程网络程序中,`fgets` 会阻塞主线程或写线程,导致线程无法及时退出,即使主线程序已经决定终止。 --- ## ✅ 问题分析 在你的 `write_thread` 函数中,使用了 `fgets` 来读取用户输入,它是一个 **阻塞式函数**。也就是说,当用户没有输入时,`fgets` 会一直等待,不会返回。这导致: - 即使主线程调用了 `atomic_store_explicit(&bRunning, false, memory_order_release);` - 写线程仍然卡在 `fgets` 中,无法检查 `bRunning` 状态,也就无法退出。 - 最终导致程序无法正常关闭。 --- ## ✅ 解决方案 要解决这个问题,我们有以下几种方案: ### ✅ 方案一:使用 `select` + `read` 实现非阻塞输入 我们可以在写线程中使用 `select` 来判断标准输入是否有数据可读,如果超时就检查退出标志。 ### ✅ 方案二:使用 `pthread_cancel()` 强制取消线程(不推荐) 虽然可以实现,但强制取消线程可能会导致资源泄漏或状态不一致。 --- ## ✅ 推荐方案:使用 `select` + `read` 以下是修改后的 `write_thread` 函数,使用非阻塞方式读取用户输入: ```c void* write_thread(void* arg) { TEST_PRINT(EN_TRACE, "in"); int iChoice = 0; ChatMessage chatMsg = {0}; fd_set readfds; struct timeval timeout; while (atomic_load_explicit(&bRunning, memory_order_acquire) && !atomic_load_explicit(&bClientShutdown, memory_order_acquire)) { pthread_testcancel(); TEST_PRINT(EN_MSG, "\n==== Chat menu ====\n"); TEST_PRINT(EN_MSG, "1. Private chat\n2. Group chat\n3. Online users\n4. Exit"); fflush(stdout); FD_ZERO(&readfds); FD_SET(STDIN_FILENO, &readfds); timeout.tv_sec = 1; // 每秒检查一次退出标志 timeout.tv_usec = 0; int ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout); if (ret == -1) { TEST_PRINT(EN_ERROR, "select error: %s", strerror(errno)); break; } else if (ret == 0) { // 超时,继续检查退出标志 continue; } else { // 有输入 scanf("%d", &iChoice); getchar(); switch (iChoice) { case 1: chatMsg.type = MSG_TEXT; strncpy(chatMsg.sender, sClientName, MAX_NAME_LEN); chatMsg.sender[MAX_NAME_LEN - 1] = '\0'; TEST_PRINT(EN_MSG, "Please enter the recipient's username: "); fgets(chatMsg.receiver, MAX_NAME_LEN, stdin); chatMsg.receiver[strcspn(chatMsg.receiver, "\n")] = '\0'; TEST_PRINT(EN_MSG, "Input message: "); fgets(chatMsg.content, MAX_MSG_LEN, stdin); chatMsg.content[strcspn(chatMsg.content, "\n")] = '\0'; chatMsg.type = MSG_TEXT; break; case 2: chatMsg.type = MSG_GROUP; strncpy(chatMsg.sender, sClientName, MAX_NAME_LEN); chatMsg.sender[MAX_NAME_LEN - 1] = '\0'; TEST_PRINT(EN_MSG, "Enter group messaging: "); fgets(chatMsg.content, MAX_MSG_LEN, stdin); chatMsg.content[strcspn(chatMsg.content, "\n")] = '\0'; strncpy(chatMsg.receiver, "ALL", MAX_NAME_LEN); chatMsg.receiver[MAX_NAME_LEN - 1] = '\0'; break; case 3: chatMsg.type = MSG_LIST; break; case 4: atomic_store_explicit(&bRunning, false, memory_order_release); goto end; default: TEST_PRINT(EN_ERROR, "invalid Choice"); } if (0 > send(sockfd, &chatMsg, sizeof(ChatMessage), 0)) { TEST_PRINT(EN_ERROR, "Send failed: %s", strerror(errno)); goto end; } } } end: TEST_PRINT(EN_TRACE, "out"); return NULL; } ``` --- ## ✅ 解释 - 使用 `select()` 来监听标准输入(STDIN_FILENO)是否就绪。 - 设置一个较短的超时时间(例如 1 秒),在每次超时后检查 `bRunning` 是否为 false。 - 如果用户没有输入,线程不会一直阻塞,而是每秒检查一次是否应该退出。 - 这样可以确保在主线程发出退出信号后,写线程能及时退出。 --- ## ✅ 补充建议 - 可以使用 `sigaction()` 替代 `signal()` 来处理信号,更安全可靠。 - 建议加入线程取消点(`pthread_testcancel()`)以支持线程取消。 - 如果使用 C++,可以考虑使用 `std::atomic` 和 `std::thread` 来提高可读性和可维护性。 --- ##
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值