DNS info Fetcher

This article explains a powerful C++ script that queries DNS records and WHOIS information for any domain.

What the Script Does

The C++ script is designed to fetch DNS records (such as A, AAAA, MX, TXT, etc.) from multiple DNS servers and retrieve WHOIS information for a given domain. It executes the following steps:

  • Queries DNS records for various types using the nslookup command.
  • Executes queries against multiple DNS servers (e.g., 8.8.8.8, 1.1.1.1).
  • Interprets specific DNS record types like SPF in TXT records and mail exchange servers in MX records.
  • Fetches WHOIS information for the domain and appends the results to the output file.
  • All output is saved to a file specified by the user.

How to Use the Script

To use this script, follow these steps:

  1. Make sure you have a C++ compiler installed (such as GCC).
  2. Ensure your system has access to `nslookup` and `whois` commands (commonly available on Linux).
  3. Compile the script using the following command in your terminal:
    g++ dns_info.cpp -o dns_info
  4. Run the compiled script with the following syntax:
    ./dns_info www.domainname.com path_to_where_you_want_to_save_results!\dns_info.txt  
  5. The output file will contain all DNS and WHOIS information for the provided domain.

Tools Required

  • A C++ compiler (e.g., GCC, Clang).
  • Access to `nslookup` and `whois` commands on your system.
  • A terminal or command prompt to execute the compiled binary.

Script Code


// Script code here (shortened for brevity)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#ifdef _WIN32
#include 
#define popen _popen
#define pclose _pclose
#endif

std::string exec(const char* cmd) {
    std::array buffer;
    std::string result;
    std::unique_ptr pipe(popen(cmd, "r"), pclose);
    if (!pipe) {
        throw std::runtime_error("popen() failed!");
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    return result;
}

std::vector split(const std::string& s, char delimiter) {
    std::vector tokens;
    std::string token;
    std::istringstream tokenStream(s);
    while (std::getline(tokenStream, token, delimiter)) {
        tokens.push_back(token);
    }
    return tokens;
}

void queryDNS(std::ofstream& outputFile, const std::string& url, const std::string& dnsServer) {
    std::string dnsTypes[] = {"A", "AAAA", "MX", "TXT", "NS", "SOA", "CNAME", "PTR"};
    for (const auto& type : dnsTypes) {
        outputFile << "DNS " << type << " Records:" << std::endl;
        std::string result = exec(("nslookup -type=" + type + " " + url + " " + dnsServer).c_str());
        outputFile << result << std::endl;

        // Interpret specific records
        if (type == "MX") {
            outputFile << "Interpretation: These are mail exchange servers for " << url << std::endl;
        } else if (type == "TXT") {
            if (result.find("v=spf1") != std::string::npos) {
                outputFile << "Interpretation: SPF record found. This specifies which IP addresses are allowed to send email for this domain." << std::endl;
            }
        } else if (type == "SOA") {
            outputFile << "Interpretation: This is the Start of Authority record, containing administrative information about the zone." << std::endl;
        }
        outputFile << std::endl;
    }
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        std::cerr << "Usage: " << argv[0] << "  " << std::endl;
        return 1;
    }

    std::string url = argv[1];
    std::string filePath = argv[2];

    std::ofstream outputFile(filePath);
    if (!outputFile.is_open()) {
        std::cerr << "Failed to create output file at: " << filePath << std::endl;
        return 1;
    }

    // Query to multiple DNS servers
    std::vector dnsServers = {"8.8.8.8", "1.1.1.1", "9.9.9.9"};
    for (const auto& server : dnsServers) {
        outputFile << "Results from DNS server " << server << ":" << std::endl;
        queryDNS(outputFile, url, server);
        outputFile << std::endl << "--------------------------------" << std::endl << std::endl;
    }

    // Attempt to get WHOIS information
    outputFile << "WHOIS Information:" << std::endl;
    outputFile << exec(("whois " + url).c_str()) << std::endl;

    outputFile.close();
    std::cout << "DNS and domain information saved to " << filePath << std::endl;
    return 0;
}
                    
Xidruk Image

Xidruk

Author and Developer of DNS Fetcher tool