diff --git a/CWeChatRobot/InjertDll.cpp b/CWeChatRobot/InjertDll.cpp index 66299b604443ca46cb8c382738d2452298dd4af6..1ce103292101df45aef0b098cc7738ac1dfa2cf1 100644 --- a/CWeChatRobot/InjertDll.cpp +++ b/CWeChatRobot/InjertDll.cpp @@ -38,6 +38,8 @@ bool Injert(DWORD dwPid,wchar_t* workPath) { string name = _com_util::ConvertBSTRToString((BSTR)workPath); if (!isFileExists_stat(name)) { MessageBox(NULL, dllpath, L"ļ", MB_ICONWARNING); + delete[] dllpath; + dllpath = NULL; return 1; } bool status = InjectDll(dwPid, dllpath); diff --git a/CWeChatRobot/pch.cpp b/CWeChatRobot/pch.cpp index ad02e9f1a11306a7625ca14ad605ab86da3b9dd2..7a706a0aa1266c615343a7a969bf1a4f414b30dc 100644 --- a/CWeChatRobot/pch.cpp +++ b/CWeChatRobot/pch.cpp @@ -44,7 +44,7 @@ wstring SelfInfoString = L""; HANDLE hProcess = NULL; -bool isFileExists_stat(string& name) { +BOOL isFileExists_stat(string& name) { struct stat buffer; return (stat(name.c_str(), &buffer) == 0); } @@ -226,4 +226,63 @@ wstring GetComWorkPath() { int pos = wpath.find_last_of(L"\\"); wpath = wpath.substr(0,pos); return wpath; +} + +static BOOL GetWeChatInstallInfo(TCHAR* lpValueName, VOID* Value, DWORD lpcbData) { + HKEY hKey = NULL; + ZeroMemory(Value, lpcbData); + LSTATUS lRet = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Tencent\\WeChat"), 0, KEY_QUERY_VALUE, &hKey); + if (lRet != 0) { + return false; + } + lRet = RegQueryValueEx(hKey, lpValueName, NULL, NULL, (LPBYTE)Value, &lpcbData); + RegCloseKey(hKey); + if (lRet != 0) { + return false; + } + return true; +} + +tstring GetWeChatInstallDir() { + TCHAR* szProductType = new TCHAR[MAX_PATH]; + GetWeChatInstallInfo((TCHAR*)TEXT("InstallPath"), (void*)szProductType, MAX_PATH); + tstring wxdir(szProductType); + delete[] szProductType; + szProductType = NULL; + return wxdir.length() == 0 ? TEXT("") : wxdir; +} + +DWORD GetWeChatVerInt() { + DWORD version = 0x0; + GetWeChatInstallInfo((TCHAR*)TEXT("Version"), (void*)&version, sizeof(DWORD)); + return version; +} + +tstring GetWeChatVerStr() { + BYTE pversion[4] = { 0 }; + GetWeChatInstallInfo((TCHAR*)TEXT("Version"), (void*)pversion, sizeof(DWORD)); + TCHAR* temp = new TCHAR[20]; + _stprintf_s(temp, 20, _T("%d.%d.%d.%d\0"), (int)(pversion[3] - 0x60), (int)pversion[2], (int)pversion[1], (int)pversion[0]); + tstring verStr(temp); + delete[] temp; + temp = NULL; + return verStr; +} + +VOID StartWeChat() +{ + tstring szAppName = GetWeChatInstallDir(); + if (szAppName.length() == 0) + return; + szAppName += TEXT("\\WeChat.exe"); + STARTUPINFO StartInfo; + ZeroMemory(&StartInfo, sizeof(StartInfo)); + PROCESS_INFORMATION procStruct; + ZeroMemory(&procStruct, sizeof(procStruct)); + StartInfo.cb = sizeof(STARTUPINFO); + if (CreateProcess((LPCTSTR)szAppName.c_str(), NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartInfo, &procStruct)) + { + CloseHandle(procStruct.hProcess); + CloseHandle(procStruct.hThread); + } } \ No newline at end of file diff --git a/CWeChatRobot/pch.h b/CWeChatRobot/pch.h index c4fd668dd6db8e4c6cd629e6cebc6217d6f8c47f..972c34c692c5d173eeeb8091f1c0833965968794 100644 --- a/CWeChatRobot/pch.h +++ b/CWeChatRobot/pch.h @@ -29,15 +29,22 @@ #include "robotdata.h" using namespace std; -struct testStruct { - DWORD type; - wchar_t* desc; -}; -bool isFileExists_stat(string& name); +#ifdef _UNICODE +#define tstring std::wstring +#else +#define tstring std::string +#endif + +BOOL isFileExists_stat(string& name); DWORD GetWeChatRobotBase(); DWORD GetWeChatPid(); DWORD StartRobotService(); DWORD StopRobotService(); BOOL CreateConsole(); -wstring GetComWorkPath(); \ No newline at end of file +wstring GetComWorkPath(); + +tstring GetWeChatInstallDir(); +DWORD GetWeChatVerInt(); +tstring GetWeChatVerStr(); +VOID StartWeChat(); \ No newline at end of file diff --git a/DWeChatRobot/dllmain.cpp b/DWeChatRobot/dllmain.cpp index 982aba242ea5951327fddf2d7b436001c9bd49c9..51b554b737778f68b9cb014467082d89981ac303 100644 --- a/DWeChatRobot/dllmain.cpp +++ b/DWeChatRobot/dllmain.cpp @@ -11,7 +11,9 @@ BOOL APIENTRY DllMain( HMODULE hModule, case DLL_PROCESS_ATTACH: { #ifdef _DEBUG - PrintProcAddr(); + if (ProcessIsWeChat()) { + PrintProcAddr(); + } #endif break; } diff --git a/DWeChatRobot/pch.cpp b/DWeChatRobot/pch.cpp index 8d27b1bcdb25c979887c0d0effce0f268d503583..95a66597178a30f8828e8122cfa1e5dbffadf40e 100644 --- a/DWeChatRobot/pch.cpp +++ b/DWeChatRobot/pch.cpp @@ -155,6 +155,7 @@ wchar_t* GetTimeW() { void PrintProcAddr() { CreateConsole(); + printf("WeChatVersion %s\n", GetWeChatVerStr().c_str()); printf("SendImage 0x%08X\n", (DWORD)SendImage); printf("SendText 0x%08X\n", (DWORD)SendText); printf("SendFile 0x%08X\n", (DWORD)SendFile); @@ -170,4 +171,71 @@ void PrintProcAddr() { printf("VerifyFriendApply 0x%08X\n", (DWORD)VerifyFriendApply); printf("AddFriendByV3 0x%08X\n", (DWORD)AddFriendByV3); printf("AddFriendByWxid 0x%08X\n", (DWORD)AddFriendByWxid); +} + +BOOL ProcessIsWeChat() +{ + char szFileFullPath[MAX_PATH] = { 0 }, szProcessName[MAX_PATH] = { 0 }; + GetModuleFileNameA(NULL, szFileFullPath, MAX_PATH); + int length = ::strlen(szFileFullPath); + for (int i = length - 1; i >= 0; i--) + { + if (szFileFullPath[i] == '\\') + { + i++; + for (int j = 0; i <= length; j++) + { + szProcessName[j] = szFileFullPath[i++]; + } + break; + } + } + + if (::strcmp(szProcessName, "WeChat.exe") != 0) + { + return FALSE; + } + else + { + return TRUE; + } +} + +DWORD GetWeChatVerInt() +{ + WCHAR VersionFilePath[MAX_PATH]; + BYTE WeChatVersion[4] = { 0 }; + if (GetModuleFileName((HMODULE)GetWeChatWinBase(), VersionFilePath, MAX_PATH) == 0) + { + return 0; + } + + VS_FIXEDFILEINFO* pVsInfo; + unsigned int iFileInfoSize = sizeof(VS_FIXEDFILEINFO); + int iVerInfoSize = GetFileVersionInfoSize(VersionFilePath, NULL); + if (iVerInfoSize != 0) { + char* pBuf = new char[iVerInfoSize]; + if (GetFileVersionInfo(VersionFilePath, 0, iVerInfoSize, pBuf)) { + if (VerQueryValue(pBuf, TEXT("\\"), (void**)&pVsInfo, &iFileInfoSize)) { + WeChatVersion[3] = (BYTE)(0x60 + (pVsInfo->dwFileVersionMS >> 16) & 0x0000FFFF); + WeChatVersion[2] = (BYTE)(pVsInfo->dwFileVersionMS & 0x0000FFFF); + WeChatVersion[1] = (BYTE)((pVsInfo->dwFileVersionLS >> 16) & 0x0000FFFF); + WeChatVersion[0] = (BYTE)(pVsInfo->dwFileVersionLS & 0x0000FFFF); + } + } + delete[] pBuf; + } + return *(DWORD*)WeChatVersion; +} + +string GetWeChatVerStr() { + DWORD WeChatVersion = GetWeChatVerInt(); + if (WeChatVersion == 0) + return "null"; + string wxver = ""; + BYTE* pWxVer = (BYTE*)&WeChatVersion; + strstream wxVer; + wxVer << (int)pWxVer[3] - 0x60 << "." << (int)pWxVer[2] << "." << (int)pWxVer[1] << "." << (int)pWxVer[0]; + wxVer >> wxver; + return wxver; } \ No newline at end of file diff --git a/DWeChatRobot/pch.h b/DWeChatRobot/pch.h index 996f86081d8c61e54936083fdee41caac57fcb0e..733a2e5b786d52c83824bd46e25da6bb2e2faf6e 100644 --- a/DWeChatRobot/pch.h +++ b/DWeChatRobot/pch.h @@ -30,9 +30,11 @@ #include "VerifyFriendApply.h" #include "AddFriend.h" #include +#include #endif //PCH_H using namespace std; +#pragma comment(lib,"version.lib") // 对于导出函数,需要使用此宏修饰 #define DLLEXPORT extern "C" __declspec(dllexport) @@ -84,3 +86,6 @@ DLLEXPORT void UnHookAll(); wstring wreplace(wstring source, wchar_t replaced, wstring replaceto); void PrintProcAddr(); wchar_t* GetTimeW(); +BOOL ProcessIsWeChat(); +DWORD GetWeChatVerInt(); +string GetWeChatVerStr(); \ No newline at end of file