0

I'm running the picoCTF PW Crack 5 gym challenge, but it is slow to iterate through the password list they provide for the challenge. Below is the code I'm using in the script to solve the challenge and would appreciate any explanation on how I could have sped up the process.

    correct = False
    with open('dictionary.txt') as pw_list:
      while correct != True:
        for line in pw_list:
          if hash_pw(line) == correct_pw_hash:
            print(line)
            correct = True
          else:
            continue
Ryukashin
  • 13
  • 3
  • multiprocessing ?? not sure how to implement it, read more lines send them to queue and calculate hashes in parallel ... but have no idea how – pippo1980 Oct 16 '22 at 18:31

1 Answers1

1

This is a shorted equivalent way:

with open('dictionary.txt') as pw_list:
    for line in pw_list:
        if hash_pw(line) == correct_pw_hash:
            print(line)
            break

To speed this up you can try multi-processing since the hashing is CPU intensive. You can try different numbers on the processes and of course you need to set the hash_pw function and correct_pw_hash variable.

from multiprocessing import Pool

def check(line):
    if hash_pw(line) == correct_pw_hash:
        print(line)
   

if __name__ == "__main__":
    # Try different values on this to get the optimal performance.
    number_of_processes=8

    p = Pool(processes=8)
    file_name  = 'dictionary.txt'
    lines = []
    with open(file_name,'r') as f:
        for line in f:
            lines.append(line.strip())

    result = p.map(check, lines)
    p.close()
    p.join()

Please note that this way the entire file is loaded into the memory. If the file is huge, things become more complex.

More about multiprocessing: https://docs.python.org/3/library/multiprocessing.html

thanos.a
  • 2,246
  • 3
  • 33
  • 29
  • 1
    Thanks that was almost instant to find the correct password. However, even though it printed the correct string right away, it kept running and didn't stop once it was found. Should there be a break somewhere in there? – Ryukashin Oct 17 '22 at 00:24
  • 1
    For this to happen, we need to create shared multiprocessing events so the child can set the event when the solution is found and also check if the even is set to prevent other children from doing their work. This is not trivial to implement. – thanos.a Oct 17 '22 at 05:31
  • @thanos.a thanks a lot, any link to good SO thread explaining how to read the file in chunks or read it line by line when the multiprocess ends so not to use a lot of memory in case of huge starting file (could be usefull to in case of having to process a lot of high resolution images and then load them only when ready to process) ) – pippo1980 Oct 17 '22 at 14:05
  • @thanos.a will `p = multiprocessing.Pool(multiprocessing.cpu_count()-1)` work to detect oprtimum number of processes to use ? – pippo1980 Oct 17 '22 at 14:13