sys.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "pch.h"
  2. #include "sys.h"
  3. #include <string>
  4. // Define a function type (adjust as needed)
  5. typedef void (*func_t)();
  6. // Global variables for trampoline and original function address
  7. void* trampoline = nullptr;
  8. func_t original_ptr = nullptr;
  9. HWND hwnd = nullptr;
  10. int orgNext = 0;
  11. void send_msg(char* msg, unsigned flag) {
  12. if (hwnd != nullptr) {
  13. COPYDATASTRUCT cds;
  14. cds.dwData = 1; // Custom identifier (can be any value)
  15. cds.cbData = strlen(msg) + 1; // Size of the string including null terminator
  16. cds.lpData = (void*)msg; // Pointer to the string data
  17. // Send the WM_COPYDATA message
  18. SendMessage(hwnd, WM_COPYDATA, (WPARAM)flag, (LPARAM)&cds);
  19. }
  20. }
  21. char* msg;
  22. unsigned flag;
  23. void __declspec(naked) override_write_screen_call()
  24. {
  25. _asm {
  26. pushad
  27. mov msg, edx
  28. mov flag, ecx
  29. }
  30. //int len = MultiByteToWideChar(CP_ACP, 0, msg, -1, NULL, 0);
  31. //wchar_t* utf16Str = new wchar_t[len];
  32. //MultiByteToWideChar(CP_ACP, 0, msg, -1, utf16Str, len);
  33. //// 使用MessageBoxW显示消息框
  34. //MessageBoxW(NULL, utf16Str, L"标题", MB_OK);
  35. send_msg(msg, flag);
  36. _asm {
  37. popad
  38. push ebp
  39. mov ebp, esp
  40. add esp, 0xFFFFFFD8
  41. push ebx
  42. push esi
  43. mov ebx, orgNext
  44. jmp ebx
  45. }
  46. //((func_t)trampoline)();
  47. }
  48. bool hook_address(void* target_address, void* hook_function) {
  49. original_ptr = (func_t)target_address;
  50. orgNext = ((int)original_ptr) + 8;
  51. // Allocate memory for trampoline (size of jump + overwritten bytes)
  52. trampoline = VirtualAlloc(nullptr, 13, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  53. if (!trampoline) return false;
  54. // Calculate relative jump addresses
  55. DWORD hook_offset = (DWORD)((char*)hook_function - (char*)target_address - 5);
  56. DWORD return_offset = (DWORD)((char*)target_address + 5 - ((char*)trampoline + 10));
  57. // Save original bytes and create jump in trampoline
  58. memcpy(trampoline, target_address, 8);
  59. *(BYTE*)((char*)trampoline + 8) = 0xE9; // jmp
  60. *(DWORD*)((char*)trampoline + 9) = return_offset;
  61. // Overwrite original function with jump to hook
  62. DWORD old_protect;
  63. VirtualProtect(target_address, 5, PAGE_EXECUTE_READWRITE, &old_protect);
  64. *(BYTE*)target_address = 0xE9; // jmp
  65. *(DWORD*)((char*)target_address + 1) = hook_offset;
  66. VirtualProtect(target_address, 5, old_protect, &old_protect);
  67. return true;
  68. }
  69. void restore_write_screen_call() {
  70. if (original_ptr == nullptr || trampoline == nullptr) {
  71. return;
  72. }
  73. DWORD old_protect;
  74. VirtualProtect(original_ptr, 8, PAGE_EXECUTE_READWRITE, &old_protect);
  75. //memcpy(original_ptr, trampoline, 8);
  76. char org[8] = { 0x55, 0x8b,0xec,0x83,0xc4,0xd8 ,0x53,0x56 };
  77. memcpy(original_ptr, org, 8);
  78. //memcpy(original_ptr, trampoline, 5);
  79. VirtualProtect(original_ptr, 8, old_protect, &old_protect);
  80. }
  81. void any_call(int* data) {
  82. void (*func)() = reinterpret_cast<void(*)()>(data);
  83. func();
  84. }
  85. void Sys::process(int code, int* data)
  86. {
  87. switch (code)
  88. {
  89. case 9001: //hook 写屏call
  90. hwnd = (HWND)data[1];
  91. hook_address((void*)data[0], override_write_screen_call);
  92. break;
  93. case 9002: //恢复写屏call
  94. restore_write_screen_call();
  95. break;
  96. case 9999: //执行任务ASM代码
  97. any_call(data);
  98. break;
  99. default:
  100. break;
  101. }
  102. }