Last active
August 29, 2015 14:26
-
-
Save samba/b238f6d9a7397129fa15 to your computer and use it in GitHub Desktop.
Resolve DNS by regex pattern. e.g. "g[o]{2}gl[aeo].com"
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 socket | |
import argparse | |
import sys | |
import re | |
def test_domain(domain_name): | |
try: | |
return bool(socket.gethostbyname(domain_name)) | |
except socket.gaierror, e: | |
return False | |
RE_PARSE_EXPR = re.compile(r'\[([^\]]+)\](?:\{([0-9]+)\})?') | |
def gen_charset(expr_charset): | |
results = [] | |
def genchars(char_range): | |
start, end = char_range.group(1, 2) | |
results.extend(range(ord(start), ord(end) +1)) | |
expr_charset = re.sub(r'(.)\-(.)', genchars, expr_charset) | |
results.extend([ ord(i) for i in expr_charset ]) | |
return results | |
class Generator(object): | |
def __init__(self, charsequences): | |
self.charsequences = charsequences | |
self.counts = [ len(q) for q in charsequences ] | |
self.total_combinations = reduce(lambda x, y: x * y, self.counts) | |
@property | |
def significance(self): | |
return reversed(list(enumerate(self.charsequences))) | |
def selectIndexSequence(self, value): | |
offset = 1 | |
for i, q in self.significance: | |
index = ((value / offset) % len(q)) | |
offset = offset * len(q) | |
yield q[index] | |
def getValueAtIndex(self, index): | |
return reversed(list(self.selectIndexSequence(index))) | |
@property | |
def maximum(self): | |
return 0 + self.total_combinations | |
@property | |
def all(self): | |
for i in range(0, self.maximum): | |
yield list(self.getValueAtIndex(i)) | |
def buildFormatter(expr): | |
fields = [] | |
def repl(match): | |
charset, howmany = match.group(1, 2) | |
if howmany is None: | |
howmany = 1 | |
fields.extend([gen_charset(charset)] * (int(howmany))) | |
return ('%c') * int(howmany) | |
result = RE_PARSE_EXPR.sub(repl, expr) | |
return result, fields | |
def generateCombinations(values): | |
return Generator(values).all | |
def generateResults(format_str, values): | |
for combo in generateCombinations(values): | |
yield (str(format_str) % tuple(combo)) | |
def main(args): | |
for expr_format in args.domain_format: | |
_format, _fields = buildFormatter(expr_format) | |
if not len(_fields): | |
if not test_domain(_format): | |
print "FAIL {0}".format(_format) | |
else: | |
for i in generateResults(_format, _fields): | |
if not test_domain(i): | |
print "FAIL {0}".format(i) | |
def options(args): | |
parser = argparse.ArgumentParser(description = 'domain resolver en-masse') | |
parser.add_argument('domain_format', metavar='FORMAT', type=str, nargs='+') | |
return parser.parse_args(args) | |
if __name__ == '__main__': | |
main(options(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment