PatchSolution2-generic.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\\PatchSolution2-generic.cpp")
  5. #define NKG_CURRENT_SOURCE_LINE() __LINE__
  6. namespace nkg {
  7. const char PatchSolution2::KeywordMeta[0x188 + 1] =
  8. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw1dqF3SkCaAAmMzs889I"
  9. "qdW9M2dIdh3jG9yPcmLnmJiGpBF4E9VHSMGe8oPAy2kJDmdNt4BcEygvssEfginv"
  10. "a5t5jm352UAoDosUJkTXGQhpAWMF4fBmBpO3EedG62rOsqMBgmSdAyxCSPBRJIOF"
  11. "R0QgZFbRnU0frj34fiVmgYiLuZSAmIbs8ZxiHPdp1oD4tUpvsFci4QJtYNjNnGU2"
  12. "WPH6rvChGl1IRKrxMtqLielsvajUjyrgOC6NmymYMvZNER3htFEtL1eQbCyTfDmt"
  13. "YyQ1Wt4Ot12lxf0wVIR5mcGN7XCXJRHOFHSf1gzXWabRSvmt1nrl7sW6cjxljuuQ"
  14. "awIDAQAB";
  15. [[nodiscard]]
  16. bool PatchSolution2::CheckKey(const RSACipher& Cipher) const noexcept {
  17. auto szPublicKey = Cipher.ExportKeyString<RSAKeyType::PublicKey, RSAKeyFormat::PEM>();
  18. for (auto pos = szPublicKey.find("-----BEGIN PUBLIC KEY-----"); pos != std::string::npos; pos = szPublicKey.find("-----BEGIN PUBLIC KEY-----", pos)) {
  19. szPublicKey.erase(pos, literal_length("-----BEGIN PUBLIC KEY-----"));
  20. }
  21. for (auto pos = szPublicKey.find("-----END PUBLIC KEY-----"); pos != std::string::npos; pos = szPublicKey.find("-----END PUBLIC KEY-----", pos)) {
  22. szPublicKey.erase(pos, literal_length("-----END PUBLIC KEY-----"));
  23. }
  24. for (auto pos = szPublicKey.find("\n"); pos != std::string::npos; pos = szPublicKey.find("\n", pos)) {
  25. szPublicKey.erase(pos, literal_length("\n"));
  26. }
  27. return szPublicKey.length() == literal_length(KeywordMeta);
  28. }
  29. void PatchSolution2::MakePatch(const RSACipher& Cipher) const {
  30. for (size_t i = 0; i < _countof(_PatchOffset); ++i) {
  31. if (_PatchOffset[i] == InvalidOffset) {
  32. throw Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("PatchSolution2 has not been ready yet."));
  33. }
  34. }
  35. auto pbImage = _Image.ImageBase<uint8_t*>();
  36. auto szPublicKey = Cipher.ExportKeyString<RSAKeyType::PublicKey, RSAKeyFormat::PEM>();
  37. for (auto pos = szPublicKey.find("-----BEGIN PUBLIC KEY-----"); pos != std::string::npos; pos = szPublicKey.find("-----BEGIN PUBLIC KEY-----", pos)) {
  38. szPublicKey.erase(pos, literal_length("-----BEGIN PUBLIC KEY-----"));
  39. }
  40. for (auto pos = szPublicKey.find("-----END PUBLIC KEY-----"); pos != std::string::npos; pos = szPublicKey.find("-----END PUBLIC KEY-----", pos)) {
  41. szPublicKey.erase(pos, literal_length("-----END PUBLIC KEY-----"));
  42. }
  43. for (auto pos = szPublicKey.find("\n"); pos != std::string::npos; pos = szPublicKey.find("\n", pos)) {
  44. szPublicKey.erase(pos, literal_length("\n"));
  45. }
  46. if (szPublicKey.length() != literal_length(KeywordMeta)) {
  47. throw Exception(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), TEXT("szPublicKey.length() != literal_length(KeywordMeta)"));
  48. }
  49. _putts(TEXT("*******************************************************"));
  50. _putts(TEXT("* PatchSolution2 *"));
  51. _putts(TEXT("*******************************************************"));
  52. for (size_t i = 0; i < _countof(_PatchOffset); i += 2) {
  53. static_assert(_countof(_PatchOffset) % 2 == 0);
  54. LOG_HINT(0, "+0x%.8zx: %.2x %.2x %.2x -> %.2x %.2x %.2x | +0x%.8zx: %.2x %.2x %.2x -> %.2x %.2x %.2x",
  55. _PatchOffset[i],
  56. pbImage[_PatchOffset[i]],
  57. pbImage[_PatchOffset[i] + 1],
  58. pbImage[_PatchOffset[i] + 2],
  59. pbImage[_PatchOffset[i]],
  60. pbImage[_PatchOffset[i] + 1],
  61. szPublicKey[i],
  62. _PatchOffset[i + 1],
  63. pbImage[_PatchOffset[i + 1]],
  64. pbImage[_PatchOffset[i + 1] + 1],
  65. pbImage[_PatchOffset[i + 1] + 2],
  66. pbImage[_PatchOffset[i + 1]],
  67. pbImage[_PatchOffset[i + 1] + 1],
  68. szPublicKey[i + 1]
  69. );
  70. pbImage[_PatchOffset[i] + 2] = szPublicKey[i];
  71. pbImage[_PatchOffset[i + 1] + 2] = szPublicKey[i + 1];
  72. }
  73. }
  74. }