命令注入攻擊(Command Injection),是指黑客通過利用HTML代碼輸入機制缺陷(例如缺乏有效驗證限制的表格域)來改變網頁的動態生成的內容。從而可以使用系統命令操作,實現使用遠程數據來構造要執行的命令的操作。
PHP中可以使用下列四個函數來執行外部的應用程序或函數:system、exec、passthru、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 要執行的命令,如下例所示,表示通過提交http://www.xxxxxx.com/ex1.php?dir=| cat /etc/passwd操作,執行命令變成了system("ls -al | cat /etc/passwd"),輸出/etc/passwd 文件的具體內容。
//ex1.php
<?php
$dir = $_GET["dir"];
if (isset($dir))
{
echo "";
system("ls -al ".$dir);
echo "";
}
?>
比如這段代碼可以構造輸入參數讓遠程服務器執行“whoami”命令,ip=127.0.0.1|whoami

