ya_makaron

chat hook

Dec 16th, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. DWORD Hook_ChatMessage_Addr;
  4. bool message_permission, hooks_is_created;
  5.  
  6. void __stdcall ChangeString(char* str)
  7. {
  8.     if (message_permission)
  9.     {
  10.         message_permission = false;
  11.         return;
  12.     }
  13.     if (strstr(str, "Warning:"))
  14.     {
  15.         std::string newstr = str;
  16.         newstr.replace(newstr.find("Warning"), newstr.find(":"), "NedoWarning");
  17.         strcpy_s(str, 256, newstr.c_str());
  18.     }
  19.     else if (strstr(str, "<Warning>"))
  20.     {
  21.         std::string newstr = str;
  22.         newstr.replace(newstr.find("<Warning>"), newstr.find(" "), "NedoWarning:");
  23.         strcpy_s(str, 256, newstr.c_str());
  24.     }
  25. }
  26.  
  27. void __declspec(naked) Hook_ChatMessage()
  28. {
  29.     __asm
  30.     {
  31.         add esp, 0x4
  32.         pop esi
  33.         add esp, 0x48
  34.         pushad
  35.         pushfd
  36.         push esi
  37.         call ChangeString
  38.         popfd
  39.         popad
  40.         ret 0xC
  41.     }
  42. }
  43.  
  44.  
  45. void create_jmp_hook(DWORD addr, DWORD dwJumpTo, size_t size)
  46. {
  47.     DWORD old_protect;
  48.     VirtualProtect((LPVOID)addr, size, PAGE_EXECUTE_READWRITE, &old_protect);
  49.  
  50.     *reinterpret_cast<BYTE*>(addr) = 0xE9;
  51.     *reinterpret_cast<DWORD*>(addr + 0x1) = dwJumpTo - addr - 0x5;
  52.  
  53.     for (DWORD i = 0x5; i < size; i++)
  54.         *reinterpret_cast<BYTE*>(addr + i) = 0x90;
  55.  
  56.     VirtualProtect((LPVOID)addr, size, old_protect, &old_protect);
  57. }
  58.  
  59. void init_hooks()
  60. {
  61.     Hook_ChatMessage_Addr = (DWORD)GetModuleHandleA("samp.dll") + 0x63C8A;
  62.  
  63.     create_jmp_hook(Hook_ChatMessage_Addr, (DWORD)Hook_ChatMessage, 7);
  64.  
  65.     hooks_is_created = true;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment