This repository has been archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cc
executable file
·318 lines (267 loc) · 11.4 KB
/
main.cc
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
This file is part of Reverse Engine.
Fix for https://github.com/ValveSoftware/csgo-osx-linux/issues/11
that allows you to download resources from community servers.
UPD: #11 issue fixed by Volvo
Copyright (C) 2017-2018 Ivan Stepanov <ivanstepanovftw@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <iomanip>
#include <chrono>
#include <type_traits>
#include <zconf.h>
#include <functional>
#include <cmath>
#include <cassert>
#include <sys/wait.h>
#include <curl/curl.h>
#include <reverseengine/core.hh>
#include <reverseengine/value.hh>
#include <reverseengine/scanner.hh>
using namespace std;
using namespace std::chrono;
using namespace std::literals;
const string target = "csgo_linux64";
const string delimeter = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
const char *DOWNLOADMANAGER_SIGNATURE = "\x55\x48\x8D\x3D\x00\x00\x00\x00\x48\x89\xE5\x5D\xE9\xBF\xFF\xFF\xFF";
const char *DOWNLOADMANAGER_MASK = "xxxx????xxxxxxxxx";
bool nowrite = false, nobz2 = false;
int afterDelay = 10;
inline bool file_exists(const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
// https://stackoverflow.com/a/27172926/3303059
// I am not satisfied with this "solution" at all!
static int exec_prog(const char *prog, const char *arg1, const char *arg2) {
pid_t my_pid;
int status, timeout;
if (0 == (my_pid = fork())) {
if (-1 == execlp(prog, prog, arg1, arg2, NULL)) {
perror("child process execve failed [%m]");
return -1;
}
}
timeout = 20000;
while (0 == waitpid(my_pid, &status, WNOHANG)) {
if ( --timeout < 0 ) {
perror("timeout");
return -1;
}
usleep(500);
}
return 0;
}
int
main(int argc, char* argv[])
{
if (argc > 0)
nowrite = false;
if (getuid() > 0) {
cerr<<"Warning: non root user"<<endl;
}
/// Trainer and scanner example
RE::handler csgo;
RE::Region *engine_client_so;
stage_waiting:;
cout<<"1. Waiting for process."<<endl;
cout<<"process: "<<target<<endl;
for(;;) {
csgo.attach(target);
if (csgo.is_good())
break;
usleep(500'000);
}
cout<<"2. Found!"<<endl;
cout<<"pid: "<<csgo.pid<<endl;
cout<<"title: "<<csgo.title<<endl;
cout<<"path: "<<csgo.get_path()<<endl;
cout<<"working_directory: "<<csgo.get_working_directory()<<endl;
cout<<delimeter<<endl;
stage_updating:;
cout<<"1. Updating regions."<<endl;
for(;;) {
csgo.update_regions();
engine_client_so = csgo.get_region_by_name("engine_client.so");
if (engine_client_so->is_good())
break;
if (!csgo.is_running())
goto stage_waiting;
usleep(500'000);
}
cout<<"Regions added: "<<csgo.regions.size()<<", ignored: "<<csgo.regions_ignored.size()<<endl;
// cout<<"Found region: "<<*engine_client_so<<endl;
cout<<delimeter<<endl;
//fixme[high]: ?????????????????????????
std::string moddir = csgo.get_path().string().substr(0, csgo.get_path().string().find_last_of("/\\")) + "/csgo";
struct stat moddirstat;
stat(moddir.c_str(), &moddirstat);
uintptr_t foundDownloadManagerMov{};
if (!csgo.find_pattern(&foundDownloadManagerMov,
engine_client_so,
DOWNLOADMANAGER_SIGNATURE,
DOWNLOADMANAGER_MASK))
{
clog<<"Not found :("<<endl;
return 1;
}
foundDownloadManagerMov += 1;
cout << ">>> found TheDownloadManager mov: "<<RE::HEX(foundDownloadManagerMov)<<endl;
uintptr_t downloadManager = csgo.get_absolute_address(foundDownloadManagerMov, 3, 7);
cout << ">>> Address of TheDownloadManager: "<<RE::HEX(downloadManager)<<endl;
uintptr_t m_activeRequest, m_queuedRequests_List, curReq;
int m_queuedRequests_Count, one = 1, httpdone = 2, httperror = 4, m_activeRequest_status = httperror;
bool bAsHTTP;
char baseURLbuf[PATH_MAX], gamePathbuf[PATH_MAX];
CURL *curl;
FILE *fp;
CURLcode res;
char curlerr[CURL_ERROR_SIZE];
curl = curl_easy_init();
unsigned long responseCode;
if(!curl) {
cout << "Failed to initialize cURL!" << endl;
exit(1);
}
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curlerr);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, true);
// Error [22]: HTTP response code said error
// The requested URL returned error: 404 Not Found
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 3);
curl_easy_setopt(curl, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Half-Life 2");
while (csgo.is_running()) {
sleep(1);
size_t copied = csgo.read(downloadManager + 4u * 8u, &m_activeRequest, sizeof(m_activeRequest));
if (copied != sizeof(unsigned long)) {
cout<<"copied: "<<copied<<endl;
cout << "copy failed: " << std::strerror(errno) << '\n';
continue;
}
if (csgo.read(downloadManager, &m_queuedRequests_List, sizeof(unsigned long)) != sizeof(unsigned long))
continue;
if (csgo.read(downloadManager + 2 * 8, &m_queuedRequests_Count, sizeof(unsigned long)) != sizeof(unsigned long))
printf("Cannot read utils");
void** requestList = (void**) malloc((m_queuedRequests_Count + 1) * sizeof(void*));
*requestList = (void*) m_activeRequest;
csgo.read(m_queuedRequests_List, requestList + 1, m_queuedRequests_Count * sizeof(void*));
for(int i = 0; i < m_queuedRequests_Count+1; ++i) {
curReq = (unsigned long)(requestList[i]);
csgo.read(curReq + 20, &baseURLbuf, sizeof(baseURLbuf));
csgo.read(curReq + 532, &gamePathbuf, sizeof(gamePathbuf));
printf("heheeeey! #%d/%d: %s%s\n", i, m_queuedRequests_Count, baseURLbuf, gamePathbuf);
}
//cout << std::dec << m_queuedRequests_Count << endl;
m_activeRequest_status = httperror;
for(int i = 0; i < m_queuedRequests_Count+1; ++i) {
csgo.read(downloadManager + 4 * 8, &m_activeRequest, sizeof(unsigned long));
if(!m_activeRequest)
break;
if(!nowrite)
csgo.write(m_activeRequest + 8, &httperror, sizeof(int));
// Process the active request first, then proceed further. If we write status to the active request,
// the vector gets shifted and we will skip some files.
curReq = (unsigned long)(requestList[i]);
csgo.read(curReq + 3, &bAsHTTP, sizeof(bool));
if(!bAsHTTP)
continue;
csgo.read(curReq + 20, &baseURLbuf, sizeof(baseURLbuf));
csgo.read(curReq + 532, &gamePathbuf, sizeof(gamePathbuf));
std::string baseURL(baseURLbuf);
std::string gamePath(gamePathbuf);
std::string downloadURL = baseURL + gamePath;
std::string saveURL = moddir + "/" + gamePath;
std::string gamePathWithBZ2 = gamePath + ".bz2";
std::string downloadDir = saveURL.substr(0, saveURL.find_last_of("/\\"));
if (file_exists(saveURL)) {
cout<<"#"<<i<<"/"<<m_queuedRequests_Count<<" file exists: "<<gamePath<<endl;
goto SKIPLOOP;
}
cout<<"#"<<i<<"/"<<m_queuedRequests_Count<<" downloading: "<<downloadURL<<endl;
// might as well do it the ghetto way - we're on Linux, so this is available
exec_prog("mkdir", "-p", downloadDir.c_str());
fp = fopen(saveURL.c_str(), "wb");
if(fp == NULL) {
cout << "Failed to fopen " << saveURL << endl;
//if(i >= 0)
if(!nowrite)
csgo.write(curReq + 8, &httperror, sizeof(int));
continue;
}
curl_easy_setopt(curl, CURLOPT_URL, downloadURL.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
fclose(fp);
// HTTP/1.1 200 OK
if(responseCode != 200 || res) {
cout << "Error [" << dec << res << " / HTTP: " << responseCode << "]: " << curl_easy_strerror(res) << endl;
if(strlen(curlerr))
cout << curlerr << endl;
unlink(saveURL.c_str());
goto SKIPLOOP;
}
if(!nobz2) {
char *ext = strrchr(gamePathbuf, '.');
if(ext && !strcmp(ext, ".bz2")) {
// The file is a BZ2 archive, we can unpack it and just skip the next download.
exec_prog("bzip2", "-dk", saveURL.c_str());
}
}
SKIPLOOP:
if(!res) {
chown(saveURL.c_str(), moddirstat.st_uid, moddirstat.st_gid);
}
if(!nowrite)
csgo.write(curReq + 8, &httpdone, sizeof(int));
}
free(requestList);
if(nowrite) {
sleep(afterDelay);
continue;
}
// in case we're downloading faster than CSGO's updating the thread
usleep(200000);
csgo.read(downloadManager + 2 * 8, &m_queuedRequests_Count, sizeof(unsigned long));
// cout << m_queuedRequests_Count << endl;
for (int i = 0; i < m_queuedRequests_Count + 1; ++i) {
csgo.read(downloadManager + 4 * 8, &m_activeRequest, sizeof(unsigned long));
csgo.write(m_activeRequest + 8, &httperror, sizeof(int));
// cout << i << endl;
usleep(50000);
}
/*while (m_queuedRequests_Count) {
for (int i = 0; i < m_queuedRequests_Count; ++i) {
curReq = (unsigned long)(requestList[i]);
csgo.Write((void*) (curReq + 8), &httpdone, sizeof(int));
}
cout << m_queuedRequests_Count << endl;
csgo.Read((void*) (downloadManager + 2 * 8), &m_queuedRequests_Count, sizeof(unsigned long));
csgo.Read((void*) (downloadManager + 4 * 8), &m_activeRequest, sizeof(unsigned long));
if(!m_activeRequest)
break;
csgo.Write((void*) (m_activeRequest + 8), &httpdone, sizeof(int));
usleep(50000);
}*/
//csgo.Write((void*) (m_activeRequest + 8), &m_activeRequest_status, sizeof(int));
}
curl_easy_cleanup(curl);
cout << "Game ended." << endl;
return 0;
}