php - 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 本身验证了密码正确并且命令有效。
解决方案
使用 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
在不使用管道的情况下调用此函数。
仍在寻找为什么管道操作员不起作用的答案。
推荐阅读
- java - 如何将多部分/混合有效负载作为多部分使用?
- google-apps-script - 将数据从 JavaScript POST 到 Google Apps 脚本
- javascript - 省略号开始时显示文本
- python - 带有日期时间的 Numba
- python - 函数调用中的结果指针
- java - 为什么 String.toLowerCase() 返回的字符串没有被实习?
- python - 如何将实时音频 URL 传递给 Google Speech to Text API
- java - 计算数组中的元素
- sql - 命名子查询与使用 AS 给子查询结果起别名有什么区别?
- c# - 将 lightInject 转换为 .netcore DI