PatchSolution4-amd64.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "PatchSolutions.hpp"
  2. #include <xstring.hpp>
  3. #undef NKG_CURRENT_SOURCE_FILE
  4. #undef NKG_CURRENT_SOURCE_LINE
  5. #define NKG_CURRENT_SOURCE_FILE() TEXT(".\\navicat-patcher\\PatchSolution4-amd64.cpp")
  6. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  7. namespace nkg {
  8. PatchSolution4::PatchSolution4(const ImageInterpreter& Image) :
  9. _Image(Image),
  10. _DisassemblyEngine(CS_ARCH_X86, CS_MODE_64),
  11. _AssemblyEngine(KS_ARCH_X86, KS_MODE_64),
  12. _pbPatchMachineCode(nullptr),
  13. _pbPatchNewPublicKey(nullptr) { _DisassemblyEngine.Option(CS_OPT_DETAIL, CS_OPT_ON); }
  14. PatchSolution4::PatchSolution4(const ImageInterpreter* Image) :
  15. _Image(*Image),
  16. _DisassemblyEngine(CS_ARCH_X86, CS_MODE_64),
  17. _AssemblyEngine(KS_ARCH_X86, KS_MODE_64),
  18. _pbPatchMachineCode(nullptr),
  19. _pbPatchNewPublicKey(nullptr) { _DisassemblyEngine.Option(CS_OPT_DETAIL, CS_OPT_ON); }
  20. bool PatchSolution4::FindPatchOffset() noexcept {
  21. try {
  22. _pbPatchMachineCode = _Image.SearchSection<uint8_t*>(".text", [](const uint8_t* p) {
  23. __try {
  24. return
  25. p[0] == 0x48 && p[1] == 0x8d && // prefix of "lea rcx, [rbp+5Fh+var_38]"
  26. p[4] == 0x48 && p[5] == 0x83 && // prefix of "cmp [rbp+5Fh+var_20], 10h"
  27. p[9] == 0x48 && p[10] == 0x0f && p[11] == 0x43 && // prefix of "cmovnb rcx, [rbp+5Fh+var_38]"
  28. p[14] == 0x48 && p[15] == 0x8d && // prefix of "lea rax, [rbp+5Fh+var_58]"
  29. p[18] == 0x48 && p[19] == 0x83 && // prefix of "cmp [rbp+5Fh+var_40], 10h"
  30. p[23] == 0x48 && p[24] == 0x0f && p[25] == 0x43 && // prefix of "cmovnb rax, [rbp+5Fh+var_58]"
  31. p[28] == 0x44 && p[29] == 0x0f && p[30] == 0xb6 && // prefix of "movzx r8d, byte ptr [rax+rdi]"
  32. p[33] == 0x44 && p[34] == 0x02 && // prefix of "add r8b, [rcx+rdi]"
  33. p[37] == 0xba && // prefix of "mov edx, 1"
  34. p[42] == 0x48 && p[43] == 0x8b && // prefix of "mov rcx, rbx"
  35. p[45] == 0xe8; // prefix of "call sub_1806E65F0"
  36. } __except (EXCEPTION_EXECUTE_HANDLER) {
  37. return false;
  38. }
  39. });
  40. auto RegisterIndex = X86_REG_INVALID;
  41. auto Disassembler = _DisassemblyEngine.CreateDisassembler();
  42. Disassembler.SetContext({ _pbPatchMachineCode, 45, _Image.PointerToVa(_pbPatchMachineCode) });
  43. if (Disassembler.Next() && Disassembler.Next() && Disassembler.Next() && Disassembler.Next() && Disassembler.Next() && Disassembler.Next() && Disassembler.Next()) {
  44. auto lpInsn = Disassembler.GetInstruction();
  45. if (_stricmp(lpInsn->mnemonic, "movzx") == 0 &&
  46. lpInsn->detail->x86.op_count == 2 &&
  47. lpInsn->detail->x86.operands[0].type == X86_OP_REG &&
  48. lpInsn->detail->x86.operands[1].type == X86_OP_MEM &&
  49. lpInsn->detail->x86.operands[1].size == 1 &&
  50. lpInsn->detail->x86.operands[1].mem.scale == 1)
  51. {
  52. RegisterIndex = lpInsn->detail->x86.operands[1].mem.index;
  53. } else {
  54. throw nkg::Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("Unexpected machine code."));
  55. }
  56. } else {
  57. throw nkg::Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("Disassemble failed."));
  58. }
  59. if (Disassembler.Next() && Disassembler.Next() && Disassembler.Next()) {
  60. auto lpInsn = Disassembler.GetInstruction();
  61. //
  62. // The previous instruction of "call sub_1806E65F0" should set RCX register.
  63. //
  64. if (_stricmp(lpInsn->mnemonic, "mov") != 0 ||
  65. lpInsn->detail->x86.op_count < 1 ||
  66. lpInsn->detail->x86.operands[0].type != X86_OP_REG ||
  67. lpInsn->detail->x86.operands[0].reg != X86_REG_RCX)
  68. {
  69. throw nkg::Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("Unexpected machine code."));
  70. }
  71. } else {
  72. throw nkg::Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("Disassemble failed."));
  73. }
  74. for (size_t i = 0; i < _Image.NumberOfSections(); ++i) {
  75. auto lpSectionHeader = _Image.ImageSectionHeader(i);
  76. if (lpSectionHeader->SizeOfRawData > lpSectionHeader->Misc.VirtualSize) {
  77. auto cbReserved =
  78. lpSectionHeader->SizeOfRawData -
  79. lpSectionHeader->Misc.VirtualSize;
  80. if (cbReserved >= 0x188) {
  81. _pbPatchNewPublicKey = _Image.ImageSectionView<uint8_t*>(lpSectionHeader, lpSectionHeader->Misc.VirtualSize);
  82. auto Assembler = _AssemblyEngine.CreateAssembler();
  83. _NewMachineCode = Assembler.GenerateMachineCode(
  84. std::xstring::format(
  85. TEXT(
  86. "lea rax, qword ptr[0x%.16llx];"
  87. "mov r8b, byte ptr[rax + %hs];"
  88. "mov edx, 1;"
  89. ),
  90. _Image.PointerToVa(_pbPatchNewPublicKey),
  91. _DisassemblyEngine.GetRegisterName(RegisterIndex)
  92. ).explicit_string().c_str(),
  93. _Image.PointerToVa(_pbPatchMachineCode)
  94. );
  95. // >>>>>>>>>>>> .text:00000001819B02C0 48 8D 4D 27 lea rcx, [rbp + 5Fh + var_38]
  96. // .text:00000001819B02C4 48 83 7D 3F 10 cmp[rbp + 5Fh + var_20], 10h
  97. // 42 BYTES .text:00000001819B02C9 48 0F 43 4D 27 cmovnb rcx, [rbp + 5Fh + var_38]
  98. // .text:00000001819B02CE 48 8D 45 07 lea rax, [rbp + 5Fh + var_58]
  99. // THESE CODE .text:00000001819B02D2 48 83 7D 1F 10 cmp[rbp + 5Fh + var_40], 10h
  100. // WILL BE .text:00000001819B02D7 48 0F 43 45 07 cmovnb rax, [rbp + 5Fh + var_58]
  101. // REPLACED .text:00000001819B02DC 44 0F B6 04 38 movzx r8d, byte ptr[rax + rdi]
  102. // .text:00000001819B02E1 44 02 04 39 add r8b, [rcx + rdi]
  103. // <<<<<<<<<<<< .text:00000001819B02E5 BA 01 00 00 00 mov edx, 1
  104. // .text:00000001819B02EA 48 8B CB mov rcx, rbx
  105. // .text:00000001819B02ED E8 FE 62 D3 FE call sub_1806E65F0
  106. while (_NewMachineCode.size() < 42) {
  107. _NewMachineCode.emplace_back(0x90); // padding with "nop"
  108. }
  109. if (_NewMachineCode.size() != 42) {
  110. throw nkg::Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("Something unexpected happens."));
  111. }
  112. LOG_SUCCESS(0, "PatchSolution4 ...... Ready to apply");
  113. LOG_HINT(4, "Machine code patch VA = 0x%zx", _Image.PointerToVa(_pbPatchMachineCode));
  114. LOG_HINT(4, "New public key VA = 0x%zx", _Image.PointerToVa(_pbPatchNewPublicKey));
  115. LOG_HINT(4, "New public key offset = 0x%zx", _Image.PointerToFileOffset(_pbPatchNewPublicKey));
  116. return true;
  117. }
  118. }
  119. }
  120. throw nkg::Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("No space to store public key."));
  121. } catch (nkg::Exception&) {
  122. _pbPatchMachineCode = nullptr;
  123. _pbPatchNewPublicKey = nullptr;
  124. _NewMachineCode.clear();
  125. LOG_FAILURE(0, "PatchSolution4 ...... Omitted");
  126. return false;
  127. }
  128. }
  129. }