-1

So I'm writing this bruteforce script in python3 and it works fine but it is obiouvsly very slow, both becouse python is not very indicated for this stuff and because I don't know how to implement multithreading. I'm only creating this script because I'm interested in ethical hacking and I want to see how multithreading would make it better. So the interested part of code is this:

def bruto(charset1, maxlength1):
   return(''.join(candidate) for candidate in  chain.from_iterable(product(charset, repeat=i) for i in range(1, maxlength + 1)))

for guess1 in bruto(charset,maxlength):
    guess_hash1 = getattr(hashlib, algorithm)(bytes(guess1, 'utf-8')).hexdigest()

How can I implement multithreading? Thank in advance and sorry for the noobiness

game0ver
  • 1,250
  • 9
  • 22
Desertcod98
  • 31
  • 1
  • 3

1 Answers1

1

Using threading with cpu-bound tasks will make your application slower due to global interpreter lock. You should use multiprocessing instead.

The simplest way would be to use ProcessPoolExecutor to calcuate hash values

from concurrent.futures.process import ProcessPoolExecutor

def get_hash(guess):
    return guess, getattr(hashlib, algorithm)(bytes(guess, "utf-8")).hexdigest()

with ProcessPoolExecutor(max_workers=4) as executor:
    guesses = bruto(charset, maxlength)
    all_hashes = executor.map(get_hash, guesses)

Keep in mind that this might also slow your application :) because of the IPC overhead. If single get_hash is too quick you could set some bigger chunksize in executor.map(chunksize=100)

Python parallelism is full of suprises :)

RafalS
  • 5,834
  • 1
  • 20
  • 25