【发布时间】: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
}
将 www-data 添加到组影子是否对内部网络的专用服务器有很大的安全风险? (我意识到在共享主机服务器上,它可能让黑客有机会使用盐值来破解其他用户的密码)
我使用的方法还有其他安全问题吗?
有什么建议让它更可靠吗?
【问题讨论】:
-
我对影子组的工作方式并不十分熟悉,但让 PHP 访问它听起来很危险——如果我理解正确的话,一个包含调用中断的 PHP 脚本可能会让攻击者获取内容的
/etc/shadow。有没有可以选择性运行的原生 Unix/Linux 命令? -
有效点。我不知道更好的方法来处理这个问题。我尝试过的另一种方法 - 也可以创建一个 shell 脚本,该脚本使用
su以用户身份登录,并返回退出代码 0 表示成功。然后可以从 php 文件中调用它。这似乎有点矫枉过正,但也许它更安全。 -
这是一个很酷的主意。听起来很多对我的耳朵更安全。我会将我的 cmets 转换为带有一些附加注释的答案
-
我的感觉是这可能更适合服务器故障 - 那里的人通常比 SO 人群更了解这种东西。也许我们应该投票支持迁移?
-
如果您可以更改系统上的设置,那么我建议您为此任务使用 PHP 扩展。 PHP/PAM to change user password 或通过 LDAP 设置间接访问。
标签: php linux user-accounts