> 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/post-exploitation/linux-persistence.md).

# Linux 永続化

ユーザーアカウント、SSH キー、cron ジョブ、起動スクリプト、シェルプロファイル、Web アクセス検証、レポート作成、クリーンアップを含む、許可されたポストエクスプロイトラボ向けの Linux 永続化メモ。

永続化は、最初のシェル取得後にアクセスを再現可能にするポストエクスプロイテーション段階です。実際の活動では、これらの手法は明示的に対象範囲に含まれている場合にのみ使用し、変更はすべて記録し、テストを終了する前に永続化をすべて削除してください。

{% hint style="warning" %}
永続化では、ユーザーアカウント、ログイン経路、スケジュールされたタスク、起動時ファイル、または Web ルートを変更します。各コマンドはラボまたは許可された評価作業として扱い、行った変更ごとにクリーンアップメモを残してください。
{% endhint %}

## クイックマップ

| Method                 | 最適な用途                              | 主な証拠                                                      |
| ---------------------- | ---------------------------------- | --------------------------------------------------------- |
| ユーザーアカウントの操作           | 管理されたラボでの永続化または復旧テスト。              | `/etc/passwd`, `/etc/shadow`, `/etc/group`、sudoers の追加設定。 |
| SSH の authorized\_keys | 既知のユーザーに対する信頼できる鍵ベースのアクセス。         | `~/.ssh/authorized_keys`、ファイル権限、SSH ログ。                   |
| cronジョブ                | 無害な検証コマンドを定期的に繰り返す。                | `/etc/crontab`, `/etc/cron.d/*`、ユーザーの crontab。            |
| 起動スクリプト                | 再起動後またはサービス開始後に実行。                 | `/etc/rc.local`、systemd ユニット、プロファイルファイル。                  |
| シェルプロファイル              | 対話型ログイン時にトリガーされる。                  | `.bashrc`, `.profile`, `/etc/profile`.                    |
| Web アクセスの検証            | 書き込み可能な Web ルートとサーバーサイド実行を安全に確認する。 | Web ルートのファイル、アクセスログ、Web サーバーユーザー。                         |

## ユーザーアカウントの操作

管理対象のアカウントを作成するのは目立ち、検出されやすく、通常はラボ内またはルール・オブ・エンゲージメントで明示的に許可されている場合にのみ許容されます。

### 新しい特権ユーザーを作成する

```bash
useradd -m -s /bin/bash assessment-user
usermod -aG sudo assessment-user
passwd assessment-user
```

Debian/Ubuntu:

```bash
adduser assessment-user sudo
```

CentOS/RHEL:

```bash
usermod -aG wheel assessment-user
```

### 既存ユーザーの変更

```bash
usermod -s /bin/bash user
usermod -aG sudo user
echo "user ALL=(ALL:ALL) ALL" > /etc/sudoers.d/user
chmod 440 /etc/sudoers.d/user
passwd user
```

### 検証とクリーンアップ

```bash
id assessment-user
groups assessment-user
sudo -l -U assessment-user
```

クリーンアップ:

```bash
deluser assessment-user sudo 2>/dev/null
userdel -r assessment-user
rm -f /etc/sudoers.d/user
```

## SSH 永続化

SSH 鍵による永続化は、パスワード変更よりもきれいですが、ファイル整合性監視や SSH ログでは依然として非常に目立ちます。

### authorized\_keys

通常ユーザーの場合:

```bash
mkdir -p /home/user/.ssh
echo "ssh-rsa AAAAB3NzaC1yc2EA... assessment-key" >> /home/user/.ssh/authorized_keys
chmod 700 /home/user/.ssh
chmod 600 /home/user/.ssh/authorized_keys
chown -R user:user /home/user/.ssh
```

root については、明示的に許可されている場合のみ:

```bash
mkdir -p /root/.ssh
echo "ssh-rsa AAAAB3NzaC1yc2EA... assessment-key" >> /root/.ssh/authorized_keys
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
```

### セカンダリ SSH ポート

2つ目のポートを追加するのは目立つため、明確に文書化する必要があります:

```bash
echo "Port 22" >> /etc/ssh/sshd_config
echo "Port 2222" >> /etc/ssh/sshd_config
systemctl restart sshd
```

検証:

```bash
ss -tuln | grep ':22\|:2222'
ssh -i id_rsa user@target -p 2222
```

クリーンアップ:

```bash
sed -i '/Port 2222/d' /etc/ssh/sshd_config
systemctl restart sshd
```

## cron 永続化

cron 永続化はラボで有用です。実演しやすく、削除もしやすいためです。実環境では cron ジョブはしばしば監視されています。

### システム全体の cron

```bash
echo "* * * * * root /opt/assessment/validate-access.sh" >> /etc/crontab
```

### ユーザーの cron

```bash
(crontab -l 2>/dev/null; echo "* * * * * /opt/assessment/validate-access.sh") | crontab -
```

### `/etc/cron.d`

```bash
echo "* * * * * root /opt/assessment/validate-access.sh" > /etc/cron.d/system-update
chmod 644 /etc/cron.d/system-update
```

クリーンアップ:

```bash
crontab -l | grep -v '/opt/assessment/validate-access.sh' | crontab -
rm -f /etc/cron.d/system-update
sed -i '/validate-access.sh/d' /etc/crontab
```

## 起動スクリプト

起動時の永続化は、起動後またはログイン/セッション初期化中に実行されます。

### `rc.local`

```bash
cat > /etc/rc.local <<'EOF'
#!/bin/bash
/opt/assessment/validate-access.sh &
exit 0
EOF
chmod +x /etc/rc.local
```

### シェルプロファイル

ユーザープロファイル:

```bash
echo "nohup /opt/assessment/validate-access.sh >/dev/null 2>&1 &" >> ~/.bashrc
```

グローバルプロファイル:

```bash
echo "nohup /opt/assessment/validate-access.sh >/dev/null 2>&1 &" >> /etc/profile
```

クリーンアップ:

```bash
sed -i '/validate-access.sh/d' ~/.bashrc
sed -i '/validate-access.sh/d' /etc/profile
rm -f /etc/rc.local
```

## Web アクセスの検証

書き込み可能な Web ルートは、サーバーがアップロードされたコードを実行する場合、再現可能なアクセスを提供できます。実運用の Web シェルペイロードをメモに保存しないでください。代わりに、パス、実行環境、検証リクエスト、ログ、クリーンアップを記録してください。

### 安全な文書化パターン

| 項目          | 例                                    |
| ----------- | ------------------------------------ |
| Web ルート     | `/var/www/html/` または `/srv/www/`     |
| 書き込み可能なパス   | `/var/www/html/images/`              |
| 実行環境        | PHP, Python CGI, ASPX, JSP           |
| テスト用ファイル名   | 明確に識別できる評価用ファイル名                     |
| 検証リクエスト     | 現在の UID を返すなど、サーバーサイド実行を証明する無害なリクエスト |
| クリーンアップコマンド | 正確な削除コマンドと確認するログのパス                  |

### 配置チェックリスト

```bash
ls -la /var/www/ /srv/www/ 2>/dev/null
find /var/www /srv/www -type d -writable 2>/dev/null
find /var/www /srv/www -type f -mtime -1 -ls 2>/dev/null
```

検証では、可能な限り非侵襲的な操作で実行を証明し、その後すぐにファイルを削除してください。

クリーンアップ:

```bash
rm -f /path/to/authorized-test-file
find /var/www /srv/www -type f -mtime -1 -ls 2>/dev/null
grep -R "authorized-test" /var/log/apache2 /var/log/httpd /var/log/nginx 2>/dev/null
```

## 報告メモ

記録する内容:

1. 使用した正確な永続化手法。
2. 変更されたファイルパス。
3. 変更前後のユーザー、グループ、権限。
4. 検証コマンドと結果。
5. クリーンアップコマンドと確認結果。

## 簡易クリーンアップチェックリスト

```bash
grep -R "assessment-key\|validate-access.sh\|authorized-test" /etc /home /root /var/www /srv/www 2>/dev/null
find /etc/cron* -type f -mtime -7 -ls 2>/dev/null
find /var/www /srv/www -type f -mtime -7 -ls 2>/dev/null
last
journalctl -u ssh -n 100 2>/dev/null
```


---

# 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/post-exploitation/linux-persistence.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.
