【问题标题】:PHP: Run shell command with piping the passwordPHP:使用管道输入密码运行 shell 命令
【发布时间】:2024-08-02 06:30:02
【问题描述】:

我需要运行这个命令来打开一个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 本身验证了密码正确并且命令有效。

【问题讨论】:

    标签: php command-line-interface exec shell-exec


    【解决方案1】:

    使用 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
    

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

    仍在寻找为什么管道运算符不起作用的答案。

    【讨论】:

      最近更新 更多