Advertisement
alien_fx_fiend

Message-Based-Hooking

Jun 29th, 2024 (edited)
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <string>
  3. #include <TlHelp32.h>
  4. #include <tchar.h>
  5. #include <Psapi.h>
  6. #include <iostream>
  7. #include <vector>
  8.  
  9. #pragma comment(lib, "Psapi.lib")
  10.  
  11. struct ProcessInfo {
  12.     std::wstring processName;
  13.     HWND hwnd;
  14. };
  15.  
  16. std::vector<ProcessInfo> targetProcesses;
  17. bool running = true;
  18.  
  19. // ... [Keep the SetPrivilege function as it is] ...
  20. BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) {
  21.     TOKEN_PRIVILEGES tp;
  22.     LUID luid;
  23.  
  24.  
  25.     if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) {
  26.         std::wcout << L"LookupPrivilegeValue error: " << GetLastError() << std::endl;
  27.         return FALSE;
  28.     }
  29.  
  30. tp.PrivilegeCount = 1;
  31. tp.Privileges[0].Luid = luid;
  32. tp.Privileges[0].Attributes = (bEnablePrivilege) ? SE_PRIVILEGE_ENABLED : 0;
  33.  
  34. if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
  35.     std::wcout << L"AdjustTokenPrivileges error: " << GetLastError() << std::endl;
  36.     return FALSE;
  37. }
  38.  
  39. if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
  40.     std::wcout << L"The token does not have the specified privilege. \n" << std::endl;
  41.     return FALSE;
  42. }
  43.  
  44. return TRUE;
  45. }
  46.  
  47. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
  48.     DWORD processId;
  49.     GetWindowThreadProcessId(hwnd, &processId);
  50.  
  51.     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  52.     if (hProcess != NULL) {
  53.         TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
  54.         if (GetModuleFileNameEx(hProcess, NULL, szProcessName, MAX_PATH)) {
  55.             std::wstring processName(szProcessName);
  56.             size_t pos = processName.find_last_of(L"\\");
  57.             if (pos != std::wstring::npos) {
  58.                 processName = processName.substr(pos + 1);
  59.             }
  60.             CloseHandle(hProcess);
  61.  
  62.             TCHAR windowTitle[MAX_PATH];
  63.             GetWindowText(hwnd, windowTitle, MAX_PATH);
  64.  
  65.             std::wcout << L"Process: " << processName << L", Window Title: " << windowTitle << std::endl;
  66.  
  67.             if (processName == L"notepad.exe" || processName == L"notepad++.exe") {
  68.                 targetProcesses.push_back({ processName, hwnd });
  69.             }
  70.         }
  71.         else {
  72.             std::wcout << L"GetModuleFileNameEx error: " << GetLastError() << std::endl;
  73.         }
  74.     }
  75.     return TRUE;
  76. }
  77.  
  78. bool ModifyWindowTitles() {
  79.     bool anyModified = false;
  80.     for (const auto& process : targetProcesses) {
  81.         std::wstring newTitle = L"Wormhole Vortex";
  82.         if (SetWindowText(process.hwnd, newTitle.c_str())) {
  83.             anyModified = true;
  84.                         std::wcout << L"Modified window title of " << process.processName << std::endl;
  85.  
  86.         }
  87.  
  88.         else {
  89.  
  90.             std::wcout << L"Failed to modify window title of " << process.processName << std::endl;
  91.         }
  92.     }
  93.     return anyModified;
  94. }
  95.  
  96. void RestoreWindowTitles() {
  97.     for (const auto& process : targetProcesses) {
  98.                 if (SetWindowText(process.hwnd, process.processName.c_str())) {
  99.  
  100.             std::wcout << L"Restored window title of " << process.processName << std::endl;
  101.  
  102.         }
  103.  
  104.         else {
  105.  
  106.             std::wcout << L"Failed to restore window title of " << process.processName << std::endl;
  107.  
  108.         }
  109.     }
  110. }
  111.  
  112. DWORD WINAPI TitleModifierThread(LPVOID lpParam) {
  113.     while (running) {
  114.         ModifyWindowTitles();
  115.         Sleep(1000);
  116.     }
  117.     return 0;
  118. }
  119.  
  120. int main() {
  121.     // ... [Keep the privilege setting code as it is] ...
  122.     HANDLE hToken;
  123.     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  124.         std::wcout << L"OpenProcessToken error: " << GetLastError() << std::endl;
  125.         return 1;
  126.     }
  127.  
  128.  
  129.     if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
  130.         std::wcout << L"Failed to enable debug privilege." << std::endl;
  131.         CloseHandle(hToken);
  132.         return 1;
  133.     }
  134.  
  135.  
  136.     EnumWindows(EnumWindowsProc, 0);
  137.  
  138.     if (ModifyWindowTitles()) {
  139.         std::wcout << L"Notepad and Notepad++ have been modified successfully." << std::endl;
  140.     }
  141.     else {
  142.         std::wcout << L"Unable to modify Notepad or Notepad++ window titles." << std::endl;
  143.     }
  144.  
  145.     HANDLE hThread = CreateThread(NULL, 0, TitleModifierThread, NULL, 0, NULL);
  146.     if (hThread == NULL) {
  147.         std::wcout << L"Failed to create thread." << std::endl;
  148.         return 1;
  149.     }
  150.  
  151.     std::wcout << L"Window titles are being modified. Press Enter to stop and exit..." << std::endl;
  152.     std::cin.get();
  153.  
  154.     running = false;
  155.     WaitForSingleObject(hThread, INFINITE);
  156.     CloseHandle(hThread);
  157.  
  158.     RestoreWindowTitles();
  159.  
  160.     // ... [Keep the privilege resetting code as it is] ...
  161.     SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
  162.     CloseHandle(hToken);
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement