【问题标题】:security concerns regarding accessing shadow password file from php关于从 php 访问影子密码文件的安全问题
【发布时间】:2011-10-30 10:46:28
【问题描述】:

我在 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 脚本可能会让攻击者获取内容的/etc/shadow。有没有可以选择性运行的原生 Unix/Linux 命令?
  • 有效点。我不知道更好的方法来处理这个问题。我尝试过的另一种方法 - 也可以创建一个 shell 脚本,该脚本使用 su 以用户身份登录,并返回退出代码 0 表示成功。然后可以从 php 文件中调用它。这似乎有点矫枉过正,但也许它更安全。
  • 这是一个很酷的主意。听起来很多对我的耳朵更安全。我会将我的 cmets 转换为带有一些附加注释的答案
  • 我的感觉是这可能更适合服务器故障 - 那里的人通常比 SO 人群更了解这种东西。也许我们应该投票支持迁移?
  • 如果您可以更改系统上的设置,那么我建议您为此任务使用 PHP 扩展。 PHP/PAM to change user password 或通过 LDAP 设置间接访问。

标签: php linux user-accounts


【解决方案1】:

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

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

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

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

【讨论】:

  • 我认为这是要走的路。我环顾四周,其他人倾向于使用 php/pam,但经常遇到问题,或者使用 spawn/expect/send 更改/检查密码的 shell 脚本。我会效仿的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
相关资源
最近更新 更多