PHP漏洞全解(二)-命令注入攻擊.

來源:互聯網
上載者:User
命令注入攻擊

PHP中可以使用下列5個函數來執行外部的應用程式或函數

system、exec、passthru、shell_exec、“(與shell_exec功能相同)

函數原型

string system(string command, int &return_var)

command 要執行的命令

return_var 存放執行命令的執行後的狀態值

string exec (string command, array &output, int &return_var)

command 要執行的命令

output 獲得執行命令輸出的每一行字串

return_var 存放執行命令後的狀態值

void passthru (string command, int &return_var)

command 要執行的命令

return_var 存放執行命令後的狀態值

string shell_exec (string command)

command 要執行的命令

漏洞執行個體

例1:

命令注入攻擊PHP中可以使用下列5個函數來執行外部的應用程式或函數system、exec、passthru、shell_exec、“(與shell_exec功能相同)函數原型string system(string command, int &return_var)    command 要執行的命令    return_var 存放執行命令的執行後的狀態值string exec (string command, array &output, int &return_var)    command 要執行的命令    output 獲得執行命令輸出的每一行字串    return_var 存放執行命令後的狀態值void passthru (string command, int &return_var)    command 要執行的命令    return_var 存放執行命令後的狀態值    string shell_exec (string command)    command 要執行的命令漏洞執行個體例1:

我們提交http://www.sectop.com/ex1.php?dir=| cat /etc/passwd

提交以後,命令變成了 system("ls -al | cat /etc/passwd");


eval注入攻擊

eval函數將輸入的字串參數當作PHP程式碼來執行

函數原型:

mixed eval(string code_str) //eval注入一般發生在攻擊者能控制輸入的字串的時候

當我們提交http://www.sectop.com/ex2.php?arg=phpinfo();漏洞就產生了;

動態函數




php
func A()
{
dosomething();
}
func B()
{
dosomething();
}
if (isset($_GET["func"]))
{
$myfunc = $_GET["func"];
echo $myfunc();
}
?>


程式員原意是想動態調用A和B函數,那我們提交http://www.sectop.com/ex.php?func=phpinfo漏洞產生

防範方法

1、盡量不要執行外部命令

2、使用自訂函數或函數庫來替代外部命令的功能

3、使用escapeshellarg函數來處理命令參數

4、使用safe_mode_exec_dir指定可執行檔的路徑

esacpeshellarg函數會將任何引起參數或命令結束的字元轉義,單引號“’”,替換成“\’”,雙引號“"”,替換成“\"”,分號“;”替換成“\;”

用safe_mode_exec_dir指定可執行檔的路徑,可以把會使用的命令提前放入此路徑內

safe_mode = On

safe_mode_exec_dir = /usr/local/php/bin/

以上就是PHP漏洞全解(二)-命令注入攻擊的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.