PatchSolution0.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "PatchSolutions.hpp"
  2. #include "Misc.hpp"
  3. #include <memory.h>
  4. namespace nkg {
  5. const char PatchSolution0::Keyword[451] =
  6. "-----BEGIN PUBLIC KEY-----\x00"
  7. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw1dqF3SkCaAAmMzs889I\x00"
  8. "qdW9M2dIdh3jG9yPcmLnmJiGpBF4E9VHSMGe8oPAy2kJDmdNt4BcEygvssEfginv\x00"
  9. "a5t5jm352UAoDosUJkTXGQhpAWMF4fBmBpO3EedG62rOsqMBgmSdAyxCSPBRJIOF\x00"
  10. "R0QgZFbRnU0frj34fiVmgYiLuZSAmIbs8ZxiHPdp1oD4tUpvsFci4QJtYNjNnGU2\x00"
  11. "WPH6rvChGl1IRKrxMtqLielsvajUjyrgOC6NmymYMvZNER3htFEtL1eQbCyTfDmt\x00"
  12. "YyQ1Wt4Ot12lxf0wVIR5mcGN7XCXJRHOFHSf1gzXWabRSvmt1nrl7sW6cjxljuuQ\x00"
  13. "awIDAQAB\x00"
  14. "-----END PUBLIC KEY-----";
  15. PatchSolution0::PatchSolution0(const X64ImageInterpreter& Image) :
  16. m_Image(Image) {}
  17. [[nodiscard]]
  18. bool PatchSolution0::FindPatchOffset() noexcept {
  19. try {
  20. auto lpPatch = m_Image.SearchSection("__TEXT", "__cstring", [](const void* base, size_t i, size_t size) {
  21. if (i + sizeof(Keyword) <= size) {
  22. auto p = ARL::AddressOffset(base, i);
  23. return memcmp(p, Keyword, sizeof(Keyword) - 1) == 0;
  24. } else {
  25. return false;
  26. }
  27. });
  28. if (lpPatch) {
  29. m_PatchOffset = m_Image.ConvertPtrToOffset(lpPatch);
  30. } else {
  31. throw ARL::Exception(__FILE__, __LINE__, "not found.");
  32. }
  33. printf("[+] PatchSolution0 ...... Ready to apply.\n");
  34. printf(" Keyword offset = +0x%.8x\n", m_PatchOffset.value());
  35. return true;
  36. } catch (...) {
  37. printf("[-] PatchSolution0 ...... Omitted.\n");
  38. return false;
  39. }
  40. }
  41. [[nodiscard]]
  42. bool PatchSolution0::CheckKey(const RSACipher& Cipher) const noexcept {
  43. try {
  44. return Cipher.Bits() == 2048;
  45. } catch (...) {
  46. return false;
  47. }
  48. }
  49. void PatchSolution0::MakePatch(const RSACipher& Cipher) const {
  50. if (m_PatchOffset.has_value()) {
  51. std::string szPublicKeyPEM = Cipher.ExportKeyString<RSAKeyType::PublicKey, RSAKeyFormat::PEM>();
  52. for (auto& c : szPublicKeyPEM) {
  53. if (c == '\n') {
  54. c = '\x00';
  55. }
  56. }
  57. while (szPublicKeyPEM.length() < sizeof(Keyword)) {
  58. szPublicKeyPEM.push_back('\x00');
  59. }
  60. auto lpPatch = m_Image.ImageOffset(m_PatchOffset.value());
  61. puts("**************************************************************");
  62. puts("* PatchSolution0 *");
  63. puts("**************************************************************");
  64. printf("[*] Previous:\n");
  65. Misc::PrintMemory(lpPatch, sizeof(Keyword), m_Image.ImageBase());
  66. memcpy(lpPatch, szPublicKeyPEM.data(), sizeof(Keyword));
  67. printf("[*] After:\n");
  68. Misc::PrintMemory(lpPatch, sizeof(Keyword), m_Image.ImageBase());
  69. printf("\n");
  70. } else {
  71. throw ARL::Exception(__FILE__, __LINE__, "PatchSolution0: not ready yet.");
  72. }
  73. }
  74. }