pch.cpp 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// pch.cpp: 与预编译标头对应的源文件

#include "pch.h"

// 当使用预编译的头时,需要使用此源文件,编译才能成功。
DWORD SendImageOffset = 0x0;
DWORD SendTextOffset = 0x0;
DWORD SendFileOffset = 0x0;
DWORD SendArticleOffset = 0x0;
DWORD SendCardOffset = 0x0;
L
ljc545w 已提交
11
DWORD SendAtTextOffset = 0x0;
L
ljc545w 已提交
12
DWORD SendAppMsgRemoteOffset = 0x0;
13 14 15 16 17

DWORD GetFriendListInitOffset = 0x0;
DWORD GetFriendListRemoteOffset = 0x0;
DWORD GetFriendListFinishOffset = 0x0;

L
ljc545w 已提交
18
DWORD EditRemarkRemoteOffset = 0x0;
19 20 21 22 23
DWORD GetWxUserInfoOffset = 0x0;
DWORD DeleteUserInfoCacheOffset = 0x0;

DWORD GetSelfInfoOffset = 0x0;
DWORD DeleteSelfInfoCacheOffset = 0x0;
L
ljc545w 已提交
24
DWORD SearchContactByNetRemoteOffset = 0x0;
L
ljc545w 已提交
25
DWORD isWxLoginOffset = 0;
26

L
ljc545w 已提交
27 28
DWORD VerifyFriendApplyOffset = 0x0;

29 30
DWORD CheckFriendStatusRemoteOffset = 0x0;

31 32 33
DWORD HookReceiveMessageRemoteOffset = 0x0;
DWORD UnHookReceiveMessageRemoteOffset = 0x0;

L
ljc545w 已提交
34
DWORD GetChatRoomMemberNicknameRemoteOffset = 0x0;
L
ljc545w 已提交
35
DWORD GetChatRoomMembersRemoteOffset = 0x0;
L
ljc545w 已提交
36 37 38 39 40
DWORD DelChatRoomMemberRemoteOffset = 0x0;
DWORD AddChatRoomMemberRemoteOffset = 0x0;
DWORD SetChatRoomAnnouncementRemoteOffset = 0x0;
DWORD SetChatRoomNameRemoteOffset = 0x0;
DWORD SetChatRoomSelfNicknameRemoteOffset = 0x0;
L
ljc545w 已提交
41

L
ljc545w 已提交
42 43
DWORD GetDbHandlesRemoteOffset = 0x0;
DWORD ExecuteSQLRemoteOffset = 0x0;
L
ljc545w 已提交
44
DWORD SelectDataRemoteOffset = 0x0;
L
ljc545w 已提交
45 46
DWORD BackupSQLiteDBRemoteOffset = 0x0;

L
ljc545w 已提交
47 48
DWORD AddFriendByWxidRemoteOffset = 0x0;
DWORD AddFriendByV3RemoteOffset = 0x0;
L
ljc545w 已提交
49
DWORD DeleteUserRemoteOffset = 0x0;
L
ljc545w 已提交
50
DWORD AddBrandContactRemoteOffset = 0x0;
L
ljc545w 已提交
51

L
ljc545w 已提交
52 53 54 55 56 57 58
DWORD HookImageMsgRemoteOffset = 0x0;
DWORD UnHookImageMsgRemoteOffset = 0x0;
DWORD HookVoiceMsgRemoteOffset = 0x0;
DWORD UnHookVoiceMsgRemoteOffset = 0x0;

DWORD ChangeWeChatVerRemoteOffset = 0x0;

59 60
wstring SelfInfoString = L"";

L
ljc545w 已提交
61
BOOL isFileExists_stat(string& name) {
62 63 64 65
    struct stat buffer;
    return (stat(name.c_str(), &buffer) == 0);
}

L
ljc545w 已提交
66
BOOL CreateConsole() {
67 68 69 70 71 72 73 74 75 76 77 78
    if (AllocConsole()) {
        AttachConsole(GetCurrentProcessId());
        FILE* retStream;
        freopen_s(&retStream, "CONOUT$", "w", stdout);
        if (!retStream) throw std::runtime_error("Stdout redirection failed.");
        freopen_s(&retStream, "CONOUT$", "w", stderr);
        if (!retStream) throw std::runtime_error("Stderr redirection failed.");
        return 0;
    }
    return 1;
}

L
ljc545w 已提交
79 80
DWORD GetWeChatRobotBase(DWORD pid) {
    HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
81 82 83 84 85 86 87 88 89
    if (!hProcess)
        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;
L
ljc545w 已提交
90
    HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetModuleHandleW, pRemoteAddress, 0, &dwID);
91 92 93 94 95 96 97 98 99
    if (hThread) {
        WaitForSingleObject(hThread, INFINITE);
        GetExitCodeThread(hThread, &dwHandle);
    }
    else {
        return 0;
    }
    CloseHandle(hThread);
    VirtualFreeEx(hProcess, pRemoteAddress, 0, MEM_RELEASE);
L
ljc545w 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    CloseHandle(hProcess);
    return dwHandle;
}

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);
126 127 128
    return dwHandle;
}

L
ljc545w 已提交
129
BOOL GetProcOffset(wchar_t* workPath) {
130
    wchar_t* dllpath = new wchar_t[MAX_PATH];
L
ljc545w 已提交
131
    memset(dllpath, 0, MAX_PATH * 2);
132 133 134 135
    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);
L
ljc545w 已提交
136 137 138 139 140
        return 0;
    }
    HMODULE hd = LoadLibrary(dllpath);
    if (!hd) {
        return 0;
141 142 143 144 145 146 147 148 149 150 151 152
    }
    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;
L
ljc545w 已提交
153 154
    DWORD SendAtTextProcAddr = (DWORD)GetProcAddress(hd, SendAtTextRemote);
    SendAtTextOffset = SendAtTextProcAddr - WeChatBase;
L
ljc545w 已提交
155 156
    DWORD SendAppMsgProcAddr = (DWORD)GetProcAddress(hd, SendAppMsgRemote);
    SendAppMsgRemoteOffset = SendAppMsgProcAddr - WeChatBase;
157 158 159 160 161 162 163 164

    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;

L
ljc545w 已提交
165 166
    DWORD EditRemarkRemoteAddr = (DWORD)GetProcAddress(hd, EditRemarkRemote);
    EditRemarkRemoteOffset = EditRemarkRemoteAddr - WeChatBase;
167 168 169 170 171
    DWORD GetWxUserInfoProcAddr = (DWORD)GetProcAddress(hd, GetWxUserInfoRemote);
    GetWxUserInfoOffset = GetWxUserInfoProcAddr - WeChatBase;
    DWORD DeleteUserInfoCacheProcAddr = (DWORD)GetProcAddress(hd, DeleteUserInfoCacheRemote);
    DeleteUserInfoCacheOffset = DeleteUserInfoCacheProcAddr - WeChatBase;

L
ljc545w 已提交
172 173 174
    DWORD VerifyFriendApplyProcAddr = (DWORD)GetProcAddress(hd, VerifyFriendApplyRemote);
    VerifyFriendApplyOffset = VerifyFriendApplyProcAddr - WeChatBase;

175 176 177 178
    DWORD GetSelfInfoProcAddr = (DWORD)GetProcAddress(hd, GetSelfInfoRemote);
    GetSelfInfoOffset = GetSelfInfoProcAddr - WeChatBase;
    DWORD DeleteSelfInfoCacheProcAddr = (DWORD)GetProcAddress(hd, DeleteSelfInfoCacheRemote);
    DeleteSelfInfoCacheOffset = DeleteSelfInfoCacheProcAddr - WeChatBase;
L
ljc545w 已提交
179 180
    DWORD SearchContactByNetRemoteAddr = (DWORD)GetProcAddress(hd, SearchContactByNetRemote);
    SearchContactByNetRemoteOffset = SearchContactByNetRemoteAddr - WeChatBase;
L
ljc545w 已提交
181 182
    DWORD isWxLoginAddr = (DWORD)GetProcAddress(hd, isWxLoginRemote);
    isWxLoginOffset = isWxLoginAddr - WeChatBase;
183 184 185 186

    DWORD CheckFriendStatusRemoteAddr = (DWORD)GetProcAddress(hd, CheckFriendStatusRemote);
    CheckFriendStatusRemoteOffset = CheckFriendStatusRemoteAddr - WeChatBase;

187 188 189 190 191
    DWORD HookReceiveMessageRemoteAddr = (DWORD)GetProcAddress(hd, HookReceiveMessageRemote);
    HookReceiveMessageRemoteOffset = HookReceiveMessageRemoteAddr - WeChatBase;
    DWORD UnHookReceiveMessageRemoteAddr = (DWORD)GetProcAddress(hd, UnHookReceiveMessageRemote);
    UnHookReceiveMessageRemoteOffset = UnHookReceiveMessageRemoteAddr - WeChatBase;

L
ljc545w 已提交
192 193
    DWORD GetChatRoomMemberNicknameRemoteAddr = (DWORD)GetProcAddress(hd, GetChatRoomMemberNicknameRemote);
    GetChatRoomMemberNicknameRemoteOffset = GetChatRoomMemberNicknameRemoteAddr - WeChatBase;
L
ljc545w 已提交
194 195 196
    DWORD GetChatRoomMembersRemoteAddr = (DWORD)GetProcAddress(hd, GetChatRoomMembersRemote);
    GetChatRoomMembersRemoteOffset = GetChatRoomMembersRemoteAddr - WeChatBase;

L
ljc545w 已提交
197 198 199 200 201 202 203 204 205 206 207
    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;

L
ljc545w 已提交
208 209 210 211
    DWORD GetDbHandlesRemoteAddr = (DWORD)GetProcAddress(hd, GetDbHandlesRemote);
    GetDbHandlesRemoteOffset = GetDbHandlesRemoteAddr - WeChatBase;
    DWORD ExecuteSQLRemoteAddr = (DWORD)GetProcAddress(hd, ExecuteSQLRemote);
    ExecuteSQLRemoteOffset = ExecuteSQLRemoteAddr - WeChatBase;
L
ljc545w 已提交
212 213
    DWORD SelectDataRemoteAddr = (DWORD)GetProcAddress(hd, SelectDataRemote);
    SelectDataRemoteOffset = SelectDataRemoteAddr - WeChatBase;
L
ljc545w 已提交
214 215 216
    DWORD BackupSQLiteDBRemoteAddr = (DWORD)GetProcAddress(hd, BackupSQLiteDBRemote);
    BackupSQLiteDBRemoteOffset = BackupSQLiteDBRemoteAddr - WeChatBase;

L
ljc545w 已提交
217 218 219 220
    DWORD AddFriendByWxidRemoteAddr = (DWORD)GetProcAddress(hd, AddFriendByWxidRemote);
    AddFriendByWxidRemoteOffset = AddFriendByWxidRemoteAddr - WeChatBase;
    DWORD AddFriendByV3RemoteAddr = (DWORD)GetProcAddress(hd, AddFriendByV3Remote);
    AddFriendByV3RemoteOffset = AddFriendByV3RemoteAddr - WeChatBase;
L
ljc545w 已提交
221 222
    DWORD DeleteUserRemoteAddr = (DWORD)GetProcAddress(hd, DeleteUserRemote);
    DeleteUserRemoteOffset = DeleteUserRemoteAddr - WeChatBase;
L
ljc545w 已提交
223 224
    DWORD AddBrandContactRemoteAddr = (DWORD)GetProcAddress(hd, AddBrandContactRemote);
    AddBrandContactRemoteOffset = AddBrandContactRemoteAddr - WeChatBase;
L
ljc545w 已提交
225

L
ljc545w 已提交
226 227 228 229 230 231 232 233 234 235 236 237
    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;

238 239 240
    FreeLibrary(hd);
    delete[] dllpath;
    dllpath = NULL;
L
ljc545w 已提交
241
    return 1;
242 243 244 245 246 247 248 249 250 251 252 253 254
}

DWORD GetWeChatPid() {
    HWND hCalc = FindWindow(NULL, L"微信");
    DWORD wxPid = 0;
    GetWindowThreadProcessId(hCalc, &wxPid);
    if (wxPid == 0) {
        hCalc = FindWindow(NULL, L"微信测试版");
        GetWindowThreadProcessId(hCalc, &wxPid);
    }
    return wxPid;
}

L
ljc545w 已提交
255
DWORD StartRobotService(DWORD pid) {
256 257
    wstring wworkPath = GetComWorkPath();
    wchar_t* workPath = (wchar_t*)wworkPath.c_str();
L
ljc545w 已提交
258 259 260 261 262 263
    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;
    };
L
ljc545w 已提交
264
    bool status = Inject(pid, workPath);
265 266 267
    return status;
}

L
ljc545w 已提交
268
DWORD StopRobotService(DWORD pid) {
L
ljc545w 已提交
269
    DWORD cpid = GetCurrentProcessId();
L
ljc545w 已提交
270
    if (pid == 0)
L
ljc545w 已提交
271
        return cpid;
L
ljc545w 已提交
272
    RemoveDll(pid);
273
    ZeroMemory((wchar_t*)SelfInfoString.c_str(), SelfInfoString.length() * 2 + 2);
L
ljc545w 已提交
274
    return 0;
275 276 277 278 279 280 281 282 283
}

wstring GetComWorkPath() {
    wchar_t szFilePath[MAX_PATH + 1] = { 0 };
    GetModuleFileName(NULL, szFilePath, MAX_PATH);
    wstring wpath = szFilePath;
    int pos = wpath.find_last_of(L"\\");
    wpath = wpath.substr(0,pos);
    return wpath;
L
ljc545w 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
}

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;
L
ljc545w 已提交
312
    GetWeChatInstallInfo((TCHAR*)TEXT("CrashVersion"), (void*)&version, sizeof(DWORD));
L
ljc545w 已提交
313 314 315 316 317
    return version;
}

tstring GetWeChatVerStr() {
    BYTE pversion[4] = { 0 };
L
ljc545w 已提交
318
    GetWeChatInstallInfo((TCHAR*)TEXT("CrashVersion"), (void*)pversion, sizeof(DWORD));
L
ljc545w 已提交
319 320 321 322 323 324 325 326
    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;
}

L
ljc545w 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static bool CloseAllWxProcessMutexHandle() 
{
    HANDLE  hsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hsnapshot == INVALID_HANDLE_VALUE)
    {
        return false;
    }
    PROCESSENTRY32 pe;
    pe.dwSize = sizeof(PROCESSENTRY32);
    int flag = Process32First(hsnapshot, &pe);
    while (flag != 0)
    {
        if (lstrcmp(pe.szExeFile, L"WeChat.exe") == 0)
        {
            CloseProcessHandle(pe.th32ProcessID, L"_WeChat_App_Instance_Identity_Mutex_Name");
        }
        flag = Process32Next(hsnapshot, &pe);
    }
    CloseHandle(hsnapshot);
    return true;
}

DWORD StartWeChat()
L
ljc545w 已提交
350
{
L
ljc545w 已提交
351
    CloseAllWxProcessMutexHandle();
L
ljc545w 已提交
352 353
    tstring szAppName = GetWeChatInstallDir();
    if (szAppName.length() == 0)
L
ljc545w 已提交
354
        return 0;
L
ljc545w 已提交
355 356 357 358 359 360 361 362 363 364 365
    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);
    }
L
ljc545w 已提交
366 367 368 369 370 371 372
    if (procStruct.dwProcessId == 0)
        return 0;
    DWORD WeChatWinBase = 0;
    while ((WeChatWinBase = GetWeChatWinBase(procStruct.dwProcessId)) == 0) {
        Sleep(500);
    }
    return procStruct.dwProcessId;
L
ljc545w 已提交
373
}