PatchSolutions.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include "Exception.hpp"
  3. #include "RSACipher.hpp"
  4. #include "ResourceTraitsUnix.hpp"
  5. #include "CapstoneDisassembler.hpp"
  6. #include "KeystoneAssembler.hpp"
  7. #include "X64ImageInterpreter.hpp"
  8. #include <map>
  9. #include <optional>
  10. namespace nkg {
  11. class PatchSolution {
  12. public:
  13. [[nodiscard]]
  14. virtual bool FindPatchOffset() noexcept = 0;
  15. [[nodiscard]]
  16. virtual bool CheckKey(const RSACipher& Cipher) const noexcept = 0;
  17. virtual void MakePatch(const RSACipher& Cipher) const = 0;
  18. virtual ~PatchSolution() = default;
  19. };
  20. class PatchSolution0 final : public PatchSolution {
  21. private:
  22. static const char Keyword[451];
  23. const X64ImageInterpreter& m_Image;
  24. std::optional<X64ImageOffset> m_PatchOffset;
  25. public:
  26. PatchSolution0(const X64ImageInterpreter& Image);
  27. [[nodiscard]]
  28. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  29. virtual bool FindPatchOffset() noexcept override;
  30. [[nodiscard]]
  31. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  32. virtual bool CheckKey(const RSACipher& Cipher) const noexcept override;
  33. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  34. virtual void MakePatch(const RSACipher& Cipher) const override;
  35. };
  36. class PatchSolution1 final : public PatchSolution {
  37. private:
  38. static const uint8_t Keyword[0x188];
  39. const X64ImageInterpreter& m_Image;
  40. std::optional<X64ImageOffset> m_PatchOffset;
  41. public:
  42. PatchSolution1(const X64ImageInterpreter& Image);
  43. [[nodiscard]]
  44. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  45. virtual bool FindPatchOffset() noexcept override;
  46. [[nodiscard]]
  47. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  48. virtual bool CheckKey(const RSACipher& Cipher) const noexcept override;
  49. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  50. virtual void MakePatch(const RSACipher& Cipher) const override;
  51. };
  52. class PatchSolution2 final : public PatchSolution {
  53. private:
  54. static const char Keyword[1114];
  55. const X64ImageInterpreter& m_Image;
  56. CapstoneEngine m_DisassemblerEngine;
  57. KeystoneEngine m_AssemblerEngine;
  58. std::optional<X64ImageOffset> m_KeywordOffset;
  59. std::optional<X64ImageOffset> m_FunctionOffset;
  60. std::optional<X64ImageAddress> m_StdStringAppendStubRva;
  61. [[nodiscard]]
  62. const char* TryResolveStubHelper(const void* lpStubHelperProc) const;
  63. public:
  64. PatchSolution2(const X64ImageInterpreter& Image);
  65. [[nodiscard]]
  66. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  67. virtual bool FindPatchOffset() noexcept override;
  68. [[nodiscard]]
  69. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  70. virtual bool CheckKey(const RSACipher& Cipher) const noexcept override;
  71. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  72. virtual void MakePatch(const RSACipher& Cipher) const override;
  73. };
  74. class PatchSolution3 final : public PatchSolution {
  75. private:
  76. const X64ImageInterpreter& m_Image;
  77. CapstoneEngine m_DisassemblerEngine;
  78. KeystoneEngine m_AssemblerEngine;
  79. void* m_lpfnGenerateKeyA;
  80. void* m_lpfnGenerateKeyB;
  81. std::vector<uint8_t> m_fnNewGenerateKeyA;
  82. std::vector<uint8_t> m_fnNewGenerateKeyB;
  83. void ScanInstructions(
  84. std::map<X64ImageAddress, X64ImageSize>& Instructions,
  85. const section_64* lpSection,
  86. const void* lpProcStart
  87. ) const;
  88. [[nodiscard]]
  89. const char* TryResolveStubHelper(const void* lpStubHelperProc) const;
  90. public:
  91. PatchSolution3(const X64ImageInterpreter& Image);
  92. [[nodiscard]]
  93. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  94. virtual bool FindPatchOffset() noexcept override;
  95. [[nodiscard]]
  96. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  97. virtual bool CheckKey(const RSACipher& Cipher) const noexcept override;
  98. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  99. virtual void MakePatch(const RSACipher& Cipher) const override;
  100. };
  101. }