欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

讓PHP以ROOT權(quán)限執(zhí)行系統(tǒng)命令的方法

 更新時(shí)間:2011年02月10日 04:38:57   作者:  
這種問題我想大家可能都遇到過,網(wǎng)友提供的解決方法也很多。我也只是結(jié)合自己系統(tǒng)的需求并結(jié)合網(wǎng)友的解決方案來總結(jié)的一種方法
用來作為解決php以root權(quán)限執(zhí)行一些普通用戶不能執(zhí)行的命令或應(yīng)用的參考。
其實(shí)php里的popen()函數(shù)是可以解決這個(gè)問題的,但是由于某些版本的linux(如我使用的Centos 5)對系統(tǒng)安全的考慮,
使得這個(gè)問題解決起來麻煩了好多。先來看一個(gè)網(wǎng)友使用popen()函數(shù)的例子。
復(fù)制代碼 代碼如下:

/* PHP中如何增加一個(gè)系統(tǒng)用戶
下面是一段例程,增加一個(gè)名字為james的用戶,
root密碼是 louis。僅供參考
*/
$sucommand = "su root --command";
$useradd = "/scripts/demo/runscripts.php";
$rootpasswd = "louis";
$user = "james";
$user_add = sprintf("%s %s",$sucommand,$useradd);
$fp = @popen($user_add,"w");
@fputs($fp,$rootpasswd);
@pclose($fp);

經(jīng)過自己的測試,證實(shí)此段代碼是不能實(shí)現(xiàn)(至少在我的系統(tǒng)里是這樣的)作者想要獲得的結(jié)果的。經(jīng)過自己很長時(shí)間的google之后,
問題的關(guān)鍵是su root這個(gè)命令需要的密碼必須以終端的方式輸入,不能通過其它的方式(我也不知道還有沒有其它的方式)獲得。
又由于項(xiàng)目要求不能使用類似于sudo這種應(yīng)用,無奈之下,我選擇了網(wǎng)友提出的用編寫C程序的方法來解決此問題。
首先寫個(gè)C程序,命名為:run.c 放在目錄/scripts/demo/下
復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
uid_t uid ,euid;
//char cmd[1024]; //變量暫時(shí)未使用
uid = getuid() ;
euid = geteuid();
printf("my uid :%u\n",getuid()); //這里顯示的是當(dāng)前的uid 可以注釋掉.
printf("my euid :%u\n",geteuid()); //這里顯示的是當(dāng)前的euid
if(setreuid(euid, uid)) //交換這兩個(gè)id
perror("setreuid");
printf("after setreuid uid :%u\n",getuid());
printf("afer sertreuid euid :%u\n",geteuid());
system("/scripts/demo/runscripts.php"); //執(zhí)行腳本
return 0;
}

編譯該文件:
gcc -o run -Wall run.c
在該路徑下生成run文件,這個(gè)可執(zhí)行文件。如果現(xiàn)在用PHP腳本調(diào)用 該run的話,即使setreuid了 也是不行的。
接下來要做的是:給run賦予suid權(quán)限
# chmod u+s run
# ls
# -rwsr-xr-x 1 root root 5382 Jul 2 21:45 run
好了,已經(jīng)設(shè)置上了,再寫一個(gè)php頁面調(diào)用它。
復(fù)制代碼 代碼如下:

<?php
echo '<pre>';
$last_line = system('/scripts/demo/run', $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

在瀏覽器中瀏覽。
my uid :48
my euid :0
after setreuid uid :0
afer sertreuid euid :48

--------------------------------------------------------------------------------
Last line of the output: afer sertreuid euid :48
--------------------------------------------------------------------------------
Return value: 0
該命令執(zhí)行成功。
從顯示結(jié)果可以看出: apache(daemon)的uid 為48(事實(shí)上很多l(xiāng)inux系統(tǒng)下daemon的uid為2)。
調(diào)用setreuid后將有效用戶id和實(shí)際用戶id互換了。(必須在chmod u+s生效的情況下) 使apache當(dāng)前的uid為0這樣就能執(zhí)行root命令了。
只需要更改 C文件中的system所要執(zhí)行的命令就可以實(shí)現(xiàn)自己的PHP以root角色執(zhí)行命令了。

在玩C 以前 玩過一段時(shí)間的PHP, 哪個(gè)時(shí)候需要用PHP 來運(yùn)行root命令,一直未果,直到有一天搜索到了super這個(gè)插件.
隨著玩C的日子多了.發(fā)現(xiàn)可以用C語言來包裹 要運(yùn)行的外部命令. 實(shí)驗(yàn)了一下.成功了.
不需要任何外部工具就可以實(shí)現(xiàn)用PHP 執(zhí)行root命令.
我下面就把方法發(fā)布給大家,有需求用php來運(yùn)行root命令的朋友可以不用發(fā)愁了.
平臺:Linux. 實(shí)驗(yàn)命令iptables 當(dāng)前的目錄是/var/www/html/http
寫程序的時(shí)候 用root用戶
大家都知道iptables 非root用戶不能運(yùn)行.
首先寫個(gè)C程序
命名為:ipt.c
復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
uid_t uid ,euid;
uid = getuid() ;
euid = geteuid();
printf("my uid :%u\n",getuid()); //這里顯示的是當(dāng)前的uid 可以注釋掉.
printf("my euid :%u\n",geteuid()); //這里顯示的是當(dāng)前的euid
if(setreuid(euid, uid)) //交換這兩個(gè)id
perror("setreuid");
printf("after setreuid uid :%u\n",getuid());
printf("afer sertreuid euid :%u\n",geteuid());
system("/sbin/iptables -L"); //執(zhí)行iptables -L命令
return 0;
}


編譯該文件 gcc -o ipt -Wall ipt.c
在該路徑下生成ipt 這個(gè)可執(zhí)行文件.
如果現(xiàn)在用PHP網(wǎng)頁調(diào)用 該ipt的話,即使setreuid了 也是不行的.
接下來要做的是chmod u+s ./ipt
ls 一下
-rwsr-xr-x 1 root root 5382 Jul 2 21:45 ipt
s位已經(jīng)設(shè)置上了.
再寫一個(gè)php頁面調(diào)用它.
復(fù)制代碼 代碼如下:

<?php
echo '<pre>';
$last_line = system('/var/www/html/http/ipt', $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>

在瀏覽器中瀏覽.

[color=Red]Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
Chain OUTPUT (policy ACCEPT)
target prot opt source destination [/color]
[color=Blue]my uid :48
my euid :0
after setreuid uid :0
afer sertreuid euid :48[/color]

--------------------------------------------------------------------------------
Last line of the output: afer sertreuid euid :48
--------------------------------------------------------------------------------
Return value: 0

該命令執(zhí)行成功..
眾所周知: apache的uid 為48. 調(diào)用setreuid后 將有效用戶id 和實(shí)際用戶id互換了.(必須在chmod u+s生效的情況下) 使apache當(dāng)前的 uid為0 這樣就能執(zhí)行root命令了。

大家只需要更改 C文件中的 system所要執(zhí)行的命令就可以實(shí)現(xiàn)自己的PHP執(zhí)行root命令了.

相關(guān)文章

最新評論