我正在CentOS 6.3机器上为我们的客户build立一个个人保pipe箱。 服务器将通过SFTP和基于PHP的专有HTTP服务进行访问。 这台机器将在我们的DMZ,所以它必须是安全的。 正因为如此,我有apache作为非特权用户运行,强化了apache,操作系统,PHP的安全性,在iptables中应用了大量的过滤,并应用了一些限制性的TCP包装器。 现在你可能已经怀疑这个人来了,SELinux也将被强制执行。
我正在设置PAM来使用MySQL,以便我的Web应用程序用户可以login。 这些用户都将在一个只能使用SSH的SFTP组中,用户将被连接到他们自己的“home”文件夹。
为了让这个SELinux希望文件夹有user_home_t标签。 父目录也只能由root用户写入。 如果这些限制不符合,SELinux将立即终止SSHpipe道。
需要通过http和SFTP访问的文件,所以我做了一个SELinux模块,允许Apache使用user_home_dir_t标签search/ attr /读/写等目录到目录。
由于sftp用户存储在MySQL中,我想在创build用户时设置其家庭目录。 这是一个问题,因为Apache没有对/ home目录的写入权限,只能由root来写,因为它需要保持SELinux和OpenSSH的快乐。
基本上我需要让Apache只做一些作为root的任务,而且只能在/ home中。 所以我需要以某种方式暂时提升权限,或让root用户为Apache执行这些任务。
我需要使用root权限来执行以下操作。
mkdir /home/userdir/ mkdir /home/userdir/userdir chmod -R 0755 /home/userdir umask 011 /home/userdir/userdir chcon -R -t user_home_t /home/userdir chown -R user:sftp_admin /home/userdir/userdir chmod 2770 /home/userdir/userdir
这将为用户创build一个家,现在我有一个想法,可以工作,克朗。 这意味着服务器需要检查每分钟没有家的用户,然后在创build用户时,界面会平均冻结30秒,然后才能确认我不喜欢的帐户。 有没有人知道是否可以用sudoers做些事情? 或者任何其他的想法,欢迎…
谢谢你的时间!
创build一个脚本来实现你描述的命令,然后运行setuid ,使其能够在任何人调用它时以根用户身份运行。 有关setuid的更多信息,请参阅http://en.wikipedia.org/wiki/Setuid 。
从PHP开始,你可以直接调用system('/bin/setupNewUser'); 像往常一样,脚本将作为根运行。
限制注入机会的替代解决scheme,并在禁用脚本上执行setuid执行的系统上工作:
创build一个有setuid的小程序。 下面列出了一个例子:
#include <stdlib.h> #include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { if (argc != 2) { cout << "Invalid arguments"; return 1; } //make sure the argument passed to the script has only [az] for (char* i = argv[1]; *i != '\0'; i++) { //check if the current character falls outside an allowed range if (!( //Allowed ranges: // between a and z ('a' <= *i && *i <= 'z') // between 0 and 9 || ('0' <= *i && *i <= '9') // '_' or '-' || ('_' == *i) || ('-' == *i) )) { cout << "Illegal character in username: " << *i << endl; return 2; } } //append the username string command = string("/test/setupUser.sh "); command += string(argv[1]); //execute the command and return the result transparently // return system(command.c_str()); // * OR * // call mkdir directly }
从PHP调用setuid程序
system('/test/setupUser '.escapeshellarg($username));
至less不是你build议的方式 – Apache没有理由以root身份执行任何操作。
由于Apache将拒绝以root身份运行(除了母进程绑定到80端口),所以你所build议的恶意程度应该是显而易见的。
以不可信的用户input作为根源是一个可怕的想法和确定的火灾方式来发现黑客可以键入一个盒子的所有有趣的事情会让你的系统做你不期望的东西。
如果你真的想这样做:
4770 ( rwsrwx--- ) 这将允许你的用户读/写/等。 目录的内容和目录上的SetUID位将强制用户成为所有文件的所有者(因此,他们可以在使用SCP / SFTPlogin时操作它们)。