Skip to content

Commit 644ecd2

Browse files
committed
Detect hosts file overrides
1 parent 0a349d2 commit 644ecd2

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

auth.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ std::string curl_escape(CURL* curl, const std::string& input);
7676
auto check_section_integrity( const char *section_name, bool fix ) -> bool;
7777
void integrity_check();
7878
void integrity_watchdog();
79+
std::string extract_host(const std::string& url);
80+
bool hosts_override_present(const std::string& host);
7981
std::string seed;
8082
void cleanUpSeedData(const std::string& seed);
8183
std::string signature;
@@ -1721,6 +1723,45 @@ std::string curl_escape(CURL* curl, const std::string& input)
17211723
return out;
17221724
}
17231725

1726+
std::string extract_host(const std::string& url)
1727+
{
1728+
std::string host = url;
1729+
const auto scheme_pos = host.find("://");
1730+
if (scheme_pos != std::string::npos)
1731+
host = host.substr(scheme_pos + 3);
1732+
const auto slash_pos = host.find('/');
1733+
if (slash_pos != std::string::npos)
1734+
host = host.substr(0, slash_pos);
1735+
const auto colon_pos = host.find(':');
1736+
if (colon_pos != std::string::npos)
1737+
host = host.substr(0, colon_pos);
1738+
return host;
1739+
}
1740+
1741+
bool hosts_override_present(const std::string& host)
1742+
{
1743+
if (host.empty())
1744+
return false;
1745+
const char* sysroot = std::getenv("SystemRoot");
1746+
std::string hosts_path = sysroot ? std::string(sysroot) : "C:\\Windows";
1747+
hosts_path += "\\System32\\drivers\\etc\\hosts";
1748+
std::ifstream file(hosts_path);
1749+
if (!file.good())
1750+
return false;
1751+
std::string line;
1752+
while (std::getline(file, line)) {
1753+
auto hash_pos = line.find('#');
1754+
if (hash_pos != std::string::npos)
1755+
line = line.substr(0, hash_pos);
1756+
if (line.find(host) == std::string::npos)
1757+
continue;
1758+
// basic whole-word check
1759+
if (line.find(" " + host) != std::string::npos || line.find("\t" + host) != std::string::npos)
1760+
return true;
1761+
}
1762+
return false;
1763+
}
1764+
17241765
void KeyAuth::api::setDebug(bool value) {
17251766
KeyAuth::api::debug = value;
17261767
}
@@ -1729,6 +1770,10 @@ std::string KeyAuth::api::req(const std::string& data, const std::string& url) {
17291770
signature.clear();
17301771
signatureTimestamp.clear();
17311772
integrity_check();
1773+
const auto host = extract_host(url);
1774+
if (hosts_override_present(host)) {
1775+
error(XorStr("Hosts file override detected for API host."));
1776+
}
17321777

17331778
CURL* curl = curl_easy_init();
17341779
if (!curl) {

0 commit comments

Comments
 (0)