【问题标题】:Python subprocess broken by "echo y" pipePython 子进程被“echo y”管道破坏
【发布时间】:2017-04-28 00:57:50
【问题描述】:

为什么当我尝试在子进程中使用 echo 管道发送 yes 回复时命令会中断,但当我手动将其输入命令提示符时却可以正常工作?

请注意,我尝试了使用和不使用空格,结果是相同的,但是如果您将看到的任何一个完全放入命令提示符中,它就会起作用而不是给出错误

WindowsError: [Error 2] The system cannot find the file specified


Python Interpreter Output

C:\Windows\System32>python
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> p = subprocess.Popen("echo y|cacls {0} /d SYSTEM".format("C:\\ProgramData\\Microsoft\\Diagnosis\\ETLLogs\\AutoLogger\\AutoLogger-Diagtrack-Listener.etl", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>> p = subprocess.Popen("echo y|cacls {0} /d SYSTEM".format("C:\ProgramData\Microsoft\Diagnosis\ETLLogs\AutoLogger\AutoLogger-Diagtrack-Listener.etl", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>> p = subprocess.Popen("echo y| cacls {0} /d SYSTEM".format("C:\\ProgramData\\Microsoft\\Diagnosis\\ETLLogs\\AutoLogger\\AutoLogger-Diagtrack-Listener.etl", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\subprocess.py", line 390, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
>>> p = subprocess.Popen("cacls {0} /d SYSTEM".format("C:\\ProgramData\\Microsoft\\Diagnosis\\ETLLogs\\AutoLogger\\AutoLogger-Diagtrack-Listener.etl", stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE))
>>> Are you sure (Y/N)?

请注意,当我完全删除回声时,它最终会起作用,但是我永远无法真正完成运行它,因为它永远不会得到“是”(echo y|)的回复......而且没有@987654325不幸的是,@ 开关内置在命令中。

【问题讨论】:

  • 您是否也从父进程运行whoami 进行验证? IsNTAdmin() 可能不知道 UAC。
  • @kichik 不,实际上,即使以管理员身份运行命令提示符,然后运行 ​​python 解释器并执行 print os.popen("whoami").read();print getpass.getuser() 都返回用户,而不是管理员......所以事实上父进程是管理员,不影响子进程......这很糟糕。我的问题仍然存在......有没有办法使用主进程的提升来调用子进程?
  • 这不是正常行为。 CreateProcess 使用当前访问令牌创建一个新进程,但如果可执行文件安全性具有低完整性标签,则中等完整性进程将创建一个低完整性进程。
  • 预期的正常行为是继承父访问令牌的副本,因此由提升的进程创建的进程应该具有高完整性、启用管理员组和完整的管理员权限集.
  • 不强制意味着它不启用SeTakeOwnerShipPrivilege,它允许管理员获取文件的所有权而不管DACL。但我确认它会在这种情况下工作。该文件已由管理员组拥有,并且您在该组中具有完全控制权。您有权获得所有权。

标签: python subprocess


【解决方案1】:

看起来这个问题类似于:How to use Subprocess in Windows

"echo" 不是程序,而是 shell 的内置功能,因此正确的功能需要指令 "shell=True",所以你的命令应该是:

p = subprocess.Popen("echo y|cacls {0} /d SYSTEM".format("C:\\ProgramData\\Microsoft\\Diagnosis\\ETLLogs\\AutoLogger\\AutoLogger-Diagtrack-Listener.etl", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE))

这也是错误的原因(因为 echo),因为 cacls 是程序,所以它在生成错误后运行(没有你需要的 echo y|)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 2016-06-05
    相关资源
    最近更新 更多