From 09a2b9cdaf020a7dac6701765a846807ef6db814 Mon Sep 17 00:00:00 2001 From: Jeremy Nation Date: Sun, 2 Oct 2016 22:57:52 +0000 Subject: [PATCH] Modify subdomain sorting --- sublist3r.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/sublist3r.py b/sublist3r.py index b8cafe5..dcc2230 100644 --- a/sublist3r.py +++ b/sublist3r.py @@ -17,6 +17,7 @@ import multiprocessing import threading import dns.resolver import socket +import functools from subbrute import subbrute from collections import Counter from Queue import Queue @@ -78,6 +79,45 @@ def write_file(filename, subdomains): for subdomain in subdomains: f.write(subdomain+"\r\n") +def subdomain_cmp(d1, d2): + """cmp function for subdomains d1 and d2. + + This cmp function orders subdomains from the top-level domain at the right + reading left, then moving '^' and 'www' to the top of their group. For + example, the following list is sorted correctly: + + [ + 'example.com', + 'www.example.com', + 'a.example.com', + 'www.a.example.com', + 'b.a.example.com', + 'b.example.com', + 'example.net', + 'www.example.net', + 'a.example.net', + ] + + """ + d1 = d1.split('.')[::-1] + d2 = d2.split('.')[::-1] + + val = 1 if d1>d2 else (-1 if d1 len(d2)) and + (d2[-1] == 'www') and + (d1[:len(d2)-1] == d2[:-1])): + val = 1 + elif d1[:-1] == d2[:-1]: + if d1[-1] == 'www': + val = -1 + elif d2[-1] == 'www': + val = 1 + return val + class enumratorBase(object): def __init__(self, base_url, engine_name, domain, subdomains=None): subdomains = subdomains or [] @@ -1033,7 +1073,10 @@ def main(): subdomains = search_list.union(bruteforce_list) if subdomains: - subdomains = sorted(subdomains) + subdomains = sorted( + subdomains, + key=functools.cmp_to_key(subdomain_cmp), + ) if savefile: write_file(savefile, subdomains)