PatchSolution0.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "PatchSolution.hpp"
  2. #include "Exception.hpp"
  3. #include "Helper.hpp"
  4. #include <tchar.h>
  5. #undef __BASE_FILE__
  6. #define __BASE_FILE__ TEXT("PatchSolution0.cpp")
  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. PatchSolution0::PatchSolution0() :
  18. _PatchOffset(-1) {}
  19. void PatchSolution0::SetFile(void* pFile) {
  20. if (!_MainExeInterpreter.ParseImage(pFile, true)) {
  21. throw Exception(__BASE_FILE__, __LINE__,
  22. TEXT("Invalid PE file."));
  23. }
  24. }
  25. bool PatchSolution0::FindPatchOffset() noexcept {
  26. _PatchOffset = -1;
  27. auto pResourceSectionHeader =
  28. _MainExeInterpreter.GetSectionHeader(".rsrc");
  29. auto pResourceSection =
  30. _MainExeInterpreter.GetSectionView<uint8_t>(".rsrc");
  31. if (pResourceSectionHeader == nullptr || pResourceSection == nullptr)
  32. return false;
  33. for (DWORD i = 0; i < pResourceSectionHeader->SizeOfRawData; ++i) {
  34. if (memcmp(pResourceSection + i, Keyword, 460) == 0) {
  35. _PatchOffset = pResourceSectionHeader->PointerToRawData + i;
  36. _tprintf_s(TEXT("[*] PatchSolution0 ...... Ready to apply\n"));
  37. _tprintf_s(TEXT(" |--[*] Keyword offset = +0x%.8lx\n"), _PatchOffset);
  38. _tprintf_s(TEXT(" |\n"));
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. bool PatchSolution0::CheckKey(RSACipher* pCipher) const {
  45. std::string PublicKeyString =
  46. pCipher->ExportKeyString<RSAKeyType::PublicKey, RSAKeyFormat::PEM>();
  47. Helper::StringReplace<std::string>(PublicKeyString, "\n", "\r\n");
  48. return PublicKeyString.length() == 460;
  49. }
  50. void PatchSolution0::MakePatch(RSACipher* pCipher) const {
  51. uint8_t* pView = _MainExeInterpreter.GetImageBase<uint8_t>();
  52. std::string PublicKeyString =
  53. pCipher->ExportKeyString<RSAKeyType::PublicKey, RSAKeyFormat::PEM>();
  54. Helper::StringReplace<std::string>(PublicKeyString, "\n", "\r\n");
  55. _putts(TEXT("******************************************"));
  56. _putts(TEXT("* PatchSulution0 *"));
  57. _putts(TEXT("******************************************"));
  58. _tprintf_s(TEXT("@ +0x%.8lx\n"), _PatchOffset);
  59. _putts(TEXT("Previous:"));
  60. Helper::PrintMemory(pView + _PatchOffset,
  61. pView + _PatchOffset + 460,
  62. pView);
  63. memcpy(pView + _PatchOffset, PublicKeyString.c_str(), 460);
  64. _putts(TEXT("After:"));
  65. Helper::PrintMemory(pView + _PatchOffset,
  66. pView + _PatchOffset + 460,
  67. pView);
  68. }