【问题标题】:Setting a PHP cookie value to be intentionally vulnerable将 PHP cookie 值设置为故意易受攻击
【发布时间】:2017-11-28 10:36:52
【问题描述】:

首先,我是 PHP 和一般编码的新手。

我目前正在创建一个 Web 应用程序,该应用程序故意容易受到攻击,以向学生介绍基于 Web 的漏洞。 Web 应用程序由多个级别组成,每个级别都包含不同的漏洞。

在当前关卡中,当用户成功登录关卡时,我尝试将 cookie 名称设置为“Authenticated”,值为“0”。当他们到达该页面时,他们会收到一个未通过身份验证的 PHP 错误。我希望他们能够拦截页面请求,将值更改为“1”,然后作为更改值的结果,接收包含下一级密码的 PHP 回显。

这是我的主页(level6.php):

 <?php
 session_start();
 if(!isset($_SESSION['user'])){
    header("Location:../level5/login6.php");
 }
 include("authentication.php");
 ?>

<!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" xml:lang="en" lang="en">
<head>
   <meta charset='utf-8'>
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <link rel="stylesheet" href="../css/wargames.css">
   <script src="http://code.jquery.com/jquery-latest.min.js" 
type="text/javascript"></script>
   <script src="js/script.js"></script>
   <title>Generic Web App Title</title>
   <div id="logodiv"><img src="../images/test.png" width="50%"></div>
   <div id='cssmenu'>
<ul>
   <li><a href='#'>Cryptography</a></li>
   <li><a href='#'>Directory Traversal</a></li>
   <li><a href='#'>SQL Injection</a></li>
   <li><a href='#'>Malicious Redirects</a></li>
   <li><a href='#'>Burp Suite</a></li>
   <li><a href='#'>nmap</a></li>
   <li><a href='#'>John the Ripper</a></li>
   <li><a href='#'>Information Gathering</a></li>
   <li><a href='#'>Reporting</a></li>
</ul>
</div>
</head>
<body background="../images/background.jpg">
<br />
<div id='announcements' style="margin: 0 auto;"><h3 align='center'>Welcome 
to Level 6!</h3></b>
<hr>
<span><?php echo $error; ?></span>
</div>
<br />
<div id="pagefoot">
<div id='footer' align='center'>####
<br />
####
</div>
</div>
</body>
</script>
</html>`

这是后台运行的 PHP (authentication.php):

<?php
$error=''; //
$cookie_name="Authenticated";
$cookie_value="0";
setcookie($cookie_name, $cookie_value);

if($cookie_value = "0") {
    $error = "You are not authorized to view this page!";
}
else {
        if($cookie_value = "1") {
            $error = "Success! The password for the next level is...";
        }
}

?>

感谢您的帮助。

编辑:这是我的登录代码。用户将进入登录页面。登录页面是基本的,包含此代码...

<?php
session_start();
$error=''; //
if(isset($_POST['submit'])){
    if(empty($_POST['user']) || empty($_POST['pass'])){
        $error = "Username or Password is Invalid";
    }
    else
    {
        //Define $user and $pass
        $user=$_POST['user'];
        $pass=$_POST['pass'];
        //Establish Connection with server by passing server_name, user_id 
and pass as a parameter
        $sqli = mysqli_connect("localhost", "", "");
        //Select Database
        $db = mysqli_select_db($sqli, "");
        //sql query to fetech information of registered user and finds user 
match.
    $query = mysqli_query($sqli, "SELECT * FROM members WHERE id=6 AND 
password='$pass' AND username='$user'");

        $rows = mysqli_num_rows($query);
        if($rows == 1){
            $_SESSION['user'] = md5($pass);
            header("Location: ../level6/level6.php"); //Redirect to 
protected page
        }
        else
        {
            $error = "Username or Password is Invalid";
        }
        mysqli_close($sqli); //Close Conenction
        }
    }
?>

然后登录代码将对用户进行身份验证并将他们带到上面的页面。

【问题讨论】:

  • if($cookie_value == "0") { 使用这个
  • 设置 cookie 值后,您在 if 条件下检查 cookie 的值,然后我认为它总是显示您无权查看此页面!因为你将 cookie_value 的值设置为 0 ,所以它覆盖它。

标签: php session cookies web-applications session-cookies


【解决方案1】:

我又看了一遍,试试这个:

$cookie_name="Authenticated";

// this checks if the value has been set already
 if (!isset($_GET['cookie_value'])) {
// if no value is set, it defaults to 0 and displays the error message
$cookie_value=0;
$error = "You are not authorized to view this page!";
echo $error;



}

// Now here if the value has been set in the url 
else{
    $cookie_value=$_GET['cookie_value'];

        //when 0 ,error
        if($cookie_value ==0) {
            $error = "You are not authorized to view this page!";
            echo $error;
        }
        // and when its 1 success
        elseif ($cookie_value ==1) {

            $error = "Success! The password for the next level is...";
            echo $error;
            }


}

// when a value is confirmed the cookie is then set
setcookie($cookie_name, $cookie_value);

?>

【讨论】:

  • 完美,除非我最初导航到该页面,即当用户登录并被带到该页面时,他们收到错误 Undefined index: cookie_value in ..\level6\authentication.php在第 4 行。如果我随后在 URL 中分配值,则此工作。有没有办法在页面本身上解决这个问题?
  • 这很好用!但是,我确实必须摆脱 echo 语句。这是一个很好的解决方法,尽管它不像我想要的那样工作。我可以解决这个问题,但是很快,除了在 URL 中更改 cookie 值并将其反映在页面上吗?我在 chrome 中使用代理和 cookie 扩展的尝试没有奏效。
【解决方案2】:

一直这样是因为你已经设置了cookie值=0,不管你设置多少次,页面都会重置为0,需要改成$cookie_value= $_GET变量

【讨论】:

    【解决方案3】:

    authentication.php

    <?php
    $error=''; 
    
    //Here fetch (GET) the data from the database for cookie_value and assign to
    //$cookie_value variable.
    
    $cookie_name = "Authenticated";
    $cookie_value = $_GET["cookie_value"]; //Here Get The Value of cookie from DB
    
    if( $cookie_name == 'Authenticated' && $cookie_value == "0") {
        // Do what you want if cookie_value is `0`. or set the cookie. or redirect to another page if unauthorized user. 
       //setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
        $error = "You are not authorized to view this page!";
    }
    else {
            if( $cookie_name == 'Authenticated' && $cookie_value == "1") {
    
                $error = "Success! The password for the next level is...";
            }
    }
    
    ?>
    

    注意:

    • = 运算符用于仅分配 value
    • = = 运算符用于仅比较 values 而不是 datatype
    • = = = 运算符用于比较 valuesdatatype

    【讨论】:

    • 感谢您的评论。我用过这个,我仍然有同样的问题。如果我刷新页面,GET 包含值“0”,然后我将其更改为“1”。在响应中,我收到另一个值“0”,然后将其更改为 1,但这不会提示成功错误。有什么想法吗?
    • 一件事.. 设置 cookie 值后,您在 if 条件下检查 cookie 的值,然后我认为它总是向您显示 You are not authorized to view this page! bcoz 您将 cookie_value 的值设置为 0
    • 运气不好,cookie 名称和值错误的未定义索引。
    • 你能解释一下吗..你如何获得用户cookie的价值..是10
    • 注册时要保存user_status = '0'默认值...明白!!!
    猜你喜欢
    • 2018-11-17
    • 2014-02-25
    • 2016-04-18
    • 2018-01-19
    • 2019-10-09
    • 1970-01-01
    • 2015-10-03
    • 2019-08-10
    • 2016-12-17
    相关资源
    最近更新 更多