Skip to content

Instantly share code, notes, and snippets.

@0xbharath
Last active September 9, 2024 14:31
Show Gist options
  • Save 0xbharath/cb4a0d35b2031255e9e8c63d14f074bd to your computer and use it in GitHub Desktop.
Save 0xbharath/cb4a0d35b2031255e9e8c63d14f074bd to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
namecheap-export-dns-records.py - Extract DNS records from Namecheap
This script can export DNS records from a domain in Namecheap and print the domains detected.
python namecheap-export-dns-records.py
To use the script you need to enable the Namecheap API on your account and
whitelist the public IPv4 address of the host you're running the script on. See
https://www.namecheap.com/support/api/intro/ for details.
You need to provide valid Namecheap API details in the global variables.
Based on work by Jonathan Kamens <jik@kamens.us>
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License at
<https://www.gnu.org/licenses/> for more details.
"""
import argparse
from itertools import count
import json
from lxml import etree
import requests
import sys
import yaml
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
namecheap_api_url = 'https://api.namecheap.com/xml.response'
#api_user = "<namecheap_api_username>"
#user_name = "<namecheap_account_username>"
#api_key = "<namecheap_api_key>"
#client_ip = "<whitelisted_client_ip_address>" # Client IP white-listing is mandatory in Namecheap
def make_namecheap_request(data):
request = data.copy()
request.update({
'ApiUser': api_user,
'UserName': user_name,
'ApiKey': api_key,
'ClientIP': client_ip,
})
response = requests.post(namecheap_api_url, request)
response.raise_for_status()
response_xml = etree.XML(response.content)
if response_xml.get('Status') != 'OK':
raise Exception('Bad response: {}'.format(response.content))
return response_xml
def get_records(sld, tld):
response = make_namecheap_request({
'Command': 'namecheap.domains.dns.getHosts',
'SLD': sld,
'TLD': tld})
host_elements = response.xpath(
'/x:ApiResponse/x:CommandResponse/x:DomainDNSGetHostsResult/x:host',
namespaces={'x': 'http://api.namecheap.com/xml.response'})
records = [dict(h.attrib) for h in host_elements]
for record in records:
record.pop('AssociatedAppTitle', None)
record.pop('FriendlyName', None)
record.pop('HostId', None)
record['HostName'] = record.pop('Name')+"."+sld+"."+tld
record.pop('IsActive', None)
record.pop('IsDDNSEnabled', None)
if record['Type'] != 'MX':
record.pop('MXPref', None)
record['RecordType'] = record.pop('Type')
if record['TTL'] == '1800':
record.pop('TTL')
return records
def do_export(sld,tld):
records = get_records(sld,tld)
for record in records:
print(record['HostName'])
def dict_hash(d):
d = d.copy()
name = d.pop('HostName')
type_ = d.pop('RecordType')
return (type_, name, json.dumps(d, sort_keys=True))
def main():
domain = "example.org"
(sld, tld) = domain.split('.', 1)
logging.info(f'Enumerating domain: {domain}')
do_export(sld,tld)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment