LuaUtils.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "LuaUtils.h"
  2. namespace fs = std::filesystem;
  3. const PIMAGE_SECTION_HEADER get_section_by_name(HMODULE base, const char* name) {
  4. const PIMAGE_DOS_HEADER hdos = (PIMAGE_DOS_HEADER)base;
  5. const PIMAGE_NT_HEADERS hpe = (PIMAGE_NT_HEADERS)((PBYTE)hdos + hdos->e_lfanew);
  6. const PIMAGE_SECTION_HEADER sections = IMAGE_FIRST_SECTION(hpe);
  7. for (int i = 0; i < hpe->FileHeader.NumberOfSections; i++) {
  8. if (strcmp((const char*)sections[i].Name, name) == 0) {
  9. return &sections[i];
  10. }
  11. }
  12. return NULL;
  13. }
  14. uintptr_t PatternScan(LPCSTR module, LPCSTR pattern) {
  15. static auto pattern_to_byte = [](const char* pattern) {
  16. auto bytes = std::vector<int>{};
  17. auto start = const_cast<char*>(pattern);
  18. auto end = const_cast<char*>(pattern) + strlen(pattern);
  19. for (auto current = start; current < end; ++current) {
  20. if (*current == '?') {
  21. ++current;
  22. if (*current == '?')
  23. ++current;
  24. bytes.push_back(-1);
  25. }
  26. else {
  27. bytes.push_back(strtoul(current, &current, 16));
  28. }
  29. }
  30. //for (auto& b : bytes) Utils::ConsolePrint("%02x ", b);
  31. return bytes;
  32. };
  33. auto mod = GetModuleHandleA(module);
  34. if (!mod)
  35. return 0;
  36. auto dosHeader = (PIMAGE_DOS_HEADER)mod;
  37. auto ntHeaders = (PIMAGE_NT_HEADERS)((std::uint8_t*)mod + dosHeader->e_lfanew);
  38. auto sizeOfImage = ntHeaders->OptionalHeader.SizeOfImage;
  39. auto patternBytes = pattern_to_byte(pattern);
  40. auto scanBytes = reinterpret_cast<std::uint8_t*>(mod);
  41. auto s = patternBytes.size();
  42. auto d = patternBytes.data();
  43. for (auto i = 0ul; i < sizeOfImage - s; ++i) {
  44. bool found = true;
  45. for (auto j = 0ul; j < s; ++j) {
  46. if (scanBytes[i + j] != d[j] && d[j] != -1) {
  47. found = false;
  48. break;
  49. }
  50. }
  51. if (found) {
  52. return (uintptr_t)&scanBytes[i];
  53. }
  54. }
  55. return 0;
  56. }
  57. bool cmp_pat(const uint8_t* pat, const char* mask, size_t pattern_len, const uint8_t* ptr) {
  58. for (int i = 0; i < pattern_len; i++)
  59. if (mask[i] == 'x' && pat[i] != ptr[i])
  60. return false;
  61. return true;
  62. }
  63. const uint8_t* find_pat(const uint8_t* pat, const char* mask, const uint8_t* start, size_t size, bool reverse) {
  64. auto pattern_len = strlen(mask);
  65. auto end = start + size - pattern_len;
  66. if (reverse) {
  67. for (auto ptr = end; ptr >= start; ptr--)
  68. if (cmp_pat(pat, mask, pattern_len, ptr) == true)
  69. return ptr;
  70. } else {
  71. for (auto ptr = start; ptr <= end; ptr++)
  72. if (cmp_pat(pat, mask, pattern_len, ptr) == true)
  73. return ptr;
  74. }
  75. return 0;
  76. }
  77. const uint8_t* find_ref_relative(const uint8_t* addr, const uint8_t* start, size_t size, bool reverse) {
  78. auto end = start + size - 4;
  79. if (reverse) {
  80. for (auto ptr = end; ptr >= start; ptr--)
  81. if (*(uint32_t*)(ptr) + ptr + 4 == addr)
  82. return ptr;
  83. } else {
  84. for (auto ptr = start; ptr <= end; ptr++)
  85. if (*(uint32_t*)(ptr) + ptr + 4 == addr)
  86. return ptr;
  87. }
  88. return 0;
  89. }