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


security concerns regarding accessing shadow password file from php

我在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添加到组影子中是否对内网专用服务器有很大的安全风险?(我意识到在共享主机服务器上,黑客可能有机会使用盐值来破解其他用户的密码)

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

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

我不太熟悉影子组是如何工作的,但是让PHP访问它听起来真的很危险-一个PHP脚本与一个破碎的include调用可以让攻击者获得/etc/shadow的内容。虽然这并不等于获得根访问权限,但在公开场合使用加密密码仍然是令人讨厌的。

如果没有本机Unix/Linux命令可以验证用户你可以有选择地参加竞选,我想你的主意

我尝试过的另一种方法(它也有效)是制作一个shell脚本,使用su作为用户登录,并返回成功的退出代码0。这可以从php文件中调用。

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