about summary refs log tree commit diff stats
path: root/Source/Memory.h
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2019-11-16 21:15:59 -0800
committerjbzdarkid <jbzdarkid@gmail.com>2019-11-16 21:15:59 -0800
commitfd2fa2211dc09c9030601fde1afd2f7823b22ed8 (patch)
treedfd3d3195315b69e5ccb397f7aedde115fb7e6e6 /Source/Memory.h
parentee426c3bde4b4c7fb1ea38a953d849e8893d8311 (diff)
downloadwitness-tutorializer-fd2fa2211dc09c9030601fde1afd2f7823b22ed8.tar.gz
witness-tutorializer-fd2fa2211dc09c9030601fde1afd2f7823b22ed8.tar.bz2
witness-tutorializer-fd2fa2211dc09c9030601fde1afd2f7823b22ed8.zip
Cleanup tabs -> spaces, actually free memory
Diffstat (limited to 'Source/Memory.h')
-rw-r--r--Source/Memory.h132
1 files changed, 66 insertions, 66 deletions
diff --git a/Source/Memory.h b/Source/Memory.h index af4f0ae..70a271e 100644 --- a/Source/Memory.h +++ b/Source/Memory.h
@@ -19,103 +19,103 @@ enum class ProcStatus {
19// http://stackoverflow.com/q/32798185 19// http://stackoverflow.com/q/32798185
20// http://stackoverflow.com/q/36018838 20// http://stackoverflow.com/q/36018838
21// http://stackoverflow.com/q/1387064 21// http://stackoverflow.com/q/1387064
22// https://github.com/fkloiber/witness-trainer/blob/master/source/foreign_process_memory.cpp
22class Memory final : public std::enable_shared_from_this<Memory> { 23class Memory final : public std::enable_shared_from_this<Memory> {
23public: 24public:
24 Memory(const std::wstring& processName); 25 Memory(const std::wstring& processName);
25 ~Memory(); 26 ~Memory();
26 void StartHeartbeat(HWND window, std::chrono::milliseconds beat = std::chrono::milliseconds(1000)); 27 void StartHeartbeat(HWND window, std::chrono::milliseconds beat = std::chrono::milliseconds(1000));
27 28
28 Memory(const Memory& memory) = delete; 29 Memory(const Memory& memory) = delete;
29 Memory& operator=(const Memory& other) = delete; 30 Memory& operator=(const Memory& other) = delete;
30 31
31 template <class T> 32 template <class T>
32 std::vector<T> ReadArray(int panel, int offset, int size) { 33 std::vector<T> ReadArray(int id, int offset, int size) {
33 return ReadData<T>({GLOBALS, 0x18, panel*8, offset, 0}, size); 34 return ReadData<T>({GLOBALS, 0x18, id*8, offset, 0}, size);
34 } 35 }
35 36
36 template <class T> 37 template <class T>
37 void WriteArray(int panel, int offset, const std::vector<T>& data) { 38 void WriteArray(int id, int offset, const std::vector<T>& data) {
38 WriteData({GLOBALS, 0x18, panel*8, offset, 0}, data); 39 WriteData({GLOBALS, 0x18, id*8, offset, 0}, data);
39 } 40 }
40 41
41 template <class T> 42 template <class T>
42 void WriteNewArray(int panel, int offset, const std::vector<T>& data) { 43 void WriteNewArray(int id, int offset, const std::vector<T>& data) {
43 std::vector<uintptr_t> newAddr = {Allocate(data.size() * sizeof(T))}; 44 uintptr_t addr = VirtualAllocEx(_handle, nullptr, data.size() * sizeof(T), MEM_RESERVE | MEM_COMMIT, MEM_READWRITE);
44 WritePanelData(panel, offset, newAddr); 45 _allocations.emplace_back(addr);
45 WriteArray(panel, offset, data); 46 WriteEntityData(id, offset, addr);
46 } 47 WriteArray(id, offset, data);
48 }
47 49
48 template <class T> 50 template <class T>
49 std::vector<T> ReadPanelData(int panel, int offset, size_t size) { 51 std::vector<T> ReadEntityData(int id, int offset, size_t size) {
50 return ReadData<T>({GLOBALS, 0x18, panel*8, offset}, size); 52 return ReadData<T>({GLOBALS, 0x18, id*8, offset}, size);
51 } 53 }
52 54
53 template <class T> 55 template <class T>
54 void WritePanelData(int panel, int offset, const std::vector<T>& data) { 56 void WriteEntityData(int id, int offset, const std::vector<T>& data) {
55 WriteData({GLOBALS, 0x18, panel*8, offset}, data); 57 WriteData({GLOBALS, 0x18, id*8, offset}, data);
56 } 58 }
57 59
58 void AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc); 60 void AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc);
59 int ExecuteSigScans(); 61 int ExecuteSigScans();
60 62
61private: 63private:
62 template<class T> 64 template<class T>
63 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) { 65 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) {
64 if (numItems == 0) return {}; 66 if (numItems == 0) return {};
65 std::vector<T> data; 67 std::vector<T> data;
66 data.resize(numItems); 68 data.resize(numItems);
67 void* computedOffset = ComputeOffset(offsets); 69 void* computedOffset = ComputeOffset(offsets);
68 for (int i=0; i<5; i++) { 70 for (int i=0; i<5; i++) {
69 if (ReadProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * numItems, nullptr)) { 71 if (ReadProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * numItems, nullptr)) {
70 if (i != 0) { 72 if (i != 0) {
71 int k = 0; 73 int k = 0;
72 } 74 }
73 return data; 75 return data;
74 } 76 }
75 } 77 }
76 ThrowError(); 78 ThrowError();
77 return {}; 79 return {};
78 } 80 }
79 81
80 template <class T> 82 template <class T>
81 void WriteData(const std::vector<int>& offsets, const std::vector<T>& data) { 83 void WriteData(const std::vector<int>& offsets, const std::vector<T>& data) {
82 if (data.empty()) return; 84 if (data.empty()) return;
83 void* computedOffset = ComputeOffset(offsets); 85 void* computedOffset = ComputeOffset(offsets);
84 for (int i=0; i<5; i++) { 86 for (int i=0; i<5; i++) {
85 if (WriteProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * data.size(), nullptr)) { 87 if (WriteProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * data.size(), nullptr)) {
86 if (i != 0) { 88 if (i != 0) {
87 int k = 0; 89 int k = 0;
88 } 90 }
89 return; 91 return;
90 } 92 }
91 } 93 }
92 ThrowError(); 94 ThrowError();
93 } 95 }
94 96
95 void Heartbeat(HWND window); 97 void Heartbeat(HWND window);
96 bool Initialize(); 98 bool Initialize();
97 void ThrowError(); 99 void ThrowError();
98 void* ComputeOffset(std::vector<int> offsets); 100 void* ComputeOffset(std::vector<int> offsets);
99 uintptr_t Allocate(size_t bytes);
100 101
101 int _previousFrame = 0; 102 int _previousFrame = 0;
102 bool _threadActive = false; 103 bool _threadActive = false;
103 std::thread _thread; 104 std::thread _thread;
104 std::wstring _processName; 105 std::wstring _processName;
105 std::map<uintptr_t, uintptr_t> _computedAddresses; 106 std::map<uintptr_t, uintptr_t> _computedAddresses;
106 uintptr_t _baseAddress = 0; 107 uintptr_t _baseAddress = 0;
107 HANDLE _handle = nullptr; 108 HANDLE _handle = nullptr;
108 uintptr_t _freeMem = 0; 109 std::vector<uintptr_t> _allocations;
109 uintptr_t _freeMemEnd = 0; 110 struct SigScan {
110 struct SigScan { 111 std::function<void(int)> scanFunc;
111 std::function<void(int)> scanFunc; 112 bool found;
112 bool found; 113 };
113 }; 114 std::map<std::vector<byte>, SigScan> _sigScans;
114 std::map<std::vector<byte>, SigScan> _sigScans;
115 115
116 friend class Temp; 116 friend class Temp;
117 friend class ChallengeRandomizer; 117 friend class ChallengeRandomizer;
118 friend class Randomizer; 118 friend class Randomizer;
119}; 119};
120 120
121#if GLOBALS == 0x5B28C0 121#if GLOBALS == 0x5B28C0