PatchSolution0.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "PatchSolutions.hpp"
  2. #undef NKG_CURRENT_SOURCE_FILE
  3. #undef NKG_CURRENT_SOURCE_LINE
  4. #define NKG_CURRENT_SOURCE_FILE() TEXT(".\\navicat-patcher\\PatchSolution0.cpp")
  5. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  6. namespace nkg {
  7. const char PatchSolution0::Keyword[461] =
  8. "-----BEGIN PUBLIC KEY-----\r\n"
  9. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw1dqF3SkCaAAmMzs889I\r\n"
  10. "qdW9M2dIdh3jG9yPcmLnmJiGpBF4E9VHSMGe8oPAy2kJDmdNt4BcEygvssEfginv\r\n"
  11. "a5t5jm352UAoDosUJkTXGQhpAWMF4fBmBpO3EedG62rOsqMBgmSdAyxCSPBRJIOF\r\n"
  12. "R0QgZFbRnU0frj34fiVmgYiLuZSAmIbs8ZxiHPdp1oD4tUpvsFci4QJtYNjNnGU2\r\n"
  13. "WPH6rvChGl1IRKrxMtqLielsvajUjyrgOC6NmymYMvZNER3htFEtL1eQbCyTfDmt\r\n"
  14. "YyQ1Wt4Ot12lxf0wVIR5mcGN7XCXJRHOFHSf1gzXWabRSvmt1nrl7sW6cjxljuuQ\r\n"
  15. "awIDAQAB\r\n"
  16. "-----END PUBLIC KEY-----\r\n";
  17. [[nodiscard]]
  18. bool PatchSolution0::FindPatchOffset() noexcept {
  19. try {
  20. _PatchOffset = _Image.PointerToFileOffset(
  21. _Image.SearchSection<uint8_t*>(".rsrc", [](const uint8_t* p) {
  22. __try {
  23. return memcmp(p, Keyword, literal_length(Keyword)) == 0;
  24. } __except (EXCEPTION_EXECUTE_HANDLER) {
  25. return false;
  26. }
  27. })
  28. );
  29. LOG_SUCCESS(0, "PatchSolution0 ...... Ready to apply");
  30. LOG_HINT(4, "Patch offset = +0x%.8zx", _PatchOffset);
  31. return true;
  32. } catch (nkg::Exception&) {
  33. _PatchOffset = InvalidOffset;
  34. LOG_FAILURE(0, "PatchSolution0 ...... Omitted");
  35. return false;
  36. }
  37. }
  38. [[nodiscard]]
  39. bool PatchSolution0::CheckKey(const RSACipher& Cipher) const noexcept {
  40. auto szPublicKey = Cipher.ExportKeyString<RSAKeyType::PublicKey, RSAKeyFormat::PEM>();
  41. for (auto i = szPublicKey.find("\n"); i != std::string::npos; i = szPublicKey.find("\n", i + 2)) {
  42. szPublicKey.replace(i, 1, "\r\n");
  43. }
  44. return szPublicKey.length() == literal_length(Keyword);
  45. }
  46. void PatchSolution0::MakePatch(const RSACipher& Cipher) const {
  47. if (_PatchOffset == InvalidOffset) {
  48. throw Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("PatchSolution0 has not been ready yet."));
  49. }
  50. auto szPublicKey = Cipher.ExportKeyString<RSAKeyType::PublicKey, RSAKeyFormat::PEM>();
  51. for (auto i = szPublicKey.find("\n"); i != std::string::npos; i = szPublicKey.find("\n", i + 2)) {
  52. szPublicKey.replace(i, 1, "\r\n");
  53. }
  54. _putts(TEXT("*******************************************************"));
  55. _putts(TEXT("* PatchSolution0 *"));
  56. _putts(TEXT("*******************************************************"));
  57. LOG_HINT(0, "Previous:");
  58. PrintMemory(_Image.ImageOffset(_PatchOffset), literal_length(Keyword), _Image.ImageBase());
  59. memcpy(_Image.ImageOffset(_PatchOffset), szPublicKey.c_str(), literal_length(Keyword));
  60. LOG_HINT(0, "After:");
  61. PrintMemory(_Image.ImageOffset(_PatchOffset), literal_length(Keyword), _Image.ImageBase());
  62. _putts(TEXT(""));
  63. }
  64. }