Advertisement
alien_fx_fiend

API-Hooking-Process

Jun 29th, 2024
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 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.  
  8. #pragma comment(lib, "Psapi.lib")
  9.  
  10. BOOL notepadHooked = FALSE;
  11. BOOL notepadPlusPlusHooked = FALSE;
  12.  
  13. BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege) {
  14.     TOKEN_PRIVILEGES tp;
  15.     LUID luid;
  16.  
  17.     if (!LookupPrivilegeValue(NULL, lpszPrivilege, &luid)) {
  18.         std::wcout << L"LookupPrivilegeValue error: " << GetLastError() << std::endl;
  19.         return FALSE;
  20.     }
  21.  
  22.     tp.PrivilegeCount = 1;
  23.     tp.Privileges[0].Luid = luid;
  24.     tp.Privileges[0].Attributes = (bEnablePrivilege) ? SE_PRIVILEGE_ENABLED : 0;
  25.  
  26.     if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
  27.         std::wcout << L"AdjustTokenPrivileges error: " << GetLastError() << std::endl;
  28.         return FALSE;
  29.     }
  30.  
  31.     if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) {
  32.         std::wcout << L"The token does not have the specified privilege. \n" << std::endl;
  33.         return FALSE;
  34.     }
  35.  
  36.     return TRUE;
  37. }
  38.  
  39. BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) {
  40.     DWORD processId;
  41.     GetWindowThreadProcessId(hwnd, &processId);
  42.  
  43.     HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  44.     if (hProcess != NULL) {
  45.         TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
  46.         if (GetModuleFileNameEx(hProcess, NULL, szProcessName, MAX_PATH)) {
  47.             std::wstring processName(szProcessName);
  48.             size_t pos = processName.find_last_of(L"\\");
  49.             if (pos != std::wstring::npos) {
  50.                 processName = processName.substr(pos + 1);
  51.             }
  52.             CloseHandle(hProcess);
  53.  
  54.             TCHAR windowTitle[MAX_PATH];
  55.             GetWindowText(hwnd, windowTitle, MAX_PATH);
  56.  
  57.             std::wcout << L"Process: " << processName << L", Window Title: " << windowTitle << std::endl;
  58.  
  59.             if (processName == L"notepad.exe") {
  60.                 std::wstring newTitle = L"Wormhole Vortex";
  61.                 if (SetWindowText(hwnd, newTitle.c_str())) {
  62.                     std::wcout << L"Modified window title of " << processName << std::endl;
  63.                     notepadHooked = TRUE;
  64.                 }
  65.                 else {
  66.                     std::wcout << L"Failed to modify window title of " << processName << std::endl;
  67.                 }
  68.             }
  69.             if (processName == L"notepad++.exe") {
  70.                 std::wstring newTitle = L"Wormhole Vortex";
  71.                 if (SetWindowText(hwnd, newTitle.c_str())) {
  72.                     std::wcout << L"Modified window title of " << processName << std::endl;
  73.                     notepadPlusPlusHooked = TRUE;
  74.                 }
  75.                 else {
  76.                     std::wcout << L"Failed to modify window title of " << processName << std::endl;
  77.                 }
  78.             }
  79.         }
  80.         else {
  81.             std::wcout << L"GetModuleFileNameEx error: " << GetLastError() << std::endl;
  82.         }
  83.     }
  84.     return TRUE;
  85. }
  86.  
  87. int main() {
  88.     HANDLE hToken;
  89.     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  90.         std::wcout << L"OpenProcessToken error: " << GetLastError() << std::endl;
  91.         return 1;
  92.     }
  93.  
  94.     if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE)) {
  95.         std::wcout << L"Failed to enable debug privilege." << std::endl;
  96.         CloseHandle(hToken);
  97.         return 1;
  98.     }
  99.  
  100.     EnumWindows(EnumWindowsProc, 0);
  101.  
  102.     if (notepadHooked) {
  103.         std::cout << "Notepad.exe window titles modified successfully." << std::endl;
  104.     }
  105.     else {
  106.         std::cout << "Unable to modify Notepad.exe window titles." << std::endl;
  107.     }
  108.  
  109.     if (notepadPlusPlusHooked) {
  110.         std::cout << "Notepad++.exe window titles modified successfully." << std::endl;
  111.     }
  112.     else {
  113.         std::cout << "Unable to modify Notepad++.exe window titles." << std::endl;
  114.     }
  115.  
  116.     std::cout << "Press enter to exit...";
  117.     std::cin.get();
  118.  
  119.     SetPrivilege(hToken, SE_DEBUG_NAME, FALSE);
  120.     CloseHandle(hToken);
  121.     return 0;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement