Skip to content

Commit

Permalink
Merge pull request #38 from jeremyn/modify_subdomain_sorting
Browse files Browse the repository at this point in the history
Improved the subdomains sorting
  • Loading branch information
aboul3la authored Oct 4, 2016
2 parents 5e2f5bb + 09a2b9c commit cab5522
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion sublist3r.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import threading
import dns.resolver
import socket
import functools
from subbrute import subbrute
from collections import Counter
from Queue import Queue
Expand Down Expand Up @@ -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<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

class enumratorBase(object):
def __init__(self, base_url, engine_name, domain, subdomains=None):
subdomains = subdomains or []
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit cab5522

Please sign in to comment.