-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
179 lines (132 loc) · 4.84 KB
/
main.cpp
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
#include <iostream>
#include "main.h"
#include <windows.h>
#include <psapi.h>
#include <locale>
#include <codecvt>
#include <vector>
#include <list>
#include <nlohmann/json.hpp>
#define debug
/*
* print to stdout the number of suitable processes, offset and pid of result (if there is any)
*/
using json = nlohmann::json;
constexpr unsigned CHUNK_SIZE = 32;
int main() {
std::list<std::string> strs = TokenScanner::scanForTokens();
for (const auto& str : strs) {
std::cout << str << "\n";
}
return 0;
}
std::tuple<std::string, std::string> tryGetProcessName(unsigned long pid) {
HANDLE handle =
OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid);
if (!handle) {
return {"", "Unable to open process."};
}
wchar_t buffer[MAX_PATH];
if (GetModuleFileNameExW(handle, 0, buffer, MAX_PATH)) {
std::wstring processPath = buffer;
int lastSlashPosition = processPath.find_last_of(L"\\");
if (lastSlashPosition == std::string::npos) {
lastSlashPosition = 0;
} else {
lastSlashPosition++;
}
std::wstring name = processPath.substr(
lastSlashPosition, processPath.length() - lastSlashPosition);
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
std::string convertedName = converter.to_bytes(name.c_str());
return {convertedName, "1"};
}
return {"", "Failed to read process name."};
}
std::vector<unsigned long> getSuitableProcesses(std::string desiredName) {
std::vector<unsigned long> out;
unsigned long allProcesses[2048];
unsigned long cbNeeded = 0;
if (EnumProcesses(allProcesses, sizeof(allProcesses), &cbNeeded)) {
unsigned long processCount = cbNeeded / sizeof(unsigned long);
for (unsigned long i = 0; i < processCount; i++) {
unsigned long pid = allProcesses[i];
std::tuple<std::string,std::string> nametup;
nametup = tryGetProcessName(pid);
if (get<0>(nametup).empty()) {
continue;
}
if (get<0>(nametup).find(desiredName) != std::string::npos) {
out.push_back(pid);
}
}
}
return out;
}
std::vector<std::string> tryFindAccessToken(unsigned long pid) {
std::vector<std::string> out;
char* buffer = new char[CHUNK_SIZE];
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
if (!handle) {
return out;
}
char* pagePointer = 0;
MEMORY_BASIC_INFORMATION memoryInfo;
while (VirtualQueryEx(handle, pagePointer, &memoryInfo,
sizeof(MEMORY_BASIC_INFORMATION))) {
if (memoryInfo.State == MEM_COMMIT &&
memoryInfo.Protect == PAGE_READWRITE) {
for (unsigned long long i = 0; i < memoryInfo.RegionSize;
i += CHUNK_SIZE) {
void* readPointer = (void*)(pagePointer + i);
if (ReadProcessMemory(handle, readPointer, buffer, CHUNK_SIZE,
nullptr)) {
std::string bufferString = buffer;
if (bufferString.find("{\"access") != std::string::npos) {
#ifdef debug
std::cout << "Found potential token at " << i << "\n";
#endif
delete[] buffer;
buffer = new char[512];
if (ReadProcessMemory(handle, readPointer, buffer, 512,
nullptr)) {
bufferString = buffer;
size_t tokenPosition =
bufferString.find(R"({"accessToken":")");
if (tokenPosition != std::string::npos) {
#ifdef debug
std::cout << "It was real.\n";
#endif
out.push_back(bufferString.substr(
tokenPosition,
bufferString.length() - tokenPosition));
}
}
}
}
}
}
pagePointer += memoryInfo.RegionSize;
}
delete[] buffer;
return out;
}
std::list<std::string> TokenScanner::scanForTokens() {
std::list<std::string> out;
auto foundProcesses = getSuitableProcesses("FyreMC.exe");
#ifdef debug
std::cout << "Scanning " << foundProcesses.size() << " processes.\n";
#endif
for (const auto pid : foundProcesses) {
auto foundTokens = tryFindAccessToken(pid);
for (const auto& str : foundTokens) {
out.push_back(str);
#ifdef debug
out.push_back(std::to_string(pid));
#endif
}
//TODO: ad-hoc validation
}
return out;
}