Advertisement
opexxx

dnsreverser.py

Jul 14th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Quick script that attempts to find the reverse DNS info
  4. # from a provided IP range.
  5.  
  6. import argparse
  7. import os
  8. import socket
  9. import sys
  10. from netaddr import IPNetwork
  11.  
  12.  
  13. def cli_parser():
  14.  
  15.     # Command line argument parser
  16.     parser = argparse.ArgumentParser(
  17.         add_help=False,
  18.         description="DNSReverser takes IP addresses and tries to find its hostname.")
  19.     parser.add_argument(
  20.         "-f", metavar="ips.txt", default=None,
  21.         help="File containing IPs to resolve hostnames for.")
  22.     parser.add_argument(
  23.         "-ip", metavar='192.168.1.1', default=None,
  24.         help="Used to find hostname about a specific IP.")
  25.     parser.add_argument(
  26.         "-cidr", metavar='192.168.1.0/24', default=None,
  27.         help="Used to find hostnames about a specific CIDR range.")
  28.     parser.add_argument(
  29.         '-h', '-?', '--h', '-help', '--help', action="store_true",
  30.         help=argparse.SUPPRESS)
  31.     args = parser.parse_args()
  32.  
  33.     if args.h:
  34.         parser.print_help()
  35.         sys.exit()
  36.  
  37.     return args.f, args.ip, args.cidr
  38.  
  39.  
  40. def rdns_lookup(ip_address):
  41.  
  42.     try:
  43.         # Get the reverse dns name if it exists
  44.         reverse_dns = socket.gethostbyaddr(ip_address)[0]
  45.         script_out = ip_address.strip() + ' ' + reverse_dns + '\n'
  46.         with open('reverse_dns_results.txt', 'a') as rev_results:
  47.             rev_results.write(script_out)
  48.     except:
  49.         print "No Reverse DNS for " + str(ip_address) + "."
  50.  
  51.     return
  52.  
  53.  
  54. def cidr_net(cidr_range):
  55.     net_1 = IPNetwork(cidr_range)
  56.  
  57.     return net_1
  58.  
  59.  
  60. def file_read(input_file):
  61.     with open(input_file, 'r') as f:
  62.         ip_file = f.readlines()
  63.  
  64.     return ip_file
  65.  
  66.  
  67. def title():
  68.     # Clear the screen
  69.     os.system('clear')
  70.  
  71.     print "############################################################"
  72.     print "# Reverse DNS Scanner #"
  73.     print "############################################################\n"
  74.     print "Starting Reverse DNS Scan...\n"
  75.  
  76.     return
  77.  
  78.  
  79. if __name__ == '__main__':
  80.  
  81.     # Parse command line options
  82.     cli_file, cli_ip, cli_cidr = cli_parser()
  83.  
  84.     title()
  85.  
  86.     if cli_cidr is not None and cli_file is None and cli_ip is None:
  87.         ip_cidr_list = cidr_net(cli_cidr)
  88.         for ip_add in ip_cidr_list:
  89.             ip_add = str(ip_add)
  90.             ip_add = ip_add.strip()
  91.             rdns_lookup(ip_add)
  92.  
  93.     elif cli_file is not None and cli_cidr is None and cli_ip is None:
  94.         ip_file_input = file_read(cli_file)
  95.         for ip_add_file in ip_file_input:
  96.             ip_add_file = ip_add_file.strip()
  97.             rdns_lookup(ip_add_file)
  98.  
  99.     elif cli_ip is not None and cli_cidr is None and cli_file is None:
  100.         rdns_lookup(cli_ip)
  101.  
  102.     else:
  103.         print "[*]ERROR: Please start over and provide a valid input option."
  104.         sys.exit()
  105.  
  106.     print "\nScan Completed! Check the output file!\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement