使用子进程利用Python漏洞
我有一个小程序(程序a),要求输入密码,如果密码正确,它会打开一个shell。我想用python编写另一个程序(程序B),它调用程序A并提供密码作为输入,这样shell就打开了 我正在执行以下操作,但程序只是终止,不允许我使用shell使用子进程利用Python漏洞,python,Python,我有一个小程序(程序a),要求输入密码,如果密码正确,它会打开一个shell。我想用python编写另一个程序(程序B),它调用程序A并提供密码作为输入,这样shell就打开了 我正在执行以下操作,但程序只是终止,不允许我使用shell p = subprocess.Popen("./ProgramA", stdin=subprocess.PIPE) p.communicate("password") 运行程序B时,我的终端如下所示: ~/Desktop/$./ProgramB Passwor
p = subprocess.Popen("./ProgramA", stdin=subprocess.PIPE)
p.communicate("password")
运行程序B时,我的终端如下所示:
~/Desktop/$./ProgramB
Password: Here is your shell.
~/Desktop/$ (this is not the shell opened by program A)
如何修复此问题?由
programma
inhristdin启动的shell从其父进程继承管道。此时管道已关闭,因此外壳将退出
您可能希望将终端保留为stdin,但这样就很难伪造密码输入。
Popen.communicate()
关闭子进程的stdin
,并等待它终止-这是预期的行为。您所需要的只是p.stdin.write(“password”)
假设这是linux或osx,如果stdin是管道而不是终端,程序通常会以不同的方式运行,它们也会“绕过”stdin并读取终端的密码。在您的情况下,程序可能没有看到终端并退出。尝试使用python模块或阅读其源代码,了解如何为stdin打开pty。这也无法使其正常工作,因为用户;dn无法向shell提供任何交互式输入。您需要添加将任何输入转发到ProgramB
的stdin到programma
的代码。