about summary refs log tree commit diff stats
path: root/Source
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2019-11-04 10:04:49 -0800
committerjbzdarkid <jbzdarkid@gmail.com>2019-11-04 10:04:49 -0800
commite2c0268d4b82e170605d9cc43e26be7f38f2eb54 (patch)
treecb679ee56a44ec30d09176bec61ef4d9355bfcfb /Source
parent0cb49374a6d86100cd6f3bf838e79bdbac242a8e (diff)
downloadwitness-tutorializer-e2c0268d4b82e170605d9cc43e26be7f38f2eb54.tar.gz
witness-tutorializer-e2c0268d4b82e170605d9cc43e26be7f38f2eb54.tar.bz2
witness-tutorializer-e2c0268d4b82e170605d9cc43e26be7f38f2eb54.zip
Better, I guess
Diffstat (limited to 'Source')
-rw-r--r--Source/Memory.cpp25
-rw-r--r--Source/Memory.h8
2 files changed, 19 insertions, 14 deletions
diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 1f1ae0a..d7f0212 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp
@@ -2,6 +2,7 @@
2#include <psapi.h> 2#include <psapi.h>
3#include <tlhelp32.h> 3#include <tlhelp32.h>
4#include <iostream> 4#include <iostream>
5#include <cassert>
5 6
6#undef PROCESSENTRY32 7#undef PROCESSENTRY32
7#undef Process32Next 8#undef Process32Next
@@ -53,15 +54,15 @@ Memory::~Memory() {
53 } 54 }
54} 55}
55 56
56int Memory::GetCurrentFrame() 57int Memory::GetCurrentFrame() {
57{
58 int SCRIPT_FRAMES; 58 int SCRIPT_FRAMES;
59 if (GLOBALS == 0x5B28C0) { 59 if (GLOBALS == 0x5B28C0) {
60 SCRIPT_FRAMES = 0x5BE3B0; 60 SCRIPT_FRAMES = 0x5BE3B0;
61 } else if (GLOBALS == 0x62D0A0) { 61 } else if (GLOBALS == 0x62D0A0) {
62 SCRIPT_FRAMES = 0x63651C; 62 SCRIPT_FRAMES = 0x63954C;
63 } else { 63 } else {
64 throw std::exception("Unknown value for Globals!"); 64 assert(false);
65 return 0x7FFFFFFF;
65 } 66 }
66 return ReadData<int>({SCRIPT_FRAMES}, 1)[0]; 67 return ReadData<int>({SCRIPT_FRAMES}, 1)[0];
67} 68}
@@ -108,14 +109,15 @@ int Memory::ExecuteSigScans()
108} 109}
109 110
110void Memory::ThrowError() { 111void Memory::ThrowError() {
111 std::string message(256, '\0'); 112 std::wstring message(256, '\0');
112 int length = FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], static_cast<DWORD>(message.size()), nullptr); 113 int length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), 1024, &message[0], static_cast<DWORD>(message.size()), nullptr);
113 message.resize(length); 114 message.resize(length);
114 throw std::exception(message.c_str()); 115#ifndef NDEBUG
116 MessageBox(NULL, message.c_str(), L"Please tell darkid about this", MB_OK);
117#endif
115} 118}
116 119
117void* Memory::ComputeOffset(std::vector<int> offsets) 120void* Memory::ComputeOffset(std::vector<int> offsets) {
118{
119 // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t. 121 // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t.
120 int final_offset = offsets.back(); 122 int final_offset = offsets.back();
121 offsets.pop_back(); 123 offsets.pop_back();
@@ -128,7 +130,10 @@ void* Memory::ComputeOffset(std::vector<int> offsets)
128 if (search == std::end(_computedAddresses)) { 130 if (search == std::end(_computedAddresses)) {
129 // If the address is not yet computed, then compute it. 131 // If the address is not yet computed, then compute it.
130 uintptr_t computedAddress = 0; 132 uintptr_t computedAddress = 0;
131 if (!ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { 133 if (bool result = !ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) {
134 if (GetLastError() == ERROR_PARTIAL_COPY) {
135 int k = 1;
136 }
132 ThrowError(); 137 ThrowError();
133 } 138 }
134 _computedAddresses[cumulativeAddress] = computedAddress; 139 _computedAddresses[cumulativeAddress] = computedAddress;
diff --git a/Source/Memory.h b/Source/Memory.h index 9c00dab..f70de6a 100644 --- a/Source/Memory.h +++ b/Source/Memory.h
@@ -11,8 +11,7 @@
11// http://stackoverflow.com/q/32798185 11// http://stackoverflow.com/q/32798185
12// http://stackoverflow.com/q/36018838 12// http://stackoverflow.com/q/36018838
13// http://stackoverflow.com/q/1387064 13// http://stackoverflow.com/q/1387064
14class Memory 14class Memory {
15{
16public: 15public:
17 Memory(); 16 Memory();
18 bool Initialize(const std::wstring& processName); 17 bool Initialize(const std::wstring& processName);
@@ -46,11 +45,12 @@ public:
46 void AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc); 45 void AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc);
47 int ExecuteSigScans(); 46 int ExecuteSigScans();
48 47
49 void ClearOffsets() {_computedAddresses = std::map<uintptr_t, uintptr_t>();}
50
51private: 48private:
52 template<class T> 49 template<class T>
53 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) { 50 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) {
51 if (GetExitCodeProcess(_process) != STILL_ACTIVE) {
52 // Signal error, somehow
53 }
54 std::vector<T> data; 54 std::vector<T> data;
55 data.resize(numItems); 55 data.resize(numItems);
56 for (int i=0; i<5; i++) { 56 for (int i=0; i<5; i++) {