gpt4 book ai didi

php - 关于从 php 访问影子密码文件的安全问题

转载 作者:搜寻专家 更新时间:2023-10-31 21:43:09 24 4
gpt4 key购买 nike

我在 php 中编写了这个函数来检查 linux 服务器上帐户的用户/密码。它工作正常,但我有点担心安全问题。

/*    Need to add www-data to group shadow (and restart apache)
$ sudo adduser www-data shadow
$ sudo /etc/init.d/apache2 restart
Needs whois to be installed to run mkpasswd
$ sudo apt-get install whois
Assumes that sha-512 is used in shadow file
*/

function authenticate($user, $pass){
// run shell command to output shadow file, and extract line for $user
// then split the shadow line by $ or : to get component parts
// store in $shad as array
$shad = preg_split("/[$:]/",`cat /etc/shadow | grep "^$user\:"`);
// use mkpasswd command to generate shadow line passing $pass and $shad[3] (salt)
// split the result into component parts and store in array $mkps
$mkps = preg_split("/[$:]/",trim(`mkpasswd -m sha-512 $pass $shad[3]`));
// compare the shadow file hashed password with generated hashed password and return
return ($shad[4] == $mkps[3]);
}

// usage...
if(authenticate('myUsername','myPassword')){
// logged in
} else {
// not valid user
}
  1. 在内网的专用服务器上,将www-data添加到group shadow中是否存在很大的安全风险? (我意识到在共享托管服务器上它可以让黑客有机会使用盐值来破解其他用户的密码)

  2. 我使用的方法还有其他安全问题吗?

  3. 有什么建议可以让它更可靠吗?

最佳答案

我不太熟悉影子组的工作原理,但让 PHP 访问它听起来确实很危险 - 一个带有损坏的 include 调用的 PHP 脚本可能会让攻击者获得 的内容/etc/阴影。当然,这并不等同于获得 root 访问权限,但公开加密密码仍然令人讨厌。

如果没有可以验证用户身份的 native Unix/Linux 命令 你可以有选择地运行,我认为你的想法

The other way I tried - which also works is to make a shell script that uses su to log in as the user, and returns an exit code of 0 for success. This can then be called from within php file.

听起来好多了,因为它不需要打开对任何更高级别资源的访问权限。您可能只需要设置某种速率限制,这样攻击者就无法通过对本地用户帐户进行数千次失败的登录尝试来禁用它们。

关于php - 关于从 php 访问影子密码文件的安全问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7944481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com