-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathUtils.cpp
More file actions
410 lines (352 loc) · 11.1 KB
/
Copy pathUtils.cpp
File metadata and controls
410 lines (352 loc) · 11.1 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <locale>
#include <codecvt>
#include <sstream>
#include <filesystem>
#include <array>
#include <cwctype>
#include <random>
//URLDownloadToFileA
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")
#include "Utils.h"
int Utils::RandomInt(const int min, const int max)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution dist(min, max);
return dist(gen);
}
std::string Utils::ToLower(std::string str)
{
std::ranges::transform(str, str.begin(),
[](const unsigned char c) { return static_cast<char>(std::tolower(c)); });
return str;
}
std::wstring Utils::ToLower(std::wstring wstr)
{
std::ranges::transform(wstr, wstr.begin(), std::towlower);
return wstr;
}
std::string Utils::ToUpper(std::string str)
{
std::ranges::transform(str, str.begin(),
[](const unsigned char c) { return static_cast<char>(std::toupper(c)); });
return str;
}
std::wstring Utils::ToUpper(std::wstring wstr)
{
std::ranges::transform(wstr, wstr.begin(), std::towupper);
return wstr;
}
bool Utils::StringContains(std::string strA, std::string strB, const bool ignoreCase)
{
if (strA.empty() || strB.empty())
return true;
if (ignoreCase)
{
strA = ToLower(strA);
strB = ToLower(strB);
}
if (strA.find(strB) != std::string::npos)
return true;
return false;
}
bool Utils::StringContains(std::wstring wstrA, std::wstring wstrB, const bool ignoreCase)
{
if (wstrA.empty() || wstrB.empty())
return true;
if (ignoreCase)
{
wstrA = ToLower(wstrA);
wstrB = ToLower(wstrB);
}
if (wstrA.find(wstrB) != std::wstring::npos)
return true;
return false;
}
std::string Utils::WstringToString(const std::wstring& wstr)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(wstr);
}
std::wstring Utils::StringToWstring(const std::string& str)
{
try
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.from_bytes(str);
}
catch (std::range_error) // ThrowByValueCatchByReference
{
std::wostringstream s;
s << str.c_str();
return s.str();
}
}
std::vector<std::string> Utils::StringSplit(std::string str, const std::string& separator, const int max)
{
int count = 0;
size_t pos;
std::vector<std::string> vec;
while ((pos = str.find(separator)) != std::string::npos)
{
if (max != -1 && count == max)
{
break;
}
count++;
std::string token = str.substr(0, pos);
vec.emplace_back(token);
str.erase(0, pos + separator.length());
}
vec.emplace_back(str);
return vec;
}
std::string Utils::RandomString(const size_t size)
{
std::string str;
for (size_t i = 0; i < size; i++)
str += std::to_string(RandomInt(0, 1) ? RandomInt(48, 57) : RandomInt(97, 122));
return str;
}
std::wstring Utils::RandomWString(const size_t size, const std::pair<unsigned, unsigned> range)
{
std::wstring str;
if (range.first == 0 && range.second == 0)
{
for (size_t i = 0; i < size; i++)
str += std::to_wstring(RandomInt(0, 1) ? RandomInt(48, 57) : RandomInt(97, 122));
}
else
{
for (size_t i = 0; i < size; i++)
str += static_cast<wchar_t>(RandomInt(range.first, range.second));
}
return str;
}
void Utils::CopyToClipboard(const std::string& text)
{
if (!OpenClipboard(nullptr))
return;
const int wbufLen = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, nullptr, 0);
const HGLOBAL wbufHandle = GlobalAlloc(GMEM_MOVEABLE, static_cast<SIZE_T>(wbufLen) * sizeof(WCHAR));
if (wbufHandle == nullptr)
{
CloseClipboard();
return;
}
const auto wbufGlobal = static_cast<WCHAR*>(GlobalLock(wbufHandle));
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, wbufGlobal, wbufLen);
GlobalUnlock(wbufHandle);
EmptyClipboard();
if (SetClipboardData(CF_UNICODETEXT, wbufHandle) == nullptr)
GlobalFree(wbufHandle);
CloseClipboard();
}
bool Utils::DownloadFile(std::string fileName, const std::string& directory, const std::string& url)
{
// Create folder if it doesn't exists
if (!std::filesystem::exists(directory))
std::filesystem::create_directory(directory);
if (fileName[0] == '\\')
fileName.erase(0);
const std::string file = directory + "\\" + fileName;
// Don't download if file already exists
if (std::filesystem::exists(file))
return true;
const std::string fullPath = std::filesystem::current_path().string() + "\\" + file;
const std::string toDownload = url + fileName;
// Download file
if (const HRESULT result = URLDownloadToFileA(nullptr, toDownload.c_str(), fullPath.c_str(), 0, nullptr); result != S_OK)
return false;
return true;
}
bool Utils::ContainsOnlyASCII(const std::string& buff)
{
for (const char i : buff)
{
if (i == 0)
return true;
if (static_cast<unsigned char>(i) > 127)
return false;
}
return true;
}
std::string Utils::Utf8Encode(const std::wstring& wstr)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> conv;
return conv.to_bytes(wstr);
}
std::string Utils::Exec(const char* cmd)
{
std::array<char, 128> buffer;
std::string result;
const std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose);
if (!pipe)
{
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr)
{
result += buffer.data();
}
return result;
}
std::string Utils::RenameExe()
{
char szExeFileName[MAX_PATH];
static HMODULE kernel32 = GetModuleHandleA("kernel32");
static auto pGetModuleFileNameA = (decltype(&GetModuleFileNameA))GetProcAddress(kernel32, "GetModuleFileNameA");
pGetModuleFileNameA(nullptr, szExeFileName, MAX_PATH);
const auto path = std::string(szExeFileName);
const std::string exe = path.substr(path.find_last_of('\\') + 1, path.size());
std::string newName = RandomString(RandomInt(5, 10));
newName += ".exe";
if (!rename(exe.c_str(), newName.c_str()))
return newName;
return "";
}
void Utils::OpenUrl(const char* url, const char* args, int flags)
{
static decltype(&ShellExecuteA) pShellExecuteA = nullptr;
if (!pShellExecuteA) {
HMODULE shell32 = LoadLibraryA("shell32.dll");
(LPVOID&)pShellExecuteA = GetProcAddress(shell32, "ShellExecuteA");
}
if (pShellExecuteA)
pShellExecuteA(nullptr, "open", url, args, nullptr, flags);
}
void Utils::OpenUrl(const wchar_t* url, const wchar_t* args, int flags)
{
static decltype(&ShellExecuteW) pShellExecuteW = nullptr;
if (!pShellExecuteW) {
HMODULE shell32 = LoadLibraryA("shell32.dll");
(LPVOID&)pShellExecuteW = GetProcAddress(shell32, "ShellExecuteW");
}
if (pShellExecuteW)
pShellExecuteW(NULL, L"open", url, args, NULL, flags);
}
bool Utils::HideFile(const std::string& file)
{
if (const int attr = GetFileAttributesA(file.c_str()); (attr & FILE_ATTRIBUTE_HIDDEN) == 0)
{
SetFileAttributesA(file.c_str(), attr | FILE_ATTRIBUTE_HIDDEN);
return true;
}
return false;
}
bool Utils::RunAsUser(const LPCWSTR lpApplicationName, const LPWSTR lpCommandLine)
{
using tOpenProcessToken = BOOL(WINAPI*)(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle);
static auto OpenProcessToken = reinterpret_cast<tOpenProcessToken>(GetProcAddress(LoadLibraryW(L"advapi32.dll"), "OpenProcessToken"));
HANDLE hProcessToken = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hProcessToken))
{
// fails if current process isn't elevated
CloseHandle(hProcessToken);
return false;
}
using tLookupPrivilegeValueW = BOOL(WINAPI*)(LPCWSTR lpSystemName, LPCWSTR lpName, PLUID lpLuid);
static auto LookupPrivilegeValueW = reinterpret_cast<tLookupPrivilegeValueW>(GetProcAddress(
LoadLibraryW(L"advapi32.dll"), "LookupPrivilegeValueW"));
TOKEN_PRIVILEGES tkp = { 0 };
tkp.PrivilegeCount = 1;
if (!LookupPrivilegeValueW(nullptr, SE_INCREASE_QUOTA_NAME, &tkp.Privileges[0].Luid))
{
CloseHandle(hProcessToken);
return false;
}
using tAdjustTokenPrivileges = BOOL(WINAPI*)(HANDLE TokenHandle, BOOL DisableAllPrivileges,
PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState,
PDWORD ReturnLength);
static auto AdjustTokenPrivileges = reinterpret_cast<tAdjustTokenPrivileges>(GetProcAddress(
LoadLibraryW(L"advapi32.dll"), "AdjustTokenPrivileges"));
DWORD returnLength = 0;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hProcessToken, FALSE, &tkp, 0, nullptr, &returnLength))
{
CloseHandle(hProcessToken);
return false;
}
using tGetShellWindow = HWND(WINAPI*)();
static auto GetShellWindow = reinterpret_cast<tGetShellWindow>(GetProcAddress(LoadLibraryW(L"user32.dll"), "GetShellWindow"));
const HWND hwnd = GetShellWindow();
if (!hwnd)
{
CloseHandle(hProcessToken);
return false;
}
DWORD pid = 0;
GetWindowThreadProcessId(hwnd, &pid);
if (!pid)
{
CloseHandle(hProcessToken);
return false;
}
static HMODULE kernel32 = GetModuleHandleA("kernel32");
static auto pOpenProcess = (decltype(&OpenProcess))GetProcAddress(kernel32, "OpenProcess");
const HANDLE hShellProcess = pOpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
if (!hShellProcess)
{
CloseHandle(hProcessToken);
return false;
}
HANDLE hShellProcessToken = nullptr;
if (!OpenProcessToken(hShellProcess, TOKEN_DUPLICATE, &hShellProcessToken))
{
CloseHandle(hProcessToken);
CloseHandle(hShellProcess);
CloseHandle(hShellProcessToken);
return false;
}
using tDuplicateTokenEx = BOOL(WINAPI*)(HANDLE hExistingToken, DWORD dwDesiredAccess, LPSECURITY_ATTRIBUTES pTokenAttributes,
SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, TOKEN_TYPE TokenType, PHANDLE phNewToken);
static auto DuplicateTokenEx = reinterpret_cast<tDuplicateTokenEx>(GetProcAddress(LoadLibraryW(L"advapi32.dll"), "DuplicateTokenEx"));
HANDLE hPrimaryToken = nullptr;
if (!DuplicateTokenEx(hShellProcessToken, TOKEN_ALL_ACCESS, nullptr, SecurityImpersonation, TokenPrimary, &hPrimaryToken))
{
CloseHandle(hProcessToken);
CloseHandle(hShellProcess);
CloseHandle(hShellProcessToken);
CloseHandle(hPrimaryToken);
return false;
}
using tCreateProcessWithTokenW = BOOL(WINAPI*)(HANDLE hToken, DWORD dwLogonFlags, LPCWSTR lpApplicationName,
LPWSTR lpCommandLine, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation);
static auto CreateProcessWithTokenW = reinterpret_cast<tCreateProcessWithTokenW>(GetProcAddress(
LoadLibraryW(L"advapi32.dll"), "CreateProcessWithTokenW"));
PROCESS_INFORMATION pi = { nullptr };
STARTUPINFOW si = { 0 };
si.cb = sizeof(si);
si.wShowWindow = SW_SHOWNORMAL;
si.dwFlags = STARTF_USESHOWWINDOW;
if (!CreateProcessWithTokenW(hPrimaryToken, 0, lpApplicationName, lpCommandLine, 0/*DETACHED_PROCESS - error 87*/, nullptr, nullptr, &si, &pi))
{
//printf("%d",GetLastError());
CloseHandle(hProcessToken);
CloseHandle(hShellProcess);
CloseHandle(hShellProcessToken);
CloseHandle(hPrimaryToken);
CloseHandle(pi.hProcess);
return false;
}
CloseHandle(hProcessToken);
CloseHandle(hShellProcess);
CloseHandle(hShellProcessToken);
CloseHandle(hPrimaryToken);
CloseHandle(pi.hProcess);
return true;
}
cpr::Header Utils::StringToHeader(const std::string& str)
{
cpr::Header header;
for (const auto& line : StringSplit(str, "\r\n"))
{
if (const auto index = line.find(": ", 0); index != std::string::npos)
{
header.insert({ line.substr(0, index), line.substr(index + 2) });
}
}
return header;
}