Advertisement
Guest User

Untitled

a guest
Jul 8th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <windows.h>
  2. #include "minhook/MinHook.h"
  3. #include <cstdint>
  4. #include <vector>
  5. #include <string>
  6. #include <iostream>
  7. #pragma comment(lib, "libMinHook.x86.lib")
  8.  
  9. #define InRange(x, a, b) (x >= a && x <= b)
  10. #define getBit(x) (InRange((x & (~0x20)), 'A', 'F') ? ((x & (~0x20)) - 'A' + 0xA): (InRange(x, '0', '9') ? x - '0': 0))
  11. #define getByte(x) (getBit(x[0]) << 4 | getBit(x[1]))
  12.  
  13. typedef void(__cdecl* oprintfunc)(char*, ...);
  14. oprintfunc printfunc = nullptr;
  15.  
  16. std::uint8_t* scan(void* module, const char* signature)
  17. {
  18.     static auto pattern_to_byte = [](const char* pattern) {
  19.         auto bytes = std::vector<int>{};
  20.         auto start = const_cast<char*>(pattern);
  21.         auto end = const_cast<char*>(pattern) + strlen(pattern);
  22.  
  23.         for (auto current = start; current < end; ++current) {
  24.             if (*current == '?') {
  25.                 ++current;
  26.                 if (*current == '?')
  27.                     ++current;
  28.                 bytes.push_back(-1);
  29.             }
  30.             else {
  31.                 bytes.push_back(strtoul(current, &current, 16));
  32.             }
  33.         }
  34.         return bytes;
  35.     };
  36.  
  37.     auto dosHeader = (PIMAGE_DOS_HEADER)module;
  38.     auto ntHeaders = (PIMAGE_NT_HEADERS)((std::uint8_t*)module + dosHeader->e_lfanew);
  39.  
  40.     auto sizeOfImage = ntHeaders->OptionalHeader.SizeOfImage;
  41.     auto patternBytes = pattern_to_byte(signature);
  42.     auto scanBytes = reinterpret_cast<std::uint8_t*>(module);
  43.  
  44.     auto s = patternBytes.size();
  45.     auto d = patternBytes.data();
  46.  
  47.     for (auto i = 0ul; i < sizeOfImage - s; ++i) {
  48.         bool found = true;
  49.         for (auto j = 0ul; j < s; ++j) {
  50.             if (scanBytes[i + j] != d[j] && d[j] != -1) {
  51.                 found = false;
  52.                 break;
  53.             }
  54.         }
  55.         if (found) {
  56.             return &scanBytes[i];
  57.         }
  58.     }
  59.     return nullptr;
  60. }
  61.  
  62. int hookedPrint(char* Format, ...) {
  63.     std::cout << "[hookedPrint]: " << Format << std::endl;
  64.  
  65.     if (std::string(Format).find("Loading script") != -1) {
  66.         return 0;
  67.     }
  68. }
  69.  
  70. bool __stdcall DllMain( HINSTANCE hModule,
  71.                        unsigned long  ul_reason_for_call,
  72.                        void* lpReserved
  73.                      )
  74. {
  75.     if (ul_reason_for_call == 1) {
  76.         DisableThreadLibraryCalls(hModule);
  77.         AllocConsole();
  78.         FILE* fDummy;
  79.         freopen_s(&fDummy, "CONIN$", "r", stdin);
  80.         freopen_s(&fDummy, "CONOUT$", "w", stderr);
  81.         freopen_s(&fDummy, "CONOUT$", "w", stdout);
  82.  
  83.         std::uint8_t* func_addr = scan(GetModuleHandleW(L"SAMPFUNCS.asi"), "55 8B EC B8 ? ? ? ? E8 ? ? ? ? 8D 45 0C");
  84.  
  85.         MH_Initialize();
  86.         MH_CreateHook(reinterpret_cast<void*>(func_addr), &hookedPrint, reinterpret_cast<void**>(&printfunc));
  87.         MH_EnableHook(reinterpret_cast<void*>(func_addr));
  88.     }
  89.     return true;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement