entropy

 avatar
unknown
c_cpp
a month ago
3.5 kB
4
Indexable
// Find X-Entropy header
std::string entropy_value;
for (const auto& header : entropy_headers) {
    // Check for both uppercase and lowercase variants
    if (header.find("X-Entropy:") == 0 || header.find("x-entropy:") == 0) {
        size_t pos = header.find(':');
        if (pos != std::string::npos) {
            entropy_value = header.substr(pos + 1);
            // Trim whitespace
            size_t start = entropy_value.find_first_not_of(" \t\r\n");
            size_t end = entropy_value.find_last_not_of(" \t\r\n");
            if (start != std::string::npos && end != std::string::npos) {
                entropy_value = entropy_value.substr(start, end - start + 1);
            }
            break;
        }
    }
}

// Fallback - directly extract from the header we can see in the logs
if (entropy_value.empty()) {
    for (const auto& header : entropy_headers) {
        if (header.find("x-entropy:") != std::string::npos) {
            size_t pos = header.find("x-entropy:") + 10;
            entropy_value = header.substr(pos);
            size_t start = entropy_value.find_first_not_of(" \t\r\n");
            size_t end = entropy_value.find_last_not_of(" \t\r\n");
            if (start != std::string::npos && end != std::string::npos) {
                entropy_value = entropy_value.substr(start, end - start + 1);
            }
            break;
        }
    }
}

// Last resort - use the specific value from logs if nothing else works
if (entropy_value.empty() && entropy_headers.size() >= 3) {
    // Try to extract directly from the third header which contains x-entropy
    std::string third_header = entropy_headers[2];
    size_t pos = third_header.find_last_of(':');
    if (pos != std::string::npos) {
        entropy_value = third_header.substr(pos + 1);
        // Trim whitespace
        size_t start = entropy_value.find_first_not_of(" \t\r\n");
        size_t end = entropy_value.find_last_not_of(" \t\r\n");
        if (start != std::string::npos && end != std::string::npos) {
            entropy_value = entropy_value.substr(start, end - start + 1);
        }
    }
}

if (http_code == 200 && !entropy_value.empty()) {
    std::cout << "X-Entropy value: " << entropy_value << std::endl;

    // Convert hex to bytes
    std::vector<uint8_t> entropy_bytes = hex_to_bytes(entropy_value);

    if (entropy_bytes.size() < 16) {
        std::cerr << "Invalid entropy data: too short" << std::endl;
        return 1;
    }

    // First 16 bytes are the IV
    std::vector<uint8_t> iv(entropy_bytes.begin(), entropy_bytes.begin() + 16);

    // Rest is ciphertext
    std::vector<uint8_t> ciphertext(entropy_bytes.begin() + 16, entropy_bytes.end());

    // Decrypt
    std::vector<uint8_t> plaintext = aes_decrypt(ciphertext, key, iv);

    if (plaintext.size() >= 8) {
        // First 8 bytes are timestamp
        int64_t timestamp = 0;
        memcpy(&timestamp, plaintext.data(), 8);

        // Convert to string for display
        time_t time = timestamp;
        char time_str[100];
        strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", gmtime(&time));

        std::cout << "Server timestamp: " << timestamp << " (" << time_str << " UTC)" << std::endl;
    }
    else {
        std::cerr << "Decrypted data too short" << std::endl;
    }
}
else {
    std::cerr << "Failed to get entropy header" << std::endl;
}
Editor is loading...
Leave a Comment