Skip to content

Commit

Permalink
Merge pull request infosec-au#5 from fransr/master
Browse files Browse the repository at this point in the history
changed from subprocess sort to python set() for uniqueness.
  • Loading branch information
avlidienbrunn authored Jun 22, 2016
2 parents 250d6b3 + fb7619a commit fca0b84
Showing 1 changed file with 43 additions and 28 deletions.
71 changes: 43 additions & 28 deletions altdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import argparse
import threading
import subprocess
import time
import datetime
from threading import Lock
Expand All @@ -14,31 +13,21 @@
from termcolor import colored
import dns.resolver
import re
import os


def get_alteration_words(wordlist_fname):
with open(wordlist_fname, "r") as f:
return f.readlines()


# function that checks if the domain is already in the list, if arg should ignore, dont add it
def check_domain_exists(args, domain):
if args.ignore_existing is False:
return False
with open(args.input, 'r') as file:
if re.search('^{0}'.format(re.escape(domain)), file.read(), flags=re.M):
return True
return False

# will write to the file if the check returns true
def probably_write_domain(args, wp, full_url):
if check_domain_exists(args, full_url) is False:
wp.write(full_url)
def write_domain(args, wp, full_url):
wp.write(full_url)

# function inserts words at every index of the subdomain
def insert_all_indexes(args, alteration_words):
with open(args.input, "r") as fp:
with open(args.output, "a") as wp:
with open(args.output_tmp, "a") as wp:
for line in fp:
ext = tldextract.extract(line.strip())
current_sub = ext.subdomain.split(".")
Expand All @@ -51,20 +40,20 @@ def insert_all_indexes(args, alteration_words):
full_url = "{0}.{1}.{2}\n".format(
actual_sub, ext.domain, ext.suffix)
if actual_sub[-1:] is not ".":
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub.pop(index)
current_sub.append(word.strip())
actual_sub = ".".join(current_sub)
full_url = "{0}.{1}.{2}\n".format(
actual_sub, ext.domain, ext.suffix)
if len(current_sub[0]) > 0:
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub.pop()

# adds word-NUM and wordNUM to each subdomain at each unique position
def insert_number_suffix_subdomains(args, alternation_words):
with open(args.input, "r") as fp:
with open(args.output, "a") as wp:
with open(args.output_tmp, "a") as wp:
for line in fp:
ext = tldextract.extract(line.strip())
current_sub = ext.subdomain.split(".")
Expand All @@ -77,7 +66,7 @@ def insert_number_suffix_subdomains(args, alternation_words):
actual_sub = ".".join(current_sub)
# save full URL as line in file
full_url = "{0}.{1}.{2}\n".format(actual_sub, ext.domain, ext.suffix)
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub[index] = original_sub

#add wordNUM
Expand All @@ -87,13 +76,13 @@ def insert_number_suffix_subdomains(args, alternation_words):
actual_sub = ".".join(current_sub)
# save full URL as line in file
full_url = "{0}.{1}.{2}\n".format(actual_sub, ext.domain, ext.suffix)
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub[index] = original_sub

# adds word- and -word to each subdomain at each unique position
def insert_dash_subdomains(args, alteration_words):
with open(args.input, "r") as fp:
with open(args.output, "a") as wp:
with open(args.output_tmp, "a") as wp:
for line in fp:
ext = tldextract.extract(line.strip())
current_sub = ext.subdomain.split(".")
Expand All @@ -108,7 +97,7 @@ def insert_dash_subdomains(args, alteration_words):
full_url = "{0}.{1}.{2}\n".format(
actual_sub, ext.domain, ext.suffix)
if len(current_sub[0]) > 0 and actual_sub[:1] is not "-":
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub[index] = original_sub
# second dash alteration
current_sub[index] = word.strip() + "-" + \
Expand All @@ -118,13 +107,13 @@ def insert_dash_subdomains(args, alteration_words):
full_url = "{0}.{1}.{2}\n".format(
actual_sub, ext.domain, ext.suffix)
if actual_sub[-1:] is not "-":
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub[index] = original_sub

# adds prefix and suffix word to each subdomain
def join_words_subdomains(args, alteration_words):
with open(args.input, "r") as fp:
with open(args.output, "a") as wp:
with open(args.output_tmp, "a") as wp:
for line in fp:
ext = tldextract.extract(line.strip())
current_sub = ext.subdomain.split(".")
Expand All @@ -137,15 +126,15 @@ def join_words_subdomains(args, alteration_words):
# save full URL as line in file
full_url = "{0}.{1}.{2}\n".format(
actual_sub, ext.domain, ext.suffix)
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub[index] = original_sub
# second dash alteration
current_sub[index] = word.strip() + current_sub[index]
actual_sub = ".".join(current_sub)
# save second full URL as line in file
full_url = "{0}.{1}.{2}\n".format(
actual_sub, ext.domain, ext.suffix)
probably_write_domain(args, wp, full_url)
write_domain(args, wp, full_url)
current_sub[index] = original_sub


Expand Down Expand Up @@ -217,6 +206,22 @@ def get_cname(q, target, resolved_out):
"blue"))
q.put(result)

def remove_duplicates(args):
with open(args.output) as b:
blines = set(b)
with open(args.output, 'w') as result:
for line in blines:
result.write(line)

def remove_existing(args):
with open(args.input) as b:
blines = set(b)
with open(args.output_tmp) as a:
with open(args.output, 'w') as result:
for line in a:
if line not in blines:
result.write(line)
os.remove(args.output_tmp)

def get_line_count(filename):
with open(filename, "r") as lc:
Expand Down Expand Up @@ -270,6 +275,13 @@ def main():

# wipe the output before, so we fresh alternated data
open(args.output, 'w').close()

# if we should remove existing, save the output to a temporary file
if args.ignore_existing is True:
args.output_tmp = args.output + '.tmp'
else:
args.output_tmp = args.output

insert_all_indexes(args, alteration_words)
insert_dash_subdomains(args, alteration_words)
if args.add_number_suffix is True:
Expand All @@ -278,8 +290,11 @@ def main():

threadhandler = []

#Removes duplicate permutations
subprocess.call(['sort', '-u', '-o' + args.output, args.output])
# Removes already existing + dupes from output
if args.ignore_existing is True:
remove_existing(args)
else:
remove_duplicates(args)

if args.resolve:
global progress
Expand Down

0 comments on commit fca0b84

Please sign in to comment.