diff --git a/CWeChatRobot/AddBrandContact.cpp b/CWeChatRobot/AddBrandContact.cpp index e843f8ed2d1bca76c02a5040092370b266f07cef..832b54081179a90d7ffb007475bb2f4872b674be 100644 --- a/CWeChatRobot/AddBrandContact.cpp +++ b/CWeChatRobot/AddBrandContact.cpp @@ -1,33 +1,14 @@ #include "pch.h" BOOL AddBrandContact(DWORD pid,wchar_t* PublicId) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD AddBrandContactAddr = hp.GetProcAddr(AddBrandContactRemote); + if (AddBrandContactAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_publicid(hp.GetHandle(), PublicId, TEXTLENGTH(PublicId)); + if (r_publicid.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 1; - - LPVOID PublicIdaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - if (!PublicIdaddr) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, PublicIdaddr, PublicId, wcslen(PublicId) * 2 + 2, &dwWriteSize); - DWORD AddBrandContactAddr = WeChatRobotBase + AddBrandContactRemoteOffset; - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)AddBrandContactAddr, (LPVOID)PublicIdaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - - VirtualFreeEx(hProcess, PublicIdaddr, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), AddBrandContactAddr, r_publicid.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/AddChatRoomMember.cpp b/CWeChatRobot/AddChatRoomMember.cpp index 9bfa62ff154258a42e294246ccca137b3701bda9..afb1832a984f7162dd2df3c2051e7de90fa0d296 100644 --- a/CWeChatRobot/AddChatRoomMember.cpp +++ b/CWeChatRobot/AddChatRoomMember.cpp @@ -7,66 +7,26 @@ struct AddChatRoomMemberStruct DWORD length; }; -BOOL AddChatRoomMember(DWORD pid,wchar_t* chatroomid, wchar_t* wxid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) +BOOL AddChatRoomMember(DWORD pid, wchar_t* chatroomid, wchar_t* wxid) { + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD AddChatRoomMemberRemoteAddr = hp.GetProcAddr(AddChatRoomMemberRemote); + if (AddChatRoomMemberRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 0; - AddChatRoomMemberStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - AddChatRoomMemberStruct* paramAndFunc = (AddChatRoomMemberStruct*)::VirtualAllocEx(hProcess, 0, sizeof(AddChatRoomMemberStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !wxidaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - DWORD dwTId = 0; - - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.wxids = (DWORD)wxidaddr; + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + AddChatRoomMemberStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.wxids = (DWORD)r_wxid.GetAddr(); params.length = 1; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(AddChatRoomMemberStruct), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD AddChatRoomMemberAddr = WeChatRobotBase + AddChatRoomMemberRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)AddChatRoomMemberAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_wxid.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), AddChatRoomMemberRemoteAddr, r_params.GetAddr()); + return ret == 0; } -BOOL AddChatRoomMember(DWORD pid,wchar_t* chatroomid, SAFEARRAY* psaValue) { +BOOL AddChatRoomMember(DWORD pid, wchar_t* chatroomid, SAFEARRAY* psaValue) { VARIANT rgvar; rgvar.vt = VT_BSTR; HRESULT hr = S_OK; @@ -76,69 +36,36 @@ BOOL AddChatRoomMember(DWORD pid,wchar_t* chatroomid, SAFEARRAY* psaValue) { VariantInit(&rgvar); long pIndex = 0; hr = SafeArrayGetElement(psaValue, &pIndex, &rgvar); - return AddChatRoomMember(pid,chatroomid, rgvar.bstrVal); + return DelChatRoomMember(pid, chatroomid, rgvar.bstrVal); } - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - vector wxidptrs; DWORD dwWriteSize = 0; - DWORD dwTId = 0; DWORD dwId = 0; DWORD dwRet = 0; - AddChatRoomMemberStruct params = { 0 }; - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxidptrsaddr = VirtualAllocEx(hProcess, NULL, sizeof(void*) * cElements, MEM_COMMIT, PAGE_READWRITE); - AddChatRoomMemberStruct* paramAndFunc = (AddChatRoomMemberStruct*)::VirtualAllocEx(hProcess, 0, sizeof(AddChatRoomMemberStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !wxidptrsaddr || !paramAndFunc) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD AddChatRoomMemberRemoteAddr = hp.GetProcAddr(AddChatRoomMemberRemote); + if (AddChatRoomMemberRemoteAddr == 0) return 1; - } + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + vector wxidptrs; for (long i = lLbound; i < lLbound + cElements; i++) { VariantInit(&rgvar); hr = SafeArrayGetElement(psaValue, &i, &rgvar); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); + LPVOID wxidaddr = VirtualAllocEx(hp.GetHandle(), NULL, 1, MEM_COMMIT, PAGE_READWRITE); if (wxidaddr) { - WriteProcessMemory(hProcess, wxidaddr, rgvar.bstrVal, wcslen(rgvar.bstrVal) * 2 + 2, &dwWriteSize); + WriteProcessMemory(hp.GetHandle(), wxidaddr, rgvar.bstrVal, wcslen(rgvar.bstrVal) * 2 + 2, &dwWriteSize); wxidptrs.push_back(wxidaddr); } } - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (wxidptrsaddr) - WriteProcessMemory(hProcess, wxidptrsaddr, &wxidptrs[0], wxidptrs.size() * sizeof(void*), &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.wxids = (DWORD)wxidptrsaddr; + WeChatData r_wxids(hp.GetHandle(), &wxidptrs[0], wxidptrs.size() * sizeof(void*)); + AddChatRoomMemberStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.wxids = (DWORD)r_wxids.GetAddr(); params.length = wxidptrs.size(); - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(AddChatRoomMemberStruct), &dwTId); - } - else { - CloseHandle(hProcess); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_wxids.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - DWORD AddChatRoomMemberAddr = WeChatRobotBase + AddChatRoomMemberRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)AddChatRoomMemberAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return 1; - } + DWORD ret = CallRemoteFunction(hp.GetHandle(), AddChatRoomMemberRemoteAddr, r_params.GetAddr()); for (unsigned int i = 0; i < wxidptrs.size(); i++) { - VirtualFreeEx(hProcess, wxidptrs[i], 0, MEM_RELEASE); + VirtualFreeEx(hp.GetHandle(), wxidptrs[i], 0, MEM_RELEASE); } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxidptrsaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/AddFriendByV3.cpp b/CWeChatRobot/AddFriendByV3.cpp index d3db3277acedaadf5bc9582943f6a7531a0b15bf..b7dcb55bed8e3e47dce2bf930d3e204af56e68b7 100644 --- a/CWeChatRobot/AddFriendByV3.cpp +++ b/CWeChatRobot/AddFriendByV3.cpp @@ -7,45 +7,20 @@ struct AddFriendByV3Struct { }; BOOL AddFriendByV3(DWORD pid,wchar_t* v3, wchar_t* message,int AddType) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD AddFriendByV3RemoteAddr = hp.GetProcAddr(AddFriendByV3Remote); + if (AddFriendByV3RemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 1; - - LPVOID v3addr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID messageaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - AddFriendByV3Struct* paramAndFunc = (AddFriendByV3Struct*)VirtualAllocEx(hProcess, 0, sizeof(AddFriendByV3Struct), MEM_COMMIT, PAGE_READWRITE); - if (!v3addr || !messageaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, v3addr, v3, wcslen(v3) * 2 + 2, &dwWriteSize); - if(message) - WriteProcessMemory(hProcess, messageaddr, message, wcslen(message) * 2 + 2, &dwWriteSize); - + WeChatData r_v3(hp.GetHandle(), v3, TEXTLENGTH(v3)); + WeChatData r_message(hp.GetHandle(), message, TEXTLENGTH(message)); AddFriendByV3Struct params = { 0 }; - params.v3 = (DWORD)v3addr; - params.message = message ? (DWORD)messageaddr : 0; + params.v3 = (DWORD)r_v3.GetAddr(); + params.message = (DWORD)r_message.GetAddr(); params.AddType = AddType; - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwWriteSize); - DWORD AddFriendByV3Addr = WeChatRobotBase + AddFriendByV3RemoteOffset; - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)AddFriendByV3Addr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - - VirtualFreeEx(hProcess, v3addr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, messageaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_v3.GetAddr() == 0 || r_params.GetAddr() == 0) + return 1; + DWORD ret = CallRemoteFunction(hp.GetHandle(), AddFriendByV3RemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/AddFriendByWxid.cpp b/CWeChatRobot/AddFriendByWxid.cpp index 21a814d8a983872a089d2e6271f982442bc23bec..f70e26f1f3b0c8757c37293917c6dcf147d4e0e6 100644 --- a/CWeChatRobot/AddFriendByWxid.cpp +++ b/CWeChatRobot/AddFriendByWxid.cpp @@ -6,44 +6,19 @@ struct AddFriendByWxidStruct { }; BOOL AddFriendByWxid(DWORD pid,wchar_t* wxid,wchar_t* message) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD AddFriendByWxidRemoteAddr = hp.GetProcAddr(AddFriendByWxidRemote); + if (AddFriendByWxidRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 1; - - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID messageaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - AddFriendByWxidStruct* paramAndFunc = (AddFriendByWxidStruct*)VirtualAllocEx(hProcess, 0, sizeof(AddFriendByWxidStruct), MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr || !messageaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - if(message) - WriteProcessMemory(hProcess, messageaddr, message, wcslen(message) * 2 + 2, &dwWriteSize); - + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_message(hp.GetHandle(), message, TEXTLENGTH(message)); AddFriendByWxidStruct params = { 0 }; - params.wxid = (DWORD)wxidaddr; - params.message = message ? (DWORD)messageaddr : 0; - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwWriteSize); - DWORD AddFriendByWxidAddr = WeChatRobotBase + AddFriendByWxidRemoteOffset; - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)AddFriendByWxidAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, messageaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + params.wxid = (DWORD)r_wxid.GetAddr(); + params.message = (DWORD)r_message.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_wxid.GetAddr() == 0 || r_params.GetAddr() == 0) + return 1; + DWORD ret = CallRemoteFunction(hp.GetHandle(), AddFriendByWxidRemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/CheckFriendStatus.cpp b/CWeChatRobot/CheckFriendStatus.cpp index 14e0d3a4d5f4d621464fd57dcfad8cd9effa0fce..92dc18300e203b55ed7da3cf8170b475e410c83e 100644 --- a/CWeChatRobot/CheckFriendStatus.cpp +++ b/CWeChatRobot/CheckFriendStatus.cpp @@ -1,33 +1,14 @@ #include "pch.h" DWORD CheckFriendStatus(DWORD pid,wchar_t* wxid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD CheckFriendStatusRemoteAddr = hp.GetProcAddr(CheckFriendStatusRemote); + if (CheckFriendStatusRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + if (r_wxid.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwStatus = 0; - - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - DWORD CheckFriendStatusRemoteAddr = WeChatRobotBase + CheckFriendStatusRemoteOffset; - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)CheckFriendStatusRemoteAddr, (LPVOID)wxidaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwStatus); - CloseHandle(hThread); - } - - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwStatus; + DWORD ret = CallRemoteFunction(hp.GetHandle(), CheckFriendStatusRemoteAddr, r_wxid.GetAddr()); + return ret; } \ No newline at end of file diff --git a/CWeChatRobot/DbBackup.cpp b/CWeChatRobot/DbBackup.cpp index d3d0cc69ca7f3002d70b1b65c7c5e47e2d3e4e1a..4782df783e2b4ef34fa315500f2906dd4d346b79 100644 --- a/CWeChatRobot/DbBackup.cpp +++ b/CWeChatRobot/DbBackup.cpp @@ -7,46 +7,19 @@ struct BackupParams { }; BOOL BackupSQLiteDB(DWORD pid,DWORD DbHandle, BSTR savepath) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD BackupSQLiteDBRemoteAddr = hp.GetProcAddr(BackupSQLiteDBRemote); + if (BackupSQLiteDBRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwHandle = 0x0; - DWORD dwId = 0x0; - DWORD dwWriteSize = 0x0; - LPVOID savepathAddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - BackupParams* paramAndFunc = (BackupParams*)::VirtualAllocEx(hProcess, 0, sizeof(BackupParams), MEM_COMMIT, PAGE_READWRITE); - if (!savepathAddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } char* a_savepath = _com_util::ConvertBSTRToString(savepath); - if (savepathAddr) - WriteProcessMemory(hProcess, savepathAddr, a_savepath, strlen(a_savepath) + 1, &dwWriteSize); - BackupParams param = { 0 }; - param.ptrDb = DbHandle; - param.savepath = (DWORD)savepathAddr; - - if (paramAndFunc) - WriteProcessMemory(hProcess, paramAndFunc, ¶m, sizeof(BackupParams), &dwWriteSize); - - DWORD BackupSQLiteDBRemoteAddr = WeChatRobotBase + BackupSQLiteDBRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)BackupSQLiteDBRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); + WeChatData r_savepath(hp.GetHandle(), a_savepath, TEXTLENGTHA(a_savepath)); + BackupParams params = { 0 }; + params.ptrDb = DbHandle; + params.savepath = (DWORD)r_savepath.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_savepath.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - VirtualFreeEx(hProcess, savepathAddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwHandle; + DWORD ret = CallRemoteFunction(hp.GetHandle(), BackupSQLiteDBRemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/DbExecuteSql.cpp b/CWeChatRobot/DbExecuteSql.cpp index 13ec421b16371cbe0ddd82cc8fc5934a8ee745aa..714f6c9d15e3121a2d4e30dba433d7b4abecbc7e 100644 --- a/CWeChatRobot/DbExecuteSql.cpp +++ b/CWeChatRobot/DbExecuteSql.cpp @@ -147,54 +147,23 @@ VOID ReadSQLResultFromWeChatProcess(HANDLE hProcess,DWORD dwHandle) { } SAFEARRAY* ExecuteSQL(DWORD pid,DWORD DbHandle,BSTR sql) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return NULL; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return NULL; - } + WeChatProcess hp(pid); + if (!hp.m_init) return NULL; ClearResultArray(); - DWORD dwHandle = 0x0; - DWORD dwId = 0x0; - DWORD dwWriteSize = 0x0; - LPVOID sqlAddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - executeParams* paramAndFunc = (executeParams*)::VirtualAllocEx(hProcess, 0, sizeof(executeParams), MEM_COMMIT, PAGE_READWRITE); - if (!sqlAddr || !paramAndFunc) { - CloseHandle(hProcess); + // DWORD ExecuteSQLRemoteAddr = hp.GetProcAddr(ExecuteSQLRemote); + DWORD ExecuteSQLRemoteAddr = hp.GetProcAddr(SelectDataRemote); + if (ExecuteSQLRemoteAddr == 0) return NULL; - } char* a_sql = _com_util::ConvertBSTRToString(sql); - if(sqlAddr) - WriteProcessMemory(hProcess, sqlAddr, a_sql, strlen(a_sql) + 1, &dwWriteSize); - executeParams param = { 0 }; - param.ptrDb = DbHandle; - param.ptrSql = (DWORD)sqlAddr; - - if(paramAndFunc) - WriteProcessMemory(hProcess, paramAndFunc, ¶m, sizeof(executeParams), &dwWriteSize); - - // DWORD ExecuteSQLRemoteAddr = WeChatRobotBase + ExecuteSQLRemoteOffset; - DWORD SelectDataRemoteAddr = WeChatRobotBase + SelectDataRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SelectDataRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); + WeChatData r_sql(hp.GetHandle(), a_sql, TEXTLENGTHA(a_sql)); + executeParams params = { 0 }; + params.ptrDb = DbHandle; + params.ptrSql = (DWORD)r_sql.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_sql.GetAddr() == 0 || r_params.GetAddr() == 0) return NULL; - } - if (!dwHandle) { - CloseHandle(hProcess); - return NULL; - } - ReadSQLResultFromWeChatProcess(hProcess,dwHandle); + DWORD ret = CallRemoteFunction(hp.GetHandle(), ExecuteSQLRemoteAddr, r_params.GetAddr()); + ReadSQLResultFromWeChatProcess(hp.GetHandle(),ret); SAFEARRAY* psaValue = CreateSQLResultSafeArray(); - VirtualFreeEx(hProcess, sqlAddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); return psaValue; } \ No newline at end of file diff --git a/CWeChatRobot/DelChatRoomMember.cpp b/CWeChatRobot/DelChatRoomMember.cpp index 053938498e98a005fa2aeaae3cc763a2b46b7a08..5dfbf0190b8a1c08f54d7fb5b2c848dea9b3c281 100644 --- a/CWeChatRobot/DelChatRoomMember.cpp +++ b/CWeChatRobot/DelChatRoomMember.cpp @@ -8,62 +8,22 @@ struct DelChatRoomMemberStruct }; BOOL DelChatRoomMember(DWORD pid,wchar_t* chatroomid, wchar_t* wxid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD DelChatRoomMemberRemoteAddr = hp.GetProcAddr(DelChatRoomMemberRemote); + if (DelChatRoomMemberRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 0; - DelChatRoomMemberStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DelChatRoomMemberStruct* paramAndFunc = (DelChatRoomMemberStruct*)::VirtualAllocEx(hProcess, 0, sizeof(DelChatRoomMemberStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !wxidaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - DWORD dwTId = 0; - - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.wxids = (DWORD)wxidaddr; + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + DelChatRoomMemberStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.wxids = (DWORD)r_wxid.GetAddr(); params.length = 1; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(DelChatRoomMemberStruct), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD DelChatRoomMemberAddr = WeChatRobotBase + DelChatRoomMemberRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)DelChatRoomMemberAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_wxid.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), DelChatRoomMemberRemoteAddr, r_params.GetAddr()); + return ret == 0; } BOOL DelChatRoomMember(DWORD pid,wchar_t* chatroomid, SAFEARRAY* psaValue) { @@ -78,67 +38,34 @@ BOOL DelChatRoomMember(DWORD pid,wchar_t* chatroomid, SAFEARRAY* psaValue) { hr = SafeArrayGetElement(psaValue, &pIndex, &rgvar); return DelChatRoomMember(pid,chatroomid, rgvar.bstrVal); } - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - vector wxidptrs; DWORD dwWriteSize = 0; - DWORD dwTId = 0; DWORD dwId = 0; DWORD dwRet = 0; - DelChatRoomMemberStruct params = { 0 }; - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxidptrsaddr = VirtualAllocEx(hProcess, NULL, sizeof(void*) * cElements, MEM_COMMIT, PAGE_READWRITE); - DelChatRoomMemberStruct* paramAndFunc = (DelChatRoomMemberStruct*)::VirtualAllocEx(hProcess, 0, sizeof(DelChatRoomMemberStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !wxidptrsaddr || !paramAndFunc) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD DelChatRoomMemberRemoteAddr = hp.GetProcAddr(DelChatRoomMemberRemote); + if (DelChatRoomMemberRemoteAddr == 0) return 1; - } + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + vector wxidptrs; for (long i = lLbound; i < lLbound + cElements; i++) { VariantInit(&rgvar); hr = SafeArrayGetElement(psaValue, &i, &rgvar); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); + LPVOID wxidaddr = VirtualAllocEx(hp.GetHandle(), NULL, 1, MEM_COMMIT, PAGE_READWRITE); if (wxidaddr) { - WriteProcessMemory(hProcess, wxidaddr, rgvar.bstrVal, wcslen(rgvar.bstrVal) * 2 + 2, &dwWriteSize); + WriteProcessMemory(hp.GetHandle(), wxidaddr, rgvar.bstrVal, wcslen(rgvar.bstrVal) * 2 + 2, &dwWriteSize); wxidptrs.push_back(wxidaddr); } } - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (wxidptrsaddr) - WriteProcessMemory(hProcess, wxidptrsaddr, &wxidptrs[0], wxidptrs.size() * sizeof(void*), &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.wxids = (DWORD)wxidptrsaddr; + WeChatData r_wxids(hp.GetHandle(), &wxidptrs[0], wxidptrs.size() * sizeof(void*)); + DelChatRoomMemberStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.wxids = (DWORD)r_wxids.GetAddr(); params.length = wxidptrs.size(); - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(DelChatRoomMemberStruct), &dwTId); - } - else { - CloseHandle(hProcess); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_wxids.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - DWORD DelChatRoomMemberAddr = WeChatRobotBase + DelChatRoomMemberRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)DelChatRoomMemberAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return 1; - } + DWORD ret = CallRemoteFunction(hp.GetHandle(), DelChatRoomMemberRemoteAddr, r_params.GetAddr()); for (unsigned int i = 0; i < wxidptrs.size(); i++) { - VirtualFreeEx(hProcess, wxidptrs[i], 0, MEM_RELEASE); + VirtualFreeEx(hp.GetHandle(), wxidptrs[i], 0, MEM_RELEASE); } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxidptrsaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/DeleteUser.cpp b/CWeChatRobot/DeleteUser.cpp index 88a7fb60a799f403932e39bd051dda89855e5004..001cadd5d4ba80e9b9764d74eeb49706a177368a 100644 --- a/CWeChatRobot/DeleteUser.cpp +++ b/CWeChatRobot/DeleteUser.cpp @@ -1,31 +1,14 @@ #include "pch.h" BOOL DeleteUser(DWORD pid,wchar_t* wxid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD DeleteUserRemoteAddr = WeChatRobotBase + DeleteUserRemoteOffset; - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - DWORD dwId = 0; - DWORD dwRet = 0; - if (!wxidaddr) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)DeleteUserRemoteAddr, wxidaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD DeleteUserRemoteAddr = hp.GetProcAddr(DeleteUserRemote); + if (DeleteUserRemoteAddr == 0) + return 1; + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + if (r_wxid.GetAddr() == 0) + return 1; + DWORD ret = CallRemoteFunction(hp.GetHandle(), DeleteUserRemoteAddr, r_wxid.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/EditRemark.cpp b/CWeChatRobot/EditRemark.cpp index 1132f25b15a6be287139629afba63df2c0c0eaab..c755da64ee1200cb0898a6045bb03f8235c5bd82 100644 --- a/CWeChatRobot/EditRemark.cpp +++ b/CWeChatRobot/EditRemark.cpp @@ -6,44 +6,19 @@ struct EditRemarkStruct { }; BOOL EditRemark(DWORD pid,wchar_t* wxid, wchar_t* remark) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD EditRemarkRemoteAddr = hp.GetProcAddr(EditRemarkRemote); + if (EditRemarkRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 1; - - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID remarkaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - EditRemarkStruct* paramAndFunc = (EditRemarkStruct*)VirtualAllocEx(hProcess, 0, sizeof(EditRemarkStruct), MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr || !remarkaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - if (remark) - WriteProcessMemory(hProcess, remarkaddr, remark, wcslen(remark) * 2 + 2, &dwWriteSize); - + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_remark(hp.GetHandle(), remark, TEXTLENGTH(remark)); EditRemarkStruct params = { 0 }; - params.wxid = (DWORD)wxidaddr; - params.remark = remark ? (DWORD)remarkaddr : 0; - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwWriteSize); - DWORD EditRemarkAddr = WeChatRobotBase + EditRemarkRemoteOffset; - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)EditRemarkAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, remarkaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + params.wxid = (DWORD)r_wxid.GetAddr(); + params.remark = (DWORD)r_remark.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_wxid.GetAddr() == 0 || r_params.GetAddr() == 0) + return 1; + DWORD ret = CallRemoteFunction(hp.GetHandle(), EditRemarkRemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/FriendList.cpp b/CWeChatRobot/FriendList.cpp index ed73eb8228e5ad0bfdcc64a5b6d6f9a9534fc528..728fd38e0c7328e215876c81e350bd61ab7dd4bb 100644 --- a/CWeChatRobot/FriendList.cpp +++ b/CWeChatRobot/FriendList.cpp @@ -120,120 +120,72 @@ SAFEARRAY* CreateFriendArray(int FriendCount) { } SAFEARRAY* GetFriendList(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return NULL; + DWORD GetFriendListInitAddr = hp.GetProcAddr(GetFriendListInit); + DWORD GetFriendListRemoteAddr = hp.GetProcAddr(GetFriendListRemote); + DWORD GetFriendListFinishAddr = hp.GetProcAddr(GetFriendListFinish); + if (GetFriendListInitAddr == 0 || GetFriendListRemoteAddr == 0 || GetFriendListFinishAddr == 0) return NULL; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return NULL; - } - DWORD GetFriendListInitAddr = WeChatRobotBase + GetFriendListInitOffset; - DWORD GetFriendListRemoteAddr = WeChatRobotBase + GetFriendListRemoteOffset; - DWORD GetFriendListFinishAddr = WeChatRobotBase + GetFriendListFinishOffset; DWORD FriendCount = 0; - DWORD dwId, dwHandle = 0; + DWORD dwHandle = 0; // 获取好友列表的长度 - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetFriendListInitAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &FriendCount); - CloseHandle(hThread); - } + FriendCount = CallRemoteFunction(hp.GetHandle(), GetFriendListInitAddr, NULL); // 获取保存第一个好友的数据指针的结构体首地址 - hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetFriendListRemoteAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } + dwHandle = CallRemoteFunction(hp.GetHandle(), GetFriendListRemoteAddr, NULL); WxFriendAddrStruct WxFriendAddr = { 0 }; // 根据好友数量初始化全局变量 WxFriendList = new WxFriendStruct[FriendCount]; - if (dwHandle) { - for (unsigned int i = 0; i < FriendCount; i++) { - WxFriendList[i] = { 0 }; - ZeroMemory(&WxFriendAddr, sizeof(WxFriendAddrStruct)); - ReadProcessMemory(hProcess, (LPCVOID)dwHandle, &WxFriendAddr, sizeof(WxFriendAddrStruct), 0); - ReadFriendMessageByAddress(hProcess,&WxFriendAddr, &WxFriendList[i]); - // 保存下一个好友数据的结构体 - dwHandle += sizeof(WxFriendAddrStruct); - } - } - else { - CloseHandle(hProcess); + if (dwHandle == 0) return NULL; + for (unsigned int i = 0; i < FriendCount; i++) { + WxFriendList[i] = { 0 }; + ZeroMemory(&WxFriendAddr, sizeof(WxFriendAddrStruct)); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)dwHandle, &WxFriendAddr, sizeof(WxFriendAddrStruct), 0); + ReadFriendMessageByAddress(hp.GetHandle(),&WxFriendAddr, &WxFriendList[i]); + // 保存下一个好友数据的结构体 + dwHandle += sizeof(WxFriendAddrStruct); } // 清除微信进程空间中的缓存 - hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetFriendListFinishAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } - + CallRemoteFunction(hp.GetHandle(), GetFriendListFinishAddr, NULL); SAFEARRAY* psaValue = CreateFriendArray(FriendCount); for (unsigned int i = 0; i < FriendCount; i++) { FreeWxFriend(i); } delete[] WxFriendList; WxFriendList = NULL; - CloseHandle(hProcess); return psaValue; } std::wstring GetFriendListString(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return L"[]"; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return L"[]"; - } - DWORD GetFriendListInitAddr = WeChatRobotBase + GetFriendListInitOffset; - DWORD GetFriendListRemoteAddr = WeChatRobotBase + GetFriendListRemoteOffset; - DWORD GetFriendListFinishAddr = WeChatRobotBase + GetFriendListFinishOffset; + WeChatProcess hp(pid); + if (!hp.m_init) return L"[]"; + DWORD GetFriendListInitAddr = hp.GetProcAddr(GetFriendListInit); + DWORD GetFriendListRemoteAddr = hp.GetProcAddr(GetFriendListRemote); + DWORD GetFriendListFinishAddr = hp.GetProcAddr(GetFriendListFinish); DWORD FriendCount = 0; - DWORD dwId, dwHandle = 0; + DWORD dwHandle = 0; // 获取好友列表的长度 - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetFriendListInitAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &FriendCount); - CloseHandle(hThread); - } + FriendCount = CallRemoteFunction(hp.GetHandle(), GetFriendListInitAddr, NULL); // 获取保存第一个好友的数据指针的结构体首地址 - hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetFriendListRemoteAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } + dwHandle = CallRemoteFunction(hp.GetHandle(), GetFriendListRemoteAddr, NULL); WxFriendAddrStruct WxFriendAddr = { 0 }; // 根据好友数量初始化全局变量 WxFriendList = new WxFriendStruct[FriendCount]; - if (dwHandle) { - for (unsigned int i = 0; i < FriendCount; i++) { - WxFriendList[i] = { 0 }; - ZeroMemory(&WxFriendAddr, sizeof(WxFriendAddrStruct)); - ReadProcessMemory(hProcess, (LPCVOID)dwHandle, &WxFriendAddr, sizeof(WxFriendAddrStruct), 0); - ReadFriendMessageByAddress(hProcess,&WxFriendAddr, &WxFriendList[i]); - // 保存下一个好友数据的结构体 - dwHandle += sizeof(WxFriendAddrStruct); - } - } - else { - CloseHandle(hProcess); + if (dwHandle == 0) return L"[]"; + for (unsigned int i = 0; i < FriendCount; i++) { + WxFriendList[i] = { 0 }; + ZeroMemory(&WxFriendAddr, sizeof(WxFriendAddrStruct)); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)dwHandle, &WxFriendAddr, sizeof(WxFriendAddrStruct), 0); + ReadFriendMessageByAddress(hp.GetHandle(),&WxFriendAddr, &WxFriendList[i]); + // 保存下一个好友数据的结构体 + dwHandle += sizeof(WxFriendAddrStruct); } // 清除微信进程空间中的缓存 - hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetFriendListFinishAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } + CallRemoteFunction(hp.GetHandle(), GetFriendListFinishAddr, NULL); wstring message = L"["; // 构造结构化的数据 @@ -250,6 +202,5 @@ std::wstring GetFriendListString(DWORD pid) { // 释放全局变量 delete[] WxFriendList; WxFriendList = NULL; - CloseHandle(hProcess); return message; } \ No newline at end of file diff --git a/CWeChatRobot/GetChatRoomMemberNickname.cpp b/CWeChatRobot/GetChatRoomMemberNickname.cpp index 3bc7c03553c10dbbcc536988871a37609f748bfb..f9e8e1749672944181f5918507dec4d647ee90b0 100644 --- a/CWeChatRobot/GetChatRoomMemberNickname.cpp +++ b/CWeChatRobot/GetChatRoomMemberNickname.cpp @@ -8,66 +8,24 @@ struct ChatRoomMemberNicknameStruct }; wstring GetChatRoomMemberNickname(DWORD pid,wchar_t* chatroomid, wchar_t* wxid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + wchar_t buffer[33] = { 0 }; + WeChatProcess hp(pid); + if (!hp.m_init) return L""; + DWORD GetChatRoomMemberNicknameRemoteAddr = hp.GetProcAddr(GetChatRoomMemberNicknameRemote); + if (GetChatRoomMemberNicknameRemoteAddr == 0) return L""; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_nickname(hp.GetHandle(), buffer, 33 * 2); + ChatRoomMemberNicknameStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.wxid = (DWORD)r_wxid.GetAddr(); + params.nickname = (DWORD)r_nickname.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_wxid.GetAddr() == 0 || r_params.GetAddr() == 0 || r_nickname.GetAddr() == 0) return L""; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 0; - ChatRoomMemberNicknameStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID nicknameaddr = VirtualAllocEx(hProcess, NULL, 33 * 2, MEM_COMMIT, PAGE_READWRITE); - ChatRoomMemberNicknameStruct* paramAndFunc = (ChatRoomMemberNicknameStruct*)::VirtualAllocEx(hProcess, 0, sizeof(ChatRoomMemberNicknameStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !wxidaddr || !nicknameaddr || !paramAndFunc) { - CloseHandle(hProcess); - return L""; - } - DWORD dwTId = 0; - - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.wxid = (DWORD)wxidaddr; - params.nickname = (DWORD)nicknameaddr; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwTId); - } - else { - CloseHandle(hProcess); - return L""; - } - - DWORD GetChatRoomMemberNicknameAddr = WeChatRobotBase + GetChatRoomMemberNicknameRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetChatRoomMemberNicknameAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return L""; - } - wchar_t* buffer = new wchar_t[33]; - ZeroMemory(buffer, 33 * 2); - ReadProcessMemory(hProcess, nicknameaddr, buffer, 32 * 2, 0); + DWORD ret = CallRemoteFunction(hp.GetHandle(), GetChatRoomMemberNicknameRemoteAddr, r_params.GetAddr()); + ReadProcessMemory(hp.GetHandle(), r_nickname.GetAddr(), buffer, 32 * 2, 0); wstring nickname(buffer); - delete[] buffer; - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, nicknameaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); return nickname; } \ No newline at end of file diff --git a/CWeChatRobot/GetChatRoomMembers.cpp b/CWeChatRobot/GetChatRoomMembers.cpp index 1a142a71f0b6f81cb4266601e0b13be281acd7da..c8b6c130df7d8cc23053f9917bec4540551ffe3c 100644 --- a/CWeChatRobot/GetChatRoomMembers.cpp +++ b/CWeChatRobot/GetChatRoomMembers.cpp @@ -6,47 +6,25 @@ struct ChatRoomInfoStruct { }; SAFEARRAY* GetChatRoomMembers(DWORD pid,wchar_t* chatroomid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return NULL; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return NULL; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwHandle = 0; HRESULT hr = S_OK; - ChatRoomInfoStruct chatroominfo = { 0 }; - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return NULL; + DWORD GetChatRoomMembersRemoteAddr = hp.GetProcAddr(GetChatRoomMembersRemote); + if (GetChatRoomMembersRemoteAddr == 0) return NULL; - } - else { - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - } - - DWORD GetChatRoomMembersRemoteAddr = WeChatRobotBase + GetChatRoomMembersRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetChatRoomMembersRemoteAddr, (LPVOID)chatroomidaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - } - else { - CloseHandle(hProcess); + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + if (r_chatroomid.GetAddr() == 0) return NULL; - } - if (!dwHandle) { - CloseHandle(hProcess); + DWORD ret = CallRemoteFunction(hp.GetHandle(), GetChatRoomMembersRemoteAddr, r_chatroomid.GetAddr()); + + if (ret == 0) { return NULL; } - ReadProcessMemory(hProcess,(LPCVOID)dwHandle,&chatroominfo,sizeof(ChatRoomInfoStruct),0); + ChatRoomInfoStruct chatroominfo = { 0 }; + ReadProcessMemory(hp.GetHandle(),(LPCVOID)ret,&chatroominfo,sizeof(ChatRoomInfoStruct),0); wchar_t* members = new wchar_t[chatroominfo.length + 1]; ZeroMemory(members, (chatroominfo.length + 1) * 2); - ReadProcessMemory(hProcess, (LPCVOID)chatroominfo.members, members, chatroominfo.length * 2, 0); - cout << members << endl; + ReadProcessMemory(hp.GetHandle(), (LPCVOID)chatroominfo.members, members, chatroominfo.length * 2, 0); SAFEARRAYBOUND rgsaBound[2] = { {2,0},{2,0} }; SAFEARRAY* psaValue = SafeArrayCreate(VT_VARIANT, 2, rgsaBound); long keyIndex[2] = { 0,0 }; @@ -60,6 +38,5 @@ SAFEARRAY* GetChatRoomMembers(DWORD pid,wchar_t* chatroomid) { hr = SafeArrayPutElement(psaValue, keyIndex, &(_variant_t)members); delete[] members; members = NULL; - CloseHandle(hProcess); return psaValue; } \ No newline at end of file diff --git a/CWeChatRobot/GetDbHandles.cpp b/CWeChatRobot/GetDbHandles.cpp index f5ef76df52756db2a4cb0e4c803843069ec02481..686520f91a2cda7d41550eb53ebe80b2abfcd7bf 100644 --- a/CWeChatRobot/GetDbHandles.cpp +++ b/CWeChatRobot/GetDbHandles.cpp @@ -86,61 +86,42 @@ SAFEARRAY* CreateDbInfoSafeArray() { } SAFEARRAY* GetDbHandles(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + dbs.clear(); + WeChatProcess hp(pid); + if (!hp.m_init) return NULL; + DWORD GetDbHandlesRemoteAddr = hp.GetProcAddr(GetDbHandlesRemote); + if (GetDbHandlesRemoteAddr == 0) return NULL; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return NULL; - } - DWORD dwHandle = 0x0; - DWORD dwId = 0x0; - DWORD GetDbHandlesRemoteAddr = WeChatRobotBase + GetDbHandlesRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetDbHandlesRemoteAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return NULL; - } - if (!dwHandle) { - CloseHandle(hProcess); - return NULL; - } + DWORD ret = CallRemoteFunction(hp.GetHandle(), GetDbHandlesRemoteAddr, NULL); while (1) { DbInfoAddrStruct dbaddr = { 0 }; - ReadProcessMemory(hProcess, (LPCVOID)dwHandle, &dbaddr, sizeof(DbInfoAddrStruct), 0); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)ret, &dbaddr, sizeof(DbInfoAddrStruct), 0); if (dbaddr.handle == 0) break; DbInfoStruct db = { 0 }; db.handle = dbaddr.handle; db.count = dbaddr.count; db.dbname = new wchar_t[dbaddr.l_dbname + 1]; - ReadProcessMemory(hProcess, (LPCVOID)dbaddr.dbname, db.dbname, sizeof(wchar_t) * (dbaddr.l_dbname + 1), 0); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)dbaddr.dbname, db.dbname, sizeof(wchar_t) * (dbaddr.l_dbname + 1), 0); DWORD db_table_start_addr = dbaddr.v_data; while (db_table_start_addr < dbaddr.v_end1) { TableInfoAddrStruct tbaddr = { 0 }; TableInfoStruct tb = { 0 }; - ReadProcessMemory(hProcess, (LPCVOID)db_table_start_addr, &tbaddr, sizeof(TableInfoAddrStruct), 0); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)db_table_start_addr, &tbaddr, sizeof(TableInfoAddrStruct), 0); tb.name = new char[tbaddr.l_name + 1]; - ReadProcessMemory(hProcess, (LPCVOID)tbaddr.name, tb.name, tbaddr.l_name + 1, 0); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)tbaddr.name, tb.name, tbaddr.l_name + 1, 0); tb.tbl_name = new char[tbaddr.l_tbl_name + 1]; - ReadProcessMemory(hProcess, (LPCVOID)tbaddr.tbl_name, tb.tbl_name, tbaddr.l_tbl_name + 1, 0); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)tbaddr.tbl_name, tb.tbl_name, tbaddr.l_tbl_name + 1, 0); tb.rootpage = new char[tbaddr.l_rootpage + 1]; - ReadProcessMemory(hProcess, (LPCVOID)tbaddr.rootpage, tb.rootpage, tbaddr.l_rootpage + 1, 0); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)tbaddr.rootpage, tb.rootpage, tbaddr.l_rootpage + 1, 0); tb.sql = new char[tbaddr.l_sql + 1]; - ReadProcessMemory(hProcess, (LPCVOID)tbaddr.sql, tb.sql, tbaddr.l_sql + 1, 0); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)tbaddr.sql, tb.sql, tbaddr.l_sql + 1, 0); db.tables.push_back(tb); db_table_start_addr += sizeof(TableInfoAddrStruct); } dbs.push_back(db); - dwHandle += sizeof(DbInfoAddrStruct); + ret += sizeof(DbInfoAddrStruct); } SAFEARRAY* psaValue = CreateDbInfoSafeArray(); - CloseHandle(hProcess); return psaValue; } \ No newline at end of file diff --git a/CWeChatRobot/HookImageMessage.cpp b/CWeChatRobot/HookImageMessage.cpp index 627ff53ed907ffa97024989185d26b41f1263b15..76dd54c98837b60c647be4abb731dea51aa2d387 100644 --- a/CWeChatRobot/HookImageMessage.cpp +++ b/CWeChatRobot/HookImageMessage.cpp @@ -1,50 +1,23 @@ #include "pch.h" BOOL HookImageMsg(DWORD pid,wchar_t* savepath) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD HookImageMsgRemoteAddr = hp.GetProcAddr(HookImageMsgRemote); + if (HookImageMsgRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_savepath(hp.GetHandle(), savepath, TEXTLENGTH(savepath)); + if (r_savepath.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwRet = 0x0; - LPVOID savepathaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - if (!savepathaddr) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, savepathaddr, savepath, wcslen(savepath) * 2 + 2, &dwWriteSize); - DWORD HookImageMsgRemoteAddr = WeChatRobotBase + HookImageMsgRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)HookImageMsgRemoteAddr, savepathaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - VirtualFreeEx(hProcess, savepathaddr, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), HookImageMsgRemoteAddr, r_savepath.GetAddr()); + return ret == 0; } void UnHookImageMsg(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return; + DWORD UnHookImageMsgRemoteAddr = hp.GetProcAddr(UnHookImageMsgRemote); + if (UnHookImageMsgRemoteAddr == 0) return; - } - DWORD dwId = 0x0; - DWORD UnHookImageMsgRemoteAddr = WeChatRobotBase + UnHookImageMsgRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)UnHookImageMsgRemoteAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } - CloseHandle(hProcess); + CallRemoteFunction(hp.GetHandle(), UnHookImageMsgRemoteAddr, NULL); } \ No newline at end of file diff --git a/CWeChatRobot/HookVoiceMessage.cpp b/CWeChatRobot/HookVoiceMessage.cpp index 491851f4a671d9b7d98741ee57a750de79f0d3f3..0c9187bd3c8f80d3bc52f82e2cebaf1a8edee746 100644 --- a/CWeChatRobot/HookVoiceMessage.cpp +++ b/CWeChatRobot/HookVoiceMessage.cpp @@ -1,50 +1,23 @@ #include "pch.h" BOOL HookVoiceMsg(DWORD pid,wchar_t* savepath) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD HookVoiceMsgRemoteAddr = hp.GetProcAddr(HookVoiceMsgRemote); + if (HookVoiceMsgRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_savepath(hp.GetHandle(), savepath, TEXTLENGTH(savepath)); + if (r_savepath.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwRet = 0x0; - LPVOID savepathaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - if (!savepathaddr) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, savepathaddr, savepath, wcslen(savepath) * 2 + 2, &dwWriteSize); - DWORD HookVoiceMsgRemoteAddr = WeChatRobotBase + HookVoiceMsgRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)HookVoiceMsgRemoteAddr, savepathaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - VirtualFreeEx(hProcess, savepathaddr, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), HookVoiceMsgRemoteAddr, r_savepath.GetAddr()); + return ret == 0; } void UnHookVoiceMsg(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return; + DWORD UnHookVoiceMsgRemoteAddr = hp.GetProcAddr(UnHookVoiceMsgRemote); + if (UnHookVoiceMsgRemoteAddr == 0) return; - } - DWORD dwId = 0x0; - DWORD UnHookVoiceMsgRemoteAddr = WeChatRobotBase + UnHookVoiceMsgRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)UnHookVoiceMsgRemoteAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } - CloseHandle(hProcess); + CallRemoteFunction(hp.GetHandle(), UnHookVoiceMsgRemoteAddr, NULL); } \ No newline at end of file diff --git a/CWeChatRobot/InjectDll.cpp b/CWeChatRobot/InjectDll.cpp index e099254011e5e629a3b7f05e8b24a765740599e1..05bcfa6a405c2d914d85bccee63b456c0fa38132 100644 --- a/CWeChatRobot/InjectDll.cpp +++ b/CWeChatRobot/InjectDll.cpp @@ -1,37 +1,14 @@ #include "pch.h" -bool InjectDll(DWORD dwId, WCHAR* szPath)//参数1:目标进程PID 参数2:DLL路径 +bool InjectDll(DWORD dwId, WCHAR* szPath) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwId); - if (!hProcess) + WeChatProcess hp(dwId); + if (!hp.m_init) return 1; + if (hp.WeChatRobotBase() != 0) return 0; + WeChatData r_dllpath(hp.GetHandle(), szPath, TEXTLENGTH(szPath)); + if (r_dllpath.GetAddr() == 0) return 1; - if (GetWeChatRobotBase(dwId) != 0) { - CloseHandle(hProcess); - return 0; - } - - LPVOID pRemoteAddress = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - if (pRemoteAddress) - { - WriteProcessMemory(hProcess, pRemoteAddress, szPath, wcslen(szPath) * 2 + 2, &dwWriteSize); - } - else { - CloseHandle(hProcess); - return 1; - } - - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibrary, pRemoteAddress, NULL, NULL); - if (hThread) { - WaitForSingleObject(hThread, -1); - } - else { - CloseHandle(hProcess); - return 1; - } - CloseHandle(hThread); - VirtualFreeEx(hProcess, pRemoteAddress, 0, MEM_RELEASE); - CloseHandle(hProcess); + CallRemoteFunction(hp.GetHandle(), LoadLibraryW, r_dllpath.GetAddr()); return 0; } @@ -52,36 +29,12 @@ bool Inject(DWORD dwPid,wchar_t* workPath) { } BOOL RemoveDll(DWORD dwId) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwId); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(dwId); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 0; - } - DWORD dwWriteSize = 0; - HANDLE hThread = NULL; - DWORD dwID = 0; - hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)FreeConsole, NULL, 0, &dwID); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return 1; - } - hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)FreeLibrary, (LPVOID)WeChatRobotBase, 0, &dwID); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return 1; - } - CloseHandle(hProcess); + WeChatProcess hp(dwId); + if (!hp.m_init) return 1; + DWORD WeChatRobotBase = hp.WeChatRobotBase(); + if (WeChatRobotBase == 0) return 0; + CallRemoteFunction(hp.GetHandle(), FreeConsole, NULL); + CallRemoteFunction(hp.GetHandle(), FreeLibrary, WeChatRobotBase); return 0; } diff --git a/CWeChatRobot/ReceiveMessage.cpp b/CWeChatRobot/ReceiveMessage.cpp index 18eb972d3fa7272c73ec744f70f726554e098bbd..af3c4d44dcd5ece3745d7fd4bb6463c2a6bff94b 100644 --- a/CWeChatRobot/ReceiveMessage.cpp +++ b/CWeChatRobot/ReceiveMessage.cpp @@ -1,52 +1,21 @@ #include "pch.h" BOOL StartReceiveMessage(DWORD pid,int port) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD StartReceiveMessageRemoteAddr = hp.GetProcAddr(HookReceiveMessageRemote); + if (StartReceiveMessageRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - - DWORD HookReceiveMessageAddr = WeChatRobotBase + HookReceiveMessageRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)HookReceiveMessageAddr, (LPVOID)port, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - } - else { - CloseHandle(hProcess); - return 1; - } - CloseHandle(hThread); - CloseHandle(hProcess); + CallRemoteFunction(hp.GetHandle(), StartReceiveMessageRemoteAddr, port); return 0; } BOOL StopReceiveMessage(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) { - return 1; - } - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - - DWORD UnHookReceiveMessageAddr = WeChatRobotBase + UnHookReceiveMessageRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)UnHookReceiveMessageAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - } - else { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD UnHookReceiveMsgRemoteAddr = hp.GetProcAddr(UnHookReceiveMessageRemote); + if (UnHookReceiveMsgRemoteAddr == 0) return 1; - } - CloseHandle(hThread); - CloseHandle(hProcess); + CallRemoteFunction(hp.GetHandle(), UnHookReceiveMsgRemoteAddr, NULL); return 0; } \ No newline at end of file diff --git a/CWeChatRobot/SearchContactByCache.cpp b/CWeChatRobot/SearchContactByCache.cpp index 2dd8b4649c8a09029ebb2afe755e736a7c16d95c..704b0dd7f6779dc04b51c8278ea7bb64540bfa16 100644 --- a/CWeChatRobot/SearchContactByCache.cpp +++ b/CWeChatRobot/SearchContactByCache.cpp @@ -5,63 +5,32 @@ struct GetUserInfoStruct { DWORD length; }; -VOID DeleteUserInfoCache(DWORD pid,HANDLE hProcess) { - DWORD dwId = 0; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return; - } - DWORD DeleteUserInfoCacheProcAddr = WeChatRobotBase + DeleteUserInfoCacheOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)DeleteUserInfoCacheProcAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } -} - std::wstring GetWxUserInfo(DWORD pid,wchar_t* wxid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + DWORD dwReadSize = 0; + wstring info = L""; + WeChatProcess hp(pid); + if (!hp.m_init) return L"{}"; + DWORD GetWxUserInfoRemoteAddr = hp.GetProcAddr(GetWxUserInfoRemote); + DWORD DeleteUserInfoCacheProcAddr = hp.GetProcAddr(DeleteUserInfoCacheRemote); + if (GetWxUserInfoRemoteAddr == 0) return L"{}"; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + if (r_wxid.GetAddr() == 0) return L"{}"; - } - wstring WString = L""; - DWORD GetUserInfoProcAddr = WeChatRobotBase + GetWxUserInfoOffset; - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - DWORD dwId = 0; - DWORD dwHandle = 0; - GetUserInfoStruct userinfo = { 0 }; - if (!wxidaddr) { - CloseHandle(hProcess); + DWORD ret = CallRemoteFunction(hp.GetHandle(), GetWxUserInfoRemoteAddr, r_wxid.GetAddr()); + if (ret == 0) return L"{}"; - } - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetUserInfoProcAddr, wxidaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } - - if(dwHandle) - ReadProcessMemory(hProcess, (LPCVOID)dwHandle, &userinfo, sizeof(GetUserInfoStruct), &dwWriteSize); + GetUserInfoStruct userinfo = { 0 }; + ReadProcessMemory(hp.GetHandle(), (LPVOID)ret, &userinfo, sizeof(GetUserInfoStruct), &dwReadSize); if (userinfo.length) { wchar_t* wmessage = new wchar_t[userinfo.length + 1]; ZeroMemory(wmessage, (userinfo.length + 1) * 2); - ReadProcessMemory(hProcess, (LPCVOID)userinfo.message, wmessage, userinfo.length * 2, &dwWriteSize); - WString += wmessage; + ReadProcessMemory(hp.GetHandle(), (LPVOID)userinfo.message, wmessage, userinfo.length * 2, &dwReadSize); + info = (wstring)wmessage; delete[] wmessage; wmessage = NULL; } - - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - DeleteUserInfoCache(pid,hProcess); - CloseHandle(hProcess); - return WString; + CallRemoteFunction(hp.GetHandle(), DeleteUserInfoCacheProcAddr, NULL); + return info; } diff --git a/CWeChatRobot/SearchContactByNet.cpp b/CWeChatRobot/SearchContactByNet.cpp index 5f5c9234a6e6e6ca6f35a2a9f3c0d16b839f5f93..61c1906e85b58dc022e4960d7e8c7d1afa1bc205 100644 --- a/CWeChatRobot/SearchContactByNet.cpp +++ b/CWeChatRobot/SearchContactByNet.cpp @@ -134,41 +134,24 @@ static void ReadUserInfoFromMemory(HANDLE hProcess) { } SAFEARRAY* SearchContactByNet(DWORD pid,wchar_t* keyword) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + DWORD dwReadSize = 0; + WeChatProcess hp(pid); + if (!hp.m_init) return NULL; + DWORD SearchContactByNetRemoteAddr = hp.GetProcAddr(SearchContactByNetRemote); + if (SearchContactByNetRemoteAddr == 0) return NULL; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_keyword(hp.GetHandle(), keyword, TEXTLENGTH(keyword)); + if (r_keyword.GetAddr() == 0) return NULL; - } ClearUserInfoCache(); - DWORD SearchContactByNetRemoteAddr = WeChatRobotBase + SearchContactByNetRemoteOffset; - LPVOID keywordaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - DWORD dwId = 0; - DWORD dwHandle = 0; - if (!keywordaddr) { - CloseHandle(hProcess); - return NULL; - } - WriteProcessMemory(hProcess, keywordaddr, keyword, wcslen(keyword) * 2 + 2, &dwWriteSize); - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SearchContactByNetRemoteAddr, keywordaddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } - VirtualFreeEx(hProcess, keywordaddr, 0, MEM_RELEASE); - if (!dwHandle) + DWORD ret = CallRemoteFunction(hp.GetHandle(), SearchContactByNetRemoteAddr, r_keyword.GetAddr()); + if (ret == 0) return NULL; - ReadProcessMemory(hProcess, (LPCVOID)dwHandle, &userinfoaddr, sizeof(UserInfoAddr), &dwWriteSize); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)ret, &userinfoaddr, sizeof(UserInfoAddr), &dwReadSize); if (userinfoaddr.errcode == 0) { - ReadUserInfoFromMemory(hProcess); + ReadUserInfoFromMemory(hp.GetHandle()); SAFEARRAY* psa = CreateUserInfoArray(); - CloseHandle(hProcess); return psa; } - CloseHandle(hProcess); return NULL; } \ No newline at end of file diff --git a/CWeChatRobot/SelfInfo.cpp b/CWeChatRobot/SelfInfo.cpp index 828c816d69582fbe79320b9bda573c5e65363ab6..2e811e65d6aace9cc9a8a1f7f550c46834456684 100644 --- a/CWeChatRobot/SelfInfo.cpp +++ b/CWeChatRobot/SelfInfo.cpp @@ -5,77 +5,38 @@ struct GetSelfInfoStruct { DWORD length; }; -VOID DeleteSelfInfoCache(DWORD pid,HANDLE hProcess) { - DWORD dwId = 0; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - return; - } - DWORD DeleteSelfInfoCacheProcAddr = WeChatRobotBase + DeleteSelfInfoCacheOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)DeleteSelfInfoCacheProcAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } -} - std::wstring GetSelfInfo(DWORD pid) { - if (PidToSelfInfoString.count(pid)!=0) - { - return PidToSelfInfoString[pid]; - } - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + wstring SelfInfoString = L""; + DWORD dwReadSize = 0; + WeChatProcess hp(pid); + if (!hp.m_init) return L"{}"; + DWORD GetSelfInfoRemoteAddr = hp.GetProcAddr(GetSelfInfoRemote); + DWORD DeleteSelfInfoCacheRemoteAddr = hp.GetProcAddr(DeleteSelfInfoCacheRemote); + if (GetSelfInfoRemoteAddr == 0) return L"{}"; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + DWORD ret = CallRemoteFunction(hp.GetHandle(), GetSelfInfoRemoteAddr, NULL); + if (ret == 0) return L"{}"; - } - DWORD GetSelfInfoProcAddr = WeChatRobotBase + GetSelfInfoOffset; - DWORD dwWriteSize = 0; - DWORD dwId = 0; - DWORD dwHandle = 0; GetSelfInfoStruct selfinfo = { 0 }; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetSelfInfoProcAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } - if (dwHandle) - ReadProcessMemory(hProcess, (LPCVOID)dwHandle, &selfinfo, sizeof(GetSelfInfoStruct), &dwWriteSize); + ReadProcessMemory(hp.GetHandle(), (LPCVOID)ret, &selfinfo, sizeof(GetSelfInfoStruct), &dwReadSize); if (selfinfo.length) { wchar_t* wmessage = new wchar_t[selfinfo.length + 1]; ZeroMemory(wmessage, (selfinfo.length + 1) * 2); - ReadProcessMemory(hProcess, (LPCVOID)selfinfo.message, wmessage, selfinfo.length * 2, &dwWriteSize); - PidToSelfInfoString[pid] = wmessage; + ReadProcessMemory(hp.GetHandle(), (LPCVOID)selfinfo.message, wmessage, selfinfo.length * 2, &dwReadSize); + SelfInfoString = (wstring)wmessage; delete[] wmessage; wmessage = NULL; } - - DeleteSelfInfoCache(pid,hProcess); - CloseHandle(hProcess); - return PidToSelfInfoString[pid]; + CallRemoteFunction(hp.GetHandle(), DeleteSelfInfoCacheRemoteAddr, NULL); + return SelfInfoString; } BOOL isWxLogin(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return false; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return false; - } - DWORD isWxLoginAddr = WeChatRobotBase + isWxLoginOffset; - DWORD dwId, dwRet = 0; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)isWxLoginAddr, NULL, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - CloseHandle(hProcess); - return dwRet == 1; + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD isWxLoginRemoteAddr = hp.GetProcAddr(isWxLoginRemote); + if (isWxLoginRemoteAddr == 0) + return 1; + DWORD ret = CallRemoteFunction(hp.GetHandle(), isWxLoginRemoteAddr, NULL); + return ret == 1; } \ No newline at end of file diff --git a/CWeChatRobot/SendAppMsg.cpp b/CWeChatRobot/SendAppMsg.cpp index 739911ac229e5daf5d0b2c741045a348ca30bba9..e54ac10b84851ba51c16cbd144e746782f156f60 100644 --- a/CWeChatRobot/SendAppMsg.cpp +++ b/CWeChatRobot/SendAppMsg.cpp @@ -7,50 +7,22 @@ struct SendAppMsgStruct }; BOOL SendAppMsg(DWORD pid,wchar_t* wxid, wchar_t* appid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 0x0; - SendAppMsgStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID appidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - SendAppMsgStruct* paramAndFunc = (SendAppMsgStruct*)::VirtualAllocEx(hProcess, 0, sizeof(SendAppMsgStruct), MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr || !appidaddr || !paramAndFunc || !WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendAppMsgRemoteAddr = hp.GetProcAddr(SendAppMsgRemote); + if (SendAppMsgRemoteAddr == 0) { return 1; } + SendAppMsgStruct params = { 0 }; + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_appid(hp.GetHandle(), appid, TEXTLENGTH(appid)); - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - if (appidaddr) - WriteProcessMemory(hProcess, appidaddr, appid, wcslen(appid) * 2 + 2, &dwWriteSize); - - params.wxid = (DWORD)wxidaddr; - params.appid = (DWORD)appidaddr; - - if (paramAndFunc) - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwWriteSize); - - DWORD SendAppMsgRemoteAddr = WeChatRobotBase + SendAppMsgRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendAppMsgRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); + params.wxid = (DWORD)r_wxid.GetAddr(); + params.appid = (DWORD)r_appid.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!params.wxid || !params.appid || !r_params.GetAddr()) { + return 1; } - - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, appidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendAppMsgRemoteAddr, r_params.GetAddr()); return dwRet == 0; } \ No newline at end of file diff --git a/CWeChatRobot/SendArticle.cpp b/CWeChatRobot/SendArticle.cpp index d883efe5048e18a3f93fd23c3143504a26c2d17b..a66a4004d7cff231f30a20fbd99f180268222ca2 100644 --- a/CWeChatRobot/SendArticle.cpp +++ b/CWeChatRobot/SendArticle.cpp @@ -9,61 +9,27 @@ struct SendArticleStruct { }; BOOL SendArticle(DWORD pid,wchar_t* wxid, wchar_t* title, wchar_t* abstract, wchar_t* url, wchar_t* imgpath) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - SendArticleStruct params; - ZeroMemory(¶ms, sizeof(params)); - DWORD SendArticleProcAddr = WeChatRobotBase + SendArticleOffset; - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID titleaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID abstractaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID urladdr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID imgaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - SendArticleStruct* paramAndFunc = (SendArticleStruct*)::VirtualAllocEx(hProcess, 0, sizeof(SendArticleStruct), MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr || !titleaddr || !abstractaddr || !urladdr || !imgaddr || - !paramAndFunc || !WeChatRobotBase) - { - CloseHandle(hProcess); - return 1; - } - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - if (titleaddr) - WriteProcessMemory(hProcess, titleaddr, title, wcslen(title) * 2 + 2, &dwWriteSize); - if (abstractaddr) - WriteProcessMemory(hProcess, abstractaddr, abstract, wcslen(abstract) * 2 + 2, &dwWriteSize); - if (urladdr) - WriteProcessMemory(hProcess, urladdr, url, wcslen(url) * 2 + 2, &dwWriteSize); - if (imgpath && imgaddr) - WriteProcessMemory(hProcess, imgaddr, imgpath, wcslen(imgpath) * 2 + 2, &dwWriteSize); - params.wxid = (DWORD)wxidaddr; - params.title = (DWORD)titleaddr; - params.abstract = (DWORD)abstractaddr; - params.url = (DWORD)urladdr; - params.imgpath = imgpath ? (DWORD)imgaddr : 0; - - if (paramAndFunc) - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwId); - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendArticleProcAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, titleaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, abstractaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, urladdr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, imgaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendArticleRemoteAddr = hp.GetProcAddr(SendArticleRemote); + if (SendArticleRemoteAddr == 0) { + return 1; + } + SendArticleStruct params = { 0 }; + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_title(hp.GetHandle(), title, TEXTLENGTH(title)); + WeChatData r_abstract(hp.GetHandle(), abstract, TEXTLENGTH(abstract)); + WeChatData r_url(hp.GetHandle(), url, TEXTLENGTH(url)); + WeChatData r_imgpath(hp.GetHandle(), imgpath, TEXTLENGTH(imgpath)); + params.wxid = (DWORD)r_wxid.GetAddr(); + params.title = (DWORD)r_title.GetAddr(); + params.abstract = (DWORD)r_abstract.GetAddr(); + params.url = (DWORD)r_url.GetAddr(); + params.imgpath = (DWORD)r_imgpath.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!r_wxid.GetAddr() || !r_title.GetAddr() || !r_abstract.GetAddr() || !r_url.GetAddr() || !r_params.GetAddr()) { + return 1; + } + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendArticleRemoteAddr, r_params.GetAddr()); return 0; } \ No newline at end of file diff --git a/CWeChatRobot/SendAtText.cpp b/CWeChatRobot/SendAtText.cpp index f09cf5386c999506f194990ccafab49b1fd2b861..7cf0c5ad047a4dfec64191ce02c91d4bed78081b 100644 --- a/CWeChatRobot/SendAtText.cpp +++ b/CWeChatRobot/SendAtText.cpp @@ -10,66 +10,26 @@ struct SendAtTextStruct }; int SendAtText(DWORD pid,wchar_t* chatroomid, wchar_t* wxid, wchar_t* wxmsg,BOOL AutoNickName) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - SendAtTextStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxmsgaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - SendAtTextStruct* paramAndFunc = (SendAtTextStruct*)::VirtualAllocEx(hProcess, 0, sizeof(SendAtTextStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !wxidaddr || !wxmsgaddr || !paramAndFunc || !WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendAtTextRemoteAddr = hp.GetProcAddr(SendAtTextRemote); + if (SendAtTextRemoteAddr == 0) { return 1; } - DWORD dwTId = 0; - - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - if (wxmsgaddr) - WriteProcessMemory(hProcess, wxmsgaddr, wxmsg, wcslen(wxmsg) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.wxid = (DWORD)wxidaddr; - params.wxmsg = (DWORD)wxmsgaddr; - params.length = 1; + SendAtTextStruct params = { 0 }; + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_wxmsg(hp.GetHandle(), wxmsg, TEXTLENGTH(wxmsg)); + params.wxid = (DWORD)r_wxid.GetAddr(); + params.wxmsg = (DWORD)r_wxmsg.GetAddr(); + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); params.AutoNickName = AutoNickName; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(SendAtTextStruct), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD SendAtTextRemoteAddr = WeChatRobotBase + SendAtTextOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendAtTextRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - } - else { - CloseHandle(hProcess); + params.length = 1; + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!params.wxid || !params.wxmsg || !r_params.GetAddr()) { return 1; } - CloseHandle(hThread); - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxmsgaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendAtTextRemoteAddr, r_params.GetAddr()); return 0; } @@ -85,73 +45,37 @@ BOOL SendAtText(DWORD pid,wchar_t* chatroomid, SAFEARRAY* psaValue, wchar_t* wxm hr = SafeArrayGetElement(psaValue, &pIndex, &rgvar); return SendAtText(pid,chatroomid, rgvar.bstrVal, wxmsg,AutoNickName); } - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendAtTextRemoteAddr = hp.GetProcAddr(SendAtTextRemote); + if (SendAtTextRemoteAddr == 0) { return 1; } vector wxidptrs; - DWORD dwWriteSize = 0; - DWORD dwTId = 0; DWORD dwId = 0; SendAtTextStruct params = { 0 }; - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxidptrsaddr = VirtualAllocEx(hProcess, NULL, sizeof(void*) * cElements, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxmsgaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - SendAtTextStruct* paramAndFunc = (SendAtTextStruct*)::VirtualAllocEx(hProcess, 0, sizeof(SendAtTextStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !wxidptrsaddr || !wxmsgaddr || !paramAndFunc || !WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_wxmsg(hp.GetHandle(), wxmsg, TEXTLENGTH(wxmsg)); + params.wxmsg = (DWORD)r_wxmsg.GetAddr(); + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.AutoNickName = AutoNickName; for (long i = lLbound; i < lLbound + cElements; i++) { VariantInit(&rgvar); hr = SafeArrayGetElement(psaValue, &i, &rgvar); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); + LPVOID wxidaddr = WriteWeChatMemory(hp.GetHandle(), rgvar.bstrVal, TEXTLENGTH(rgvar.bstrVal)); if (wxidaddr) { - WriteProcessMemory(hProcess, wxidaddr, rgvar.bstrVal, wcslen(rgvar.bstrVal) * 2 + 2, &dwWriteSize); wxidptrs.push_back(wxidaddr); } } - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (wxidptrsaddr) - WriteProcessMemory(hProcess, wxidptrsaddr, &wxidptrs[0], wxidptrs.size() * sizeof(void*), &dwWriteSize); - - if (wxmsgaddr) - WriteProcessMemory(hProcess, wxmsgaddr, wxmsg, wcslen(wxmsg) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.wxid = (DWORD)wxidptrsaddr; - params.wxmsg = (DWORD)wxmsgaddr; + WeChatData r_wxids(hp.GetHandle(), &wxidptrs[0], wxidptrs.size() * sizeof(void*)); + params.wxid = (DWORD)r_wxids.GetAddr(); params.length = wxidptrs.size(); - params.AutoNickName = AutoNickName; - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(SendAtTextStruct), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - DWORD SendAtTextRemoteAddr = WeChatRobotBase + SendAtTextOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendAtTextRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - } - else { - CloseHandle(hProcess); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!params.chatroomid || !params.wxid || !params.wxmsg || !r_params.GetAddr()) { return 1; } - CloseHandle(hThread); + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendAtTextRemoteAddr, r_params.GetAddr()); for (unsigned int i = 0; i < wxidptrs.size(); i++) { - VirtualFreeEx(hProcess, wxidptrs[i], 0, MEM_RELEASE); + VirtualFreeEx(hp.GetHandle(), wxidptrs[i], 0, MEM_RELEASE); } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxmsgaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxidptrsaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); return 0; } \ No newline at end of file diff --git a/CWeChatRobot/SendCard.cpp b/CWeChatRobot/SendCard.cpp index 2ddbaa909d8822c4e34d44b77f9ec574ad0067b0..7c09da348d9e1324087ea8dbdddb0757d513a34d 100644 --- a/CWeChatRobot/SendCard.cpp +++ b/CWeChatRobot/SendCard.cpp @@ -7,50 +7,23 @@ struct SendCardStruct { }; BOOL SendCard(DWORD pid,wchar_t* receiver, wchar_t* sharedwxid, wchar_t* nickname) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - SendCardStruct params; - ZeroMemory(¶ms, sizeof(params)); - DWORD SendCardProcAddr = WeChatRobotBase + SendCardOffset; - LPVOID receiveraddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID sharedwxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID nicknameaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - SendCardStruct* paramAndFunc = (SendCardStruct*)::VirtualAllocEx(hProcess, 0, sizeof(SendCardStruct), MEM_COMMIT, PAGE_READWRITE); - if (!receiveraddr || !sharedwxidaddr || !nicknameaddr || - !paramAndFunc || !WeChatRobotBase) - { - CloseHandle(hProcess); - return 1; - } - if (receiveraddr) - WriteProcessMemory(hProcess, receiveraddr, receiver, wcslen(receiver) * 2 + 2, &dwWriteSize); - if (sharedwxidaddr) - WriteProcessMemory(hProcess, sharedwxidaddr, sharedwxid, wcslen(sharedwxid) * 2 + 2, &dwWriteSize); - if (nicknameaddr) - WriteProcessMemory(hProcess, nicknameaddr, nickname, wcslen(nickname) * 2 + 2, &dwWriteSize); - params.receiver = (DWORD)receiveraddr; - params.sharedwxid = (DWORD)sharedwxidaddr; - params.nickname = (DWORD)nicknameaddr; - - if (paramAndFunc) - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwId); - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendCardProcAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - CloseHandle(hThread); - } - VirtualFreeEx(hProcess, receiveraddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, sharedwxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, nicknameaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return 0; + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendCardRemoteAddr = hp.GetProcAddr(SendCardRemote); + if (SendCardRemoteAddr == 0) { + return 1; + } + SendCardStruct params = { 0 }; + WeChatData r_receiver(hp.GetHandle(), receiver, TEXTLENGTH(receiver)); + WeChatData r_sharedwxid(hp.GetHandle(), sharedwxid, TEXTLENGTH(sharedwxid)); + WeChatData r_nickname(hp.GetHandle(), nickname, TEXTLENGTH(nickname)); + params.receiver = (DWORD)r_receiver.GetAddr(); + params.sharedwxid = (DWORD)r_sharedwxid.GetAddr(); + params.nickname = (DWORD)r_nickname.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!params.receiver || !params.sharedwxid || !params.nickname || !r_params.GetAddr()) { + return 1; + } + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendCardRemoteAddr, r_params.GetAddr()); + return 0; } \ No newline at end of file diff --git a/CWeChatRobot/SendFile.cpp b/CWeChatRobot/SendFile.cpp index 7cf43a7606153c0428c801c89b3ebdce98ad4d66..a650195e0f13c355d6bc64f1876133fa722c715b 100644 --- a/CWeChatRobot/SendFile.cpp +++ b/CWeChatRobot/SendFile.cpp @@ -6,57 +6,22 @@ struct FileParamStruct { }; int SendFile(DWORD pid,wchar_t* wxid, wchar_t* filepath) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - FileParamStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID filepathaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - FileParamStruct* paramAndFunc = (FileParamStruct*)::VirtualAllocEx(hProcess, 0, sizeof(FileParamStruct), MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr || !filepathaddr || !paramAndFunc || !WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendFileRemoteAddr = hp.GetProcAddr(SendFileRemote); + if (SendFileRemoteAddr == 0) { return 1; } - DWORD dwTId = 0; - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - if (filepathaddr) - WriteProcessMemory(hProcess, filepathaddr, filepath, wcslen(filepath) * 2 + 2, &dwWriteSize); - - params.wxid = (DWORD)wxidaddr; - params.filepath = (DWORD)filepathaddr; + FileParamStruct params = { 0 }; + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_filepath(hp.GetHandle(), filepath, TEXTLENGTH(filepath)); - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD SendFileRemoteAddr = WeChatRobotBase + SendFileOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendFileRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - } - else { - CloseHandle(hProcess); + params.wxid = (DWORD)r_wxid.GetAddr(); + params.filepath = (DWORD)r_filepath.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!params.wxid || !params.filepath || !r_params.GetAddr()) { return 1; } - CloseHandle(hThread); - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, filepathaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendFileRemoteAddr, r_params.GetAddr()); return 0; } diff --git a/CWeChatRobot/SendImage.cpp b/CWeChatRobot/SendImage.cpp index 9f2614c636e1dfc6f0a7dfcd392bf1b898f890b4..73463899f8a5f98a0915d446446a76b6cc8fb0e9 100644 --- a/CWeChatRobot/SendImage.cpp +++ b/CWeChatRobot/SendImage.cpp @@ -6,57 +6,22 @@ struct ImageParamStruct { }; int SendImage(DWORD pid,wchar_t* wxid, wchar_t* imagepath) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - ImageParamStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID imagepathaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - ImageParamStruct* paramAndFunc = (ImageParamStruct*)::VirtualAllocEx(hProcess, 0, sizeof(ImageParamStruct), MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr || !imagepathaddr || !paramAndFunc || !WeChatRobotBase) { - CloseHandle(hProcess); + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendImageRemoteAddr = hp.GetProcAddr(SendImageRemote); + if (SendImageRemoteAddr == 0) { return 1; } - DWORD dwTId = 0; - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - if (imagepathaddr) - WriteProcessMemory(hProcess, imagepathaddr, imagepath, wcslen(imagepath) * 2 + 2, &dwWriteSize); - - params.wxid = (DWORD)wxidaddr; - params.imagepath = (DWORD)imagepathaddr; + ImageParamStruct params = { 0 }; + WeChatData r_wxid(hp.GetHandle(), wxid, TEXTLENGTH(wxid)); + WeChatData r_imagepath(hp.GetHandle(), imagepath, TEXTLENGTH(imagepath)); - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD SendImageRemoteAddr = WeChatRobotBase + SendImageOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendImageRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - } - else { - CloseHandle(hProcess); + params.wxid = (DWORD)r_wxid.GetAddr(); + params.imagepath = (DWORD)r_imagepath.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!params.wxid || !params.imagepath || !r_params.GetAddr()) { return 1; } - CloseHandle(hThread); - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, imagepathaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendImageRemoteAddr, r_params.GetAddr()); return 0; } diff --git a/CWeChatRobot/SendText.cpp b/CWeChatRobot/SendText.cpp index c0375d9368638c3b9c3fae28529732b1ea376f83..8075c2a6f5e8eda11a8e5fefbef268ba1f1f73c5 100644 --- a/CWeChatRobot/SendText.cpp +++ b/CWeChatRobot/SendText.cpp @@ -7,57 +7,21 @@ struct SendTextStruct }; int SendText(DWORD pid,wchar_t* wxid, wchar_t* wxmsg) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SendTextRemoteAddr = hp.GetProcAddr(SendTextRemote); + if (SendTextRemoteAddr == 0) { return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - SendTextStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID wxidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID wxmsgaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - SendTextStruct* paramAndFunc = (SendTextStruct*)::VirtualAllocEx(hProcess, 0, sizeof(SendTextStruct), MEM_COMMIT, PAGE_READWRITE); - if (!wxidaddr || !wxmsgaddr || !paramAndFunc || !WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD dwTId = 0; - - if (wxidaddr) - WriteProcessMemory(hProcess, wxidaddr, wxid, wcslen(wxid) * 2 + 2, &dwWriteSize); - - if (wxmsgaddr) - WriteProcessMemory(hProcess, wxmsgaddr, wxmsg, wcslen(wxmsg) * 2 + 2, &dwWriteSize); - - params.wxid = (DWORD)wxidaddr; - params.wxmsg = (DWORD)wxmsgaddr; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD SendTextRemoteAddr = WeChatRobotBase + SendTextOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SendTextRemoteAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); } - else { - CloseHandle(hProcess); + SendTextStruct params = { 0 }; + WeChatData r_wxid(hp.GetHandle(),wxid,TEXTLENGTH(wxid)); + WeChatData r_wxmsg(hp.GetHandle(), wxmsg, TEXTLENGTH(wxmsg)); + params.wxid = (DWORD)r_wxid.GetAddr(); + params.wxmsg = (DWORD)r_wxmsg.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (!params.wxid || !params.wxmsg || !r_params.GetAddr()) { return 1; } - CloseHandle(hThread); - VirtualFreeEx(hProcess, wxidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, wxmsgaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); + DWORD dwRet = CallRemoteFunction(hp.GetHandle(), SendTextRemoteAddr, r_params.GetAddr()); return 0; } \ No newline at end of file diff --git a/CWeChatRobot/SetChatRoomAnnouncement.cpp b/CWeChatRobot/SetChatRoomAnnouncement.cpp index bf2d0a8613b9710cb6fcd0389de8111bbcea034d..8ab4e6dd3f68b0b7379cf6ce4f5ed500291679bc 100644 --- a/CWeChatRobot/SetChatRoomAnnouncement.cpp +++ b/CWeChatRobot/SetChatRoomAnnouncement.cpp @@ -7,59 +7,19 @@ struct ChatRoomAnnouncementStruct }; BOOL SetChatRoomAnnouncement(DWORD pid,wchar_t* chatroomid, wchar_t* announcement) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SetChatRoomAnnouncementRemoteAddr = hp.GetProcAddr(SetChatRoomAnnouncementRemote); + if (SetChatRoomAnnouncementRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_announcement(hp.GetHandle(), announcement, TEXTLENGTH(announcement)); + ChatRoomAnnouncementStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.announcement = (DWORD)r_announcement.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 0; - ChatRoomAnnouncementStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID announcementaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - ChatRoomAnnouncementStruct* paramAndFunc = (ChatRoomAnnouncementStruct*)::VirtualAllocEx(hProcess, 0, sizeof(ChatRoomAnnouncementStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !announcementaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - DWORD dwTId = 0; - - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (announcement && announcementaddr) - WriteProcessMemory(hProcess, announcementaddr, announcement, wcslen(announcement) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.announcement = announcement ? (DWORD)announcementaddr : 0; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD SetChatRoomAnnouncementAddr = WeChatRobotBase + SetChatRoomAnnouncementRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SetChatRoomAnnouncementAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return 1; - } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, announcementaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), SetChatRoomAnnouncementRemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/SetChatRoomName.cpp b/CWeChatRobot/SetChatRoomName.cpp index 953ebdb1efd185603d7b5806cb6fa1225d5da71b..8e724813d436d297de33f6ba08219963a4c4a701 100644 --- a/CWeChatRobot/SetChatRoomName.cpp +++ b/CWeChatRobot/SetChatRoomName.cpp @@ -7,59 +7,19 @@ struct ChatRoomNameStruct }; BOOL SetChatRoomName(DWORD pid,wchar_t* chatroomid, wchar_t* name) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SetChatRoomNameRemoteAddr = hp.GetProcAddr(SetChatRoomNameRemote); + if (SetChatRoomNameRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_name(hp.GetHandle(), name, TEXTLENGTH(name)); + ChatRoomNameStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.name = (DWORD)r_name.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 0; - ChatRoomNameStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID nameaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - ChatRoomNameStruct* paramAndFunc = (ChatRoomNameStruct*)::VirtualAllocEx(hProcess, 0, sizeof(ChatRoomNameStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !nameaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - DWORD dwTId = 0; - - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (nameaddr) - WriteProcessMemory(hProcess, nameaddr, name, wcslen(name) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.name = (DWORD)nameaddr; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD SetChatRoomNameAddr = WeChatRobotBase + SetChatRoomNameRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SetChatRoomNameAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return 1; - } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, nameaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), SetChatRoomNameRemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/SetChatRoomSelfNickname.cpp b/CWeChatRobot/SetChatRoomSelfNickname.cpp index 712b9db7830bf5efbc5212e8d137c07aed9bb36c..5b15064120c592fa8c86f06ddeea612e83563ea6 100644 --- a/CWeChatRobot/SetChatRoomSelfNickname.cpp +++ b/CWeChatRobot/SetChatRoomSelfNickname.cpp @@ -7,59 +7,19 @@ struct ChatRoomSelfNicknameStruct }; BOOL SetChatRoomSelfNickname(DWORD pid,wchar_t* chatroomid, wchar_t* nickname) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD SetChatRoomSelfNicknameRemoteAddr = hp.GetProcAddr(SetChatRoomSelfNicknameRemote); + if (SetChatRoomSelfNicknameRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_chatroomid(hp.GetHandle(), chatroomid, TEXTLENGTH(chatroomid)); + WeChatData r_nickname(hp.GetHandle(), nickname, TEXTLENGTH(nickname)); + ChatRoomSelfNicknameStruct params = { 0 }; + params.chatroomid = (DWORD)r_chatroomid.GetAddr(); + params.nickname = (DWORD)r_nickname.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_chatroomid.GetAddr() == 0 || r_params.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwWriteSize = 0; - DWORD dwRet = 0; - ChatRoomSelfNicknameStruct params; - ZeroMemory(¶ms, sizeof(params)); - LPVOID chatroomidaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID nicknameaddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - ChatRoomSelfNicknameStruct* paramAndFunc = (ChatRoomSelfNicknameStruct*)::VirtualAllocEx(hProcess, 0, sizeof(ChatRoomSelfNicknameStruct), MEM_COMMIT, PAGE_READWRITE); - if (!chatroomidaddr || !nicknameaddr || !paramAndFunc) { - CloseHandle(hProcess); - return 1; - } - DWORD dwTId = 0; - - if (chatroomidaddr) - WriteProcessMemory(hProcess, chatroomidaddr, chatroomid, wcslen(chatroomid) * 2 + 2, &dwWriteSize); - - if (nicknameaddr) - WriteProcessMemory(hProcess, nicknameaddr, nickname, wcslen(nickname) * 2 + 2, &dwWriteSize); - - params.chatroomid = (DWORD)chatroomidaddr; - params.nickname = (DWORD)nicknameaddr; - - if (paramAndFunc) { - WriteProcessMemory(hProcess, paramAndFunc, ¶ms, sizeof(params), &dwTId); - } - else { - CloseHandle(hProcess); - return 1; - } - - DWORD SetChatRoomSelfNicknameAddr = WeChatRobotBase + SetChatRoomSelfNicknameRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)SetChatRoomSelfNicknameAddr, (LPVOID)paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - else { - CloseHandle(hProcess); - return 1; - } - VirtualFreeEx(hProcess, chatroomidaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, nicknameaddr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), SetChatRoomSelfNicknameRemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/VerifyFriendApply.cpp b/CWeChatRobot/VerifyFriendApply.cpp index 7067697edf3324ef30cdf26dc704434a1f8ca48a..858fa5a3923a7d1190f787c7dbaedce840bacf79 100644 --- a/CWeChatRobot/VerifyFriendApply.cpp +++ b/CWeChatRobot/VerifyFriendApply.cpp @@ -6,47 +6,19 @@ struct VerifyFriendApplyStruct { }; BOOL VerifyFriendApply(DWORD pid,wchar_t* v3,wchar_t* v4) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); - return 1; - } - DWORD VerifyFriendApplyProcAddr = WeChatRobotBase + VerifyFriendApplyOffset; - LPVOID v3addr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - LPVOID v4addr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - DWORD dwId = 0; - DWORD dwHandle = 0; - VerifyFriendApplyStruct apply_data = { 0 }; - if (!v3addr || !v4addr) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, v3addr, v3, wcslen(v3) * 2 + 2, &dwWriteSize); - WriteProcessMemory(hProcess, v4addr, v4, wcslen(v4) * 2 + 2, &dwWriteSize); - VerifyFriendApplyStruct* paramAndFunc = (VerifyFriendApplyStruct*)::VirtualAllocEx(hProcess, 0, sizeof(VerifyFriendApplyStruct), MEM_COMMIT, PAGE_READWRITE); - apply_data.v3 = (DWORD)v3addr; - apply_data.v4 = (DWORD)v4addr; - - if (paramAndFunc) - WriteProcessMemory(hProcess, paramAndFunc, &apply_data, sizeof(apply_data), &dwId); - else { - CloseHandle(hProcess); - return 1; - } - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)VerifyFriendApplyProcAddr, paramAndFunc, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - CloseHandle(hThread); - } - - VirtualFreeEx(hProcess, v3addr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, v4addr, 0, MEM_RELEASE); - VirtualFreeEx(hProcess, paramAndFunc, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwHandle == 0; + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD VerifyFriendApplyRemoteAddr = hp.GetProcAddr(VerifyFriendApplyRemote); + if (VerifyFriendApplyRemoteAddr == 0) + return 1; + WeChatData r_v3(hp.GetHandle(), v3, TEXTLENGTH(v3)); + WeChatData r_v4(hp.GetHandle(), v4, TEXTLENGTH(v4)); + VerifyFriendApplyStruct params = { 0 }; + params.v3 = (DWORD)r_v3.GetAddr(); + params.v4 = (DWORD)r_v4.GetAddr(); + WeChatData r_params(hp.GetHandle(), ¶ms, sizeof(params)); + if (r_v3.GetAddr() == 0 || r_v4.GetAddr() == 0 || r_params.GetAddr() == 0) + return 1; + DWORD ret = CallRemoteFunction(hp.GetHandle(), VerifyFriendApplyRemoteAddr, r_params.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/CWeChatRobot/WeChatRobot.cpp b/CWeChatRobot/WeChatRobot.cpp index 8930c0f28e45d2b3d1af21c3084881b7a56e97cd..d297f42e3dd192d5630ae70cb7b1ece2d36f7e35 100644 --- a/CWeChatRobot/WeChatRobot.cpp +++ b/CWeChatRobot/WeChatRobot.cpp @@ -120,8 +120,8 @@ STDMETHODIMP CWeChatRobot::CGetFriendList(DWORD pid, VARIANT* __result) { 锛堣冭檻鍒颁粠SAFEARRAY杞崲鍒伴傚綋鍙橀噺鍙兘杈冧负绻佺悙锛屾晠淇濈暀姝ゆ帴鍙o級 */ STDMETHODIMP CWeChatRobot::CGetFriendListString(DWORD pid, BSTR* __result) { - string smessage = _com_util::ConvertBSTRToString((BSTR)(GetFriendListString(pid).c_str())); - *__result = _com_util::ConvertStringToBSTR(smessage.c_str()); + wstring info = GetFriendListString(pid); + *__result = (_bstr_t)info.c_str(); return S_OK; } @@ -131,8 +131,8 @@ STDMETHODIMP CWeChatRobot::CGetFriendListString(DWORD pid, BSTR* __result) { * 鍙傛暟2锛氶杩斿洖鐨勫硷紝璋冪敤鏃舵棤闇鎻愪緵 */ STDMETHODIMP CWeChatRobot::CGetWxUserInfo(DWORD pid, BSTR wxid,BSTR* __result) { - string smessage = _com_util::ConvertBSTRToString((BSTR)(GetWxUserInfo(pid, wxid).c_str())); - *__result = _com_util::ConvertStringToBSTR(smessage.c_str()); + wstring info = GetWxUserInfo(pid, wxid); + *__result = (_bstr_t)info.c_str(); return S_OK; } @@ -141,8 +141,8 @@ STDMETHODIMP CWeChatRobot::CGetWxUserInfo(DWORD pid, BSTR wxid,BSTR* __result) { * 鍙傛暟1锛氶杩斿洖鐨勫硷紝璋冪敤鏃舵棤闇鎻愪緵 */ STDMETHODIMP CWeChatRobot::CGetSelfInfo(DWORD pid, BSTR* __result) { - string smessage = _com_util::ConvertBSTRToString((BSTR)(GetSelfInfo(pid).c_str())); - *__result = _com_util::ConvertStringToBSTR(smessage.c_str()); + wstring info = GetSelfInfo(pid); + *__result = (_bstr_t)info.c_str(); return S_OK; } @@ -160,8 +160,8 @@ STDMETHODIMP CWeChatRobot::CCheckFriendStatus(DWORD pid, BSTR wxid,int* __result * 鍙傛暟1锛氶杩斿洖鐨勫硷紝璋冪敤鏃舵棤闇鎻愪緵 */ STDMETHODIMP CWeChatRobot::CGetComWorkPath(BSTR* __result) { - string path = _com_util::ConvertBSTRToString((BSTR)(GetComWorkPath().c_str())); - *__result = _com_util::ConvertStringToBSTR(path.c_str()); + wstring path = GetComWorkPath(); + *__result = (_bstr_t)path.c_str(); return S_OK; } @@ -272,8 +272,8 @@ STDMETHODIMP CWeChatRobot::CAddFriendByV3(DWORD pid, BSTR v3, BSTR message,int A * 鍙傛暟1锛氶杩斿洖鐨勫硷紝璋冪敤鏃舵棤闇鎻愪緵 */ STDMETHODIMP CWeChatRobot::CGetWeChatVer(BSTR* __result) { - string path = _com_util::ConvertBSTRToString((BSTR)(GetWeChatVerStr().c_str())); - *__result = _com_util::ConvertStringToBSTR(path.c_str()); + wstring path = GetWeChatVerStr(); + *__result = (_bstr_t)path.c_str(); return S_OK; } diff --git a/CWeChatRobot/WeChatRobotCOM.vcxproj b/CWeChatRobot/WeChatRobotCOM.vcxproj index 45038b225912c65d231e8717559cf3cee211f6de..c0485bb5c1f469d83477610568e04595755e4678 100644 --- a/CWeChatRobot/WeChatRobotCOM.vcxproj +++ b/CWeChatRobot/WeChatRobotCOM.vcxproj @@ -244,6 +244,7 @@ + @@ -291,6 +292,7 @@ + diff --git a/CWeChatRobot/WeChatRobotCOM.vcxproj.filters b/CWeChatRobot/WeChatRobotCOM.vcxproj.filters index 42d6a47adb54f29e3b915303c1358aaeee860169..305424c6f695f5749d9a8d5c3547f6a90a7b09ca 100644 --- a/CWeChatRobot/WeChatRobotCOM.vcxproj.filters +++ b/CWeChatRobot/WeChatRobotCOM.vcxproj.filters @@ -113,6 +113,9 @@ {dce4ab67-7d14-41b1-8e89-cbf9a8315a3a} + + {fdd967bf-e9c0-4793-80a1-dcb87b061fc6} + @@ -232,6 +235,9 @@ 澶存枃浠 + + template + @@ -354,6 +360,9 @@ 婧愭枃浠 + + template + diff --git a/CWeChatRobot/pch.cpp b/CWeChatRobot/pch.cpp index f8794de2256f9016d67ae24fcf2971d64535e3be..afc314a57b1dae3c7bf901fa1c60335de7faba36 100644 --- a/CWeChatRobot/pch.cpp +++ b/CWeChatRobot/pch.cpp @@ -1,62 +1,8 @@ -// pch.cpp: 与预编译标头对应的源文件 +锘// pch.cpp: 涓庨缂栬瘧鏍囧ご瀵瑰簲鐨勬簮鏂囦欢 #include "pch.h" -// 当使用预编译的头时,需要使用此源文件,编译才能成功。 -DWORD SendImageOffset = 0x0; -DWORD SendTextOffset = 0x0; -DWORD SendFileOffset = 0x0; -DWORD SendArticleOffset = 0x0; -DWORD SendCardOffset = 0x0; -DWORD SendAtTextOffset = 0x0; -DWORD SendAppMsgRemoteOffset = 0x0; - -DWORD GetFriendListInitOffset = 0x0; -DWORD GetFriendListRemoteOffset = 0x0; -DWORD GetFriendListFinishOffset = 0x0; - -DWORD EditRemarkRemoteOffset = 0x0; -DWORD GetWxUserInfoOffset = 0x0; -DWORD DeleteUserInfoCacheOffset = 0x0; - -DWORD GetSelfInfoOffset = 0x0; -DWORD DeleteSelfInfoCacheOffset = 0x0; -DWORD SearchContactByNetRemoteOffset = 0x0; -DWORD isWxLoginOffset = 0; - -DWORD VerifyFriendApplyOffset = 0x0; - -DWORD CheckFriendStatusRemoteOffset = 0x0; - -DWORD HookReceiveMessageRemoteOffset = 0x0; -DWORD UnHookReceiveMessageRemoteOffset = 0x0; - -DWORD GetChatRoomMemberNicknameRemoteOffset = 0x0; -DWORD GetChatRoomMembersRemoteOffset = 0x0; -DWORD DelChatRoomMemberRemoteOffset = 0x0; -DWORD AddChatRoomMemberRemoteOffset = 0x0; -DWORD SetChatRoomAnnouncementRemoteOffset = 0x0; -DWORD SetChatRoomNameRemoteOffset = 0x0; -DWORD SetChatRoomSelfNicknameRemoteOffset = 0x0; - -DWORD GetDbHandlesRemoteOffset = 0x0; -DWORD ExecuteSQLRemoteOffset = 0x0; -DWORD SelectDataRemoteOffset = 0x0; -DWORD BackupSQLiteDBRemoteOffset = 0x0; - -DWORD AddFriendByWxidRemoteOffset = 0x0; -DWORD AddFriendByV3RemoteOffset = 0x0; -DWORD DeleteUserRemoteOffset = 0x0; -DWORD AddBrandContactRemoteOffset = 0x0; - -DWORD HookImageMsgRemoteOffset = 0x0; -DWORD UnHookImageMsgRemoteOffset = 0x0; -DWORD HookVoiceMsgRemoteOffset = 0x0; -DWORD UnHookVoiceMsgRemoteOffset = 0x0; - -DWORD ChangeWeChatVerRemoteOffset = 0x0; - -map PidToSelfInfoString; +// 褰撲娇鐢ㄩ缂栬瘧鐨勫ご鏃讹紝闇瑕佷娇鐢ㄦ婧愭枃浠讹紝缂栬瘧鎵嶈兘鎴愬姛銆 BOOL isFileExists_stat(string& name) { struct stat buffer; @@ -77,176 +23,32 @@ BOOL CreateConsole() { } DWORD GetWeChatRobotBase(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 0; + WeChatData r_dllname(hp.GetHandle(), dllname, TEXTLENGTH(dllname)); + if (r_dllname.GetAddr() == 0) return 0; - DWORD dwWriteSize = 0; - LPVOID pRemoteAddress = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - if (pRemoteAddress) - WriteProcessMemory(hProcess, pRemoteAddress, dllname, wcslen(dllname) * 2 + 2, &dwWriteSize); - else - return 0; - DWORD dwHandle, dwID; - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetModuleHandleW, pRemoteAddress, 0, &dwID); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - } - else { - return 0; - } - CloseHandle(hThread); - VirtualFreeEx(hProcess, pRemoteAddress, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwHandle; + DWORD ret = CallRemoteFunction(hp.GetHandle(), GetModuleHandleW, r_dllname.GetAddr()); + return ret; } DWORD GetWeChatWinBase(DWORD pid) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) - return 0; - DWORD dwWriteSize = 0; - LPVOID pRemoteAddress = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - if (pRemoteAddress) - WriteProcessMemory(hProcess, pRemoteAddress, L"WeChatWin.dll", wcslen(L"WeChatWin.dll") * 2 + 2, &dwWriteSize); - else - return 0; - DWORD dwHandle, dwID; - HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetModuleHandleW, pRemoteAddress, 0, &dwID); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwHandle); - } - else { - return 0; - } - CloseHandle(hThread); - VirtualFreeEx(hProcess, pRemoteAddress, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwHandle; -} - -BOOL GetProcOffset(wchar_t* workPath) { - wchar_t* dllpath = new wchar_t[MAX_PATH]; - memset(dllpath, 0, MAX_PATH * 2); - swprintf_s(dllpath, MAX_PATH, L"%ws%ws%ws", workPath, L"\\", dllname); - string name = _com_util::ConvertBSTRToString((BSTR)dllpath); - if (!isFileExists_stat(name)) { - MessageBoxA(NULL, name.c_str(), "文件不存在", MB_ICONWARNING); - return 0; - } - HMODULE hd = LoadLibrary(dllpath); - if (!hd) { + wchar_t* WeChatWin = L"WeChatWin.dll"; + WeChatProcess hp(pid); + if (!hp.m_init) return 0; + WeChatData r_dllname(hp.GetHandle(), WeChatWin, TEXTLENGTH(WeChatWin)); + if (r_dllname.GetAddr() == 0) return 0; - } - DWORD WeChatBase = (DWORD)GetModuleHandleW(dllname); - DWORD SendImageProcAddr = (DWORD)GetProcAddress(hd, SendImageRemote); - SendImageOffset = SendImageProcAddr - WeChatBase; - DWORD SendTextProcAddr = (DWORD)GetProcAddress(hd, SendTextRemote); - SendTextOffset = SendTextProcAddr - WeChatBase; - DWORD SendFileProcAddr = (DWORD)GetProcAddress(hd, SendFileRemote); - SendFileOffset = SendFileProcAddr - WeChatBase; - DWORD SendArticleProcAddr = (DWORD)GetProcAddress(hd, SendArticleRemote); - SendArticleOffset = SendArticleProcAddr - WeChatBase; - DWORD SendCardProcAddr = (DWORD)GetProcAddress(hd, SendCardRemote); - SendCardOffset = SendCardProcAddr - WeChatBase; - DWORD SendAtTextProcAddr = (DWORD)GetProcAddress(hd, SendAtTextRemote); - SendAtTextOffset = SendAtTextProcAddr - WeChatBase; - DWORD SendAppMsgProcAddr = (DWORD)GetProcAddress(hd, SendAppMsgRemote); - SendAppMsgRemoteOffset = SendAppMsgProcAddr - WeChatBase; - - DWORD GetFriendListInitProcAddr = (DWORD)GetProcAddress(hd, GetFriendListInit); - GetFriendListInitOffset = GetFriendListInitProcAddr - WeChatBase; - DWORD GetFriendListRemoteProcAddr = (DWORD)GetProcAddress(hd, GetFriendListRemote); - GetFriendListRemoteOffset = GetFriendListRemoteProcAddr - WeChatBase; - DWORD GetFriendListFinishProcAddr = (DWORD)GetProcAddress(hd, GetFriendListFinish); - GetFriendListFinishOffset = GetFriendListFinishProcAddr - WeChatBase; - - DWORD EditRemarkRemoteAddr = (DWORD)GetProcAddress(hd, EditRemarkRemote); - EditRemarkRemoteOffset = EditRemarkRemoteAddr - WeChatBase; - DWORD GetWxUserInfoProcAddr = (DWORD)GetProcAddress(hd, GetWxUserInfoRemote); - GetWxUserInfoOffset = GetWxUserInfoProcAddr - WeChatBase; - DWORD DeleteUserInfoCacheProcAddr = (DWORD)GetProcAddress(hd, DeleteUserInfoCacheRemote); - DeleteUserInfoCacheOffset = DeleteUserInfoCacheProcAddr - WeChatBase; - - DWORD VerifyFriendApplyProcAddr = (DWORD)GetProcAddress(hd, VerifyFriendApplyRemote); - VerifyFriendApplyOffset = VerifyFriendApplyProcAddr - WeChatBase; - - DWORD GetSelfInfoProcAddr = (DWORD)GetProcAddress(hd, GetSelfInfoRemote); - GetSelfInfoOffset = GetSelfInfoProcAddr - WeChatBase; - DWORD DeleteSelfInfoCacheProcAddr = (DWORD)GetProcAddress(hd, DeleteSelfInfoCacheRemote); - DeleteSelfInfoCacheOffset = DeleteSelfInfoCacheProcAddr - WeChatBase; - DWORD SearchContactByNetRemoteAddr = (DWORD)GetProcAddress(hd, SearchContactByNetRemote); - SearchContactByNetRemoteOffset = SearchContactByNetRemoteAddr - WeChatBase; - DWORD isWxLoginAddr = (DWORD)GetProcAddress(hd, isWxLoginRemote); - isWxLoginOffset = isWxLoginAddr - WeChatBase; - - DWORD CheckFriendStatusRemoteAddr = (DWORD)GetProcAddress(hd, CheckFriendStatusRemote); - CheckFriendStatusRemoteOffset = CheckFriendStatusRemoteAddr - WeChatBase; - - DWORD HookReceiveMessageRemoteAddr = (DWORD)GetProcAddress(hd, HookReceiveMessageRemote); - HookReceiveMessageRemoteOffset = HookReceiveMessageRemoteAddr - WeChatBase; - DWORD UnHookReceiveMessageRemoteAddr = (DWORD)GetProcAddress(hd, UnHookReceiveMessageRemote); - UnHookReceiveMessageRemoteOffset = UnHookReceiveMessageRemoteAddr - WeChatBase; - - DWORD GetChatRoomMemberNicknameRemoteAddr = (DWORD)GetProcAddress(hd, GetChatRoomMemberNicknameRemote); - GetChatRoomMemberNicknameRemoteOffset = GetChatRoomMemberNicknameRemoteAddr - WeChatBase; - DWORD GetChatRoomMembersRemoteAddr = (DWORD)GetProcAddress(hd, GetChatRoomMembersRemote); - GetChatRoomMembersRemoteOffset = GetChatRoomMembersRemoteAddr - WeChatBase; - - DWORD DelChatRoomMemberRemoteAddr = (DWORD)GetProcAddress(hd, DelChatRoomMemberRemote); - DelChatRoomMemberRemoteOffset = DelChatRoomMemberRemoteAddr - WeChatBase; - DWORD AddChatRoomMemberRemoteAddr = (DWORD)GetProcAddress(hd, AddChatRoomMemberRemote); - AddChatRoomMemberRemoteOffset = AddChatRoomMemberRemoteAddr - WeChatBase; - DWORD SetChatRoomAnnouncementRemoteAddr = (DWORD)GetProcAddress(hd, SetChatRoomAnnouncementRemote); - SetChatRoomAnnouncementRemoteOffset = SetChatRoomAnnouncementRemoteAddr - WeChatBase; - DWORD SetChatRoomNameRemoteAddr = (DWORD)GetProcAddress(hd, SetChatRoomNameRemote); - SetChatRoomNameRemoteOffset = SetChatRoomNameRemoteAddr - WeChatBase; - DWORD SetChatRoomSelfNicknameRemoteAddr = (DWORD)GetProcAddress(hd, SetChatRoomSelfNicknameRemote); - SetChatRoomSelfNicknameRemoteOffset = SetChatRoomSelfNicknameRemoteAddr - WeChatBase; - - DWORD GetDbHandlesRemoteAddr = (DWORD)GetProcAddress(hd, GetDbHandlesRemote); - GetDbHandlesRemoteOffset = GetDbHandlesRemoteAddr - WeChatBase; - DWORD ExecuteSQLRemoteAddr = (DWORD)GetProcAddress(hd, ExecuteSQLRemote); - ExecuteSQLRemoteOffset = ExecuteSQLRemoteAddr - WeChatBase; - DWORD SelectDataRemoteAddr = (DWORD)GetProcAddress(hd, SelectDataRemote); - SelectDataRemoteOffset = SelectDataRemoteAddr - WeChatBase; - DWORD BackupSQLiteDBRemoteAddr = (DWORD)GetProcAddress(hd, BackupSQLiteDBRemote); - BackupSQLiteDBRemoteOffset = BackupSQLiteDBRemoteAddr - WeChatBase; - - DWORD AddFriendByWxidRemoteAddr = (DWORD)GetProcAddress(hd, AddFriendByWxidRemote); - AddFriendByWxidRemoteOffset = AddFriendByWxidRemoteAddr - WeChatBase; - DWORD AddFriendByV3RemoteAddr = (DWORD)GetProcAddress(hd, AddFriendByV3Remote); - AddFriendByV3RemoteOffset = AddFriendByV3RemoteAddr - WeChatBase; - DWORD DeleteUserRemoteAddr = (DWORD)GetProcAddress(hd, DeleteUserRemote); - DeleteUserRemoteOffset = DeleteUserRemoteAddr - WeChatBase; - DWORD AddBrandContactRemoteAddr = (DWORD)GetProcAddress(hd, AddBrandContactRemote); - AddBrandContactRemoteOffset = AddBrandContactRemoteAddr - WeChatBase; - - DWORD HookImageMsgRemoteAddr = (DWORD)GetProcAddress(hd, HookImageMsgRemote); - HookImageMsgRemoteOffset = HookImageMsgRemoteAddr - WeChatBase; - DWORD UnHookImageMsgAddr = (DWORD)GetProcAddress(hd, UnHookImageMsgRemote); - UnHookImageMsgRemoteOffset = UnHookImageMsgAddr - WeChatBase; - DWORD HookVoiceMsgRemoteAddr = (DWORD)GetProcAddress(hd, HookVoiceMsgRemote); - HookVoiceMsgRemoteOffset = HookVoiceMsgRemoteAddr - WeChatBase; - DWORD UnHookVoiceMsgAddr = (DWORD)GetProcAddress(hd, UnHookVoiceMsgRemote); - UnHookVoiceMsgRemoteOffset = UnHookVoiceMsgAddr - WeChatBase; - - DWORD ChangeWeChatVerRemoteAddr = (DWORD)GetProcAddress(hd, ChangeWeChatVerRemote); - ChangeWeChatVerRemoteOffset = ChangeWeChatVerRemoteAddr - WeChatBase; - - FreeLibrary(hd); - delete[] dllpath; - dllpath = NULL; - return 1; + DWORD ret = CallRemoteFunction(hp.GetHandle(), GetModuleHandleW, r_dllname.GetAddr()); + return ret; } DWORD GetWeChatPid() { - HWND hCalc = FindWindow(NULL, L"微信"); + HWND hCalc = FindWindow(NULL, L"寰俊"); DWORD wxPid = 0; GetWindowThreadProcessId(hCalc, &wxPid); if (wxPid == 0) { - hCalc = FindWindow(NULL, L"微信测试版"); + hCalc = FindWindow(NULL, L"寰俊娴嬭瘯鐗"); GetWindowThreadProcessId(hCalc, &wxPid); } return wxPid; @@ -255,12 +57,6 @@ DWORD GetWeChatPid() { DWORD StartRobotService(DWORD pid) { wstring wworkPath = GetComWorkPath(); wchar_t* workPath = (wchar_t*)wworkPath.c_str(); - if (!GetProcOffset(workPath)) { - wchar_t info[200] = { 0 }; - swprintf_s(info, 200, L"COM无法加载位于%ws的%ws!", workPath, dllname); - MessageBox(NULL, info, L"致命错误!", MB_ICONWARNING); - return 1; - }; bool status = Inject(pid, workPath); return status; } @@ -270,7 +66,6 @@ DWORD StopRobotService(DWORD pid) { if (pid == 0) return cpid; RemoveDll(pid); - PidToSelfInfoString.erase(pid); return 0; } @@ -279,7 +74,7 @@ wstring GetComWorkPath() { GetModuleFileName(NULL, szFilePath, MAX_PATH); wstring wpath = szFilePath; int pos = wpath.find_last_of(L"\\"); - wpath = wpath.substr(0, pos); + wpath = wpath.substr(0,pos); return wpath; } @@ -324,7 +119,7 @@ tstring GetWeChatVerStr() { return verStr; } -static bool CloseAllWxProcessMutexHandle() +static bool CloseAllWxProcessMutexHandle() { HANDLE hsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hsnapshot == INVALID_HANDLE_VALUE) @@ -370,4 +165,57 @@ DWORD StartWeChat() Sleep(500); } return procStruct.dwProcessId; +} + +DWORD GetRemoteProcAddr(DWORD pid, LPWSTR modulename, LPSTR procname) { + HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); + DWORD dwId = 0, dwProcAddr = 0; + unsigned char getremoteprocasmcode[] = { + 0x55, // push ebp; + 0x8B,0xEC, // mov ebp, esp; + 0x83,0xEC,0x40, // sub esp, 0x40; + 0x57, // push edi; + 0x51, // push ecx; + 0x8B,0x7D,0x08, // mov edi, dword ptr[ebp + 0x8]; + 0x8B,0x07, // mov eax,dword ptr[edi]; + 0x50, // push eax; + 0xE8,0x00,0x00,0x00,0x00, // call GetModuleHandleW; + 0x83,0xC4,0x04, // add esp,0x4; + 0x83,0xC7,0x04, // add edi,0x4; + 0x8B,0x0F, // mov ecx, dword ptr[edi]; + 0x51, // push ecx; + 0x50, // push eax; + 0xE8,0x00,0x00,0x00,0x00, // call GetProcAddress; + 0x83,0xC4,0x08, // add esp, 0x8; + 0x59, // pop ecx; + 0x5F, // pop edi; + 0x8B,0xE5, // mov esp, ebp; + 0x5D, // pop ebp; + 0xC3 // retn; + }; + DWORD pGetModuleHandleW = (DWORD)GetModuleHandleW; + DWORD pGetProcAddress = (DWORD)GetProcAddress; + PVOID call1 = (PVOID)&getremoteprocasmcode[15]; + PVOID call2 = (PVOID)&getremoteprocasmcode[30]; + LPVOID pRemoteAddress = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_EXECUTE); + if (!pRemoteAddress) + return 0; + *(DWORD*)call1 = pGetModuleHandleW - (DWORD)pRemoteAddress - 14 - 5; + *(DWORD*)call2 = pGetProcAddress - (DWORD)pRemoteAddress - 29 - 5; + SIZE_T dwWriteSize; + WriteProcessMemory(hProcess, pRemoteAddress, getremoteprocasmcode, sizeof(getremoteprocasmcode), &dwWriteSize); + struct GetProcAddrStruct { + DWORD hModuleNameAddr; + DWORD funcnameAddr; + } params; + WeChatData r_modulename(hProcess, modulename, TEXTLENGTH(modulename)); + WeChatData r_procname(hProcess, procname, TEXTLENGTHA(procname)); + params.funcnameAddr = (DWORD)r_procname.GetAddr(); + params.hModuleNameAddr = (DWORD)r_modulename.GetAddr(); + WeChatData r_params(hProcess, ¶ms, sizeof(params)); + if (r_modulename.GetAddr() == 0 || r_procname.GetAddr() == 0 || r_params.GetAddr() == 0) + return 0; + DWORD ret = CallRemoteFunction(hProcess, pRemoteAddress, r_params.GetAddr()); + VirtualFreeEx(hProcess, pRemoteAddress, 0, MEM_RELEASE); + return ret; } \ No newline at end of file diff --git a/CWeChatRobot/pch.h b/CWeChatRobot/pch.h index a0f9cb31af51c90746bcae3f7b348336376b5070..7472bbd3302199c61c7e4e957cf9fc27069aa8aa 100644 --- a/CWeChatRobot/pch.h +++ b/CWeChatRobot/pch.h @@ -15,26 +15,31 @@ #include "stdlib.h" #include #include -#include +#include #include #include #include #include #include #include -#include #include #pragma comment(lib, "comsuppw.lib") #include "robotdata.h" +#include "templatefunc.h" using namespace std; +#define TEXTLENGTHW(buffer) buffer ? (wcslen(buffer) * 2 + 2) : 0 +#define TEXTLENGTHA(buffer) buffer ? (strlen(buffer) + 1) : 0 + #ifdef _UNICODE #define tstring std::wstring +#define TEXTLENGTH TEXTLENGTHW #else #define tstring std::string +#define TEXTLENGTH TEXTLENGTHW #endif BOOL isFileExists_stat(string& name); @@ -51,4 +56,5 @@ tstring GetWeChatInstallDir(); DWORD GetWeChatVerInt(); tstring GetWeChatVerStr(); DWORD StartWeChat(); -BOOL CloseProcessHandle(DWORD pid, wchar_t* handlename); \ No newline at end of file +BOOL CloseProcessHandle(DWORD pid, wchar_t* handlename); +DWORD GetRemoteProcAddr(DWORD pid, LPWSTR modulename, LPSTR procname); \ No newline at end of file diff --git a/CWeChatRobot/robotdata.h b/CWeChatRobot/robotdata.h index aa15eb42256d51b7f4ad79ad91826b4af5dea413..3965c994ea08314500928fdc408123b4f700bcf3 100644 --- a/CWeChatRobot/robotdata.h +++ b/CWeChatRobot/robotdata.h @@ -28,62 +28,6 @@ #include "DelChatRoomMember.h" #include "AddChatRoomMember.h" -// extern HANDLE hProcess; -extern DWORD SendImageOffset; -extern DWORD SendTextOffset; -extern DWORD SendFileOffset; -extern DWORD SendArticleOffset; -extern DWORD SendCardOffset; -extern DWORD SendAtTextOffset; -extern DWORD SendAppMsgRemoteOffset; - -extern DWORD GetFriendListInitOffset; -extern DWORD GetFriendListRemoteOffset; -extern DWORD GetFriendListFinishOffset; - -extern DWORD EditRemarkRemoteOffset; -extern DWORD GetWxUserInfoOffset; -extern DWORD DeleteUserInfoCacheOffset; -extern DWORD SearchContactByNetRemoteOffset; - -extern DWORD VerifyFriendApplyOffset; - -extern DWORD GetSelfInfoOffset; -extern DWORD DeleteSelfInfoCacheOffset; -extern map PidToSelfInfoString; -extern DWORD isWxLoginOffset; - -extern DWORD CheckFriendStatusRemoteOffset; - -extern DWORD HookReceiveMessageRemoteOffset; -extern DWORD UnHookReceiveMessageRemoteOffset; - -extern DWORD GetChatRoomMemberNicknameRemoteOffset; -extern DWORD GetChatRoomMembersRemoteOffset; -extern DWORD DelChatRoomMemberRemoteOffset; -extern DWORD AddChatRoomMemberRemoteOffset; -extern DWORD SetChatRoomAnnouncementRemoteOffset; -extern DWORD SetChatRoomNameRemoteOffset; -extern DWORD SetChatRoomSelfNicknameRemoteOffset; - -extern DWORD GetDbHandlesRemoteOffset; -extern DWORD ExecuteSQLRemoteOffset; -extern DWORD SelectDataRemoteOffset; -extern DWORD BackupSQLiteDBRemoteOffset; - -extern DWORD AddFriendByWxidRemoteOffset; -extern DWORD AddFriendByV3RemoteOffset; -extern DWORD DeleteUserRemoteOffset; -extern DWORD AddBrandContactRemoteOffset; - -extern DWORD HookImageMsgRemoteOffset; -extern DWORD UnHookImageMsgRemoteOffset; -extern DWORD HookVoiceMsgRemoteOffset; -extern DWORD UnHookVoiceMsgRemoteOffset; - -extern DWORD ChangeWeChatVerRemoteOffset; - - #define dllname L"DWeChatRobot.dll" #define SendTextRemote "SendTextRemote" diff --git a/CWeChatRobot/templatefunc.cpp b/CWeChatRobot/templatefunc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97bc864c1122dd3f3d7da9816a0dabffad5fa284 --- /dev/null +++ b/CWeChatRobot/templatefunc.cpp @@ -0,0 +1,63 @@ +#include "pch.h" + +static unsigned char GetProcAsmCode[] = { + 0x55, // push ebp; + 0x8B,0xEC, // mov ebp, esp; + 0x83,0xEC,0x40, // sub esp, 0x40; + 0x57, // push edi; + 0x51, // push ecx; + 0x8B,0x7D,0x08, // mov edi, dword ptr[ebp + 0x8]; + 0x8B,0x07, // mov eax,dword ptr[edi]; + 0x50, // push eax; + 0xE8,0x00,0x00,0x00,0x00, // call GetModuleHandleW; + 0x83,0xC4,0x04, // add esp,0x4; + 0x83,0xC7,0x04, // add edi,0x4; + 0x8B,0x0F, // mov ecx, dword ptr[edi]; + 0x51, // push ecx; + 0x50, // push eax; + 0xE8,0x00,0x00,0x00,0x00, // call GetProcAddress; + 0x83,0xC4,0x08, // add esp, 0x8; + 0x59, // pop ecx; + 0x5F, // pop edi; + 0x8B,0xE5, // mov esp, ebp; + 0x5D, // pop ebp; + 0xC3 // retn; +}; + +LPVOID WeChatProcess::GetAsmFunAddr() { + DWORD pGetModuleHandleW = (DWORD)GetModuleHandleW; + DWORD pGetProcAddress = (DWORD)GetProcAddress; + PVOID call1 = (PVOID)&GetProcAsmCode[15]; + PVOID call2 = (PVOID)&GetProcAsmCode[30]; + LPVOID pAsmFuncAddr = VirtualAllocEx(handle, NULL, 1, MEM_COMMIT, PAGE_EXECUTE); + if (!pAsmFuncAddr) + return 0; + *(DWORD*)call1 = pGetModuleHandleW - (DWORD)pAsmFuncAddr - 14 - 5; + *(DWORD*)call2 = pGetProcAddress - (DWORD)pAsmFuncAddr - 29 - 5; + SIZE_T dwWriteSize; + WriteProcessMemory(handle, pAsmFuncAddr, GetProcAsmCode, sizeof(GetProcAsmCode), &dwWriteSize); + return pAsmFuncAddr; +} + +DWORD WeChatProcess::GetProcAddr(LPSTR functionname) { + if (!AsmProcAddr || !handle) + return 0; + WeChatData r_modulename(handle, dllname, TEXTLENGTH(dllname)); + WeChatData r_functionname(handle, functionname, TEXTLENGTHA(functionname)); + DWORD params[2] = { 0 }; + params[0] = (DWORD)r_modulename.GetAddr(); + params[1] = (DWORD)r_functionname.GetAddr(); + WeChatData r_params(handle, ¶ms[0], sizeof(params)); + DWORD dwProcAddr = CallRemoteFunction(handle, AsmProcAddr, r_params.GetAddr()); + return dwProcAddr; +} + +DWORD WeChatProcess::WeChatRobotBase() { + if (!handle) + return 0; + WeChatData r_dllname(handle, dllname, TEXTLENGTH(dllname)); + if (r_dllname.GetAddr() == 0) + return 0; + DWORD ret = CallRemoteFunction(handle, GetModuleHandleW, r_dllname.GetAddr()); + return ret; +} \ No newline at end of file diff --git a/CWeChatRobot/templatefunc.h b/CWeChatRobot/templatefunc.h new file mode 100644 index 0000000000000000000000000000000000000000..aeaecdc56449b056f9e87b9be9345dc0ace2b83d --- /dev/null +++ b/CWeChatRobot/templatefunc.h @@ -0,0 +1,90 @@ +#pragma once +#include +#include + +template +T2 WriteWeChatMemory(T1 hProcess, T2 ptrvalue, T3 size) { + if (!hProcess) + return NULL; + DWORD dwWriteSize; + T2 addr = (T2)VirtualAllocEx(hProcess, NULL, size, MEM_COMMIT, PAGE_READWRITE); + if (addr) + WriteProcessMemory(hProcess, (LPVOID)addr, ptrvalue, size, &dwWriteSize); + return addr; +} + +template +DWORD CallRemoteFunction(T1 hProcess,T2 FunctionAddr,T3 params) +{ + DWORD dwRet = 0; + DWORD dwThreadId = 0; + HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)FunctionAddr, (LPVOID)params, 0, &dwThreadId); + if (hThread) { + WaitForSingleObject(hThread, INFINITE); + GetExitCodeThread(hThread, &dwRet); + CloseHandle(hThread); + } + else { + return 0; + } + return dwRet; +} + +template +class WeChatData { + +public: + WeChatData(HANDLE hProcess,T data,int size) { + this->hProcess = hProcess; + this->size = size; + if (size == 0) + this->addr = data; + else + this->addr = WriteWeChatMemory(hProcess, data, size); + } + + ~WeChatData() { + if(this->size) + VirtualFreeEx(this->hProcess, this->addr, 0, MEM_RELEASE); + } + + T GetAddr() { + return this->addr; + } + +private: + T addr; + int size; + HANDLE hProcess; +}; + +class WeChatProcess { +public: + WeChatProcess(DWORD pid) { + this->handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); + if (!this->handle) + m_init = FALSE; + else { + AsmProcAddr = this->GetAsmFunAddr(); + m_init = AsmProcAddr != 0 ? TRUE : FALSE; + } + } + ~WeChatProcess() { + if (AsmProcAddr) + VirtualFreeEx(handle, AsmProcAddr, 0, MEM_RELEASE); + if(handle) + CloseHandle(handle); + AsmProcAddr = NULL; + handle = NULL; + } + HANDLE GetHandle() { + return this->handle; + } + DWORD GetProcAddr(LPSTR functionname); + DWORD WeChatRobotBase(); + BOOL m_init = FALSE; +private: + HANDLE handle; + LPVOID AsmProcAddr = NULL; + virtual LPVOID GetAsmFunAddr(); +}; diff --git a/CWeChatRobot/wechatver.cpp b/CWeChatRobot/wechatver.cpp index bac29266ed002fb4c70ac6f0368012ee99dde5b1..b469ec82caf115f7c8e32f2693589f55028ce425 100644 --- a/CWeChatRobot/wechatver.cpp +++ b/CWeChatRobot/wechatver.cpp @@ -1,31 +1,14 @@ #include "pch.h" BOOL ChangeWeChatVer(DWORD pid,wchar_t* verStr) { - HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid); - if (!hProcess) + WeChatProcess hp(pid); + if (!hp.m_init) return 1; + DWORD ChangeWeChatVerRemoteAddr = hp.GetProcAddr(ChangeWeChatVerRemote); + if (ChangeWeChatVerRemoteAddr == 0) return 1; - DWORD WeChatRobotBase = GetWeChatRobotBase(pid); - if (!WeChatRobotBase) { - CloseHandle(hProcess); + WeChatData r_version(hp.GetHandle(), verStr, TEXTLENGTH(verStr)); + if (r_version.GetAddr() == 0) return 1; - } - DWORD dwId = 0; - DWORD dwRet = 0x0; - LPVOID verStraddr = VirtualAllocEx(hProcess, NULL, 1, MEM_COMMIT, PAGE_READWRITE); - DWORD dwWriteSize = 0; - if (!verStraddr) { - CloseHandle(hProcess); - return 1; - } - WriteProcessMemory(hProcess, verStraddr, verStr, wcslen(verStr) * 2 + 2, &dwWriteSize); - DWORD ChangeWeChatVerRemoteAddr = WeChatRobotBase + ChangeWeChatVerRemoteOffset; - HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)ChangeWeChatVerRemoteAddr, verStraddr, 0, &dwId); - if (hThread) { - WaitForSingleObject(hThread, INFINITE); - GetExitCodeThread(hThread, &dwRet); - CloseHandle(hThread); - } - VirtualFreeEx(hProcess, verStraddr, 0, MEM_RELEASE); - CloseHandle(hProcess); - return dwRet == 0; + DWORD ret = CallRemoteFunction(hp.GetHandle(), ChangeWeChatVerRemoteAddr, r_version.GetAddr()); + return ret == 0; } \ No newline at end of file diff --git a/DWeChatRobot/LogMsgInfo.cpp b/DWeChatRobot/LogMsgInfo.cpp index e00d161fb042a706e320ada99b07d964307ee513..4a61dd5bc6a4c8930461d67af23f04701adbbbc2 100644 --- a/DWeChatRobot/LogMsgInfo.cpp +++ b/DWeChatRobot/LogMsgInfo.cpp @@ -45,14 +45,8 @@ VOID PrintMsg(DWORD msg) { char* message = new char[c_size + 1]; memset(message, 0, c_size + 1); WideCharToMultiByte(CP_ACP, 0, wmessage, -1, message, c_size, 0, 0); -#ifndef USE_SOCKET - HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendLogToComServer, wmessage, NULL, &dwId); - if (hThread) - CloseHandle(hThread); -#else delete[] wmessage; wmessage = NULL; -#endif #ifdef _DEBUG cout << message; #endif diff --git a/Python/test.py b/Python/test.py index 0b418b57dc4f10c7ec23a3f55db9f694b7f5becc..6b71511da2cdbd56fa5cd6680923188abd318974 100644 --- a/Python/test.py +++ b/Python/test.py @@ -2,87 +2,91 @@ """ Created on Sat Apr 16 14:06:24 2022 -@author: lijinchao-002 +@author: ljc545w """ import time import os import wxRobot from wxRobot import WeChatRobot - -def test_SendText(wx): + + +def test_send_text(instance): path = os.path.split(os.path.realpath(__file__))[0] # image full path - imgpath = os.path.join(path,'test\\娴嬭瘯鍥剧墖.png') + img_path = os.path.join(path, 'test\\娴嬭瘯鍥剧墖.png') # file full path - filepath = os.path.join(path,'test\\娴嬭瘯鏂囦欢') - myinfo = wx.GetSelfInfo() - chatwith = wx.GetFriendByWxNickName("鏂囦欢浼犺緭鍔╂墜") - session = wx.GetChatSession(chatwith.get('wxid')) - filehelper = wx.GetWxUserInfo(chatwith.get('wxid')) - session.SendText('涓汉淇℃伅锛歿}'.format(str(myinfo.get('wxNickName')))) + filepath = os.path.join(path, 'test\\娴嬭瘯鏂囦欢') + self_info = instance.GetSelfInfo() + chat_with = instance.GetFriendByWxNickName("鏂囦欢浼犺緭鍔╂墜") + session = instance.GetChatSession(chat_with.get('wxid')) + filehelper = instance.GetWxUserInfo(chat_with.get('wxid')) + session.SendText('涓汉淇℃伅锛歿}'.format(str(self_info.get('wxNickName')))) session.SendText('濂藉弸淇℃伅锛歿}'.format(str(filehelper.get('wxNickName')))) - if os.path.exists(imgpath): session.SendImage(imgpath) - if os.path.exists(filepath): session.SendFile(filepath) - session.SendArticle("澶╂皵棰勬姤","鐐瑰嚮鏌ョ湅","http://www.baidu.com") - shared = wx.GetFriendByWxNickName("鐮佸啘缈昏韩") - if shared: session.SendCard(shared.get('wxid'),shared.get('wxNickName')) - -def test_FriendStatus(wx): - f = open('Friendstatus.txt','wt',encoding = 'utf-8') - FriendList = wx.GetFriendList() - index = "\t".join(['寰俊鍙','鏄电О','澶囨敞','鐘舵','\n']) + if os.path.exists(img_path): + session.SendImage(img_path) + if os.path.exists(filepath): + session.SendFile(filepath) + session.SendArticle("澶╂皵棰勬姤", "鐐瑰嚮鏌ョ湅", "http://www.baidu.com") + shared = instance.GetFriendByWxNickName("鐮佸啘缈昏韩") + if shared: + session.SendCard(shared.get('wxid'), shared.get('wxNickName')) + + +def test_friend_status(instance): + f = open('friend_status.txt', 'wt', encoding='utf-8') + friend_list = instance.GetFriendList() + index = "\t".join(['寰俊鍙', '鏄电О', '澶囨敞', '鐘舵', '\n']) f.writelines(index) - for Friend in FriendList: + for Friend in friend_list: result = '\t'.join( - [Friend.get('wxNumber'),Friend.get('wxNickName'),Friend.get('wxRemark'), - wx.CheckFriendStatus(Friend.get('wxid'))]) + [Friend.get('wxNumber'), Friend.get('wxNickName'), Friend.get('wxRemark'), + instance.CheckFriendStatus(Friend.get('wxid'))]) print(result) result += '\n' f.writelines(result) time.sleep(1) break f.close() - -def test_ExecuteSQL(wx): + + +def test_execute_sql(instance): try: - dbs = wx.GetDbHandles() + dbs = instance.GetDbHandles() dbname = 'MicroMsg.db' handle = dbs.get(dbname).get('Handle') sql = 'select a.UserName as `wxID`,a.Alias as `寰俊鍙穈,a.EncryptUserName as `V3鏁版嵁`,\ a.Type as `鑱旂郴浜虹被鍨媊,a.VerifyFlag as `娣诲姞鏂瑰紡`,a.Remark as `澶囨敞`,a.NickName as `鏄电О`,b.bigHeadImgUrl as `澶村儚`,\ a.ExtraBuf as `鎵╁睍鏁版嵁` \ from Contact a inner join ContactHeadImgUrl b where a.UserName=b.usrName and a.Type=3 limit 10' - result = wx.ExecuteSQL(handle,sql) + result = instance.ExecuteSQL(handle, sql) print(result) except Exception as e: print(e) - pass - -def test_BackupDb(wx): + + +def test_BackupDb(instance): try: - dbs = wx.GetDbHandles() + dbs = instance.GetDbHandles() dbname = 'MicroMsg.db' handle = dbs.get(dbname).get('Handle') - rc = wx.BackupSQLiteDB(handle,'D:\\WeChatBackup\\{}'.format(dbname)) + rc = instance.BackupSQLiteDB(handle, 'D:\\WeChatBackup\\{}'.format(dbname)) print(rc) - except: - pass + except Exception as e: + print(e) + def show_interfaces(): - robot = wxRobot._WeChatRobotClient.instance().robot + robot = wxRobot.WeChatRobot(0).robot print(robot.CGetWeChatVer()) interfaces = [i for i in dir(robot) if '_' not in i and i[0] == 'C'] for interface in interfaces: print(interface) + if __name__ == '__main__': - pids = wxRobot.GetWeChatPids() - wx_list = [WeChatRobot(pid) for pid in pids] - if len(wx_list) < 1: - wx_list = wx_list + [wxRobot.StartWeChat()] * (1 - len(wx_list)) - for wx in wx_list: - wx.StartService() - wx.StartReceiveMessage() - wxRobot.StartSocketServer() - for wx in wx_list: - wx.StopService() \ No newline at end of file + pid_list = wxRobot.get_wechat_pid_list() + wx = WeChatRobot(pid_list[0]) + wx.StartService() + wx.StartReceiveMessage() + wxRobot.register_msg_event() + wx.StopService() diff --git a/Python/wxRobot.py b/Python/wxRobot.py index 27b14febaf918fd1945d081a15fc19614fa53950..0a71ede7f0572760ae26496a5b67ecf12c8e64df 100644 --- a/Python/wxRobot.py +++ b/Python/wxRobot.py @@ -5,84 +5,89 @@ Created on Thu Feb 24 16:19:48 2022 @author: ljc545w """ -# Before use,execute `CWeChatRebot.exe /regserver` in cmd by admin user -# need `pip install comtypes` -import comtypes.client -from ctypes import wintypes +# Before use,execute `CWeChatRobot.exe /regserver` in cmd by admin user import ast import os +import ctypes +import ctypes.wintypes import socketserver import threading +# need `pip install comtypes` +import comtypes.client from comtypes.client import GetEvents from comtypes.client import PumpEvents -class _WeChatRobotClient(): + +class _WeChatRobotClient: _instance = None - + @classmethod - def instance(cls): + def instance(cls) -> '_WeChatRobotClient': if not cls._instance: cls._instance = cls() return cls._instance - + def __init__(self): self.robot = comtypes.client.CreateObject("WeChatRobot.CWeChatRobot") self.event = comtypes.client.CreateObject("WeChatRobot.RobotEvent") - self.cpid = self.robot.CStopRobotService(0) - + self.com_pid = self.robot.CStopRobotService(0) + @classmethod def __del__(cls): import psutil if cls._instance is not None: try: - cprocess = psutil.Process(cls._instance.cpid) - cprocess.kill() + com_process = psutil.Process(cls._instance.com_pid) + com_process.kill() except psutil.NoSuchProcess: pass cls._instance = None -class WeChatEventSink(): + +class WeChatEventSink: """ 鎺ユ敹娑堟伅鐨勯粯璁ゅ洖璋冿紝鍙互鑷畾涔夛紝骞跺皢瀹炰緥鍖栧璞′綔涓篠tartReceiveMsgByEvent鍙傛暟 鑷畾涔夌殑绫婚渶瑕佸寘鍚互涓嬫墍鏈夋垚鍛 """ - def OnGetMessageEvent(self,msg,*args,**kwargs): + + def OnGetMessageEvent(self, msg): print(msg) -class ReceviveMsgBaseServer(socketserver.BaseRequestHandler): - def __init__(self,*args,**kwargs): - super().__init__(*args,**kwargs) - - class ReceiveMsgStruct(comtypes.Structure): - _fields_ = [("pid",wintypes.DWORD), - ("type", wintypes.DWORD), - ("isSendMsg", wintypes.DWORD), - ("sender",comtypes.c_wchar * 80), - ("wxid",comtypes.c_wchar * 80), - ("message",comtypes.c_wchar * 0x1000B), - ("filepath",comtypes.c_wchar * 260), - ("time",comtypes.c_wchar * 30) - ] - + +class ReceiveMsgBaseServer(socketserver.BaseRequestHandler): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + class ReceiveMsgStruct(ctypes.Structure): + _fields_ = [("pid", ctypes.wintypes.DWORD), + ("type", ctypes.wintypes.DWORD), + ("isSendMsg", ctypes.wintypes.DWORD), + ("sender", ctypes.c_wchar * 80), + ("wxid", ctypes.c_wchar * 80), + ("message", ctypes.c_wchar * 0x1000B), + ("filepath", ctypes.c_wchar * 260), + ("time", ctypes.c_wchar * 30) + ] + def handle(self): conn = self.request comtypes.CoInitialize() while True: try: - ptrdata = conn.recv(1024) + ptr_data = conn.recv(1024) try: - if ptrdata.decode() == 'bye': + if ptr_data.decode() == 'bye': break - except: + except UnicodeDecodeError: pass - while len(ptrdata) < comtypes.sizeof(self.ReceiveMsgStruct): + while len(ptr_data) < ctypes.sizeof(self.ReceiveMsgStruct): data = conn.recv(1024) if len(data) == 0: break - ptrdata += data - if ptrdata: - pReceiveMsgStruct = comtypes.cast(ptrdata,comtypes.POINTER(self.ReceiveMsgStruct)) - self.msgcallback(pReceiveMsgStruct.contents) + ptr_data += data + if ptr_data: + ptr_receive_msg = ctypes.cast(ptr_data, ctypes.POINTER(self.ReceiveMsgStruct)) + ReceiveMsgBaseServer.msg_callback(ptr_receive_msg.contents) response = "200 OK" conn.sendall(response.encode()) except OSError: @@ -92,71 +97,72 @@ class ReceviveMsgBaseServer(socketserver.BaseRequestHandler): conn.sendall("200 OK".encode()) conn.close() comtypes.CoUninitialize() - - def msgcallback(self,data): + + @staticmethod + def msg_callback(data): # 涓荤嚎绋嬩腑宸茬粡娉ㄥ叆锛屾澶勭姝㈣皟鐢⊿tartService鍜孲topService - msg = {'pid':data.pid,'time':data.time,'type':data.type,'isSendMsg':data.isSendMsg,'wxid':data.wxid, - 'sendto' if data.isSendMsg else 'from':data.sender,'message':data.message} + msg = {'pid': data.pid, 'time': data.time, 'type': data.type, 'isSendMsg': data.isSendMsg, 'wxid': data.wxid, + 'sendto' if data.isSendMsg else 'from': data.sender, 'message': data.message} robot = comtypes.client.CreateObject("WeChatRobot.CWeChatRobot") event = comtypes.client.CreateObject("WeChatRobot.RobotEvent") - wx = WeChatRobot(data.pid,robot,event) + wx = WeChatRobot(data.pid, robot, event) userinfo = wx.GetWxUserInfo(data.wxid) msg['alias'] = userinfo['wxNumber'] if data.isSendMsg == 0: if '@chatroom' in data.sender: - chatroominfo = wx.GetWxUserInfo(data.sender) - msg['chatroomname'] = chatroominfo['wxNickName'] + chatroom_info = wx.GetWxUserInfo(data.sender) + msg['chatroom_name'] = chatroom_info['wxNickName'] msg['nickname'] = wx.GetChatRoomMemberNickname(data.sender, data.wxid) else: msg['nickname'] = userinfo['wxNickName'] # TODO: 鍦ㄨ繖閲屽啓棰濆鐨勬秷鎭鐞嗛昏緫 - + print(msg) robot.Release() event.Release() - -class ChatSession(): - def __init__(self,pid,robot,wxid): + +class ChatSession: + def __init__(self, pid, robot, wxid): self.pid = pid self.robot = robot - self.chatwith = wxid - - def SendText(self,msg): - return self.robot.CSendText(self.pid,self.chatwith,msg) - - def SendImage(self,imgpath): - return self.robot.CSendImage(self.pid,self.chatwith,imgpath) - - def SendFile(self,filepath): - return self.robot.CSendFile(self.pid,self.chatwith,filepath) - - def SendMp4(self,mp4path): - return self.robot.CSendImage(self.pid,self.chatwith,mp4path) - - def SendArticle(self,title,abstract,url,imgpath = None): - return self.robot.CSendArticle(self.pid,self.chatwith,title,abstract,url,imgpath) - - def SendCard(self,sharedwxid,nickname): - return self.robot.CSendCard(self.pid,self.chatwith,sharedwxid,nickname) - - def SendAtText(self,wxid:list or str or tuple,msg,AutoNickName = True): - if '@chatroom' not in self.chatwith: + self.chat_with = wxid + + def SendText(self, msg): + return self.robot.CSendText(self.pid, self.chat_with, msg) + + def SendImage(self, img_path): + return self.robot.CSendImage(self.pid, self.chat_with, img_path) + + def SendFile(self, filepath): + return self.robot.CSendFile(self.pid, self.chat_with, filepath) + + def SendMp4(self, mp4path): + return self.robot.CSendImage(self.pid, self.chat_with, mp4path) + + def SendArticle(self, title, abstract, url, img_path=None): + return self.robot.CSendArticle(self.pid, self.chat_with, title, abstract, url, img_path) + + def SendCard(self, shared_wxid, nickname): + return self.robot.CSendCard(self.pid, self.chat_with, shared_wxid, nickname) + + def SendAtText(self, wxid: list or str or tuple, msg, auto_nickname=True): + if '@chatroom' not in self.chat_with: return 1 - return self.robot.CSendAtText(self.pid,self.chatwith,wxid,msg,AutoNickName) + return self.robot.CSendAtText(self.pid, self.chat_with, wxid, msg, auto_nickname) + + def SendAppMsg(self, appid): + return self.robot.CSendAppMsg(self.pid, self.chat_with, appid) + - def SendAppMsg(self,appid): - return self.robot.CSendAppMsg(self.pid,self.chatwith,appid) +class WeChatRobot: -class WeChatRobot(): - - def __init__(self,pid:int = 0,robot = None,event = None): + def __init__(self, pid: int = 0, robot=None, event=None): self.pid = pid self.robot = robot or _WeChatRobotClient.instance().robot self.event = event or _WeChatRobotClient.instance().event self.AddressBook = [] - self.myinfo = {} - + def StartService(self) -> int: """ 娉ㄥ叆DLL鍒板井淇′互鍚姩鏈嶅姟 @@ -169,7 +175,7 @@ class WeChatRobot(): """ status = self.robot.CStartRobotService(self.pid) return status - + def IsWxLogin(self) -> int: """ 鑾峰彇寰俊鐧诲綍鐘舵 @@ -182,7 +188,7 @@ class WeChatRobot(): """ return self.robot.CIsWxLogin(self.pid) - def SendText(self,receiver:str,msg:str) -> int: + def SendText(self, receiver: str, msg: str) -> int: """ 鍙戦佹枃鏈秷鎭 @@ -199,9 +205,9 @@ class WeChatRobot(): 0鎴愬姛,闈0澶辫触. """ - return self.robot.CSendText(self.pid,receiver,msg) - - def SendImage(self,receiver:str,imgpath:str) -> int: + return self.robot.CSendText(self.pid, receiver, msg) + + def SendImage(self, receiver: str, img_path: str) -> int: """ 鍙戦佸浘鐗囨秷鎭 @@ -209,7 +215,7 @@ class WeChatRobot(): ---------- receiver : str 娑堟伅鎺ユ敹鑰厀xid. - imgpath : str + img_path : str 鍥剧墖缁濆璺緞. Returns @@ -218,9 +224,9 @@ class WeChatRobot(): 0鎴愬姛,闈0澶辫触. """ - return self.robot.CSendImage(self.pid,receiver,imgpath) - - def SendFile(self,receiver:str,filepath:str) -> int: + return self.robot.CSendImage(self.pid, receiver, img_path) + + def SendFile(self, receiver: str, filepath: str) -> int: """ 鍙戦佹枃浠 @@ -237,9 +243,9 @@ class WeChatRobot(): 0鎴愬姛,闈0澶辫触. """ - return self.robot.CSendFile(self.pid,receiver,filepath) - - def SendArticle(self,receiver:str,title:str,abstract:str,url:str,imgpath:str or None = None) -> int: + return self.robot.CSendFile(self.pid, receiver, filepath) + + def SendArticle(self, receiver: str, title: str, abstract: str, url: str, img_path: str or None = None) -> int: """ 鍙戦乆ML鏂囩珷 @@ -253,7 +259,7 @@ class WeChatRobot(): 娑堟伅鍗$墖鎽樿. url : str 鏂囩珷閾炬帴. - imgpath : str or None, optional + img_path : str or None, optional 娑堟伅鍗$墖鏄剧ず鐨勫浘鐗囩粷瀵硅矾寰勶紝涓嶉渶瑕佸彲浠ヤ笉鎸囧畾. The default is None. Returns @@ -262,9 +268,9 @@ class WeChatRobot(): 0鎴愬姛,闈0澶辫触. """ - return self.robot.CSendArticle(self.pid,receiver,title,abstract,url,imgpath) - - def SendCard(self,receiver:str,sharedwxid:str,nickname:str) -> int: + return self.robot.CSendArticle(self.pid, receiver, title, abstract, url, img_path) + + def SendCard(self, receiver: str, shared_wxid: str, nickname: str) -> int: """ 鍙戦佸悕鐗 @@ -272,7 +278,7 @@ class WeChatRobot(): ---------- receiver : str 娑堟伅鎺ユ敹鑰厀xid. - sharedwxid : str + shared_wxid : str 琚垎浜汉wxid. nickname : str 鍚嶇墖鏄剧ず鐨勬樀绉. @@ -283,21 +289,21 @@ class WeChatRobot(): 0鎴愬姛,闈0澶辫触. """ - return self.robot.CSendCard(self.pid,receiver,sharedwxid,nickname) - - def SendAtText(self,chatroomid:str,AtUsers:list or str or tuple,msg:str,AutoNickName:bool = True) -> int: + return self.robot.CSendCard(self.pid, receiver, shared_wxid, nickname) + + def SendAtText(self, chatroom_id: str, at_users: list or str or tuple, msg: str, auto_nickname: bool = True) -> int: """ 鍙戦佺兢鑹剧壒娑堟伅锛岃壘鐗规墍鏈変汉鍙互灏咥tUsers璁剧疆涓篳notify@all` 鏃犵洰鏍囩兢绠$悊鏉冮檺璇峰嬁浣跨敤鑹剧壒鎵鏈変汉 Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰ID. - AtUsers : list or str or tuple + at_users : list or str or tuple 琚壘鐗圭殑浜哄垪琛. msg : str 娑堟伅鍐呭. - AutoNickName : bool, optional + auto_nickname : bool, optional 鏄惁鑷姩濉厖琚壘鐗逛汉鏄电О. 榛樿鑷姩濉厖. Returns @@ -306,9 +312,9 @@ class WeChatRobot(): 0鎴愬姛,闈0澶辫触. """ - if '@chatroom' not in chatroomid: + if '@chatroom' not in chatroom_id: return 1 - return self.robot.CSendAtText(self.pid,chatroomid,AtUsers,msg,AutoNickName) + return self.robot.CSendAtText(self.pid, chatroom_id, at_users, msg, auto_nickname) def GetSelfInfo(self) -> dict: """ @@ -320,14 +326,13 @@ class WeChatRobot(): 璋冪敤鎴愬姛杩斿洖涓汉淇℃伅锛屽惁鍒欒繑鍥炵┖瀛楀吀. """ - myinfo = self.robot.CGetSelfInfo(self.pid).replace('\n','\\n') + self_info = self.robot.CGetSelfInfo(self.pid).replace('\n', '\\n') try: - myinfo = ast.literal_eval(myinfo) + self_info = ast.literal_eval(self_info) except SyntaxError: return {} - self.myinfo = myinfo - return self.myinfo - + return self_info + def StopService(self) -> int: """ 鍋滄鏈嶅姟锛屼細灏咲LL浠庡井淇¤繘绋嬩腑鍗歌浇 @@ -338,9 +343,9 @@ class WeChatRobot(): COM杩涚▼pid. """ - cpid = self.robot.CStopRobotService(self.pid) - return cpid - + com_pid = self.robot.CStopRobotService(self.pid) + return com_pid + def GetAddressBook(self) -> list: """ 鑾峰彇鑱旂郴浜哄垪琛 @@ -352,12 +357,12 @@ class WeChatRobot(): """ try: - FriendTuple = self.robot.CGetFriendList(self.pid) - self.AddressBook = [dict(i) for i in list(FriendTuple)] + friend_tuple = self.robot.CGetFriendList(self.pid) + self.AddressBook = [dict(i) for i in list(friend_tuple)] except IndexError: self.AddressBook = [] return self.AddressBook - + def GetFriendList(self) -> list: """ 浠庨氳褰曞垪琛ㄤ腑绛涢夊嚭濂藉弸鍒楄〃 @@ -370,12 +375,12 @@ class WeChatRobot(): """ if not self.AddressBook: self.GetAddressBook() - FriendList = [] + friend_list = [] for item in self.AddressBook: if 'wxid_' == item['wxid'][0:5]: - FriendList.append(item) - return FriendList - + friend_list.append(item) + return friend_list + def GetChatRoomList(self) -> list: """ 浠庨氳褰曞垪琛ㄤ腑绛涢夊嚭缇よ亰鍒楄〃 @@ -388,12 +393,12 @@ class WeChatRobot(): """ if not self.AddressBook: self.GetAddressBook() - ChatRoomList = [] + chatroom_list = [] for item in self.AddressBook: if '@chatroom' in item['wxid']: - ChatRoomList.append(item) - return ChatRoomList - + chatroom_list.append(item) + return chatroom_list + def GetOfficialAccountList(self) -> list: """ 浠庨氳褰曞垪琛ㄤ腑绛涢夊嚭鍏紬鍙峰垪琛 @@ -406,13 +411,13 @@ class WeChatRobot(): """ if not self.AddressBook: self.GetAddressBook() - OfficialAccountList = [] + official_account_list = [] for item in self.AddressBook: if 'wxid_' != item['wxid'][0:5] and '@chatroom' not in item['wxid']: - OfficialAccountList.append(item) - return OfficialAccountList - - def GetFriendByWxRemark(self,remark:str) -> dict or None: + official_account_list.append(item) + return official_account_list + + def GetFriendByWxRemark(self, remark: str) -> dict or None: """ 閫氳繃澶囨敞鎼滅储鑱旂郴浜 @@ -433,14 +438,14 @@ class WeChatRobot(): if item['wxRemark'] == remark: return item return None - - def GetFriendByWxNumber(self,wxnumber:str) -> dict or None: + + def GetFriendByWxNumber(self, wx_number: str) -> dict or None: """ 閫氳繃寰俊鍙锋悳绱㈣仈绯讳汉 Parameters ---------- - wxnumber : str + wx_number : str 鑱旂郴浜哄井淇″彿. Returns @@ -452,17 +457,17 @@ class WeChatRobot(): if not self.AddressBook: self.GetAddressBook() for item in self.AddressBook: - if item['wxNumber'] == wxnumber: + if item['wxNumber'] == wx_number: return item return None - - def GetFriendByWxNickName(self,wxnickname:str) -> dict or None: + + def GetFriendByWxNickName(self, nickname: str) -> dict or None: """ 閫氳繃鏄电О鎼滅储鑱旂郴浜 Parameters ---------- - wxnickname : str + nickname : str 鑱旂郴浜烘樀绉. Returns @@ -474,11 +479,11 @@ class WeChatRobot(): if not self.AddressBook: self.GetAddressBook() for item in self.AddressBook: - if item['wxNickName'] == wxnickname: + if item['wxNickName'] == nickname: return item return None - - def GetChatSession(self,wxid:str) -> 'ChatSession': + + def GetChatSession(self, wxid: str) -> 'ChatSession': """ 鍒涘缓涓涓細璇濓紝娌″お澶х敤澶 @@ -493,9 +498,9 @@ class WeChatRobot(): 杩斿洖ChatSession绫. """ - return ChatSession(self.pid,self.robot, wxid) - - def GetWxUserInfo(self,wxid:str) -> dict: + return ChatSession(self.pid, self.robot, wxid) + + def GetWxUserInfo(self, wxid: str) -> dict: """ 閫氳繃wxid鏌ヨ鑱旂郴浜轰俊鎭 @@ -510,36 +515,36 @@ class WeChatRobot(): 鑱旂郴浜轰俊鎭. """ - userinfo = self.robot.CGetWxUserInfo(self.pid,wxid).replace('\n','\\n') + userinfo = self.robot.CGetWxUserInfo(self.pid, wxid).replace('\n', '\\n') return ast.literal_eval(userinfo) - - def GetChatRoomMembers(self,chatroomid:str) -> list: + + def GetChatRoomMembers(self, chatroom_id: str) -> dict or None: """ 鑾峰彇缇ゆ垚鍛樹俊鎭 Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰id. Returns ------- - list - 缇ゆ垚鍛樹俊鎭. + dict or None + 鑾峰彇鎴愬姛杩斿洖缇ゆ垚鍛樹俊鎭紝澶辫触杩斿洖None. """ - info = dict(self.robot.CGetChatRoomMembers(self.pid,chatroomid)) + info = dict(self.robot.CGetChatRoomMembers(self.pid, chatroom_id)) if not info: return None members = info['members'].split('^G') - data = self.GetWxUserInfo(chatroomid) + data = self.GetWxUserInfo(chatroom_id) data['members'] = [] for member in members: - memberinfo = self.GetWxUserInfo(self.pid,member) - data['members'].append(memberinfo) + member_info = self.GetWxUserInfo(member) + data['members'].append(member_info) return data - - def CheckFriendStatus(self,wxid:str) -> int: + + def CheckFriendStatus(self, wxid: str) -> int: """ 鑾峰彇濂藉弸鐘舵佺爜 @@ -558,10 +563,10 @@ class WeChatRobot(): 0xB5:'琚媺榛', """ - return self.robot.CCheckFriendStatus(self.pid,wxid) - + return self.robot.CCheckFriendStatus(self.pid, wxid) + # 鎺ユ敹娑堟伅鐨勫嚱鏁 - def StartReceiveMessage(self,port:int = 10808) -> int: + def StartReceiveMessage(self, port: int = 10808) -> int: """ 鍚姩鎺ユ敹娑堟伅Hook @@ -576,9 +581,9 @@ class WeChatRobot(): 鍚姩鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - status = self.robot.CStartReceiveMessage(self.pid,port) + status = self.robot.CStartReceiveMessage(self.pid, port) return status - + def StopReceiveMessage(self) -> int: """ 鍋滄鎺ユ敹娑堟伅Hook @@ -591,7 +596,7 @@ class WeChatRobot(): """ status = self.robot.CStopReceiveMessage(self.pid) return status - + def GetDbHandles(self) -> dict: """ 鑾峰彇鏁版嵁搴撳彞鏌勫拰琛ㄤ俊鎭 @@ -602,20 +607,20 @@ class WeChatRobot(): 鏁版嵁搴撳彞鏌勫拰琛ㄤ俊鎭. """ - tablesTuple = self.robot.CGetDbHandles(self.pid) - tables = [dict(i) for i in tablesTuple] + tables_tuple = self.robot.CGetDbHandles(self.pid) + tables = [dict(i) for i in tables_tuple] dbs = {} for table in tables: dbname = table['dbname'] if dbname not in dbs.keys(): - dbs[dbname] = {'Handle':table['Handle'],'tables':[]} + dbs[dbname] = {'Handle': table['Handle'], 'tables': []} dbs[dbname]['tables'].append( - {'name': table['name'],'tbl_name': table['tbl_name'], - 'rootpage': table['rootpage'],'sql': table['sql']} - ) + {'name': table['name'], 'tbl_name': table['tbl_name'], + 'root_page': table['root_page'], 'sql': table['sql']} + ) return dbs - - def ExecuteSQL(self,handle:int,sql:str) -> list: + + def ExecuteSQL(self, handle: int, sql: str) -> list: """ 鎵цSQL @@ -632,19 +637,19 @@ class WeChatRobot(): 鏌ヨ缁撴灉. """ - result = self.robot.CExecuteSQL(self.pid,handle,sql) + result = self.robot.CExecuteSQL(self.pid, handle, sql) if len(result) == 0: return [] query_list = [] keys = list(result[0]) for item in result[1:]: query_dict = {} - for key,value in zip(keys,item): + for key, value in zip(keys, item): query_dict[key] = value if not isinstance(value, tuple) else bytes(value) query_list.append(query_dict) return query_list - - def BackupSQLiteDB(self,handle:int,BackupFile:int) -> int: + + def BackupSQLiteDB(self, handle: int, filepath: str) -> int: """ 澶囦唤鏁版嵁搴 @@ -652,7 +657,7 @@ class WeChatRobot(): ---------- handle : int 鏁版嵁搴撳彞鏌. - BackupFile : int + filepath : int 澶囦唤鏂囦欢淇濆瓨浣嶇疆. Returns @@ -661,13 +666,13 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - BackupFile = BackupFile.replace('/','\\') - savepath = BackupFile.replace(BackupFile.split('\\')[-1],'') - if not os.path.exists(savepath): - os.makedirs(savepath) - return self.robot.CBackupSQLiteDB(self.pid,handle,BackupFile) - - def VerifyFriendApply(self,v3:str,v4:str) -> int: + filepath = filepath.replace('/', '\\') + save_path = filepath.replace(filepath.split('\\')[-1], '') + if not os.path.exists(save_path): + os.makedirs(save_path) + return self.robot.CBackupSQLiteDB(self.pid, handle, filepath) + + def VerifyFriendApply(self, v3: str, v4: str) -> int: """ 閫氳繃濂藉弸璇锋眰 @@ -684,9 +689,9 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊.. """ - return self.robot.CVerifyFriendApply(self.pid,v3,v4) - - def AddFriendByWxid(self,wxid:str,message:str or None) -> int: + return self.robot.CVerifyFriendApply(self.pid, v3, v4) + + def AddFriendByWxid(self, wxid: str, message: str or None) -> int: """ wxid鍔犲ソ鍙 @@ -703,9 +708,9 @@ class WeChatRobot(): 璇锋眰鍙戦佹垚鍔熻繑鍥0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CAddFriendByWxid(self.pid,wxid,message) - - def AddFriendByV3(self,v3:str,message:str or None,AddType:int) -> int: + return self.robot.CAddFriendByWxid(self.pid, wxid, message) + + def AddFriendByV3(self, v3: str, message: str or None, add_type: int = 0x6) -> int: """ v3鏁版嵁鍔犲ソ鍙 @@ -715,7 +720,7 @@ class WeChatRobot(): v3鏁版嵁(encryptUserName). message : str or None 楠岃瘉淇℃伅. - AddType : int + add_type : int 娣诲姞鏂瑰紡(鏉ユ簮).鎵嬫満鍙: 0xF;寰俊鍙: 0x3;QQ鍙: 0x1;鏈嬪弸楠岃瘉娑堟伅: 0x6. Returns @@ -724,8 +729,8 @@ class WeChatRobot(): 璇锋眰鍙戦佹垚鍔熻繑鍥0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CAddFriendByV3(self.pid,v3,message,AddType) - + return self.robot.CAddFriendByV3(self.pid, v3, message, add_type) + def GetWeChatVer(self) -> str: """ 鑾峰彇寰俊鐗堟湰鍙 @@ -737,8 +742,8 @@ class WeChatRobot(): """ return self.robot.CGetWeChatVer() - - def GetUserInfoByNet(self,keyword:str) -> dict or None: + + def GetUserInfoByNet(self, keyword: str) -> dict or None: """ 缃戠粶鏌ヨ鐢ㄦ埛淇℃伅 @@ -753,18 +758,18 @@ class WeChatRobot(): 鏌ヨ鎴愬姛杩斿洖鐢ㄦ埛淇℃伅,鏌ヨ澶辫触杩斿洖None. """ - userinfo = self.robot.CSearchContactByNet(self.pid,keyword) + userinfo = self.robot.CSearchContactByNet(self.pid, keyword) if userinfo: return dict(userinfo) return None - - def AddBrandContact(self,PublicId:str) -> int: + + def AddBrandContact(self, public_id: str) -> int: """ 鍏虫敞鍏紬鍙 Parameters ---------- - PublicId : str + public_id : str 鍏紬鍙穒d. Returns @@ -773,9 +778,9 @@ class WeChatRobot(): 璇锋眰鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CAddBrandContact(self.pid,PublicId) - - def ChangeWeChatVer(self,version:str) -> int: + return self.robot.CAddBrandContact(self.pid, public_id) + + def ChangeWeChatVer(self, version: str) -> int: """ 鑷畾涔夊井淇$増鏈彿锛屼竴瀹氱▼搴︿笂闃叉鑷姩鏇存柊 @@ -790,15 +795,15 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CChangeWeChatVer(self.pid,version) - - def HookImageMsg(self,savepath:str) -> int: + return self.robot.CChangeWeChatVer(self.pid, version) + + def HookImageMsg(self, save_path: str) -> int: """ 寮濮婬ook鏈姞瀵嗗浘鐗 Parameters ---------- - savepath : str + save_path : str 鍥剧墖淇濆瓨璺緞(缁濆璺緞). Returns @@ -807,8 +812,8 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CHookImageMsg(self.pid,savepath) - + return self.robot.CHookImageMsg(self.pid, save_path) + def UnHookImageMsg(self) -> int: """ 鍙栨秷Hook鏈姞瀵嗗浘鐗 @@ -820,14 +825,14 @@ class WeChatRobot(): """ return self.robot.CUnHookImageMsg(self.pid) - - def HookVoiceMsg(self,savepath:str) -> int: + + def HookVoiceMsg(self, save_path: str) -> int: """ 寮濮婬ook璇煶娑堟伅 Parameters ---------- - savepath : str + save_path : str 璇煶淇濆瓨璺緞(缁濆璺緞). Returns @@ -836,8 +841,8 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CHookVoiceMsg(self.pid,savepath) - + return self.robot.CHookVoiceMsg(self.pid, save_path) + def UnHookVoiceMsg(self) -> int: """ 鍙栨秷Hook璇煶娑堟伅 @@ -850,7 +855,7 @@ class WeChatRobot(): """ return self.robot.CUnHookVoiceMsg(self.pid) - def DeleteUser(self,wxid:str) -> int: + def DeleteUser(self, wxid: str) -> int: """ 鍒犻櫎濂藉弸 @@ -865,9 +870,9 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CDeleteUser(self.pid,wxid) + return self.robot.CDeleteUser(self.pid, wxid) - def SendAppMsg(self,wxid:str,appid:str) -> int: + def SendAppMsg(self, wxid: str, appid: str) -> int: """ 鍙戦佸皬绋嬪簭 @@ -884,16 +889,16 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CSendAppMsg(self.pid,wxid,appid) - - def EditRemark(self,wxid:str,remark:str or None) -> int: + return self.robot.CSendAppMsg(self.pid, wxid, appid) + + def EditRemark(self, wxid: str, remark: str or None) -> int: """ 淇敼濂藉弸鎴栫兢鑱婂娉 Parameters ---------- wxid : str - wxid鎴朿hatroomid. + wxid鎴朿hatroom_id. remark : str or None 瑕佷慨鏀圭殑澶囨敞. @@ -903,15 +908,15 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CEditRemark(self.pid,wxid,remark) - - def SetChatRoomName(self,chatroomid:str,name:str) -> int: + return self.robot.CEditRemark(self.pid, wxid, remark) + + def SetChatRoomName(self, chatroom_id: str, name: str) -> int: """ 淇敼缇ゅ悕绉.璇风‘璁ゅ叿鏈夌浉鍏虫潈闄愬啀璋冪敤銆 Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰id. name : str 瑕佷慨鏀逛负鐨勭兢鍚嶇О. @@ -922,15 +927,15 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CSetChatRoomName(self.pid,chatroomid,name) - - def SetChatRoomAnnouncement(self,chatroomid:str,announcement:str or None) -> int: + return self.robot.CSetChatRoomName(self.pid, chatroom_id, name) + + def SetChatRoomAnnouncement(self, chatroom_id: str, announcement: str or None) -> int: """ 璁剧疆缇ゅ叕鍛.璇风‘璁ゅ叿鏈夌浉鍏虫潈闄愬啀璋冪敤銆 Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰id. announcement : str or None 鍏憡鍐呭. @@ -941,15 +946,15 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CSetChatRoomAnnouncement(self.pid,chatroomid,announcement) - - def SetChatRoomSelfNickname(self,chatroomid:str,nickname:str) -> int: + return self.robot.CSetChatRoomAnnouncement(self.pid, chatroom_id, announcement) + + def SetChatRoomSelfNickname(self, chatroom_id: str, nickname: str) -> int: """ 璁剧疆缇ゅ唴涓汉鏄电О Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰id. nickname : str 瑕佷慨鏀逛负鐨勬樀绉. @@ -960,15 +965,15 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CSetChatRoomSelfNickname(self.pid,chatroomid,nickname) - - def GetChatRoomMemberNickname(self,chatroomid:str,wxid:str) -> str: + return self.robot.CSetChatRoomSelfNickname(self.pid, chatroom_id, nickname) + + def GetChatRoomMemberNickname(self, chatroom_id: str, wxid: str) -> str: """ 鑾峰彇缇ゆ垚鍛樻樀绉 Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰id. wxid : str 缇ゆ垚鍛榳xid. @@ -979,17 +984,17 @@ class WeChatRobot(): 鎴愬姛杩斿洖缇ゆ垚鍛樻樀绉,澶辫触杩斿洖绌哄瓧绗︿覆. """ - return self.robot.CGetChatRoomMemberNickname(self.pid,chatroomid,wxid) - - def DelChatRoomMember(self,chatroomid:str,wxids:str or list or tuple) -> str: + return self.robot.CGetChatRoomMemberNickname(self.pid, chatroom_id, wxid) + + def DelChatRoomMember(self, chatroom_id: str, wxid_list: str or list or tuple) -> str: """ 鍒犻櫎缇ゆ垚鍛.璇风‘璁ゅ叿鏈夌浉鍏虫潈闄愬啀璋冪敤銆 Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰id. - wxids : str or list or tuple + wxid_list : str or list or tuple 瑕佸垹闄ょ殑鎴愬憳wxid鎴杦xid鍒楄〃. Returns @@ -998,17 +1003,17 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CDelChatRoomMember(self.pid,chatroomid,wxids) - - def AddChatRoomMember(self,chatroomid:str,wxids:str or list or tuple) -> str: + return self.robot.CDelChatRoomMember(self.pid, chatroom_id, wxid_list) + + def AddChatRoomMember(self, chatroom_id: str, wxid_list: str or list or tuple) -> str: """ 娣诲姞缇ゆ垚鍛.璇风‘璁ゅ叿鏈夌浉鍏虫潈闄愬啀璋冪敤銆 Parameters ---------- - chatroomid : str + chatroom_id : str 缇よ亰id. - wxids : str or list or tuple + wxid_list : str or list or tuple 瑕佹坊鍔犵殑鎴愬憳wxid鎴杦xid鍒楄〃. Returns @@ -1017,9 +1022,10 @@ class WeChatRobot(): 鎴愬姛杩斿洖0,澶辫触杩斿洖闈0鍊. """ - return self.robot.CAddChatRoomMember(self.pid,chatroomid,wxids) - -def GetWeChatPids() -> list: + return self.robot.CAddChatRoomMember(self.pid, chatroom_id, wxid_list) + + +def get_wechat_pid_list() -> list: """ 鑾峰彇鎵鏈夊井淇id @@ -1030,35 +1036,37 @@ def GetWeChatPids() -> list: """ import psutil - pids = [] + pid_list = [] process_list = psutil.pids() for pid in process_list: if psutil.Process(pid).name() == 'WeChat.exe': - pids.append(pid) - return pids - -def StartWeChat() -> 'WeChatRobot': + pid_list.append(pid) + return pid_list + + +def start_wechat() -> 'WeChatRobot' or None: """ 鍚姩寰俊 Returns ------- - WeChatRobot - 鎴愬姛杩斿洖WeChatRobot瀵硅薄,澶辫触杩斿洖False. + WeChatRobot or None + 鎴愬姛杩斿洖WeChatRobot瀵硅薄,澶辫触杩斿洖None. """ pid = _WeChatRobotClient.instance().robot.CStartWeChat() if pid != 0: return WeChatRobot(pid) - return False + return None + -def RegisterMsgEvent(EventSink:'WeChatEventSink' or None = None) -> None: +def register_msg_event(event_sink: 'WeChatEventSink' or None = None) -> None: """ 閫氳繃COM缁勪欢杩炴帴鐐规帴鏀舵秷鎭紝鐪熸鐨勫洖璋 Parameters ---------- - EventSink : object, optional + event_sink : object, optional 鍥炶皟鐨勫疄鐜扮被锛岃绫昏缁ф壙`WeChatEventSink`绫绘垨瀹炵幇鍏朵腑鐨勬柟娉. Returns @@ -1069,19 +1077,20 @@ def RegisterMsgEvent(EventSink:'WeChatEventSink' or None = None) -> None: """ event = _WeChatRobotClient.instance().event if event is not None: - sink = EventSink or WeChatEventSink() - ConnectionPoint = GetEvents(event,sink) - assert ConnectionPoint != None + sink = event_sink or WeChatEventSink() + connection_point = GetEvents(event, sink) + assert connection_point is not None while True: try: PumpEvents(2) - except: + except KeyboardInterrupt: break - del ConnectionPoint - -def StartSocketServer(port:int = 10808, - RequestHandler: 'ReceviveMsgBaseServer' = ReceviveMsgBaseServer, - mainThread = True) -> int or None: + del connection_point + + +def start_socket_server(port: int = 10808, + request_handler: 'ReceiveMsgBaseServer' = ReceiveMsgBaseServer, + main_thread=True) -> int or None: """ 鍒涘缓娑堟伅鐩戝惉绾跨▼ @@ -1090,25 +1099,25 @@ def StartSocketServer(port:int = 10808, port : int socket鐨勭洃鍚鍙e彿. - RequestHandler : ReceviveMsgBaseServer - 鐢ㄤ簬澶勭悊娑堟伅鐨勭被锛岄渶瑕佺户鎵胯嚜socketserver.BaseRequestHandler鎴朢eceviveMsgBaseServer + request_handler : ReceiveMsgBaseServer + 鐢ㄤ簬澶勭悊娑堟伅鐨勭被锛岄渶瑕佺户鎵胯嚜socketserver.BaseRequestHandler鎴朢eceiveMsgBaseServer - mainThread : bool + main_thread : bool 鏄惁鍦ㄤ富绾跨▼涓惎鍔╯erver Returns ------- int or None - mainThread涓篎alse鏃惰繑鍥炵嚎绋媔d,鍚﹀垯杩斿洖None. + main_thread涓篎alse鏃惰繑鍥炵嚎绋媔d,鍚﹀垯杩斿洖None. """ - ip_port=("127.0.0.1",port) + ip_port = ("127.0.0.1", port) try: - s = socketserver.ThreadingTCPServer(ip_port,RequestHandler) - if mainThread: + s = socketserver.ThreadingTCPServer(ip_port, request_handler) + if main_thread: s.serve_forever() else: - socket_server = threading.Thread(target = s.serve_forever) + socket_server = threading.Thread(target=s.serve_forever) socket_server.setDaemon(True) socket_server.start() return socket_server.ident @@ -1117,14 +1126,15 @@ def StartSocketServer(port:int = 10808, except Exception as e: print(e) return None - -def StopSocketServer(threadid:int) -> None: + + +def stop_socket_server(thread_id: int) -> None: """ 寮哄埗缁撴潫娑堟伅鐩戝惉绾跨▼ Parameters ---------- - threadid : int + thread_id : int 娑堟伅鐩戝惉绾跨▼ID. Returns @@ -1133,18 +1143,19 @@ def StopSocketServer(threadid:int) -> None: . """ - if not threadid: + if not thread_id: return import inspect try: - tid = comtypes.c_long(threadid) + tid = comtypes.c_long(thread_id) + res = 0 if not inspect.isclass(SystemExit): - exctype = type(SystemExit) - res = comtypes.pythonapi.PyThreadState_SetAsyncExc(tid, comtypes.py_object(exctype)) + exec_type = type(SystemExit) + res = comtypes.pythonapi.PyThreadState_SetAsyncExc(tid, comtypes.py_object(exec_type)) if res == 0: raise ValueError("invalid thread id") elif res != 1: - comtypes.ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) + ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") - except: - pass \ No newline at end of file + except (ValueError, SystemError): + pass diff --git a/README.md b/README.md index 81788a947607aeacfa2f010b5f51d96dd814f2f3..e34df60ba5f51763b6d667a0cb25e578a4a8a15d 100644 --- a/README.md +++ b/README.md @@ -7,19 +7,15 @@ PC寰俊鏈哄櫒浜猴紝瀹炵幇浠ヤ笅鍔熻兘锛 5. 妫娴嬪ソ鍙嬬姸鎬侊紙鏄惁濂藉弸銆佽鍒犻櫎銆佽鎷夐粦锛 6. 鎺ユ敹鍚勭被娑堟伅锛屽彲鍐欏洖璋冨嚱鏁拌繘琛屽鐞 7. 灏佽COM鎺ュ彛锛屾柟渚夸娇鐢ㄨ嚜宸卞枩娆㈢殑璇█杩涜璋冪敤 +8. 缇ょ鐞 +9. 寰俊澶氬紑 # 鐢ㄩ 1. 娣樺鍙戝崟 2. 鏃犵棔娓呯矇 3. 寰俊鍏紬鍙烽噰闆 4. 鑱婂ぉ璁板綍澶囦唤 -5. 鍏朵粬浣犺兘鎯冲埌鐨勭敤閫 - -# tips -1銆佸綋鍓嶅垎鏀槸鍏煎澶氬紑鐨凚eta鐗堟湰锛屽彲閫氳繃pid杩涜澶氬紑绠$悊 -2銆乣CStartWeChat`鎺ュ彛鍙墦寮涓涓柊鐨勫井淇″疄渚嬪苟杩斿洖璇ヨ繘绋嬬殑pid锛屼絾浠嶇劧闇瑕佺敤鎴锋墜鍔ㄨ皟鐢╜CStartRobotService`杩涜娉ㄥ叆 -3銆佸凡缁忛噸鏂版暣鐞唒ython socket server鍜岃繛鎺ョ偣锛屽彲浠ュ疄鐜板寰俊娑堟伅鑱氬悎 -4銆佸彟澶栦竴涓皬灏忕殑璇夋眰锛屽鏋滄偍鎵鍦ㄧ殑鍏徃鏈塁++鎴朠ython宀椾綅绌虹己锛屽苟涓斿姙鍏湴鐐瑰湪鍖椾含銆佹繁鍦筹紝甯屾湜鑳芥彁渚涘唴鎺ㄦ満浼氱粰鎴戯紝鍙互閫氳繃ljc545w@qq.com鑱旂郴鍒版垜锛屼笉鑳滄劅婵~ +5. 鍏朵粬浣犺兘鎯冲埌鐨勭敤閫 # 鍙敤鐗堟湰 寰俊鐢佃剳鐗**3.5.0.46** @@ -60,7 +56,7 @@ CWeChatRobot.exe /unregserver 鍙傝僛ESDK](/ESDK)锛屾劅璋lovezm 鐨勮础鐚 # 鏇村鍔熻兘 鍚庣画璁″垝鍔熻兘锛 -1. 淇敼濂藉弸澶囨敞 +1. 瀹炵幇http璋冪敤 鏈夌┖鐨勬椂鍊欎細鎸夌収涓婅堪椤哄簭杩涜寮鍙戯紝涓嶈繃鍢涳紝璁″垝鍙槸璁″垝锛屽鏋滄湭瀹炵幇涔熻瑙佽皡 **涔熸杩庢偍鎻愪氦PR** @@ -115,7 +111,13 @@ CWeChatRobot.exe /unregserver 1. 宸查傞厤3.7.0.30鐗堟湰 ## 2022.07.19 1. 鏂板淇敼澶囨敞鎺ュ彛 -1. 鏂板缇ょ鐞嗗姛鑳斤紝鍖呮嫭娣诲姞鎴愬憳銆佸垹闄ゆ垚鍛樸佽缃叕鍛娿佷慨鏀圭兢鍚嶇О銆佽缃兢鍐呬釜浜烘樀绉般佽幏鍙栫兢鎴愬憳鏄电О +2. 鏂板缇ょ鐞嗗姛鑳斤紝鍖呮嫭娣诲姞鎴愬憳銆佸垹闄ゆ垚鍛樸佽缃叕鍛娿佷慨鏀圭兢鍚嶇О銆佽缃兢鍐呬釜浜烘樀绉般佽幏鍙栫兢鎴愬憳鏄电О +## 2022.07.24 +1. 娣诲姞澶氬紑绠$悊 +## 2022.07.28 +1. 瑙e喅閮ㄥ垎宸茬煡闂锛屼紭鍖栧寮绠$悊 +2. 閲嶆瀯COM涓殑閮ㄥ垎瀹炵幇 + # 鎵撹祻浣滆 璇风粰浣滆呬竴涓猻tar锛屾劅璋㈡劅璋 # 鍏嶈矗澹版槑 diff --git a/Release/CWeChatRobot.exe b/Release/CWeChatRobot.exe index 23a033237bcf3a5676952324d4700b54e07147dd..fe8287accfef34d35f6d664135b49c8ad09a25cf 100644 Binary files a/Release/CWeChatRobot.exe and b/Release/CWeChatRobot.exe differ diff --git a/Release/DWeChatRobot.dll b/Release/DWeChatRobot.dll index d70535fd716442c5ff6812b8be84ae97951435c0..a19da08bf4b2b0a79250778a90ed81185d659623 100644 Binary files a/Release/DWeChatRobot.dll and b/Release/DWeChatRobot.dll differ diff --git a/Release/socket/SWeChatRobot.dll b/Release/socket/SWeChatRobot.dll index fee31d21ec8e905897009fd3236f7aec9bc52caf..2d9297632483801f2b5ba7b7ab1021372f9dc403 100644 Binary files a/Release/socket/SWeChatRobot.dll and b/Release/socket/SWeChatRobot.dll differ diff --git a/Release/socket/wxDriver.dll b/Release/socket/wxDriver.dll index 3c6dcd6b8ce00ecec1aeec334a57d2aacce7332e..068fa713c5125a31eb617e020f2b3b9ec13c9771 100644 Binary files a/Release/socket/wxDriver.dll and b/Release/socket/wxDriver.dll differ diff --git a/Release/socket/wxDriver64.dll b/Release/socket/wxDriver64.dll index 2a22409aa00e6c189064d53c7277f9243ef1968b..ee8f362a947c7e03b650dd9f8f7e83a2c46fd913 100644 Binary files a/Release/socket/wxDriver64.dll and b/Release/socket/wxDriver64.dll differ