telnet -l 仍然要求登录

telnet -l 仍然要求登录

我正在尝试编写一个 C 库,用于连接具有 telnet 服务器的机器人并与之对话。

服务器信息如下:

  • 用户:as
  • ip:192.168.0.1
  • 港口:23
  • 它没有密码

在 robots_init() 函数中,我想连接到机器人,并且希望使用以下代码来完成此操作:

FILE *telnet = popen("telnet -l as 192.168.0.1 23", "w");

man telnet

SYNOPSIS
     telnet [-468ELadr] [-S tos] [-b address] [-e escapechar] [-l user]
            [-n tracefile] [host [port]]

[...]
     -l user
             Specify user as the user to log in as on the remote system. This
             is accomplished by sending the specified name as the USER envi‐
             ronment variable, so it requires that the remote system support
             the TELNET NEW-ENVIRON option. This option implies the -a option,
             and may also be used with the open command.

然而,在编写 C 代码之前,我尝试在 docker 容器中设置一个 telnet 服务器,并通过另一个 docker 作为客户端与其通信。

运行服务器:

sudo docker container run --name telnet-server --publish 2323:23 --detach --restart unless-stopped secobau/telnetd:alpine-1.1

该服务器有用户 = user; ip = 172.17.0.1;端口 = 2323;并且没有密码。

运行客户端:

sudo docker run --interactive --tty --name=telnet-client debian:testing bash
apt-get update
apt-get install telnet --yes
telnet -l user 172.17.0.1 2323

我希望这会给我一个已经登录的 telnet 连接(或者至少直接询问我密码),但它一直要求登录。

是telnet的bug吗?我应该如何自动化这个?

如果telnet -l不起作用我想我必须通过管道编写如下内容:

fprintf(telnet, "as\n");  // user
fprintf(telnet, "\n"); // password (no password)

答案1

有趣的问题,它显示了使用 Docker 快速设置多个彼此完全隔离的不同服务是多么容易。

我认为 telnetd 仍然要求输入密码,因为它-l /bin/login在服务器端被加注,正如您在secobau/telnetd 的 Dockerfile:alpine-1.1

ARG LOGIN=/bin/login
ARG PORT=23
ARG USER=user

ENV CMD "/usr/sbin/telnetd -p $PORT -b $ADDR -l $LOGIN -F"

您可以创建一个非常相似的 Dockerfile,但替换/bin/login/bin/sh,它就可以工作。例如,新的 Dockerfile 可能是:

FROM alpine

ARG ADDR=0.0.0.0
ARG LOGIN=/bin/login
ARG PORT=23
ARG USER=user

ENV CMD "/usr/sbin/telnetd -p $PORT -b $ADDR -l /bin/sh -F"

EXPOSE $PORT

RUN apk update && apk upgrade && apk add busybox-extras
RUN adduser -D $USER && echo -e "\n\n" | passwd $USER

RUN echo "$CMD" | tee cmd.sh && chmod +x cmd.sh
CMD ./cmd.sh

构建它:

docker build -t my/telnet .

运行:

docker container run --name my-telnet-server --publish 2323:23 --detach --restart unless-stopped  my/telnet

您现在应该能够无需从客户端容器输入用户名和密码即可登录:

root@e55f8f1f3e55:/# telnet -l user 172.17.0.1 2323
Trying 172.17.0.1...
Connected to 172.17.0.1.
Escape character is '^]'.

/ #

顺便说一句,你不必使用 sudo 运行 docker,只需将自己添加到 docker 组即可。

相关内容