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 (計算機術語)