5

我是编写代码的初学者,我从 Python 开始,因为它似乎是最简洁和最容易开始的(我目前有 Python 3.2)。现在我已经阅读了一些关于 python 编码的在线书籍等,我已经制作了一些小程序,仅此而已。

但是后来我想制作一个可以暴力破解随机密码的程序,例如:

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

但这并不是真正的暴力攻击,它更多的是尝试随机猜测密码。我认为蛮力攻击是首先用 1 位数字尝试所有可能性,然后是 2、3 等等。但我不知道如何做到这一点。

如果有人能说出如何创建一个程序,该程序首先检查所有可能性 1 位数字,如果可能的话,以正确的顺序(0、1、2、3 等),然后是 2、3 和 4 位数字,我将不胜感激。

然后我可以解决它,并了解有关 Python 的更多信息。

4

5 回答 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 的灵活性和可用的工具。

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

于 2012-07-06T18:40:45.763 回答
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) 
于 2012-07-06T18:17:08.130 回答
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()
于 2021-04-29T13:09:28.353 回答
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"))
于 2019-03-10T16:25:38.767 回答
-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>
于 2016-10-07T18:32:04.613 回答