-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.h
293 lines (242 loc) · 7.37 KB
/
Utils.h
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
#pragma once
#include <Windows.h>
#include <string>
#include <iostream>
#include <locale>
#include <utility>
#include <codecvt>
#include <fstream>
#include <sstream>
#include <Psapi.h>
#include <vector>
#include <WinInet.h>
#include <Shlwapi.h>
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "Shlwapi.lib")
unsigned __int64 _starting_tick;
unsigned __int64 _finish_tick;
#define ERRORMSGBOX(s, s1) MessageBox(NULL, s, s1, MB_ICONERROR | MB_OK);
#define INFOMSGBOX(s, s1) MessageBox(NULL, s, s1, MB_ICONINFORMATION | MB_OK);
#define MSFOUND(o) throw std::exception("malicious item '" o "' found");
#define CHECKPROC(p) strcmp(entry.szExeFile, p) == 0
#define CheatSquadModule "csapi.dll"
namespace Timer {
void Reset() {
_starting_tick = 0;
_finish_tick = 0;
}
void Start() {
Reset();
_starting_tick = GetTickCount64();
}
double GetElapsedTime() {
if (_starting_tick & _finish_tick)
return (_finish_tick - _starting_tick) / 1000.0;
return 0.0;
}
double Stop() {
_finish_tick = GetTickCount64();
return GetElapsedTime();
}
}
void AddressWrite(unsigned char* address, int opcode)
{
PDWORD(oldProtection) = new DWORD;
VirtualProtect((LPVOID)address, sizeof(int), PAGE_EXECUTE_READWRITE, oldProtection);
*address = opcode;
VirtualProtect((LPVOID)address, sizeof(int), *oldProtection, oldProtection);
delete oldProtection;
}
void AddressWriteShellCode(unsigned char* address, std::vector<BYTE> sc)
{
PDWORD(oldProtection) = new DWORD;
VirtualProtect((LPVOID)address, sc.size(), PAGE_EXECUTE_READWRITE, oldProtection);
for (int n = 0; n < sc.size(); n++) {
*(address + n) = sc[n];
}
VirtualProtect((LPVOID)address, sc.size(), *oldProtection, oldProtection);
delete oldProtection;
}
namespace Files {
void GetFile(const char* dllName, const char* fileName, char* buffer, int bfSize) {
GetModuleFileName(GetModuleHandle(dllName), buffer, bfSize);
if (strlen(fileName) + strlen(buffer) < MAX_PATH) {
char* pathEnd = strrchr(buffer, '\\');
strcpy(pathEnd + 1, fileName);
}
else {
*buffer = 0;
}
}
long int GetFileSize(FILE* ifile) {
long int fsize = 0;
long int fpos = ftell(ifile);
fseek(ifile, 0, SEEK_END);
fsize = ftell(ifile);
fseek(ifile, fpos, SEEK_SET);
return fsize;
}
int ReadFile(const std::string& path, std::string& out, unsigned char binary) {
std::ios::openmode mode = std::ios::in;
if (binary)
mode |= std::ios::binary;
std::ifstream file(path, mode);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
out = buffer.str();
file.close();
return 1;
}
file.close();
return 0;
}
int WriteFile(const std::string& path, std::string data, unsigned char binary) {
std::ios::openmode mode = std::ios::out;
if (binary)
mode |= std::ios::binary;
std::ofstream file(path, mode);
if (file.is_open()) {
file << data;
file.close();
return 1;
}
file.close();
return 0;
}
void GetFilesInDirectory(std::vector<std::string> &out, const std::string &directory, unsigned char includePath) // thx stackoverflow
{
HANDLE dir;
WIN32_FIND_DATA file_data;
if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
return; /* No files found */
do {
const std::string file_name = file_data.cFileName;
const std::string full_file_name = directory + "/" + file_name;
const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (file_name[0] == '.')
continue;
if (is_directory)
continue;
out.push_back(includePath ? full_file_name : file_name);
} while (FindNextFile(dir, &file_data));
FindClose(dir);
}
int FileCheck(int pid, std::vector<std::string> list) {
int count = 0;
HANDLE proc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pid);
if (proc) {
char pathRaw[MAX_PATH];
if (GetModuleFileNameEx(proc, NULL, pathRaw, MAX_PATH)) {
std::string path = pathRaw;
path = path.substr(0, path.find_last_of("\\/")); // remove file name
std::vector<std::string> files;
GetFilesInDirectory(files, path, false);
for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); i++) {
for (std::vector<std::string>::iterator j = list.begin(); j != list.end(); j++) {
if (*i == *j) count++;
}
}
}
}
CloseHandle(proc);
return count;
}
}
namespace Processes {
int GetProcessByImageName(const char* imageName) {
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (strcmp(entry.szExeFile, imageName) == 0)
{
CloseHandle(snapshot);
return entry.th32ProcessID;
}
}
}
CloseHandle(snapshot);
return 0;
}
void Pause() {
THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
HANDLE hThreads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, NULL);
if (Thread32First(hThreads, &te32)) {
while (Thread32Next(hThreads, &te32)) {
if (te32.th32OwnerProcessID == GetCurrentProcessId() && te32.th32ThreadID != GetCurrentThreadId()) {
HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, false, te32.th32ThreadID);
SuspendThread(hThread);
CloseHandle(hThread);
}
}
}
CloseHandle(hThreads);
}
void Resume() {
THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
HANDLE hThreads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, NULL);
if (Thread32First(hThreads, &te32)) {
while (Thread32Next(hThreads, &te32)) {
if (te32.th32OwnerProcessID == GetCurrentProcessId() && te32.th32ThreadID != GetCurrentThreadId()) {
HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, false, te32.th32ThreadID);
ResumeThread(hThread);
CloseHandle(hThread);
}
}
}
CloseHandle(hThreads);
}
}
namespace Internet {
using namespace std;
string replaceAll(string subject, const string& search,
const string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
string DownloadURL(string URL) {
HINTERNET interwebs = InternetOpenA("Mozilla/5.0", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
HINTERNET urlFile;
string rtn;
if (interwebs) {
urlFile = InternetOpenUrlA(interwebs, URL.c_str(), NULL, NULL, NULL, NULL);
if (urlFile) {
char buffer[2000];
DWORD bytesRead;
do {
InternetReadFile(urlFile, buffer, 2000, &bytesRead);
rtn.append(buffer, bytesRead);
memset(buffer, 0, 2000);
} while (bytesRead);
InternetCloseHandle(interwebs);
InternetCloseHandle(urlFile);
string p = replaceAll(rtn, "|n", "\r\n");
return p;
}
}
InternetCloseHandle(interwebs);
string p = replaceAll(rtn, "|n", "\r\n");
return p;
}
}
std::string RandomString(int length) {
std::string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::string ppj;
int pos = 0;
while (pos != length) {
int ppk = ((rand() % (str.size() - 1)));
ppj = ppj + str[ppk];
pos += 1;
}
return ppj.c_str();
}