Skip to content

Commit

Permalink
Simplify subdomain sorting
Browse files Browse the repository at this point in the history
Replace sorting algorithm with a simplified version. Sort order is
not affected by this change.
  • Loading branch information
test committed Nov 27, 2016
1 parent 7850789 commit 80a6458
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions sublist3r.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import multiprocessing
import threading
import socket
import functools
from collections import Counter

# external modules
Expand Down Expand Up @@ -95,10 +94,10 @@ def write_file(filename, subdomains):
f.write(subdomain + "\r\n")


def subdomain_cmp(d1, d2):
"""cmp function for subdomains d1 and d2.
def subdomain_sorting_key(hostname):
"""Sorting key for subdomains
This cmp function orders subdomains from the top-level domain at the right
This sorting key 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:
Expand All @@ -115,24 +114,10 @@ def subdomain_cmp(d1, d2):
]
"""
d1 = d1.split('.')[::-1]
d2 = d2.split('.')[::-1]

val = 1 if d1 > d2 else (-1 if d1 < d2 else 0)
if ((len(d1) < len(d2)) and
(d1[-1] == 'www') and
(d1[: - 1] == d2[:len(d1) - 1])):
val = -1
elif ((len(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
parts = hostname.split('.')[::-1]
if parts[-1] == 'www':
return parts[:-1], 1
return parts, 0


class enumratorBase(object):
Expand Down Expand Up @@ -984,10 +969,8 @@ def main(domain, threads, savefile, ports, silent, verbose, enable_bruteforce, e
subdomains = search_list.union(bruteforce_list)

if subdomains:
subdomains = sorted(
subdomains,
key=functools.cmp_to_key(subdomain_cmp),
)
subdomains = sorted(subdomains, key=subdomain_sorting_key)

if savefile:
write_file(savefile, subdomains)

Expand Down

0 comments on commit 80a6458

Please sign in to comment.