标题: [wargames] Natas 18 解答
分类: 安全测试
创建: 2022-12-04 17:35
修改:
链接: http://0x2531.tech/testing/202212041735.txt
--------------------------------------------------------------------------------
OverTheWire 设计了一系列 wargames,Natas 是其中之一,主要关注基本的服务端 Web 安全问题。
最近抽空玩了下,还挺有趣。
Natas 项目地址:https://overthewire.org/wargames/natas/
Natas 18 主要考察代码审计方面的能力,具体知识点是 HTTP 会话和暴力破解。
主要的后端代码如下:
==========
";
}
}
function my_session_start() {
if(array_key_exists("PHPSESSID", $_COOKIE) and
isValidID($_COOKIE["PHPSESSID"])) {
if(!session_start()) {
debug("Session start failed");
return false;
} else {
debug("Session start ok");
if(!array_key_exists("admin", $_SESSION)) {
debug("Session was old: admin flag set");
$_SESSION["admin"] = 0; // backwards compatible, secure
}
return true;
}
}
return false;
}
function print_credentials() {
if($_SESSION and array_key_exists("admin", $_SESSION) and
$_SESSION["admin"] == 1) {
print "You are an admin. The credentials for the next level are:
";
print "
Username: natas19\n"; print "Password:"; } else { print "You are logged in as a regular user. Login as an admin to retrieve credentials for natas19."; } } $showform = true; if(my_session_start()) { print_credentials(); $showform = false; } else { if(array_key_exists("username", $_REQUEST) && array_key_exists("password", $_REQUEST)) { session_id(createID($_REQUEST["username"])); session_start(); $_SESSION["admin"] = isValidAdminLogin(); debug("New session started"); $showform = false; print_credentials(); } } if($showform) { ?>
Please login with your admin account to retrieve credentials for natas19.
========== 提交表单后,使用 session_id 函数创建会话 id,审计代码可知,该会话 id 介于 1~640 之间。 $maxid = 640; function createID($user) { global $maxid; return rand(1, $maxid); } 补充说明下,会话也是一种 cookie,只是其值保存在服务端,客户端通过 PHPSESSID 只保存会话 id。 当 cookie 传递到服务端后,后端代码通过会话 id 获取到会话实体。如:当前会话是否属于 admin 用 户。 当会话属于 admin 时,即可获取到下一关的密码。 if($_SESSION and array_key_exists("admin", $_SESSION) and $_SESSION["admin"] == 1) { print "You are an admin. The credentials for the next level are:Username: natas19\n"; print "Password:"; } 如何知道哪个是 admin 会话 id呢?考虑到总共只有 640 个会话,直接暴力破解看页面响应即可。 可以直接使用 Burp Suite 的 Intruder 模块,能方便的生成这些会话 id。 首先,payload type 选择 Numbers,然后设置 payload options,从1到640步长为1生成640个会 话 id 序列。最后,设置命中 admin 会话的内容特征,可以是“You are an admin”,也可以不设置, 待所有 payload 跑完后,对响应结果排序也可以。 最后,获取到 natas19 的密码:8LMJEhKFbMKIL2mxQKjv0aEDdk7zpT0s。