Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用子进程利用Python漏洞_Python - Fatal编程技术网

使用子进程利用Python漏洞

使用子进程利用Python漏洞,python,Python,我有一个小程序(程序a),要求输入密码,如果密码正确,它会打开一个shell。我想用python编写另一个程序(程序B),它调用程序A并提供密码作为输入,这样shell就打开了 我正在执行以下操作,但程序只是终止,不允许我使用shell p = subprocess.Popen("./ProgramA", stdin=subprocess.PIPE) p.communicate("password") 运行程序B时,我的终端如下所示: ~/Desktop/$./ProgramB Passwor

我有一个小程序(程序a),要求输入密码,如果密码正确,它会打开一个shell。我想用python编写另一个程序(程序B),它调用程序A并提供密码作为输入,这样shell就打开了

我正在执行以下操作,但程序只是终止,不允许我使用shell

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
的代码。