-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathmain-accessingMemory.cpp
More file actions
192 lines (151 loc) · 3.99 KB
/
main-accessingMemory.cpp
File metadata and controls
192 lines (151 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
DWORD getPIDFromWindow(HWND window)
{
DWORD PID;
GetWindowThreadProcessId(window, &PID);
return PID;
}
DWORD getPIDByName(std::wstring name)
{
DWORD PID = -1;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
std::wstring binaryPath = entry.szExeFile;
if (binaryPath.find(name) != std::wstring::npos)
{
PID = entry.th32ProcessID;
break;
}
}
}
CloseHandle(snapshot);
return PID;
}
template<typename T>
T readMemoryAPI(HANDLE process, LPVOID address)
{
T value;
ReadProcessMemory(process, address, &value, sizeof(T), NULL);
return value;
}
template<typename T>
void writeMemoryAPI(HANDLE process, LPVOID address, T value)
{
WriteProcessMemory(process, address, &value, sizeof(T), NULL);
}
template<typename T>
DWORD protectMemory(HANDLE process, LPVOID address, DWORD prot)
{
DWORD oldProt;
VirtualProtectEx(process, address, sizeof(T), prot, &oldProt);
return oldProt;
}
void readAndWriteMemoryAPI(HANDLE process, LPVOID address)
{
DWORD value = readMemoryAPI<DWORD>(process, address);
printf("Current mem value is %d\n", value);
value++;
DWORD oldProt = protectMemory<DWORD>(process, address, PAGE_READWRITE);
writeMemoryAPI<DWORD>(process, address, value);
protectMemory<DWORD>(process, address, oldProt);
value = readMemoryAPI<DWORD>(process, address);
printf("New mem value is %d\n", value);
}
template<typename T>
T readMemoryPointer(LPVOID address)
{
return *((T*)address);
}
template<typename T>
void writeMemoryPointer(LPVOID address, T value)
{
*((T*)address) = value;
}
template<typename T>
T* pointMemory(LPVOID address)
{
return ((T*)address);
}
void readAndWriteMemoryMarshall(LPVOID address)
{
DWORD value = readMemoryPointer<DWORD>(address);
printf("Current mem value is %d\n", value);
value++;
writeMemoryPointer<DWORD>(address, value);
value = readMemoryPointer<DWORD>(address);
printf("New mem value is %d\n", value);
}
DWORD getMyBaseAddressGMH()
{
return (DWORD)GetModuleHandle(NULL);
}
DWORD getMyBaseAddressFS()
{
DWORD newBase;
__asm
{
MOV EAX, DWORD PTR FS:[0x30]
MOV EAX, DWORD PTR DS:[EAX+0x8]
MOV newBase, EAX
}
return newBase;
}
DWORD getRemoteBaseAddress(HANDLE process)
{
DWORD newBase;
// get the address of kernel32.dll
HMODULE k32 = GetModuleHandleA("kernel32.dll");
// get the address of GetModuleHandle()
LPVOID funcAdr = GetProcAddress(k32, "GetModuleHandleA");
if (!funcAdr) funcAdr = GetProcAddress(k32, "GetModuleHandleW");
// create the thread
HANDLE thread = CreateRemoteThread(process, NULL, NULL, (LPTHREAD_START_ROUTINE)funcAdr, NULL, NULL, NULL);
// let the thread finish
WaitForSingleObject(thread, INFINITE);
// get the exit code
GetExitCodeThread(thread, &newBase);
// clean up the thread handle
CloseHandle(thread);
return newBase;
}
void printMyBaseAddresses(HANDLE Process)
{
DWORD base1 = getMyBaseAddressGMH();
DWORD base2 = getMyBaseAddressFS();
DWORD base3 = getRemoteBaseAddress(Process);
if (base1 != base2 || base2 != base3)
printf("Woah, this should be impossible!\n");
else
printf("My base address is 0x%08x\n", base1);
}
int main(void)
{
HANDLE proc = OpenProcess(
PROCESS_VM_OPERATION |
PROCESS_VM_READ |
PROCESS_VM_WRITE |
PROCESS_CREATE_THREAD,
FALSE, GetCurrentProcessId());
printMyBaseAddresses(proc);
// get my PID from window
wchar_t myTitle[1024];
GetConsoleTitle(&myTitle[0], 1024);
HWND myWindow = FindWindow(NULL, myTitle);
auto myPID = getPIDFromWindow(myWindow);
printf("My pid is %d\n", myPID);
// get explorer PID by process name
auto explorerPID = getPIDByName(L"explorer.exe");
printf("Explorer pid is %d\n", explorerPID);
// lets do some memory stuff.. to ourself
DWORD someValue = 1234;
readAndWriteMemoryAPI(proc, &someValue);
readAndWriteMemoryMarshall(&someValue);
system("pause");
}