#!/usr/bin/python

# wlsort.py v0.4 last modified 2009-11-02
#
# Without any arguments, this script will remove junk (eg. empty lines), remove
# duplicates, and sort a word list. However, if needed you can also generate a
# word list with various combinations of upper/lower cases, append digits, and
# specify a minimum/maximum word length.
#
# GNU General Public License version 3 only
#
# http://krhis.net

import sys

arguments = sys.argv
del arguments[0]

ckcap = False
ckupper = False
cklower = False
ckorig = True
upperrange = 0
minlength = 0
maxlength = 0
filename = False

for argument in arguments:
    if argument == "-h" or argument == "--help" or len(arguments) < 1:
        print "Description"
        print "Without any arguments, this script will remove junk (eg. empty lines), remove"
        print "duplicates, and sort a word list."
        print ""
        print "Switches"
        print "-c     Capitalise first letter"
        print "-u     Output word in upper case"
        print "-l     Output word in lower case"
        print "-o     Do not output original case, usable only when -c, -u, or -l are used"
        print "-a100  Append digits 0-100 to the end of the word, this will also pad zeros"
        print "-x8    Remove words less then 8 characters in length, helpful if you are"
        print "       attacking a WPA key (it's your access point, right?)"
        print "-y64   Remove words more then 64 characters in length, helpful if you are"
        print "       attacking a site with POST's and you know that the input tag states"
        print "       maxlength=64"
        print "-h     Help"
        print ""
        print "Example"
        print "$ python wlsort.py -c -a9 words.lst"
        if len(arguments) < 1:
            sys.exit(1)
        else:
            sys.exit(0)
    elif argument == "-c":
        ckcap = True
    elif argument == "-u":
        ckupper = True
    elif argument == "-l":
        cklower = True
    elif argument == "-o":
        ckorig = False
    elif argument[0:2] == "-a":
        upperrange = int(argument[2:])
    elif argument[0:2] == "-x":
        minlength = int(argument[2:])
    elif argument[0:2] == "-y":
        maxlength = int(argument[2:])
    elif argument[0] == "-":
        print "[!] Unknown argument",argument
        sys.exit(1)
    else:
        filename = argument

print " [+] Loading lines..."

if filename:
    try:
        words = open(filename,"r").readlines()
    except(IOError):
        print " [!] Incorrect read path"
        sys.exit(1)
else:
    print " [!] Incorrect read path"
    sys.exit(1)

strippedwords = []
for word in words:
    strippedwords.append(word.strip())
words = strippedwords
strippedwords = [] # Free memory

print " [+] Removing junk lines..."
nonjunkwords = []
for word in words:
    if word == "":
        pass
    elif word == "\n":
        pass
    elif word == " ":
        pass
    else:
        nonjunkwords.append(word)
words = nonjunkwords
nonjunkwords = [] # Free memory

if ckcap or ckupper or cklower:
    print " [+] Creating case sensitive words..."
    casewords = []
    for word in words:
        if ckorig:
            casewords.append(word)
        if ckcap:
            casewords.append(word.capitalize())
        if ckupper:
            casewords.append(word.upper())
        if cklower:
            casewords.append(word.lower())
    words = casewords
    casewords = [] # Free memory

if upperrange > 0:
    print " [+] Appending digits..."
    digitwords = []
    for word in words:
        i = 0
        digitwords.append(word)
        while i <= upperrange:
            digitwords.append(word+str(i))
            a = 0
            while len(str(i))+a < len(str(upperrange)):
                a += 1
                digitwords.append(word+("0"*a)+str(i))
            i += 1

    words = digitwords
    digitwords = [] # Free memory

if minlength != 0 and maxlength != 0 and minlength >= maxlength:
    print " [!] Maximum word length can not be less than or equal to minimum word length"
    sys.exit(1)

if minlength > 0:
    minwords = []
    print " [+] Removing words less then "+str(minlength)+" in length..."
    for word in words:
        if len(word) >= minlength:
            minwords.append(word)
    words = minwords
    minwords = [] # Free memory

if maxlength > 0:
    maxwords = []
    print " [+] Removing words greater then "+str(maxlength)+" in length..."
    for word in words:
        if len(word) <= maxlength:
            maxwords.append(word)
    words = maxwords
    maxwords = [] # Free memory

print " [+] Removing duplicates and sorting..."
words = sorted(set(words))

print " [+] Saving to "+filename+".out..."
try:
    output = open(filename+".out","w")
    for word in words:
        output.write(word+"\n")
    output.close()
except(IOError):
    print " [!] Unable to open "+filename+".out file for writing"
    sys.exit(1)
