SFTP端口转发:启用抑制功能
引言
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Linux最适合用作服务器管理、嵌入式系统和桌面环境。1)在服务器管理中,Linux用于托管网站、数据库和应用程序,提供稳定性和可靠性。2)在嵌入式系统中,Linux因其灵活性和稳定性被广泛应用于智能家居和汽车电子系统。3)在桌面环境中,Linux提供了丰富的应用和高效的性能。

Linux的五个基本组件是:1.内核,管理硬件资源;2.系统库,提供函数和服务;3.Shell,用户与系统交互的接口;4.文件系统,存储和组织数据;5.应用程序,利用系统资源实现功能。

Linux系统管理是通过配置、监控和维护来确保系统稳定、高效和安全。1.掌握shell命令如top、systemctl。2.使用apt或yum管理软件包。3.编写自动化脚本提高效率。4.调试常见错误如权限问题。5.通过监控工具优化性能。

Linux基础学习从零开始的方法包括:1.了解文件系统和命令行界面,2.掌握基本命令如ls、cd、mkdir,3.学习文件操作,如创建和编辑文件,4.探索高级用法如管道和grep命令,5.掌握调试技巧和性能优化,6.通过实践和探索不断提升技能。

Linux在服务器、嵌入式系统和桌面环境中的应用广泛。1)在服务器领域,Linux因其稳定性和安全性成为托管网站、数据库和应用的理想选择。2)在嵌入式系统中,Linux因其高度定制性和高效性而受欢迎。3)在桌面环境中,Linux提供了多种桌面环境,满足不同用户需求。

Linux设备是运行Linux操作系统的硬件设备,包括服务器、个人电脑、智能手机和嵌入式系统。它们利用Linux的强大功能执行各种任务,如网站托管和大数据分析。

Linux的缺点包括用户体验、软件兼容性、硬件支持和学习曲线。1.用户体验不如Windows或macOS友好,依赖命令行界面。2.软件兼容性不如其他系统,缺乏许多商业软件的原生版本。3.硬件支持不如Windows全面,可能需要手动编译驱动程序。4.学习曲线较陡峭,掌握命令行操作需要时间和耐心。

互联网运行不依赖单一操作系统,但Linux在其中扮演重要角色。Linux广泛应用于服务器和网络设备,因其稳定性、安全性和可扩展性受欢迎。
