Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brute force script in Python 3.2

I'm a beginner in writing code and I've started with Python because it seemed the neatest and the easiest to start with (I currently have Python 3.2). Now I've read some online books and so on about coding in python, I've made some small programs and that's it.

But then I wanted to make a program that could brute-force a random password like:

PassWord = random.randint(0,9999)

I made something that could try random passwords:

import random
PassWord = str(random.randint(0,9999))
Trial = ' '
while Trial != PassWord:
    Trial = str(random.randint(0,9999))
    print(Trial)
    if Trial == PassWord:
        print('The password is: '+PassWord)
        input()

But that's not really a brute-force attack, it's more trying to randomly guess a password. I think a Brute-Force attack is first tries all possibility's with 1 digit then 2, 3 and so on. But I have no clue and knowledge how to do this.

I would really appreciate if someone would say how to create a program that first checks all possibilities with 1 digit and if possible, in the right order (0,1,2,3 and so on), then 2,3 and 4 digits.

Then I could work around on it, and learn more about Python.

like image 685
Yegers Avatar asked Jul 06 '12 18:07

Yegers


People also ask

What is a brute force script?

Brute force is a simple attack method and has a high success rate. Some attackers use applications and scripts as brute force tools. These tools try out numerous password combinations to bypass authentication processes. In other cases, attackers try to access web applications by searching for the right session ID.

What is a brute force in Python?

A brute force algorithm solves a problem through exhaustion: it goes through all possible choices until a solution is found. The time complexity of a brute force algorithm is often proportional to the input size. Brute force algorithms are simple and consistent, but very slow.

Is brute force still used?

Yes, it works. The bruteforce technique used often is based on a list of commonly used passwords.


1 Answers

Code first:

from itertools import product

chars = '0123456789' # chars to look for

for length in range(1, 3): # only do lengths of 1 + 2
    to_attempt = product(chars, repeat=length)
    for attempt in to_attempt:
        print(''.join(attempt))

itertools.product produces a Cartesian join of its input(s) - in this case, it's being 'joined' to itself. So in the first iteration, each single character is printed. Then in the next iteration, because of repeat=length (and length is now == 2), generates '00', '01', etc... It's worth trying it and seeing the output to understand it better.

This also means you can throw in letters (uppercase/lowercase), and change the upperbound in the range function.

It's certainly not going to break the world of code-breaking, but should give you an idea of the flexibility of Python and the tools available to you.

I'll leave you to check the passwords match and break out the loop.

like image 79
Jon Clements Avatar answered Sep 18 '22 12:09

Jon Clements