What happens in python when you use the thread function in this code
Question: My question is how does python create the thread for the testing of multiple
passwords? And why is it more efficient?

    
    
    import zipfile
    from threading import Thread
    
    def extractFile(zFile, password):
        try:
            zFile.extractall(pwd=password)
            print '[+] Found password ' + password + '\n'
        except:
            pass
    
    def main():
        zFile = zipfile.ZipFile('evil.zip')
        passFile = open('dictionary.txt')
        for line in passFile.readlines():
            password = line.strip('\n')
            t = Thread(target=extractFile, args=(zFile, password))
            t.start()
    
    if __name__ == '__main__':
        main()`
    


Answer: The use of multi-threading makes this code less efficient.

The work that is being down here is CPU oriented. Python threads do not work
well at distributing CPU oriented work because only one thread can do work at
a time (because of the Python GIL or Global Intepreter Lock)!

Python threads are more useful for blocking operations such as network calls,
disk reads etc.

On top of this, making a lot of threads adds a great deal of overhead, because
there is a thread scheduler constantly switching which thread is running so
that no thread starves. In your example, there is a thread being _created for
each file in the line_ , which is pretty bad if there is more than 30-50
lines.

To answer your question:

> how does python create the thread for the testing of multiple passwords?

It simply runs the function `extractFile` in a new thread for each line in
your file.

> and why is it more efficient?

Due to limitations of the Python GIL, and overhead of having more than 20
threads, this is definitely **not** more efficient than running through the
entire file without multi-threading.

