/ deps / KittyMemoryEx / KittyMemoryMgr.hpp
KittyMemoryMgr.hpp
  1  #pragma once
  2  
  3  #include "KittyUtils.hpp"
  4  #include "KittyIOFile.hpp"
  5  #include "KittyMemoryEx.hpp"
  6  #include "KittyMemOp.hpp"
  7  #include "MemoryPatch.hpp"
  8  #include "MemoryBackup.hpp"
  9  #include "KittyScanner.hpp"
 10  #include "KittyTrace.hpp"
 11  #include "KittyAsm.hpp"
 12  #include "KittyPtrValidator.hpp"
 13  #include "KittyPerfEvent.hpp"
 14  
 15  using KittyMemoryEx::EProcMapFilter;
 16  using KittyMemoryEx::ProcMap;
 17  using KittyMemoryEx::ProcStatus;
 18  
 19  /**
 20   * @brief Class that manages memory operations for a remote process.
 21   */
 22  class KittyMemoryMgr
 23  {
 24  private:
 25      bool _init;
 26      pid_t _pid;
 27      std::string _process_name;
 28      EKittyMemOP _eMemOp;
 29      std::unique_ptr<IKittyMemOp> _pMemOp;
 30      std::unique_ptr<IKittyMemOp> _pMemOpPatch;
 31  
 32  public:
 33      /// @brief MemoryPatch Manager
 34      MemoryPatchMgr memPatch;
 35      /// @brief MemoryBackup Manager
 36      MemoryBackupMgr memBackup;
 37      /// @brief Memory Scanner Manager
 38      KittyScannerMgr memScanner;
 39      /// @brief Elf Scanner Manager
 40      ElfScannerMgr elfScanner;
 41  
 42  #ifdef __ANDROID__
 43      /// @brief Linker Scanner Manager
 44      LinkerScannerMgr linkerScanner;
 45      /// @brief NativeBridge Scanner Manager
 46      NativeBridgeScannerMgr nbScanner;
 47  #endif
 48  
 49      /// @brief Process Trace Manager
 50      KittyTraceMgr trace;
 51  
 52      KittyMemoryMgr() : _init(false), _pid(0), _eMemOp(EK_MEM_OP_NONE)
 53      {
 54      }
 55  
 56      /**
 57       * @brief Initializes the memory manager.
 58       * @param pid Remote process ID.
 59       * @param eMemOp Memory read & write operation type.
 60       * @param initMemPatch If true, initializes MemoryPatch and MemoryBackup instances.
 61       * @return True if initialization is successful, false otherwise.
 62       */
 63      bool initialize(pid_t pid, EKittyMemOP eMemOp, bool initMemPatch);
 64  
 65      /**
 66       * @brief Returns the process ID.
 67       */
 68      inline pid_t processID() const
 69      {
 70          return _pid;
 71      }
 72  
 73      /**
 74       * @brief Returns the process name.
 75       * @return The name of the remote process.
 76       */
 77      inline std::string processName() const
 78      {
 79          return _process_name;
 80      }
 81  
 82      /**
 83       * @brief Checks if memory operations are valid.
 84       */
 85      inline bool isMemValid() const
 86      {
 87          return _init && _pid && _pMemOp.get();
 88      }
 89  
 90      /**
 91       * @brief Returns memory operations pointer.
 92       */
 93      inline IKittyMemOp *memOp() const
 94      {
 95          return _pMemOp.get();
 96      }
 97  
 98      /**
 99       * @brief Reads remote memory from a specified address.
100       * @param address The address in remote memory to read.
101       * @param buffer The buffer to store the read data.
102       * @param len The number of bytes to read.
103       * @return The number of bytes read.
104       */
105      size_t readMem(uintptr_t address, void *buffer, size_t len) const;
106  
107      /**
108       * @brief Writes remote memory to a specified address.
109       * @param address The address in remote memory to write to.
110       * @param buffer The buffer containing the data to write.
111       * @param len The number of bytes to write.
112       * @return The number of bytes written.
113       */
114      size_t writeMem(uintptr_t address, void *buffer, size_t len) const;
115  
116      /**
117       * @brief Reads a string from remote memory.
118       * @param address The address in remote memory to read the string from.
119       * @param maxLen The maximum length of the string to read.
120       * @return The string read from remote memory.
121       */
122      std::string readMemStr(uintptr_t address, size_t maxLen) const;
123  
124      /**
125       * @brief Writes a string to remote memory.
126       * @param address The address in remote memory to write the string to.
127       * @param str The string to write to remote memory.
128       * @return True if the string is written successfully, false otherwise.
129       */
130      bool writeMemStr(uintptr_t address, std::string str) const;
131  
132      /**
133       * @brief Dumps a memory range to a file.
134       * @param start The start address of the memory range to dump.
135       * @param end The end address of the memory range to dump.
136       * @param path The path to the file where the memory range will be dumped.
137       * @return True if the memory range is dumped successfully, false otherwise.
138       */
139      bool dumpMemRange(uintptr_t start, uintptr_t end, const std::string &path) const;
140  
141      /**
142       * @brief Dumps a memory mapped file from a remote process.
143       * @param memFile The memory file to dump.
144       * @param destination The path where the memory file will be dumped.
145       * @return True if the memory mapped file is dumped successfully, false otherwise.
146       */
147      bool dumpMemFile(const std::string &memFile, const std::string &destination) const;
148  
149      /**
150       * @brief Dumps a memory mapped ELF file from a remote process.
151       * @param elf The ELF scanner to use for dumping the ELF file.
152       * @param destination The path where the ELF file will be dumped.
153       * @return True if the ELF file is dumped successfully, false otherwise.
154       */
155      bool dumpMemELF(const ElfScanner &elf, const std::string &destination) const;
156  };