暴力破解脚本

问题描述 投票:0回答:5

在 Python 3.2 中,我想暴力破解随机密码,例如:

PassWord = random.randint(0,9999)

我做了一些可以尝试随机密码的东西:

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()

它试图随机猜测密码,但暴力攻击会尝试所有可能性。如何首先以正确的顺序(0、1、2、3 等)检查 1 位数字的所有可能性,然后检查 2、3 和 4 位数字?

python python-3.x brute-force
5个回答
17
投票

先写代码:

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
产生其输入的笛卡尔连接 - 在这种情况下,它被“连接”到自身。因此,在第一次迭代中,每个字符都会被打印出来。然后在下一次迭代中,由于
repeat=length
(并且
length
现在 == 2),生成 '00'、'01' 等...值得尝试并查看输出以更好地理解它。

这也意味着您可以输入字母(大写/小写),并更改

range
函数中的上限。

它当然不会打破代码破译的世界,但应该让您了解 Python 的灵活性和可用的工具。

我将让您检查密码是否匹配并打破循环。


2
投票

你想要这样的东西:

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) 

1
投票

只要运行这个程序,你就会得到你想要的。 但请记住更改我在代码中注释的字段的值。

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()

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"))

-2
投票

这就是我所做的,它非常无效并且写得不好,我目前正在尝试将它放入一个函数中,这样它就可以做任何长度,但它只做4 atm。我使用了一串字母/数字并按顺序运行,直到答案 = 密码:

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

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

如果你重视你的眼睛,请不要看下面(警告你它很乱)。

<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>
© www.soinside.com 2019 - 2024. All rights reserved.