Created
February 7, 2024 06:48
-
-
Save kimboslice99/0b7306a0bae17a49312c887d29fe314b to your computer and use it in GitHub Desktop.
Bulk DNS queries at blazing fast speed! (A/AAAA)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// enter domain names in a file, newline delimited | |
$filename = 'domain_names.txt'; | |
// read the file | |
$fp = @fopen($filename, 'r'); | |
!$fp?die('File not found!'):''; | |
$domains = explode(PHP_EOL, fread($fp, filesize($filename))); | |
fclose($fp); | |
// Mapping for DNS record types | |
$typeMapping = [ | |
1 => 'A', // Mapping for A record type | |
28 => 'AAAA', // Mapping for AAAA record type | |
// Add more mappings as needed for other record types | |
]; | |
$dnsRecords = []; // Array to store DNS records | |
$mh = curl_multi_init(); // Initialize cURL multi-handler for concurrent requests | |
$ch = []; // Array to store individual cURL handles | |
// Loop through each domain to make A and AAAA DNS record requests | |
foreach ($domains as $domain) { | |
// cURL handle for A record request | |
$chA = curl_init(); | |
$optionsA = [ | |
CURLOPT_URL => "https://dns.google/resolve?name=$domain&type=A", // DNS query URL for A records | |
CURLOPT_RETURNTRANSFER => true, // Return the transfer as a string | |
CURLOPT_HTTP_VERSION => 31, // Set to 31 to require HTTP/3, 30 to allow fallback | |
]; | |
curl_setopt_array($chA, $optionsA); | |
curl_multi_add_handle($mh, $chA); | |
$ch[] = $chA; | |
// cURL handle for AAAA record request | |
$chAAAA = curl_init(); | |
$optionsAAAA = [ | |
CURLOPT_URL => "https://dns.google/resolve?name=$domain&type=AAAA", // DNS query URL for AAAA records | |
CURLOPT_RETURNTRANSFER => true, // Return the transfer as a string | |
CURLOPT_HTTP_VERSION => 31, // Set to 31 to require HTTP/3, 30 to allow fallback | |
]; | |
curl_setopt_array($chAAAA, $optionsAAAA); | |
curl_multi_add_handle($mh, $chAAAA); | |
$ch[] = $chAAAA; | |
} | |
// Execute the cURL multi-handler to perform concurrent requests | |
do { | |
while (($info = curl_multi_info_read($mh)) !== false) { | |
curl_multi_remove_handle($mh, $info['handle']); | |
} | |
curl_multi_exec($mh, $running); | |
} while ($running > 0); | |
// Process the responses for each domain | |
foreach ($ch as $index => $handle) { | |
$response = curl_multi_getcontent($handle); | |
$domain = $domains[(int)($index / 2)]; // Cast to int to avoid float to int conversion warning | |
// Process $response as needed | |
$decodedResponse = json_decode($response, true); | |
// Check if the 'Answer' key exists in the response | |
if (isset($decodedResponse['Answer'])) { | |
$records = $decodedResponse['Answer']; | |
$recordType = null; | |
$recordType = $typeMapping[$records[0]['type']]; | |
// If neither A nor AAAA found, mark as 'Not Found' | |
$recordType = $recordType ?? 'Not Found'; | |
// Store DNS records in the array | |
$dnsRecords[$domain][$recordType] = array_column($records, 'data'); | |
} else { | |
// If 'Answer' key is not found, mark as 'Record Not Found' | |
$recordType = $typeMapping[$decodedResponse['Question'][0]['type']]; | |
$dnsRecords[$domain]["$recordType"] = ["# Record Not Found"]; | |
} | |
} | |
curl_multi_close($mh); // Close the cURL multi-handler | |
$detailed = $json = $yaml = 0; | |
if(isset($_GET['detailed'])) | |
$detailed = 1; | |
if(isset($_GET['json'])) | |
$json = 1; | |
if(isset($_GET['yaml']) && function_exists('yaml_emit')) | |
$yaml = 1; | |
if($json){ | |
header('Content-Type: application/json'); | |
print json_encode($dnsRecords); | |
} elseif($yaml){ | |
header('Content-Type: text/yaml'); | |
print yaml_emit($dnsRecords); | |
} else { | |
header('Content-Type: text/plain'); | |
// Output the DNS records in a human-readable format | |
foreach ($dnsRecords as $domain => $records) { | |
if($detailed) | |
echo "# Domain: $domain".PHP_EOL; | |
foreach ($records as $type => $values) { | |
if($detailed) | |
echo "# $type".PHP_EOL; | |
foreach ($values as $value) { | |
echo "$value".PHP_EOL; | |
} | |
} | |
if($detailed) | |
echo PHP_EOL; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment