命令注入
命令注入(Command Injection
),對一些函數(shù)的參數(shù)沒有做過濾或過濾不嚴(yán)導(dǎo)致的,可以執(zhí)行系統(tǒng)或者應(yīng)用指令(CMD
命令或者 bash
命令)的一種注入攻擊手段。
常見的執(zhí)行系統(tǒng)命令的函數(shù)有
- system()
- passthru()
- exec()
- shell_exec()
- popen()
- proc_open()
- pcntl_exec()
system()函數(shù)
string system ( string $command [, int $return_var ] )
$command
為執(zhí)行的命令,return_var
可選,用來存放命令執(zhí)行后的狀態(tài)碼
system()函數(shù)執(zhí)行有回顯,將執(zhí)行結(jié)果輸出到頁面上
passthru()函數(shù)
void passthru ( string $command [, int $return_var ] )
和system函數(shù)類似,$command
為執(zhí)行的命令,return_var
可選,用來存放命令執(zhí)行后的狀態(tài)碼
執(zhí)行有回顯,將執(zhí)行結(jié)果輸出到頁面上
?php passthru("whoami");?>
exec()函數(shù)
string exec ( string $command [, array $output [, int $return_var ]] )
$command是要執(zhí)行的命令
$output
是獲得執(zhí)行命令輸出的每一行字符串,$return_var
用來保存命令執(zhí)行的狀態(tài)碼(檢測成功或失敗)
exec()函數(shù)執(zhí)行無回顯,默認(rèn)返回最后一行結(jié)果
?php echo exec("whoami");?>
?php
$test = "ipconfig";
exec($test,$array);
print_r($array);
?>
shell_exec()函數(shù)
string shell_exec( string command)
command
是要執(zhí)行的命令
shell_exec()函數(shù)默認(rèn)無回顯,通過 echo 可將執(zhí)行結(jié)果輸出到頁面
?php echo shell_exec("whoami");?>
反引號 `
shell_exec() 函數(shù)實(shí)際上僅是反撇號 (`) 操作符的變體,當(dāng)禁用shell_exec時(shí),` 也不可執(zhí)行
在php中稱之為執(zhí)行運(yùn)算符,PHP 將嘗試將反引號中的內(nèi)容作為 shell 命令來執(zhí)行,并將其輸出信息返回
popen()函數(shù)
resource popen ( string $command , string $mode )
函數(shù)需要兩個參數(shù),一個是執(zhí)行的命令command
,另外一個是指針文件的連接模式mode
,有r
和w
代表讀和寫。
函數(shù)不會直接返回執(zhí)行結(jié)果,而是返回一個文件指針,但是命令已經(jīng)執(zhí)行。
popen()
打開一個指向進(jìn)程的管道,該進(jìn)程由派生給定的command
命令執(zhí)行而產(chǎn)生。
返回一個和fopen()
所返回的相同的文件指針,只不過它是單向的(只能用于讀或?qū)懀┎⑶冶仨氂?code>pclose()來關(guān)閉。
此指針可以用于fgets()
,fgetss()
和 fwrite()
?php popen( 'whoami >> c:/1.txt', 'r' ); ?>
?php
$test = "ls /tmp/test";
$fp = popen($test,"r");
//popen打一個進(jìn)程通道
while (!feof($fp)) {
//從通道里面取得東西
$out = fgets($fp, 4096);
echo $out;
//打印出來 }pclose($fp);?>
proc_open()函數(shù)
resource proc_open (string $cmd ,array $descriptorspec ,array $pipes [, string $cwd [, array $env [, array $other_options ]]])
與Popen函數(shù)類似,但是可以提供雙向管道
?php
$test = "ipconfig";
$array = array(array("pipe","r"), //標(biāo)準(zhǔn)輸入
array("pipe","w"), //標(biāo)準(zhǔn)輸出內(nèi)容
array("pipe","w") //標(biāo)準(zhǔn)輸出錯誤
);
$fp = proc_open($test,$array,$pipes); //打開一個進(jìn)程通道
echo stream_get_contents($pipes[1]); //為什么是$pipes[1],因?yàn)?是輸出內(nèi)容 proc_close($fp);
?>
pcntl_exec()函數(shù)
void pcntl_exec ( string $path [, array $args [, array $envs ]] )
path是可執(zhí)行二進(jìn)制文件路徑或一個在文件第一行指定了 一個可執(zhí)行文件路徑標(biāo)頭的腳本
args是一個要傳遞給程序的參數(shù)的字符串?dāng)?shù)組。
pcntl
是linux
下的一個擴(kuò)展,需要額外安裝,可以支持 php 的多線程操作。
pcntl_exec
函數(shù)的作用是在當(dāng)前進(jìn)程空間執(zhí)行指定程序,版本要求:PHP > 4.2.0
?php
pcntl_exec( "/bin/bash" , array("whoami"));
?>
對這些危險(xiǎn)函數(shù),可以在php.ini中禁用,進(jìn)行安全加固

到此這篇關(guān)于PHP執(zhí)行系統(tǒng)命令函數(shù)實(shí)例講解的文章就介紹到這了,更多相關(guān)PHP執(zhí)行系統(tǒng)命令函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- PHP的命令行擴(kuò)展Readline相關(guān)函數(shù)的使用
- PHP中垃圾回收相關(guān)函數(shù)的使用
- PHP中斷言函數(shù)的使用詳解
- 詳解各種PHP函數(shù)漏洞
- PHP危險(xiǎn)函數(shù)禁用深入詳解
- PHP的imageTtfText()函數(shù)深入詳解
- PHP之header函數(shù)詳解
- php中sort函數(shù)排序知識點(diǎn)總結(jié)
- php中rsort函數(shù)實(shí)例用法
- 淺談定義一個PHP函數(shù)