Python 3.2中的蛮力脚本 [英] Brute force script in Python 3.2

查看:90
本文介绍了Python 3.2中的蛮力脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编写代码的初学者,因此我开始使用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和3,我将不胜感激. 4位数字.

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

解决方案

代码优先:

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'等.值得尝试一下,并观察输出以更好地理解它. /p>

这也意味着您可以输入字母(大写/小写),并在range函数中更改上限.

这肯定不会打破代码破解的世界,但是应该让您了解Python的灵活性以及您可以使用的工具.

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

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.

解决方案

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.

这篇关于Python 3.2中的蛮力脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆