4
\$\begingroup\$

I created a python script that attempts to find weak passwords based on a keyword. I really enjoy programming with Python but I don't have a ton of experience so I'm hoping to get feedback on whether I should change how I have implemented anything below so that I am in line with best practices.

The code below is basically just processing all of the arguments and making calls to specific functions based on what flags/arguments are passed in.

Are there any obvious changes that I should make to the code below? Is this the correct way to handle arguments of this nature?

#get the password and options from the command line
opts, args = getopt.getopt(sys.argv[1:], ':o:t:d:g:necf')
# print opts
# print args
password = args[0].lower()

outputFile = ''
target = '' 
data = ''
text = ''
numbersFlag = False
copyFlag = False
fullFlag = False
requestFlag = False

for opt, arg in opts:
    if opt == "-o": #output file
        outputFile = arg
    if opt == "-f": #generate full password list
        fullFlag = True 
    elif opt == "-t": #target of the POST request
        requestFlag = True
        target = arg
    elif opt == "-d": #data for the POST requet
        requestFlag = True
        data = arg
    elif opt == "-c": #copy output to the clipboard
        copyFlag = True
    elif opt == "-g": #text to be searched for in POST response
        requestFlag = True
        text = arg
    # elif opt == "-e": #append extra character
    #   letters.append(dummyCharacters)
    elif opt == "-n": #append numbers to end
        numbersFlag = True

#load full or basic password list based on arguments passed in
passwords = fullSub(password) if fullFlag else basicSub(password)
if fullFlag:
    passwords = fullSub(password)
elif numbersFlag:
    passwords = appendNumbers(password)
else:
    passwords = basicSub(password)

#save passwords to file
if outputFile != '':
    f = open(outputFile, 'w')
    for password in passwords:
        f.write("".join(password) + '\n')
    f.close()
#copy passwords to clipboard
elif copyFlag:
    pwList = ''
    i=0
    for password in passwords:
        i+=1
        pwList += "".join(password) + '\n'
    print `i` + " passwords copied to the clipboard."
    pyperclip.copy(pwList)
#make request using passwords
elif requestFlag:
    #make sure all required values were passed in
    if data == '':
        print "You must provide data in order to make a HTTP request.  Example: -d [email protected]&password={0}"
        sys.exit()
    elif text == '':
        print "You must specify what text to search for in the response in order to make a HTTP request. Example: -g success:true"
        sys.exit()
    elif target == "":
        print "You must specify a target URL in order to make a HTTP request"
        sys.exit()
    makeRequests(target, data, passwords,text)
else:
    i = 0
    for password in passwords:
        i+=1
        print "".join(password)
    print `i` + " passwords generated."
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Some remarks about your code:

  1. Use if __name__ == '__main__':
  2. In my opinion argparse is better then the getopt way,
  3. Divide your code into functions for seperation of control
  4. Use string formatting
  5. Uphold PEP8 guidelines for a cleaner code

1.

def main():
    parse_options()
    do_stuff()

if __name__ == '__main__':
    main()

2. Example of parsing options via argparse

def parse_options():
    parser = argparse.ArgumentParser(usage='%(prog)s [options] <password>',
                                     description='Password-Tool',
                                     formatter_class=argparse.RawDescriptionHelpFormatter,
                                     epilog=
'''
Examples
'''

                                        )
    parser.add_argument('-o', action="store_true", help='output file')
    ...
    args = parser.parse_args()

3. When dividing code into seperate functions it makes for a more readable code, and that way it can be imported by other programs.


4. When using print formatting looks better and correct way would be:

print '{} some text here'.format(i)


\$\endgroup\$
1
0
\$\begingroup\$

No.

While this works and is not bad, there is a much better way. Python provides the module argparse to facilitate the parsing and handling of arguments. I would recommend learning and using that.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.