首页 > 解决方案 > PHP:使用管道输入密码运行 shell 命令

问题描述

我需要运行这个命令来打开一个mac加密的图像,我忘记了密码,想要暴力破解密码。

为了检查我的代码是否正常工作,我创建了一个新图像来检查它。这没用。

$command = "echo -n Secure@3@1| hdiutil attach -stdinpass Hidden.dmg"

// I have tried exec, shell_exec, popen like
exec($command);

// Every time it returns `hdiutil: attach failed - Authentication error`

有人可以建议代码有什么问题,因为我已经从 shell 本身验证了密码正确并且命令有效。

标签: phpcommand-line-interfaceexecshell-exec

解决方案


使用 proc_open 找到答案,留在这里以备将来参考。

从https://www.php.net/manual/en/function.system.php#94929复制了这个函数。

function my_exec($cmd, $input='')
{
    $proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes);

    fwrite($pipes[0], $input);fclose($pipes[0]);
    $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]);
    $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]);
    $rtn=proc_close($proc);
    return array('stdout'=>$stdout,
               'stderr'=>$stderr,
               'return'=>$rtn
          );
}

my_exec('hdiutil attach -stdinpass Hidden.dmg', 'Secure@3@1'); // Worked

在不使用管道的情况下调用此函数。

仍在寻找为什么管道操作员不起作用的答案。


推荐阅读