Brute force script in Python 3.2
Asked Answered
C

5

5

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.

Cassock answered 6/7, 2012 at 18:13 Comment(2)
Correct a brute force checks each value in order. Your code above has no guarantee of ever finding the PasswordLupien
check out: putsnip.com/s/oBecky
G
17

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.

Godless answered 6/7, 2012 at 18:40 Comment(0)
L
2

You want something like this:

PassWord = str(random.randint(0,9999))#example password 
for i in range(10000):    #0-9999
   Trial = str(i)  
   if Trial == Password:  
       print('Found password: ' + Password) 
Lupien answered 6/7, 2012 at 18:17 Comment(0)
H
1

just run this programme and you will get what you want. but remember to change the values of fields where I've commented in code.

import random
import time
import os
from tqdm import tqdm
import random
import getpass

def loading():
    for index in tqdm(range(100), desc = "loading..."):
        time.sleep(0.1)

    pss = random.randint(1, 9)
    print("found a digit-", pss)
    print("hunt will continue in 2 seconds...")

def pas():
    pas = getpass.getpass("enter password: ")
    if pas == ("root"):  #you can change it according to your need
        menu()
    else:
        end()

def login():
    usid = input("enter username: ")
    if usid == ("root"): #change this too according to your need
        pas()
    else:
        end()

def end():
    os.system('cls')
    print("okk!! bye...")
    exit()


def menu():
    os.system('cls')
    input("enter ip address:")
    os.system('cls')
    print("starting the hunt...")
    time.sleep(2)
    os.system('cls')

    loading()
    time.sleep(2)
    loading()
    time.sleep(2)
    loading()
    time.sleep(2)
    loading()
    time.sleep(2)
    loading()
    time.sleep(2)
    loading()
    time.sleep(2)
    loading()
    time.sleep(2)
    loading()
    time.sleep(2)
    loading()
    print("completed! combine the digits and that's your password...")
    end()

login()
Hindbrain answered 29/4, 2021 at 13:9 Comment(1)
anonymous is looking for youAntonio
F
0
import random

digits=list(range(0,1000))
password=random.randint(0,1000)
eachdigit=-1
print(password)
while eachdigit!=password:
    for eachdigit in digits:
        print(eachdigit)
        if eachdigit==password:
            print("Password is found:"+str(eachdigit)+"---------------------------")
            password=str(input("Enter new password if you wish"))
Fungible answered 10/3, 2019 at 16:25 Comment(1)
Please provide an explanation. We don't like answers that are purely codeUnderlinen
R
-2

This is what I have done, it is extremely ineffective and badly written, I am currently trying to put it into a function so it will do any length but it only does 4 atm. I used a string of letters/numbers and ran through that in sequence until answer = password:

idea = ["a","b","c","d","e","1","2","3",.........]
var = 0
answer = ""

while answer != password:
     answer = idea[var]
     print(answer)
     var += 1

If you value your eyes don't look below (warning you it is messy).

<pre><code>
password = input("pass:")
#idea = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9"," "]
#THE IDEA ABOVE IS THE FULL A-Z, 0-9 (takes ages)
password = input("pass:")
idea = ["a","b","c","d"]
awnser = [""] *6
var = 0
var1 = 0
var2 = 0
var3 = 0
char = 0

while awnser != password:
    awnser = idea[var]
    print(awnser)
    if var != len(idea):
        var += 1
    if var == len(idea):
        var = 0
        while awnser != password:
            awnser = idea[var]+idea[var1]
            print(awnser)
            if var != len(idea):
                var += 1
            if var == len(idea):
                if var1 != len(idea):
                    var = 0
                    var1 += 1
                if var1 == len(idea):
                    var = 0
                    var1 = 0
                    while awnser != password:
                        awnser = idea[var]+idea[var1]+idea[var2]
                        print(awnser)
                        if var != len(idea):
                            var += 1
                        if var == len(idea):
                            if var1 != len(idea):
                                var = 0
                                var1 += 1
                            if var1 == len(idea):
                                if var2 != len(idea):
                                    var = 0
                                    var1 = 0
                                    var2 += 1
                                if var2 == len(idea):
                                    var = 0
                                    var1 = 0
                                    var2 = 0
                                    while awnser != password:
                                        awnser = idea[var]+idea[var1]+idea[var2]+idea[var3]
                                        print(awnser)
                                        if var != len(idea):
                                            var += 1
                                        if var == len(idea):
                                            if var1 != len(idea):
                                                var = 0
                                                var1 += 1
                                            if var1 == len(idea):
                                                if var2 != len(idea):
                                                    var = 0
                                                    var1 = 0
                                                    var2 += 1
                                                if var2 == len(idea):
                                                    print("==============================================")
                                                    print("Password too long or characters not in string!")
                                                    print("==============================================")
                                                    break

print("==================")
print("")
input("Password = "+awnser)



</code></pre>
Ruffian answered 7/10, 2016 at 18:32 Comment(2)
The repeated code and the highly nested nature of these loops indicate there are probably some improvements that could be made here! If you like, why not submit it to the Code Review site?Preach
Thanks, I am currently attempting to work out how to put it into for example a defined function using the def x(y,z): command. I will probably end up submitting it but I want to give myself another day or so. As for the pasting I will try...Ruffian

© 2022 - 2024 — McMap. All rights reserved.