PatchSolutions.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include "RSACipher.hpp"
  3. #include "Elf64Interpreter.hpp"
  4. #include "CapstoneDisassembler.hpp"
  5. #include "KeystoneAssembler.hpp"
  6. #include <optional>
  7. namespace nkg {
  8. class PatchSolution {
  9. protected:
  10. struct PatchMarkType {
  11. uint32_t Starter;
  12. uint8_t Data[0x188];
  13. uint32_t Terminator;
  14. };
  15. static constexpr uint32_t PatchMarkStarter = 0xdeadbeef;
  16. static constexpr uint32_t PatchMarkTerminator = 0xbeefdead;
  17. void SearchFreeSpace(std::map<Elf64_Off, Elf64_Xword>& SpaceMap, const Elf64Interpreter& Image);
  18. public:
  19. [[nodiscard]]
  20. virtual bool FindPatchOffset() noexcept = 0;
  21. [[nodiscard]]
  22. virtual bool CheckKey(const RSACipher& Cipher) const noexcept = 0;
  23. virtual void MakePatch(const RSACipher& Cipher) const = 0;
  24. virtual ~PatchSolution() = default;
  25. };
  26. class PatchSolution0 final : public PatchSolution {
  27. private:
  28. const Elf64Interpreter& m_Image;
  29. CapstoneEngine m_DisassemblyEngine;
  30. KeystoneEngine m_AssemblyEngine;
  31. const Elf64_Phdr* m_RefSegment;
  32. std::optional<Elf64_Off> m_PatchMarkOffset;
  33. std::optional<Elf64_Addr> m_MachineCodeRva;
  34. std::optional<size_t> m_MachineCodeSize;
  35. std::vector<uint8_t> m_MachineCodeNew;
  36. public:
  37. PatchSolution0(const Elf64Interpreter& Image);
  38. [[nodiscard]]
  39. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  40. virtual bool FindPatchOffset() noexcept override;
  41. [[nodiscard]]
  42. // NOLINTNEXTLINE: mark "virtual" explicitly for more readability
  43. virtual bool CheckKey(const RSACipher& Cipher) const noexcept override;
  44. virtual void MakePatch(const RSACipher& Cipher) const override;
  45. };
  46. }