在PHP中 執行root 命令_PHP教程

來源:互聯網
上載者:User
在學習C 以前 學過一段時間的PHP, 哪個時候需要用PHP 來運行root命令,一直未果,直到有一天搜尋到了super這個外掛程式.

隨著學習C的日子多了.發現可以用C語言來包裹 要啟動並執行外部命令. 實驗了一下.成功了.

不需要任何外部工具就可以實現用PHP 執行root命令.

平台:Linux. 實驗命令iptables 當前的目錄是/var/www/html/http
寫程式的時候 用root使用者

大家都知道iptables 非root使用者不能運行.

首先寫個C程式

命名為:ipt.c

[CODE]

#include
#include
#include
#include

int main()
{
uid_t uid ,euid;

uid = getuid() ;
euid = geteuid();

printf("my uid :%u",getuid()); //這裡顯示的是當前的uid 可以注釋掉.
printf("my euid :%u",geteuid()); //這裡顯示的是當前的euid
if(setreuid(euid, uid)) //交換這兩個id
perror("setreuid");
printf("after setreuid uid :%u",getuid());
printf("afer sertreuid euid :%u",geteuid());

system("/sbin/iptables -L"); //執行iptables -L命令
return 0;
}
[/CODE]


編譯該檔案 gcc -o ipt -Wall ipt.c

在該路徑下產生ipt 這個可執行檔.

如果現在用PHP網頁調用 該ipt的話,即使setreuid了 也是不行的.

接下來要做的是chmod u+s ./ipt

ls 一下
-rwsr-xr-x 1 root root 5382 Jul 2 21:45 ipt

s位已經設定上了.

再寫一個php頁面調用它.

[CODE]
echo
; 

$last_line = system(/var/www/html/http/ipt, $retval);

echo

Last line of the output: . $last_line .
Return value: . $retval;
?>
[/CODE]

在瀏覽器中瀏覽.


[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


該命令執行成功..

眾所周知: apache的uid 為48. 調用setreuid後 將有效使用者id 和實際使用者id互換了.(必須在chmod u+s生效的情況下) 使apache當前的 uid為0 這樣就能執行root命令了。

http://www.bkjia.com/PHPjc/486042.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486042.htmlTechArticle在學習C以前學過一段時間的PHP,哪個時候需要用PHP來運行root命令,一直未果,直到有一天搜尋到了super這個外掛程式. 隨著學習C的日子多了.發現可...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.