PatchSolution.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. #include "RSACipher.hpp"
  3. #include "CapstoneDisassembler.hpp"
  4. #include "ImageInterpreter.hpp"
  5. class PatchSolution{
  6. public:
  7. virtual void SetFile(void* pFile) = 0;
  8. virtual bool FindPatchOffset() noexcept = 0;
  9. virtual bool CheckKey(RSACipher* pCipher) const = 0;
  10. virtual void MakePatch(RSACipher* pCipher) const = 0;
  11. virtual ~PatchSolution() {}
  12. };
  13. // PatchSolution0 will replace the RSA public key stored in main application.
  14. // Main application can be "Navicat.exe", "Modeler.exe" or "Rviewer.exe"
  15. class PatchSolution0 : public PatchSolution {
  16. private:
  17. static const char Keyword[461];
  18. ImageInterpreter _MainExeInterpreter;
  19. off_t _PatchOffset;
  20. public:
  21. PatchSolution0();
  22. virtual void SetFile(void* pFile) override;
  23. virtual bool FindPatchOffset() noexcept override;
  24. virtual bool CheckKey(RSACipher* pCipher) const override;
  25. virtual void MakePatch(RSACipher* cipher) const override;
  26. };
  27. // PatchSolution1, 2, 3 will replace the RSA public key stored in libcc.dll
  28. class PatchSolution1 : public PatchSolution {
  29. private:
  30. enum KeywordDataType {
  31. IMM_DATA,
  32. STRING_DATA
  33. };
  34. struct KeywordInfo {
  35. const char* Data;
  36. size_t Length;
  37. KeywordDataType Type;
  38. };
  39. struct PatchPointInfo {
  40. uint8_t* Ptr;
  41. size_t PatchSize;
  42. size_t MaxPatchSize;
  43. };
  44. static const KeywordInfo Keywords[5];
  45. ImageInterpreter _LibccDllInterpreter;
  46. mutable PatchPointInfo _Patches[5];
  47. public:
  48. PatchSolution1();
  49. virtual void SetFile(void* pFile) override;
  50. virtual bool FindPatchOffset() noexcept override;
  51. virtual bool CheckKey(RSACipher* pCipher) const override;
  52. virtual void MakePatch(RSACipher* pCipher) const override;
  53. };
  54. class PatchSolution2 : public PatchSolution {
  55. private:
  56. static const char KeywordsMeta[0x188 + 1];
  57. static uint8_t Keywords[0x188][5];
  58. ImageInterpreter _LibccDllInterpreter;
  59. off_t _PatchOffsets[0x188];
  60. void BuildKeywords() noexcept;
  61. public:
  62. PatchSolution2();
  63. virtual void SetFile(void* pFile) override;
  64. virtual bool FindPatchOffset() noexcept override;
  65. virtual bool CheckKey(RSACipher* pCipher) const override;
  66. virtual void MakePatch(RSACipher* pCipher) const override;
  67. };
  68. class PatchSolution3 : public PatchSolution {
  69. private:
  70. enum KeywordDataType {
  71. IMM_DATA,
  72. STRING_DATA,
  73. };
  74. struct KeywordInfo {
  75. uint8_t Data[8];
  76. size_t Length;
  77. KeywordDataType Type;
  78. bool NotRecommendedToModify;
  79. };
  80. struct PatchPointInfo {
  81. union {
  82. uint8_t* Ptr;
  83. const uint8_t* ConstPtr;
  84. } Opcode;
  85. uint64_t OpcodeRva;
  86. uint8_t* PatchPtr;
  87. size_t PatchSize;
  88. char* OriginalStringPtr;
  89. char* ReplaceStringPtr;
  90. };
  91. static const KeywordInfo Keywords[111];
  92. CapstoneEngine _CapstoneEngine;
  93. ImageInterpreter _LibccDllInterpreter;
  94. mutable PatchPointInfo _Patches[111];
  95. bool CheckIfMatchPattern(cs_insn* pInsn) const;
  96. bool CheckIfFound(cs_insn* pInsn, size_t i) const;
  97. PatchPointInfo
  98. CreatePatchPoint(const uint8_t* pOpcode, cs_insn* pInsn, size_t i) const;
  99. CapstoneDisassembler::Context
  100. GetJumpedBranch(const CapstoneDisassembler::Context& NotJumpedBranch, cs_insn* pJxxInsn) const;
  101. CapstoneDisassembler::Context
  102. HandleJcc(const CapstoneDisassembler::Context& NotJumpedBranch,
  103. const CapstoneDisassembler::Context& JumpedBranch,
  104. size_t i) const;
  105. public:
  106. PatchSolution3();
  107. virtual void SetFile(void* pLibccFile) override;
  108. virtual bool FindPatchOffset() noexcept override;
  109. virtual bool CheckKey(RSACipher* pCipher) const override;
  110. virtual void MakePatch(RSACipher* pCipher) const override;
  111. };