Skip to content

Commit f3da3fa

Browse files
committed
Harden parsing and curl escaping
1 parent 24b6892 commit f3da3fa

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

auth.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void checkFiles();
7272
void checkRegistry();
7373
void error(std::string message);
7474
std::string generate_random_number();
75+
std::string curl_escape(CURL* curl, const std::string& input);
7576
std::string seed;
7677
void cleanUpSeedData(const std::string& seed);
7778
std::string signature;
@@ -100,7 +101,7 @@ void KeyAuth::api::init()
100101
XorStr("type=init") +
101102
XorStr("&ver=") + version +
102103
XorStr("&hash=") + hash +
103-
XorStr("&name=") + curl_easy_escape(curl, name.c_str(), 0) +
104+
XorStr("&name=") + curl_escape(curl, name) +
104105
XorStr("&ownerid=") + ownerid;
105106

106107
// to ensure people removed secret from main.cpp (some people will forget to)
@@ -1421,8 +1422,8 @@ std::string KeyAuth::api::webhook(std::string id, std::string params, std::strin
14211422
auto data =
14221423
XorStr("type=webhook") +
14231424
XorStr("&webid=") + id +
1424-
XorStr("&params=") + curl_easy_escape(curl, params.c_str(), 0) +
1425-
XorStr("&body=") + curl_easy_escape(curl, body.c_str(), 0) +
1425+
XorStr("&params=") + curl_escape(curl, params) +
1426+
XorStr("&body=") + curl_escape(curl, body) +
14261427
XorStr("&conttype=") + contenttype +
14271428
XorStr("&sessionid=") + sessionid +
14281429
XorStr("&name=") + name +
@@ -1601,7 +1602,15 @@ void KeyAuth::api::logout() {
16011602

16021603
int VerifyPayload(std::string signature, std::string timestamp, std::string body)
16031604
{
1604-
long long unix_timestamp = std::stoll(timestamp);
1605+
long long unix_timestamp = 0;
1606+
try {
1607+
unix_timestamp = std::stoll(timestamp);
1608+
}
1609+
catch (...) {
1610+
std::cerr << "[ERROR] Invalid timestamp format\n";
1611+
MessageBoxA(0, "Signature verification failed (invalid timestamp)", "KeyAuth", MB_ICONERROR);
1612+
exit(2);
1613+
}
16051614

16061615
auto current_time = std::chrono::system_clock::now();
16071616
long long current_unix_time = std::chrono::duration_cast<std::chrono::seconds>(
@@ -1678,14 +1687,30 @@ std::string get_str_between_two_str(const std::string& s,
16781687
const std::string& start_delim,
16791688
const std::string& stop_delim)
16801689
{
1681-
unsigned first_delim_pos = s.find(start_delim);
1682-
unsigned end_pos_of_first_delim = first_delim_pos + start_delim.length();
1683-
unsigned last_delim_pos = s.find(stop_delim);
1690+
const auto first_delim_pos = s.find(start_delim);
1691+
if (first_delim_pos == std::string::npos)
1692+
return {};
1693+
const auto end_pos_of_first_delim = first_delim_pos + start_delim.length();
1694+
const auto last_delim_pos = s.find(stop_delim, end_pos_of_first_delim);
1695+
if (last_delim_pos == std::string::npos || last_delim_pos < end_pos_of_first_delim)
1696+
return {};
16841697

16851698
return s.substr(end_pos_of_first_delim,
16861699
last_delim_pos - end_pos_of_first_delim);
16871700
}
16881701

1702+
std::string curl_escape(CURL* curl, const std::string& input)
1703+
{
1704+
if (!curl)
1705+
return input;
1706+
char* escaped = curl_easy_escape(curl, input.c_str(), 0);
1707+
if (!escaped)
1708+
return {};
1709+
std::string out(escaped);
1710+
curl_free(escaped);
1711+
return out;
1712+
}
1713+
16891714
void KeyAuth::api::setDebug(bool value) {
16901715
KeyAuth::api::debug = value;
16911716
}

auth.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ namespace KeyAuth {
124124
api::user_data.createdate = data[XorStr("createdate")];
125125
api::user_data.lastlogin = data[XorStr("lastlogin")];
126126

127-
for (int i = 0; i < data[XorStr("subscriptions")].size(); i++) { // Prompto#7895 & stars#2297 was here
127+
for (size_t i = 0; i < data[XorStr("subscriptions")].size(); i++) { // Prompto#7895 & stars#2297 was here
128128
subscriptions_class subscriptions;
129129
subscriptions.name = data[XorStr("subscriptions")][i][XorStr("subscription")];
130130
subscriptions.expiry = data[XorStr("subscriptions")][i][XorStr("expiry")];
@@ -153,7 +153,7 @@ namespace KeyAuth {
153153
api::response.success = data["success"]; // intentional. Possibly trick a reverse engineer into thinking this string is for login function
154154
api::response.message = data["message"];
155155
api::response.channeldata.clear(); //If you do not delete the data before pushing it, the data will be repeated. github.com/TTakaTit
156-
for (const auto sub : data["messages"]) {
156+
for (const auto& sub : data["messages"]) {
157157

158158
std::string authoroutput = sub[XorStr("author")];
159159
std::string messageoutput = sub["message"];

utils.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ std::string utils::get_hwid() {
1212
}
1313

1414
std::time_t utils::string_to_timet(std::string timestamp) {
15-
auto cv = strtol(timestamp.c_str(), NULL, 10);
16-
17-
return (time_t)cv;
15+
char* end = nullptr;
16+
auto cv = strtol(timestamp.c_str(), &end, 10);
17+
if (end == timestamp.c_str())
18+
return 0;
19+
return static_cast<time_t>(cv);
1820
}
1921

2022
std::tm utils::timet_to_tm(time_t timestamp) {

0 commit comments

Comments
 (0)