首页 > 系统教程 > 操作系统 > SFTP端口转发:启用抑制功能

SFTP端口转发:启用抑制功能

Joseph Gordon-Levitt
发布: 2025-03-17 09:43:07
原创
201 人浏览过

SFTP Port Forwarding: Enabling Suppressed Functionality

引言

SSH协议支持三大类远程服务器活动:a) 命令执行(包括登录shell),b) 网络转发和操作,以及c) 文件传输。

OpenSSH维护者已确定sftp和scp对端口转发(通过-L和-R选项)没有合法用途。在使用这些实用程序进行文件传输期间,一个明确禁用这些功能的标志会被无条件地传递给子SSH可执行文件。

某些用户可能确实需要这些功能。一个明显的子集是渗透测试人员,他们的任务是验证此功能是否在公共SFTP服务器上被明确禁用。

以下是两种启用这些被抑制功能的技术,方法是修改sftp二进制文件本身的字符串,或通过能够轻松编辑命令行的shell进行重定向。根据平台的功能,可能需要任一技术才能实现此目标。

抑制细节

首先,重要的是找到感兴趣的运行进程。下面的shell函数将显示与shell模式匹配的PID(请注意,这不是正则表达式)。这在Debian dash(和大多数其他常用shell)下运行,并依赖于BSD的ps选项:

<code>pps () { local a= b= c= IFS=$'\r'; ps ax | while read -r a
    do [ "$b" ] || c=1; for b; do case "$a" in *"$b"*) c=1;;
        esac; done; [ "$c" ] && printf '%s\n' "$a" && c=; done; }</code>
登录后复制

启动一个传统的SFTP会话,以检查与其相关的进程:

<code>$ id
uid=1001(aturing) gid=1001(aturing) groups=1001(aturing)...

$ sftp aturing@sftp.victimandum.com
aturing@sftp.victimandum.com's password:
Connected to sftp.victimandum.com.
sftp></code>
登录后复制

我们假设上面的本地UNIX用户在远程SFTP服务器上拥有相同用户名帐户。

会话运行后,对用户名进行本地进程搜索将显示由SFTP生成的子SSH进程:

<code>$ pps aturing
    PID TTY      STAT   TIME COMMAND
   9666 pts/0    S      0:00 sftp aturing@sftp.victimandum.com
   9667 pts/0    S      0:00 /usr/bin/ssh -oForwardX11 no -oPermitLocalCommand no -oClearAllForwardings yes -oForwardAgent no -l aturing -s -- sftp.victimandum.com sftp</code>
登录后复制

上面的ClearAllForwardings yes参数将抑制任何转发尝试,而无需采取任何措施来破坏它。

-L和-R端口转发标志并非作为SFTP命令行的有效选项存在,但我们可以使用-S选项明确触发它们以指定自定义SSH处理程序,在本例中为邮件服务器:

<code>$ cat portssh
#!/bin/sh

exec ssh -L2525:smtp.victimandum.com:25 "$@"</code>
登录后复制

如果转发抑制没有到位,则此SFTP调用足以建立转发连接:

<code>$ sftp -S ./portssh -oClearAllForwardings\ no aturing@sftp.victimandum.com
aturing@sftp.victimandum.com's password:
Connected to sftp.victimandum.com.
sftp></code>
登录后复制

现在可以在子SSH进程中看到转发尝试:

<code>$ pps aturing
    PID TTY      STAT   TIME COMMAND
   9897 pts/0    S      0:00 sftp -S ./portssh -oClearAllForwardings no aturing@sftp.victimandum.com
   9898 pts/0    S      0:00 ssh -L2525:smtp.victimandum.com:25 -oForwardX11 no -oPermitLocalCommand no -oClearAllForwardings yes -o ClearAllForwardings no -oForwardAgent no -l aturing -s -- sftp.victimandum.com sftp</code>
登录后复制

但是,由于显式覆盖,尝试通过本地转发端口联系远程邮件服务器是不成功的:

<code>$ nc localhost 2525
$</code>
登录后复制

此无条件抑制在源代码中可见:

<code>$ sed -n /X11/,/Forwardings/p openssh-8.7p1/sftp.c
    addargs(&args, "-oForwardX11 no");
    addargs(&args, "-oPermitLocalCommand no");
    addargs(&args, "-oClearAllForwardings yes");</code>
登录后复制

这些静态字符串在编译后的二进制文件中也可见:

<code>$ strings /usr/bin/sftp | grep [-]o[CFP]
-oForwardX11 no
-oPermitLocalCommand no
-oClearAllForwardings yes
-oForwardAgent no
-oPort %d</code>
登录后复制

最后,文档清楚地说明了这种抑制是有意的,并给出了合理的理由:

<code>$ man ssh_config | sed -n /ClearAllForwardings/,/default/p
     ClearAllForwardings
             Specifies that all local, remote, and dynamic port forwardings
             specified in the configuration files or on the command line be
             cleared.  This option is primarily useful when used from the
             ssh(1) command line to clear port forwardings set in configura‐
             tion files, and is automatically set by scp(1) and sftp(1).  The
             argument must be yes or no (the default).</code>
登录后复制

更改编译后的字符串

对于那些希望禁用默认的ClearAllForwardings yes配置的人来说,一个选项是使用sed直接编辑SFTP二进制文件中的字符串(假设平台的sed是二进制安全的):

<code>$ sed 's/AllForwardings yes/AllForwardings no /'  sftp.noclearforward</code>
登录后复制

这种直接修改比编译新的二进制文件要容易得多。

我们可以确认字符串已成功修改:

<code>$ strings ./sftp.noclearforward | grep [-]o[CFP]
-oForwardX11 no
-oPermitLocalCommand no
-oClearAllForwardings no
-oForwardAgent no
-oPort %d</code>
登录后复制

虽然修改后的SFTP的内容和校验和将不同,但任何存在的Linux BuildID sha1都将保持不变(但在使用编辑后的SFTP时,请不要提交支持工单):

<code>$ file /usr/bin/sftp ./sftp.noclearforward
/usr/bin/sftp:         ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=d7e77e24d5fac0fdc89e62a4c9c656091f2c4a33, for GNU/Linux 3.2.0, stripped
./sftp.noclearforward: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=d7e77e24d5fac0fdc89e62a4c9c656091f2c4a33, for GNU/Linux 3.2.0, stripped

$ sha1sum /usr/bin/sftp ./sftp.noclearforward
d8bdaf0b4642b9c324f9c2e0aeee2d9578fbe383  /usr/bin/sftp
b12dda8ecfd7bd2847919b5531aea7c03364c123  ./sftp.noclearforward

$ sha256sum /usr/bin/sftp ./sftp.noclearforward
986eecdfc654c9b3ff3fd0dce59690d47cf56be96a4b98a04a3682aef95d3f52  /usr/bin/sftp
c8f99ce33fc129250c11dc6dbb8a01112e01124e470a92d0acefb955fd17d670  ./sftp.noclearforward</code>
登录后复制

可以调用修改后的SFTP二进制文件来启用端口转发:

<code>$ chmod 755 sftp.noclearforward

$ ./sftp.noclearforward -S ./portssh aturing@sftp.victimandum.com
aturing@sftp.victimandum.com's password:
Connected to sftp.victimandum.com.
sftp></code>
登录后复制

现在可以在子进程中看到修改后的设置:

<code>$ pps aturing
    PID TTY      STAT   TIME COMMAND
   9991 pts/0    S      0:00 ./sftp.noclearforward -S ./portssh aturing@sftp.victimandum.com
   9992 pts/0    S      0:00 ssh -L2525:smtp.victimandum.com:25 -oForwardX11 no -oPermitLocalCommand no -oClearAllForwardings no  -oForwardAgent no -l aturing -s -- sftp.victimandum.com sftp</code>
登录后复制

该功能在远程服务器上已启用并可运行,并且可以在单独的shell中验证连接:

<code>$ nc localhost 2525
220 smtp.victimandum.com Microsoft ESMTP MAIL Service, Version: 1.2.3456.78901 ready at  Sun, 1 Jan 2023 01:23:45 -0100
^C</code>
登录后复制
登录后复制

当服务器上禁用转发功能时,客户端将在连接尝试时收到指示此状态的通知:

<code>channel 3: open failed: administratively prohibited: open failed</code>
登录后复制

分配不受信任帐户的SFTP管理员应该可能验证服务器配置是否明确禁用了转发和命令执行。

超越POSIX Shell

虽然dash和POSIX标准提供set --作为重置命令行参数的方法,但bash和ksh93中提供了更高级的功能:

<code>$ cat ynargs
#!/bin/bash

echo "${@//yes/no}"</code>
登录后复制

快速测试确认成功编辑:

<code>$ ./ynargs -oForwardX11 no -oPermitLocalCommand yes -oClearAllForwardings yes -oForwardAgent no
-oForwardX11 no -oPermitLocalCommand no -oClearAllForwardings no -oForwardAgent no</code>
登录后复制

请注意,上面的${@//.../...}不是有效的POSIX,并且在dash或任何从pdksh派生的shell(mksh,oksh)中都不能运行。许多平台没有捆绑具有此功能的shell(例如Android和OpenBSD,尽管有添加它们的方法);对于受限平台,二进制编辑技术可能更直接,而不是安装替代shell。

要利用具有功能强大的shell的此功能,我们创建一个目录,然后在其中创建一个清除问题设置的SSH包装器:

<code>$ cat ~/switcharoo/ssh
#!/bin/bash

exec /usr/bin/ssh "${@//yes/no}"</code>
登录后复制

然后在$PATH中设置系统SSH之前的目录:

<code>$ export PATH=~/switcharoo:$PATH

$ which ssh
~/switcharoo/ssh</code>
登录后复制

然后,我们在这种修改后的环境下调用系统SFTP:

<code>$ /usr/bin/sftp -S ./portssh aturing@sftp.victimandum.com
aturing@sftp.victimandum.com's password:
Connected to sftp.victimandum.com.
sftp></code>
登录后复制

我们观察到shell重置了问题参数:

<code>$ pps aturing
    PID TTY      STAT   TIME COMMAND
  10058 pts/0    S      0:00 /usr/bin/sftp -S ./portssh aturing@sftp.victimandum.com
  10059 pts/0    S      0:00 /usr/bin/ssh -L2525:smtp.victimandum.com:25 -oForwardX11 no -oPermitLocalCommand no -oClearAllForwardings no  -oForwardAgent no -l aturing -s -- sftp.victimandum.com sftp</code>
登录后复制

再次确认了到转发端口的本地连接:

<code>$ nc localhost 2525
220 smtp.victimandum.com Microsoft ESMTP MAIL Service, Version: 1.2.3456.78901 ready at  Sun, 1 Jan 2023 01:23:45 -0100
^C</code>
登录后复制
登录后复制

作为最终演示,可以使用以下脚本进行完整的SMTP交换:

<code>$ cat awkmail
#!/bin/gawk -f

BEGIN { smtp="/inet/tcp/0/localhost/2525";
ORS="\r\n"; r=ARGV[1]; s=ARGV[2]; sbj=ARGV[3]; # /bin/awkmail to from subj  0) print                 |& smtp

print "."                                |& smtp;  smtp |& getline j; print j
print "quit"                             |& smtp;  smtp |& getline j; print j

close(smtp) } # /inet/protocol/local-port/remote-host/remote-port</code>
登录后复制

我们可以使用该脚本将自身邮件发送到目标SMTP服务器可以访问的远程收件人:

<code>$ ./awkmail jatanasoff@victimandum.com aturning@localhost awkmail  Queued mail for delivery</code>
登录后复制

在高度受控的环境中,这些功能的存在并非最佳。

服务器限制

可以理解的是,SFTP管理员不希望允许其用户在服务器的帮助下进行任意TCP连接,这可能会使敏感网络面临风险。限制此活动是一种谨慎的安全设置。

常见的限制性配置是将不受信任的SFTP用户添加到组中,然后在sshd_config中约束此组的活动:

<code>Match Group sftponly
  ChrootDirectory %h
  ForceCommand internal-sftp
  AllowTcpForwarding no</code>
登录后复制

此推荐配置通常足以阻止所有转发尝试。

建议添加DisableForwarding yes:

<code>$ man sshd_config | sed -n /DisableForwarding/,/configurations/p
     DisableForwarding
             Disables all forwarding features, including X11, ssh-agent(1),
             TCP and StreamLocal.  This option overrides all other forwarding-
             related options and may simplify restricted configurations.</code>
登录后复制

这留给管理员练习。

结论

过于严格的SFTP客户端设置可能会导致某种程度的服务器管理盲目性。SFTP客户端限制很容易通过多种方法规避。

对于SFTP服务器管理员来说,了解哪些地方受到限制以及在哪里限制非常重要,并且不要依赖客户端来保护服务器免受任意TCP控制。客户端受用户控制,如果配置错误,则很难实现对服务器的TCP命令。任何测试都应该在用户ssh_config中没有广泛设置转发的情况下进行,注意文档中的警告。

虽然这种功能可能有可以想象的合法用途,但滥用行为将很少见。

这些问题并非新鲜事物,因为站点exec的变体几十年来一直存在于明文FTP中。SFTP并不是明文文件传输的简单替代品,它本身也具有许多易于利用的功能。

希望管理员能够使用这些方法验证其服务器的安全性,以免措手不及。

以上是SFTP端口转发:启用抑制功能的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板