Skip to content

Commit 82941a9

Browse files
authored
Update auth.cpp
1 parent a1f3403 commit 82941a9

File tree

1 file changed

+166
-49
lines changed

1 file changed

+166
-49
lines changed

auth.cpp

Lines changed: 166 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ bool initalized;
7171

7272
void KeyAuth::api::init()
7373
{
74-
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)modify, 0, 0, 0);
74+
#if defined(__x86_64__) || defined(_M_X64)
75+
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)modify, 0, 0, 0);
76+
#endif
7577

7678
if (ownerid.length() != 10 || secret.length() != 64)
7779
{
@@ -101,6 +103,35 @@ void KeyAuth::api::init()
101103
XorStr("&enckey=") + sentKey +
102104
XorStr("&name=") + curl_easy_escape(curl, name.c_str(), 0) +
103105
XorStr("&ownerid=") + ownerid;
106+
107+
if (path != "") {
108+
//get the contents of the file
109+
std::ifstream file(path);
110+
std::string token;
111+
std::string thash;
112+
std::getline(file, token);
113+
114+
auto exec = [&](const char* cmd) -> std::string
115+
{
116+
uint16_t line = -1;
117+
std::array<char, 128> buffer;
118+
std::string result;
119+
std::unique_ptr<FILE, decltype(&_pclose)> pipe(_popen(cmd, "r"), _pclose);
120+
if (!pipe) {
121+
throw std::runtime_error(XorStr("popen() failed!"));
122+
}
123+
124+
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
125+
result = buffer.data();
126+
}
127+
return result;
128+
};
129+
130+
thash = exec(("certutil -hashfile \"" + path + XorStr("\" MD5 | find /i /v \"md5\" | find /i /v \"certutil\"")).c_str());
131+
132+
data += XorStr("&token=").c_str() + token;
133+
data += XorStr("&thash=").c_str() + path;
134+
}
104135
curl_easy_cleanup(curl);
105136

106137
auto response = req(data, url);
@@ -1204,6 +1235,37 @@ void KeyAuth::api::forgot(std::string username, std::string email)
12041235
load_response_data(json);
12051236
}
12061237

1238+
void KeyAuth::api::logout() {
1239+
checkInit();
1240+
1241+
auto data =
1242+
XorStr("type=logout") +
1243+
XorStr("&sessionid=") + sessionid +
1244+
XorStr("&name=") + name +
1245+
XorStr("&ownerid=") + ownerid;
1246+
auto response = req(data, url);
1247+
auto json = response_decoder.parse(response);
1248+
if (json[(XorStr("success"))]) {
1249+
1250+
//clear all old user data from program
1251+
user_data.createdate.clear();
1252+
user_data.ip.clear();
1253+
user_data.hwid.clear();
1254+
user_data.lastlogin.clear();
1255+
user_data.username.clear();
1256+
user_data.subscriptions.clear();
1257+
1258+
//clear sessionid
1259+
sessionid.clear();
1260+
1261+
//clear enckey
1262+
enckey.clear();
1263+
1264+
}
1265+
1266+
load_response_data(json);
1267+
}
1268+
12071269
// credits https://stackoverflow.com/a/3790661
12081270
static std::string hexDecode(const std::string& hex)
12091271
{
@@ -1242,7 +1304,7 @@ std::string KeyAuth::api::req(std::string data, std::string url) {
12421304

12431305
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
12441306

1245-
curl_easy_setopt(curl, CURLOPT_NOPROXY, XorStr( "keyauth.win" ) );
1307+
curl_easy_setopt(curl, CURLOPT_NOPROXY, ( "keyauth.win" ) );
12461308

12471309
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
12481310
curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L);
@@ -1289,101 +1351,103 @@ void error(std::string message) {
12891351
__fastfail(0);
12901352
}
12911353
// code submitted in pull request from https://github.com/Roblox932
1292-
auto check_section_integrity( const char *section_name, bool fix = false ) -> bool
1354+
auto check_section_integrity(const char* section_name, bool fix = false) -> bool
12931355
{
1294-
const auto map_file = []( HMODULE hmodule ) -> std::tuple<std::uintptr_t, HANDLE>
1356+
const auto map_file = [](HMODULE hmodule) -> std::tuple<std::uintptr_t, HANDLE>
12951357
{
1296-
wchar_t filename[ MAX_PATH ];
1358+
wchar_t filename[MAX_PATH];
12971359
DWORD size = MAX_PATH;
12981360
QueryFullProcessImageName(GetCurrentProcess(), 0, filename, &size);
12991361

13001362

1301-
const auto file_handle = CreateFile( filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
1302-
if ( !file_handle || file_handle == INVALID_HANDLE_VALUE )
1363+
const auto file_handle = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
1364+
if (!file_handle || file_handle == INVALID_HANDLE_VALUE)
13031365
{
13041366
return { 0ull, nullptr };
13051367
}
13061368

1307-
const auto file_mapping = CreateFileMapping( file_handle, 0, PAGE_READONLY, 0, 0, 0 );
1308-
if ( !file_mapping )
1369+
const auto file_mapping = CreateFileMapping(file_handle, 0, PAGE_READONLY, 0, 0, 0);
1370+
if (!file_mapping)
13091371
{
1310-
CloseHandle( file_handle );
1372+
CloseHandle(file_handle);
13111373
return { 0ull, nullptr };
13121374
}
13131375

1314-
return { reinterpret_cast< std::uintptr_t >( MapViewOfFile( file_mapping, FILE_MAP_READ, 0, 0, 0 ) ), file_handle };
1376+
return { reinterpret_cast<std::uintptr_t>(MapViewOfFile(file_mapping, FILE_MAP_READ, 0, 0, 0)), file_handle };
13151377
};
13161378

1317-
const auto hmodule = GetModuleHandle( 0 );
1318-
if ( !hmodule ) return true;
1379+
const auto hmodule = GetModuleHandle(0);
1380+
if (!hmodule) return true;
13191381

1320-
const auto base_0 = reinterpret_cast< std::uintptr_t >( hmodule );
1321-
if ( !base_0 ) return true;
1382+
const auto base_0 = reinterpret_cast<std::uintptr_t>(hmodule);
1383+
if (!base_0) return true;
13221384

1323-
const auto dos_0 = reinterpret_cast< IMAGE_DOS_HEADER * >( base_0 );
1324-
if ( dos_0->e_magic != IMAGE_DOS_SIGNATURE ) return true;
1385+
const auto dos_0 = reinterpret_cast<IMAGE_DOS_HEADER*>(base_0);
1386+
if (dos_0->e_magic != IMAGE_DOS_SIGNATURE) return true;
13251387

1326-
const auto nt_0 = reinterpret_cast< IMAGE_NT_HEADERS * >( base_0 + dos_0->e_lfanew );
1327-
if ( nt_0->Signature != IMAGE_NT_SIGNATURE ) return true;
1388+
const auto nt_0 = reinterpret_cast<IMAGE_NT_HEADERS*>(base_0 + dos_0->e_lfanew);
1389+
if (nt_0->Signature != IMAGE_NT_SIGNATURE) return true;
13281390

1329-
auto section_0 = IMAGE_FIRST_SECTION( nt_0 );
1391+
auto section_0 = IMAGE_FIRST_SECTION(nt_0);
13301392

1331-
const auto [base_1, file_handle] = map_file( hmodule );
1332-
if ( !base_1 || !file_handle || file_handle == INVALID_HANDLE_VALUE ) return true;
1393+
const auto [base_1, file_handle] = map_file(hmodule);
1394+
if (!base_1 || !file_handle || file_handle == INVALID_HANDLE_VALUE) return true;
13331395

1334-
const auto dos_1 = reinterpret_cast< IMAGE_DOS_HEADER * >( base_1 );
1335-
if ( dos_1->e_magic != IMAGE_DOS_SIGNATURE )
1396+
const auto dos_1 = reinterpret_cast<IMAGE_DOS_HEADER*>(base_1);
1397+
if (dos_1->e_magic != IMAGE_DOS_SIGNATURE)
13361398
{
1337-
UnmapViewOfFile( reinterpret_cast< void * >( base_1 ) );
1338-
CloseHandle( file_handle );
1399+
UnmapViewOfFile(reinterpret_cast<void*>(base_1));
1400+
CloseHandle(file_handle);
13391401
return true;
13401402
}
13411403

1342-
const auto nt_1 = reinterpret_cast< IMAGE_NT_HEADERS * >( base_1 + dos_1->e_lfanew );
1343-
if ( nt_1->Signature != IMAGE_NT_SIGNATURE ||
1404+
const auto nt_1 = reinterpret_cast<IMAGE_NT_HEADERS*>(base_1 + dos_1->e_lfanew);
1405+
if (nt_1->Signature != IMAGE_NT_SIGNATURE ||
13441406
nt_1->FileHeader.TimeDateStamp != nt_0->FileHeader.TimeDateStamp ||
1345-
nt_1->FileHeader.NumberOfSections != nt_0->FileHeader.NumberOfSections )
1407+
nt_1->FileHeader.NumberOfSections != nt_0->FileHeader.NumberOfSections)
13461408
{
1347-
UnmapViewOfFile( reinterpret_cast< void * >( base_1 ) );
1348-
CloseHandle( file_handle );
1409+
UnmapViewOfFile(reinterpret_cast<void*>(base_1));
1410+
CloseHandle(file_handle);
13491411
return true;
13501412
}
13511413

1352-
auto section_1 = IMAGE_FIRST_SECTION( nt_1 );
1414+
auto section_1 = IMAGE_FIRST_SECTION(nt_1);
13531415

13541416
bool patched = false;
1355-
for ( auto i = 0; i < nt_1->FileHeader.NumberOfSections; ++i, ++section_0, ++section_1 )
1417+
for (auto i = 0; i < nt_1->FileHeader.NumberOfSections; ++i, ++section_0, ++section_1)
13561418
{
1357-
if ( strcmp( reinterpret_cast< char * >( section_0->Name ), section_name ) ||
1358-
!( section_0->Characteristics & IMAGE_SCN_MEM_EXECUTE ) ) continue;
1419+
if (strcmp(reinterpret_cast<char*>(section_0->Name), section_name) ||
1420+
!(section_0->Characteristics & IMAGE_SCN_MEM_EXECUTE)) continue;
13591421

1360-
for ( auto i = 0u; i < section_0->SizeOfRawData; ++i )
1422+
for (auto i = 0u; i < section_0->SizeOfRawData; ++i)
13611423
{
1362-
const auto old_value = *reinterpret_cast< BYTE * >( base_1 + section_1->PointerToRawData + i );
1424+
const auto old_value = *reinterpret_cast<BYTE*>(base_1 + section_1->PointerToRawData + i);
13631425

1364-
if ( *reinterpret_cast< BYTE * >( base_0 + section_0->VirtualAddress + i ) == old_value )
1426+
if (*reinterpret_cast<BYTE*>(base_0 + section_0->VirtualAddress + i) == old_value)
13651427
{
13661428
continue;
13671429
}
13681430

1369-
if ( fix )
1431+
if (fix)
13701432
{
1371-
DWORD new_protect { PAGE_EXECUTE_READWRITE }, old_protect;
1372-
VirtualProtect( ( void * )( base_0 + section_0->VirtualAddress + i ), sizeof( BYTE ), new_protect, &old_protect );
1373-
*reinterpret_cast< BYTE * >( base_0 + section_0->VirtualAddress + i ) = old_value;
1374-
VirtualProtect( ( void * )( base_0 + section_0->VirtualAddress + i ), sizeof( BYTE ), old_protect, &new_protect );
1433+
DWORD old_protect = 0;
1434+
DWORD* target_address = reinterpret_cast<DWORD*>(base_0 + section_0->VirtualAddress + i);
1435+
DWORD new_protect = PAGE_EXECUTE_READWRITE;
1436+
1437+
if (target_address != nullptr) {
1438+
if (VirtualProtect(reinterpret_cast<void*>(target_address), sizeof(DWORD), new_protect, &old_protect)) {}
1439+
else { break; }
1440+
}
1441+
//
1442+
*reinterpret_cast<BYTE*>(base_0 + section_0->VirtualAddress + i) = old_value;
1443+
VirtualProtect((void*)(base_0 + section_0->VirtualAddress + i), sizeof(BYTE), old_protect, &new_protect);
13751444
}
13761445

13771446
patched = true;
13781447
}
13791448

13801449
break;
13811450
}
1382-
1383-
UnmapViewOfFile( reinterpret_cast< void * >( base_1 ) );
1384-
CloseHandle( file_handle );
1385-
1386-
return patched;
13871451
}
13881452

13891453
std::string checksum()
@@ -1432,8 +1496,61 @@ std::string getPath() {
14321496
}
14331497
}
14341498

1499+
void RedactField(nlohmann::json& jsonObject, const std::string& fieldName)
1500+
{
1501+
1502+
if (jsonObject.contains(fieldName)) {
1503+
jsonObject[fieldName] = "REDACTED";
1504+
}
1505+
}
1506+
14351507
void debugInfo(std::string data, std::string url, std::string response) {
14361508

1509+
//turn response into json
1510+
nlohmann::json responses = nlohmann::json::parse(response);
1511+
RedactField(responses, "sessionid");
1512+
RedactField(responses, "ownerid");
1513+
RedactField(responses, "app");
1514+
RedactField(responses, "name");
1515+
RedactField(responses, "contents");
1516+
RedactField(responses, "key");
1517+
RedactField(responses, "username");
1518+
RedactField(responses, "password");
1519+
RedactField(responses, "secret");
1520+
RedactField(responses, "version");
1521+
RedactField(responses, "fileid");
1522+
RedactField(responses, "webhooks");
1523+
std::string redacted_response = responses.dump();
1524+
1525+
//turn data into json
1526+
std::replace(data.begin(), data.end(), '&', ' ');
1527+
1528+
nlohmann::json datas;
1529+
1530+
std::istringstream iss(data);
1531+
std::vector<std::string> results((std::istream_iterator<std::string>(iss)),
1532+
std::istream_iterator<std::string>());
1533+
1534+
for (auto const& value : results) {
1535+
datas[value.substr(0, value.find('='))] = value.substr(value.find('=') + 1);
1536+
}
1537+
1538+
RedactField(datas, "sessionid");
1539+
RedactField(datas, "ownerid");
1540+
RedactField(datas, "app");
1541+
RedactField(datas, "name");
1542+
RedactField(datas, "key");
1543+
RedactField(datas, "username");
1544+
RedactField(datas, "password");
1545+
RedactField(datas, "contents");
1546+
RedactField(datas, "secret");
1547+
RedactField(datas, "version");
1548+
RedactField(datas, "fileid");
1549+
RedactField(datas, "webhooks");
1550+
1551+
std::string redacted_data = datas.dump();
1552+
1553+
14371554
//gets the path
14381555
std::string path = getPath();
14391556

@@ -1496,7 +1613,7 @@ void debugInfo(std::string data, std::string url, std::string response) {
14961613

14971614
std::string currentTimeString = std::to_string(hours) + ":" + formattedMinutes + " " + period;
14981615

1499-
std::string contents = "\n\n@ " + currentTimeString + "\nData sent : " + data + "\nResponse : " + response + "Sent to: " + url;
1616+
std::string contents = "\n\n@ " + currentTimeString + "\nData sent : " + redacted_data + "\nResponse : " + redacted_response + "Sent to: " + url;
15001617

15011618
logfile << contents;
15021619

0 commit comments

Comments
 (0)