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

# Linux 持久化

持久化是后渗透阶段，在第一次获取 shell 后使访问变得可重复。在真实演练中，仅在明确纳入范围时使用这些技术，记录每一处更改，并在关闭测试前移除所有持久化。

{% hint style="warning" %}
持久化会修改用户账户、登录路径、计划任务、启动文件或 Web 根目录。将每条命令都视为实验环境或授权评估操作，并为你所做的每一处更改保留清理记录。
{% endhint %}

## 快速映射

| 方法         | 最佳用途                     | 主要证据                                                           |
| ---------- | ------------------------ | -------------------------------------------------------------- |
| 用户账户操作     | 受控实验环境持久化或恢复测试。          | `/etc/passwd`, `/etc/shadow`, `/etc/group`，sudoers drop-in 文件。 |
| SSH 授权密钥   | 为已知用户提供可靠的基于密钥的访问。       | `~/.ssh/authorized_keys`，文件权限，SSH 日志。                          |
| cron 任务    | 按计划重复执行无害的验证命令。          | `/etc/crontab`, `/etc/cron.d/*`，用户 crontab。                    |
| 启动脚本       | 在重启或服务启动后执行。             | `/etc/rc.local`，systemd 单元，profile 文件。                         |
| Shell 配置文件 | 在交互式登录时触发。               | `.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 日志中仍然非常显眼。

### 授权密钥

对于普通用户：

```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 端口

添加第二个端口会很显眼，应清楚记录：

```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
```

### Shell 配置文件

用户配置文件：

```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 shell 载荷；应记录路径、运行时、验证请求、日志以及清理步骤。

### 安全文档模式

| 字段      | 示例                             |
| ------- | ------------------------------ |
| 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 dynamically by asking a question.

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

```
GET https://hacking-notes.jord4n.pro/zh/post-exploitation/linux-persistence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
