让lampp的nobody用户可执行远程需root权限的命令

来源:互联网 发布:最大的云计算使用者 编辑:程序博客网 时间:2024/04/29 02:08

 

 

问题描述:web server 安装了lampp,现在需要从web server调用远程主机serverroot权限的命令。

 

Web Server:  10.53.11.44  client3

Server:      10.53.11.58  mds12

 

具体配置过程如下:

1.开放本机nobody用户

# su nobody
This account is currently not available.
# cat /etc/passwd|grep nobody
nobody:x:99:99:Nobody: /sbin/nologin

#创建一个临时主目录
# mkdir -p /home/nobody
# chown nobody.nobody /home/nobody/ -R

#修改nobody用户的信息
# usermod -s /bin/bash -d /home/nobody/ nobody

# cat /etc/passwd|grep nobody
nobody:x:99:99:Nobody:/home/nobody/:/bin/bash

2.创建nobody用户密钥文件

#su nobody
bash-3.2$ ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/home/nobody//.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/nobody//.ssh/id_rsa.

Your public key has been saved in /home/nobody//.ssh/id_rsa.pub.

The key fingerprint is:

7f:13:a0:9c:1b:b1:c2:c9:c5:07:26:ff:af:36:96:ef nobody@client3

bash-3.2$ cd

bash-3.2$ cd .ssh

bash-3.2$ ls

id_rsa       id_rsa.pub

bash-3.2$ exit

exit

3.配置本机和远程主机无密码访问

#先配置本机的root权限无密码访问,再配置远程无密码访问。否则本地文件没有操作权限,无法配置成功。

[root@client3 nobody]# cd /home/nobody/

[root@client3 nobody]# cat .ssh/id_rsa.pub >> /root/.ssh/authorized_keys

[root@client3 nobody]# su nobody

bash-3.2$ ssh -l root 10.53.11.44 hostname

The authenticity of host '10.53.11.44 (10.53.11.44)' can't be established.

RSA key fingerprint is 3d:d2:28:83:86:57:2e:e4:cb:34:e4:35:21:da:ec:de.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '10.53.11.44' (RSA) to the list of known hosts.

client3

bash-3.2$ cat .ssh/id_rsa.pub |ssh -l root 10.53.11.58 'cat - >> /root/.ssh/authorized_keys'

root@10.53.11.58's password:

bash-3.2$ ssh -l root 10.53.11.58 hostname

mds12

 

如果是apache不是lampp的话,应该将nobody用户替换为apache用户。

4.参考资料

参考网页:

http://bbs.51cto.com/thread-572339-1.html

 

apache可执行远程需root权限的命令

想在Web页面上去调用一个需要root权限的命令时,可以使用赋予脚本suid权限,并且交换euiduid的方式来解决。但现在又遇到一个新问题,httpd服务器与需执行的root权限命令并不在一台服务器上。也就是说,需要从Web页面上调用一个远程服务器上的需root权限的系统命令。这可是一个让人头疼的问题。

一、系统架构
先看看两台服务器的信息:
引用
Web Server
: 红旗 Asianux 3.0apache 2.0IP192.168.228.155
Gateway Server
: 红旗 DC Server 5.0IP
192.168.228.153


我需要实现的,就是通过点击Web Server上的按钮,然后运行一个CGI脚本,该脚本可以运行Gateway Server上的iptables命令。然后修改FORWARD表等防火墙信息。

iptables命令需要root权限,问题就出来了。

二、解决过程
虽然问题看上去比较麻烦,实际上比suid权限来得简单。
我解决的思路是,既然要执行远程的命令,那肯定是需要进行远程登陆的。所以,我只要让Web Server上的apache用户可以远程无密码,以root用户登陆到Gateway Server上,问题自然可以解决了。
1
、尝试
开始,我先在Web Server机器上,创建了其root用户到Gateway Server的密钥信任。有:
引用
# ssh 192.168.228.153
Last login: Tue Mar 17 18:22:33 2009 from 61.144.140.56
[root@printserver ~]#


然后,修改CGI脚本:

my $HTML=qx(/usr/bin/ssh -l root 192.168.228.153 /sbin/iptables -L);

访问页面,报503错误。httpd后台日志报:
引用
[Fri Mar 19 21:21:44 2009] [error] [client 10.8.1.10] Could not create directory '/var/www/.ssh'./r
[Fri Mar 19 21:21:44 2009] [error] [client 10.8.1.10] Host key verification failed./r


当然,这是可以预见的。虽然root用户做了密钥信任,但httpd服务是以apache用户执行的,所以没有作用。
但这可以给我们一个提示,是否只要给apache用户做密钥信任就可以了?

2
、给apache用户做密钥信任
先看看apache用户的信息:
引用
# su - apache
This account is currently not available.
# cat /etc/passwd|grep apache
apache:x:48:48:Apache:/var/www:/sbin/nologin


看来直接切换到apache用户来做是不行了。做个中转吧。
创建一个临时主目录

# mkdir -p /home/apache
# chown apache.apache /home/apache/ -R

修改apache用户的信息
引用
# usermod -s /bin/bash -d /home/apache/ apache
# cat /etc/passwd|grep apache
apache:x:48:48:Apache:/home/apache/:/bin/bash


切换到apache用户,并实现密钥信任
创建密钥:
引用
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/apache//.ssh/id_rsa):
Created directory '/home/apache//.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/apache//.ssh/id_rsa.
Your public key has been saved in /home/apache//.ssh/id_rsa.pub.
The key fingerprint is:
94:16:24:6b:53:31:46:63:56:97:d4:0f:79:9c:2e:a3 apache@ftpserver


拷贝公钥:
引用
$ cat .ssh/id_rsa.pub |ssh -l root 192.168.228.153 'cat - >> /root/.ssh/authorized_keys'
RSA key fingerprint is 9a:f4:88:16:c7:4d:22:0b:b9:0a:47:ec:27:80:66:e6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.228.153' (RSA) to the list of known hosts.
root@192.168.228.153's password:
$


尝试远程登陆:
引用
$ ssh -l root 192.168.228.153
Last login: Wed Mar 18 16:04:52 2009 from rfgz.3322.org
[root@printserver ~]#


密码信任创建成功。但我们还需要还原apache用户的信息,而其原主目录是/var/www/,所以,需要拷贝证书到对应的路径上。
拷贝证书
apache用户本身是不能进行的,需要以管理员操作:
引用
$ cp -a .ssh/ /var/www/
cp:
无法创建目录 “/var/www/.ssh”: 权限不够
$ ll /var/www -d
drwxr-xr-x 11 root root 4096 12-01 18:44 /var/www
$ exit
# cd /home/apache/
# cp -a .ssh/ /var/www/


恢复apache用户的主目录

# usermod -d /var/www apache

再实施信任关系
引用
# su - apache
$ pwd
/var/www
$ ssh -l root 192.168.228.153
Last login: Wed Mar 18 16:06:49 2009 from 192.168.228.135
[root@printserver ~]#


至此,信任关系应该是做好了。

恢复apache用户信息,删除临时文件
引用
$ exit
logout
# usermod -s /sbin/nologin apache
# rm -rf /home/apache/


3
、重新尝试执行远程命令
CGI
脚本不需要修改,代码是:

#!/usr/bin/perl -w
use strict;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);
print header (-charset=>"gb2312");

print scalar localtime();
my $HTML=qx(/usr/bin/ssh -l root 192.168.228.153 /sbin/iptables -L);
print "<br>",$HTML;

print "<br>End";

浏览器结果:

Gateway Server
上的系统日志显示:
引用
Mar 18 16:31:35 printserver sshd[716]: Accepted publickey for root from 192.168.228.155 port 59779 ssh2

信任关系通过,远程执行root权限命令成功。

二、其他
1
、用于解决本地问题
sshd
是服务端、客户端应用程序,完全可以把本地同时作为服务端和客户端。也就是说,把apache用户的id_rsa.pub拷贝到本地的root用户下的~/.ssh/authorized_keys文件中,即可解决Web上执行本地root权限命令的问题。
2
、安全
这是一说再说的问题了,root的权限是非常大的,务必做好信息输入的安全检查,防止漏洞。

原创粉丝点击