DVWA 通关指南:Weak Session IDs (弱会话)

Weak Session IDs (弱会话)

Knowledge of a session ID is often the only thing required to access a site as a specific user after they have logged in, if that session ID is able to be calculated or easily guessed, then an attacker will have an easy way to gain access to user accounts without having to brute force passwords or find other vulnerabilities such as Cross-Site Scripting.
如果会话 ID 可以计算或很容易猜到的话,通常只需要,这样攻击者就可以利用知道会话 ID 轻松地访问用户帐户,而不必强行使用密码或发现其他漏洞,如跨站点脚本 (XSS)。
This module uses four different ways to set the dvwaSession cookie value, the objective of each level is to work out how the ID is generated and then infer the IDs of other system users.
本模块使用四种不同的方法来设置 dvwaSession cookie 值,每个级别的目标是计算出 ID 是如何生成的,然后推断出其他系统用户的 ID。

Low Level

The cookie value should be very obviously predictable.
cookie 值非常明显并且是可预测的。

源码审计

源码如下,setcookie() 函数向客户端发送一个 HTTP cookie。如果用户 SESSION 中的 last_session_id 不存在就设为 0,生成 cookie 时就在 cookies 上 dvwaSessionId + 1。这种生成方式过分简单了,而且非常容易被伪造。

<?php

$html = "";

if ($_SERVER['REQUEST_METHOD'] == "POST"){
    if (!isset ($_SESSION['last_session_id'])) {
        $_SESSION['last_session_id'] = 0;
    }
    $_SESSION['last_session_id']++;
    $cookie_value = $_SESSION['last_session_id'];
    setcookie("dvwaSession", $cookie_value);
}
?> 

攻击方式

首先在网页生成 cookie,可以见到 cookie 的格式异常简单,“dvwaSession=” 再加上个 id 数字。

接下来再访问网页时,就会使用该分配的 cookie。

Medium Level

The value looks a little more random than on low but if you collect a few you should start to see a pattern.
这个 cookie 看起来比 low 随机一些,但是如果你收集几个 cookie,就应该能看到其中的规律。

源码审计

源码如下,medium 的源码是基于时间戳生成的。

<?php

$html = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $cookie_value = time();
    setcookie("dvwaSession", $cookie_value);
}
?> 

攻击方式

抓包获取一些 cookie 如下,可以看出 cookie 这是基于时间戳生成的。

1600747711
1600747758
1600747770


在伪造 cookie 的时候,就生成一下对应的时间戳即可。

High Level

First work out what format the value is in and then try to work out what is being used as the input to generate the values.
首先计算出值的格式,然后尝试计算出将什么用作生成值的输入。
Extra flags are also being added to the cookie, this does not affect the challenge but highlights extra protections that can be added to protect the cookies.
额外的标志也被添加到 cookie 中,可以添加的额外标识符来保护 cookie。

源码审计

源码如下,high 的源码和 low 是一样的,但是将生成的 cookie 进行了 md5 加密。

 <?php

$html = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    if (!isset ($_SESSION['last_session_id_high'])) {
        $_SESSION['last_session_id_high'] = 0;
    }
    $_SESSION['last_session_id_high']++;
    $cookie_value = md5($_SESSION['last_session_id_high']);
    setcookie("dvwaSession", $cookie_value, time()+3600, "/vulnerabilities/weak_id/", $_SERVER['HTTP_HOST'], false, false);
}

?>

攻击方式

抓包看看,cookie 的值和 md5 加密很像。

将 cookie 进行 md5 解密,这串密文的解密结果就是 1。伪造 cookie 的时候和 low 级别一样,然后进行 md5 加密即可。

Impossible Level

The cookie value should not be predictable at this level but feel free to try.
cookie 值在这个级别上不应该是可预测的,但是可以尝试一下。
As well as the extra flags, the cookie is being tied to the domain and the path of the challenge.
除了额外的标志外,cookie 还被绑定到域和路径。

<?php

$html = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    $cookie_value = sha1(mt_rand() . time() . "Impossible");
    setcookie("dvwaSession", $cookie_value, time()+3600, "/vulnerabilities/weak_id/", $_SERVER['HTTP_HOST'], true, true);
}
?> 

cookie 采用随机数、时间戳和固定字符串"Impossible",再进行 sha1 运算,这样得出的 cookie 就很难猜测了。

总结与防御

在网络应用中,“Session” 称为“会话控制”,Session 对象存储特定用户会话所需的属性及配置信息。用户在应用程序的 Web 页面之间跳转时,存储在 Session 对象中的变量将不会丢失。当用户请求来自应用程序的网页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话,会话状态仅在支持 cookie 的浏览器中保留。
使用 Cookie 的网站服务器为用户产生一个唯一的识别码,利用此识别码网站就能够跟踪该用户在该网站的活动。攻击者可以利用某位用户的 cookie,伪造成该用户在网页上执行一系列操作。所以在设计网页时,cookie 的生成方式要设计得尽量复杂。

参考资料

DVWA学习之Weak Session IDs (弱会话IDs)
DVWA 黑客攻防演练(七)Weak Session IDs
session (计算机术语)

posted @ 2020-09-24 03:18  乌漆WhiteMoon  阅读(1440)  评论(0编辑  收藏  举报