SQL Injection
SQL注入简介
A SQL injection attack consists of insertion or "injection" of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system. SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands.
SQL injection errors occur when:
Data enters a program from an
untrusted source.
The data used to dynamically
construct a SQL query
SQL注入种类
1.没有正确过滤转义字符
比如statement := "SELECT * FROM users WHERE name = '" + userName + "';"
如果输入username为a' or 't'='t,则原始语句发生了变化:SELECT * FROM users WHERE name = 'a' OR 't'='t';
如果这种代码被用于一个认证过程,那么这个例子就能够强迫选择一个合法的用户名,因为赋值't'='t永远是正确的。
2.Incorrect type handling
如果一个用户提供的字段并非一个强类型,或者没有实施类型强制,就会发生这种形式的攻击。当在一个SQL语句中使用一个数字字段时,如果程序员没有检查用户输入的合法性(是否为数字型)就会发生这种攻击。例如:statement := "SELECT * FROM data WHERE id = " + a_variable + ";"
如果终端用户选择一个字符串,就绕过了对转义字符的需要。例如,将a_variable设置为:1;DROP TABLE users,它会将“users”表从数据库中删除,SQL语句变成:SELECT * FROM DATA WHERE id = 1;DROP TABLE users;
3.数据库服务器中的漏洞
有时,数据库服务器软件中也存在着漏洞,如MYSQL服务器中mysql_real_escape_string()函数漏洞。这种漏洞允许一个攻击者根据错误的统一字符编码执行一次成功的SQL注入式攻击。
4.盲目SQL注入式攻击
手动SQl注入一般步骤
1. 判断是否有注入点
a.Eg:- http://www.website.com/index.php?id=49'
b.在49后面加上【'】
c.如果报错就说明有SQL注入漏洞
2. 找出Column的数目
后面加上order by number, 如果报错则说明超过数目。
说明Column数为9
3. 找出most vulnerable column
http://www.website.com/news.php?id==-49 union all select 1,2,3,4,5,6,7,8,9--
记住,必须加符号-在数字前,是(id=-32) instead of (id=32) 【如果id=32,就会显示articleid为32的文章,由于我们提交的articleid=1是article表里存在的,执行结果就是真了,自然返回前面SELECT的结果,当提交空值或者提交一个不存在的值,就会得到我们想要的东西】
most vulnerable column会显示在页面上。
4. 利用vulnerable column进行exploit
如:http://www.website.com/news.php?id==-49 union all select 1,2,3,4,5,6,7,8,@@version--
http://www.securityhunk.com/2011/01/sql-injection-basic-and-detail-tutorial.html