1

Its an infinite loop problem, fellow in the comments set me straight, feel free to take this down if you wish.

I have read some other questions and answers here and tried implementing the suggestions, but to no avail. Because I am not writing proprietary code it is ok for me to post this in its entirety, it not that long, but I hope stack exchange doesn't mind... Also, feel free to use and or modify as you wish.

    #!/usr/bin/env python2.7

    import sys, random, subprocess, signal


    def main(argv=None):
        if argv is None:
            argv = sys.argv
        def signal_handler(signal, frame):
            fdHT.close()
            fdWL.close()
            print '\n'
            return 1

        signal.signal(signal.SIGINT, signal_handler)

        pathToWL = str(sys.argv[1])
        pathForHT = str(sys.argv[2])
        mId = str(sys.argv[3])

        SaltCharSet = str("a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9")
        SaltCharSet = SaltCharSet.split(" ")

        try:
            fdWL = open(pathToWL, 'r')
        except:
            print "Could not open wordlist file."
            return 2

        try:
            fdHT = open(pathForHT, 'a')
        except:
            print "Could not open file for hash table"
            fdWL.close()
            return 3

        #We have our wordlist now step through the thing and start generating hashes.

        toStop = False
        #cursor = 0 #Use the cursor later once this program evolves

        #print 'Entering while 1'
        while(toStop == False):
            try:
                ln = str(fdWL.readline())
            except:
                fdHT.close()
                fdWL.close()
                return 4
            if ln == '':
                toStop = True #This should have been an ASSIGNMENT not a test, was ==
            ln = ln.strip("\n")
            ln = ln.strip("\r")
            if len(ln) < 6:
                continue
            # create random salts
            # send ln, id, and salts to hashpipe
            salt = []
            lenOfSalt = random.randint(6,16)
            #print 'Entering while 2'
            while(len(salt) < lenOfSalt + 1):
                aORn = random.randint(0,1)
                if aORn == 0:# Its a letter
                    uORl = random.randint(0,1)
                    if uORl == 0:
                        salt.append(SaltCharSet[(random.randint(0,25))].upper())
                    elif uORl == 1:
                        salt.append(SaltCharSet[(random.randint(0,25))].lower())
                    else:
                        print "Random Int 'uORl' out of bounds"
                        fdHT.close()
                        fdWL.close()
                        toStop = True
                        return 5 # I don't know what happened
                elif aORn == 1:# Its a number
                    salt.append(SaltCharSet[(random.randint(26, 35))])
                else:
                    print "Random Int 'aORn' out of bounds"
                    fdHT.close()
                    fdWL.close()
                    toStop = True
                    return 6 # I don't know what happened
            #Generated Salt
            salt = "".join(salt)
            wholeArg2 = str("$"+mId+"$"+salt)
            try:
                mHash = str(subprocess.check_output([r"hashpipe", ln, wholeArg2]))
            except:
                print " error getting hash"
                #Clean-up
                fdHT.close()
                fdWL.close()
                toStop = True
                return 7
            #Generated hash, now write it to the fdHT file
            print str(ln+"\t"+mHash)
            fdHT.write(str(ln+"\t"+mHash))
            #cursor = fdWL.tell()

        print 'done with whiles'
        fdHT.close()
        fdWL.close()

        return 0

    if __name__ == "__main__":
        sys.exit(main())

This script is calls a little C program I wrote to hash a string using GNUs crypt_r() function... It hangs at the end and I have to use ctrl-c to bail... here is some sample output...

    zurumba`tico    $6$96u6sUy05rM69$1NLxLYXS9tAf05szgV0/GH6pvykOPsuEIlGxOkDOMNEixGiN8oeTG.xxIq/D19YpArMWtD1xJMG9sKWgA9xzK/

    zurupeto    $6$O2510Y900o02008$BO2OadT8Bvje78C2JhuZ6r/.iJHz.s9UfET8MU93iGy57bbe/qh9/Uj4jSkVSCyknegnkAB2JF7vRgWohkGVI0

    zutana  $6$Ur2i9m95$E2WqrEnld4aPa1bYAlCNnMEE0nmwxNlfB9ozVc3I6NCKXHqnSyspZrqIq2usqNf2JwlVF1myhqIn26a71Dm510

    zutano  $6$8x482Lar4qj$LupCZ9t2ImG.nRVH9xHPsyyx9emiImNTRaErxNrtsGyWjeO3XZLzj.F1D3eQOsiurQeQMWeQ3lF5ef.o3iesg.

    zuzo`n  $6$G9JP2GE$FAoZGGQNNycPfGCHq/Ra4MSQNknATMgHLzk8N9FHDefbZm/Hcx6JdV/sZdbkFHVVkoRjTnoUP9mw6CkE/.4fI.

    ^C

Where have I gone wrong?

  • out of curiousty, why not use python's [crypt](https://docs.python.org/2/library/crypt.html) or [hashlib](https://docs.python.org/2/library/hashlib.html) modules? – aruisdante Apr 27 '14 at 20:17
  • 1
    But, in the general case, you have two potentially infinite ``while`` loops. Are you sure their termination conditions are being hit? I'd also suggest breaking this down into a few subfunctions instead of a giant monolithic one, it'll make it a lot easier to debug. – aruisdante Apr 27 '14 at 20:18
  • The program stops, but does not return, so when I come back to my pc I have to ctrl-c, its not that big of a deal but its not right. I am pretty sure it stops while it gets to the EOF or the '' string. And the length of the salt will always eventually be met, from 6 to 16 it just keeps adding psuedo random alpha-numeric chars to a list, which will eventually always get to at most 16. I think this has more to do with killing the main() function thread, but not necessarily the program's execution. Some quirk with sys.exit() I think, perhaps I need to catch an exception thrown by sys.exit() – Overloaded_Operator Apr 27 '14 at 20:41
  • Actually @aruisdante is correct, I put a print 'at EOF' and the `if ln == '' ` statement and it was continuously printing "at EOF". Now I need to learn how to properly read an EOF, thanks mate. – Overloaded_Operator Apr 27 '14 at 20:46
  • 2
    If the point is to read lines from a file, just use ``with open(...) as f:`` followed by ``for line in f:`` (examples [here](https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects)). It will automatically stop at the end of the file. Assuming the file isn't being written to as you're reading it. It will also correctly ``close`` the file when you're done reading it. – aruisdante Apr 27 '14 at 20:56

2 Answers2

1

I think there are two changes you can make that will improve your chances of writing code that works correctly.

First, use with statements to handle opening and closing your files. Using with open("filename") as file_obj will automatically close the file if an exception or return statement happens later on in the code.

Second, use a for loop to read the lines of the file, rather than calling readline on each cycle of a while loop. The for loop will run until the end of the file is found, and then stop. If you need to react differently to the file ending than to some other termination conditions, you can use break for the special termination cases and put an else clause on the loop (which will only run if the for loop complets without breaking).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • Actually the bug was using test "==" after the `if ln = ''` it should have been `toStop = True` and not `toStop == True` my bad. I am reading in huge wordlists and would rather not read the whole file at once. I know I can use a read generator, I just learned about that here http://stackoverflow.com/questions/4426581/python-eof-for-multi-byte-requests-of-file-read However, I think `readline()` does just that, iterates over the file one line at a time, so why duplicate that functionality especially when I don't know how long a line is to set n in `read(n)` – Overloaded_Operator Apr 27 '14 at 21:39
  • I will have to give your suggestion a try, may reduce the amount of code I need to write, if I do file reading with python in the future (more than likely). – Overloaded_Operator Apr 27 '14 at 21:53
  • @Overloaded_Operator: Ah, good catch on the root of the issue in the current code. I was mostly giving a "style" answer since there the top of the question said that the infinite loop had already been identified. Anyway, using a loop over the file object directly does essentially the same thing as your current loop with `readline`, it just takes care of handling EOF for you. No need to `read` the whole file at once! – Blckknght Apr 27 '14 at 23:32
1

There may be a better answer, and I more than likely am not very "pythonic", I am not one to say, but this fixed the issue, but also probably will raise another, I am forseeing an empty line containing nothing but "\r\n" or "\n\r".

Update: I moved the if statement back before the strips and it still worked on this file, but may not work generally. Time will tell, I just need this to work though, and it does.

Original

    if ln == '':
            toStop == True #This should have been an ASSIGNMENT not a test, was ==
        ln = ln.strip("\n")
        ln = ln.strip("\r")

Working if ln == '': print 'at EOF' toStop = True ln = ln.strip("\n") ln = ln.strip("\r")

The output

    Entering while 2
    zurupeto    $6$w2206117WV1E1$JHkrmXu/RZirctvucEUT8eCNLOTpivn4LwoNRtmq6OfocHhf54bkoggjxVN/35k3j9WgJ5NaXWZY0nDML4qUf1

    Entering while 2
    zutana  $6$wfIF8ry4934271Si$4U8oiMp9/RA1glWf8aVV/uWqMyYybR1VbpR4qbcgNyv3YCERkdzaSpcJMLXtD9uT9Wt0nb4D5oqiJjfMM.8w01

    Entering while 2
    zutano  $6$3gi4C0V2hD49dSOU$y09sCx5aUnheWGKA0HnnRBnINojfZY6WY.CaKeMAej9Y9KdXfBE/aQVqcxxvFbBRuL.Q5Tzr2d1V9oznZPaFs/

    Entering while 2
    zuzo`n  $6$mkZ810VB4Qft90ap$PbtYHX9e5PvnlzgtgODOYb/U3EmgA1AsCrf7tLRnE/DQ8hQ1Ltx3yuul5HEz9JAXnMzflcoN6Vwdfl38oIIOS0

    at EOF
    done with whiles