1+ import requests
2+ import whois
3+
4+ import argparse
5+
6+
7+ def is_registered (domain_name ):
8+ """
9+ A function that returns a boolean indicating
10+ whether a `domain_name` is registered
11+ """
12+ try :
13+ w = whois .whois (domain_name )
14+ except Exception :
15+ return False
16+ else :
17+ return bool (w .domain_name )
18+
19+
20+ def get_discovered_subdomains (domain , subdomain_list , timeout = 2 ):
21+ # a list of discovered subdomains
22+ discovered_subdomains = []
23+ for subdomain in subdomain_list :
24+ # construct the url
25+ url = f"http://{ subdomain } .{ domain } "
26+ try :
27+ # if this raises a connection error, that means the subdomain does not exist
28+ requests .get (url , timeout = timeout )
29+ except requests .ConnectionError :
30+ # if the subdomain does not exist, just pass, print nothing
31+ pass
32+ else :
33+ print ("[+] Discovered subdomain:" , url )
34+ # append the discovered subdomain to our list
35+ discovered_subdomains .append (url )
36+
37+ return discovered_subdomains
38+
39+
40+ if __name__ == "__main__" :
41+ parser = argparse .ArgumentParser (description = "Domain name information extractor, uses WHOIS db and scans for subdomains" )
42+ parser .add_argument ("domain" , help = "The domain name without http(s)" )
43+ parser .add_argument ("-t" , "--timeout" , type = int , default = 2 ,
44+ help = "The timeout in seconds for prompting the connection, default is 2" )
45+ parser .add_argument ("-s" , "--subdomains" , default = "subdomains.txt" ,
46+ help = "The file path that contains the list of subdomains to scan, default is subdomains.txt" )
47+ parser .add_argument ("-o" , "--output" ,
48+ help = "The output file path resulting the discovered subdomains, default is {domain}-subdomains.txt" )
49+
50+ # parse the command-line arguments
51+ args = parser .parse_args ()
52+ if is_registered (args .domain ):
53+ whois_info = whois .whois (args .domain )
54+ # print the registrar
55+ print ("Domain registrar:" , whois_info .registrar )
56+ # print the WHOIS server
57+ print ("WHOIS server:" , whois_info .whois_server )
58+ # get the creation time
59+ print ("Domain creation date:" , whois_info .creation_date )
60+ # get expiration date
61+ print ("Expiration date:" , whois_info .expiration_date )
62+ # print all other info
63+ print (whois_info )
64+
65+ print ("=" * 50 , "Scanning subdomains" , "=" * 50 )
66+ # read all subdomains
67+ with open (args .subdomains ) as file :
68+ # read all content
69+ content = file .read ()
70+ # split by new lines
71+ subdomains = content .splitlines ()
72+ discovered_subdomains = get_discovered_subdomains (args .domain , subdomains )
73+ # make the discovered subdomains filename dependant on the domain
74+ discovered_subdomains_file = f"{ args .domain } -subdomains.txt"
75+ # save the discovered subdomains into a file
76+ with open (discovered_subdomains_file , "w" ) as f :
77+ for subdomain in discovered_subdomains :
78+ print (subdomain , file = f )
0 commit comments