Created
June 13, 2017 13:19
-
-
Save rxwx/652ef15c8851f87f53d72e9cb7cd3c1d to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import OpenSSL | |
from iptools import IpRangeList | |
import ssl | |
import socket | |
import sys | |
import argparse | |
def do_scan(range, csv=False): | |
host_list = [] | |
iprange = [] | |
socket.setdefaulttimeout(1) | |
for ip in IpRangeList(range): | |
try: | |
sys.stdout.write("[*] Testing IP: "+ip) | |
cert = ssl.get_server_certificate((ip, 443),ssl.PROTOCOL_TLSv1) | |
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) | |
components = x509.get_subject().get_components() | |
host = dict(components)['CN'] | |
sys.stdout.write('\r') | |
sys.stdout.write("[+] Found host: "+ip+','+host+"\n") | |
host_list.append(ip+','+host) | |
except Exception as e: | |
sys.stdout.write('\r') | |
pass | |
print "\n[+] Done! We found the following hosts:\n" | |
for h in host_list: | |
print "\t%s" % h | |
if csv: | |
write_csv(csv, host_list) | |
def write_csv(csv, results): | |
f = open(csv, 'w') | |
for l in results: | |
f.write(l+"\n") | |
f.close() | |
def main(): | |
parser = argparse.ArgumentParser(description='Hostname finder') | |
parser.add_argument("-r", "--range", help="CIDR IP Range", dest="range") | |
parser.add_argument("-c", "--csv", help="csv file to write output to", dest="csv", required=False) | |
args = parser.parse_args() | |
if args.range: | |
if args.csv: | |
do_scan(args.range, csv=args.csv) | |
else: | |
do_scan(args.range) | |
else: | |
parser.print_help() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment