Created
December 31, 2015 16:17
-
-
Save NateBrune/34d1133e70dca361693c 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 os | |
import sys | |
import dns.resolver | |
import Queue | |
import time | |
import threading | |
# Various settings | |
numthreads = 150 | |
resultList = [] | |
wq = Queue.Queue() | |
# Thread Launcher | |
def launchThreads(argsfile,numthreads): | |
global wq | |
# Enqueing Stuff | |
with open(args.file, "r") as ipsfile: | |
for ip in ipsfile: | |
wq.put(ip) | |
# Spawning Threads | |
for i in range(numthreads): | |
t = threading.Thread(target=tRun) | |
t.start() | |
while threading.active_count() > 1: | |
time.sleep(0.1) | |
# Thread | |
def tRun(): | |
global wq | |
global resultList | |
while not wq.empty(): | |
ip = wq.get() | |
resolver = dns.resolver.Resolver() | |
resolver.lifetime = args.timeout | |
target = args.url or 'google.com' | |
resolver.nameservers = [ip] | |
# If we get an answer, it's open | |
try: | |
answers = resolver.query(target, 'A') | |
print '%s,open' % (ip.rstrip()) | |
resultList.append('%s' % (ip)) | |
# If no response do nothing | |
except (dns.resolver.NoAnswer, dns.resolver.NoNameservers), ex: | |
print '%s,closed' % (ip.rstrip()) | |
# If timeout do nothing | |
except dns.resolver.Timeout, ex: | |
print '%s,unresponsive' % (ip.rstrip()) | |
pass | |
# If something else went sideways do nothing | |
except: | |
print '%s,unresponsive' % (ip.rstrip()) | |
pass | |
if __name__ == '__main__': | |
# Process command line options | |
import argparse | |
parser = argparse.ArgumentParser(description = 'Scan IPv6 file for DNS resolvers') | |
parser.add_argument('-t', '--timeout', type = float, default = 1.0, help = 'timeout for each request') | |
parser.add_argument('-u', '--url', default = 'finn.hype', help = 'hostname to use as target') | |
parser.add_argument('file', type = str, help = 'list of ip addresses') | |
args = parser.parse_args() | |
# Run scans, die properly on CTRL-C | |
try: | |
launchThreads(args.file,numthreads) | |
# Print results | |
print 'Final result list' | |
for x in resultList: | |
print x | |
print "Number of open resolvers: %s" %(len(resultList)) | |
except KeyboardInterrupt, ex: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment