0

I'm trying to create a password cracker using brute force(I'm testing it on a really simple html login page I created), the program worked but would take too long to get the password right, so I had the idea to run the cracking function once for each starting digit, from zero through nine, using multiprocessing.I want to stop all processes when one of them gets to login, but could only stop the one that figures the password out so far.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import multiprocessing
import sys
import signal


password = ''
currentUrl = ""


def toNumber(num1, num2, num3, num4, num5, num6, num7, num8):
    password = str(num1) + str(num2) + str(num3) + str(num4) + str(num5) + str(num6) + \
        str(num7) + str(num8)
    return (password)


def crack(startWith):
    wb = webdriver.Chrome('./chromedriver')
    wb.get("http://127.0.0.1:5500/index.html")
    inputEmail = wb.find_element_by_id('email')
    inputPassword = wb.find_element_by_id('password')
    submitbtn = wb.find_element_by_class_name('login-btn')
    inputEmail.send_keys("My personal email")
    for first in range(startWith, 10):
        for second in range(0, 10):
            for third in range(0, 10):
                for fourth in range(0, 10):
                    for fifth in range(0, 10):
                        for sixth in range(0, 10):
                            for seventh in range(0, 10):
                                for eighth in range(0, 10):
                                    password = toNumber(
                                        first, second, third, fourth, fifth, sixth, seventh, eighth)
                                    inputPassword.send_keys(password)
                                    submitbtn.click()
                                    inputPassword.send_keys(
                                        Keys.CONTROL + "a")
                                    inputPassword.send_keys(Keys.BACKSPACE)
                                    print(password)
                                    if(wb.current_url != "http://127.0.0.1:5500/index.html"):
                                        sys.exit()


start = time.time()
pool = multiprocessing.Pool()
pool.map(crack, range(0, 10))
end = time.time()
print("Time elapsed: " + end - start)

2 Answers2

0

Simplest way would be to create a flag using Shared Memory, visible from all of the subprocesses. In the innermost loop of crack() you'd check the state of that shared variable before proceeding, and change its value if any of the subprocesses find a result.

See here for documentation on multiprocessing.Value: https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Value

And here, a similar question that should give you ideas for how to proceed: Python multiprocessing and a shared counter

Michael
  • 2,344
  • 6
  • 12
0

The first way that comes to mind is os.setpgrp to create a process group, which can be sent a signal such as interrupt or terminate en masse, but that's Unix specific (won't work on Windows).

With the multiprocessing pool, there's a similar facility in the terminate method, automatically called on cleanup, so there's very little the main code needs to do. However, the map call waits for results from all the workers, and arranges them in order. If you want the first successful result, you may want to use e.g. imap_unordered. You'll want to check if the result was a hit or miss, since unsuccessful workers could finish before the successful one.

def crack(startWith):
    for i in xrange(int(1e7)):
        pass = '{}{:07}'.format(startWith,i)
        result = testpass(pass)
        if result:
            return (pass, result)      # Not sys.exit()!
pool = multiprocessing.Pool()
for result in pool.imap_unordered(crack, range(0,10)):
    if result:
        print(result)
        break
Yann Vernier
  • 15,414
  • 2
  • 28
  • 26