首先我們來了解基礎
基礎知識來源於:<web安全攻防>徐焱
命令執行漏洞
應用程序有時需要調用一些執行系統命令的函數,如在PHP中,使用system、exec、shell_exec、passthru、popen、proc_popen等函數可以執行系統命令。當黑客能控制這些函數中的參數時,就可以將惡意的系統命令拼接到正常命令中,從而造成命令執行漏洞,這就是命令執行漏洞。
先了解下這些知識
Windows管道符
“|”:直接執行后面的語句。如:ping 127.0.0.1|whoami
“||”:如果前面執行的語句出錯澤執行后面的語句,前面的語句智能為假 如:ping 2 || whoami
“&”:如果前面的語句為假則直接執行后面的語句,前面的語句可真可假 如 ping 127.0.0.1&whoami
“&&”:如果前面的語句為假則直接出錯,也不執行后面的語句,前面的語句只能為真。例如:ping 127.0.0.1&&whoami
Linux管道符
“;”:執行完前面的語句再執行后面的 例如:ping 127.0.0.1;whoami
“|”:顯示后面語句的執行結果 例如:ping 127.0.0.1|whoami
“||”:當前面的語句只能怪出錯時,執行后面的語句,例如:ping 1||whoami
“&”:如果當前面的語句為假則直接執行后面的語句,前面的語句可真可假。例如:ping 127.0.0.1&whoami
“&&”:如果前面的語句為假則直接出錯,也不執行后面的,前面的語句只能為真 例如:ping 127.0.0.1&&whoami
測試學習我們可以寫一個簡單的PHP
1 <?php 2 3 echo system(“pint -n 2 ”.$_GET[‘IP’]); 4 5 ?>
搭建好我們可以進行簡單漏洞攻擊 如圖我們執行了查看系統當前用戶命令
DVWA靶場的命令執行漏洞
LOW
Low Command Injection Source
先分析代碼!
1. <?php 2. 3. if( isset( $_POST[ 'Submit' ] ) ) { 4. // Get input 5. $target = $_REQUEST[ 'ip' ]; 6. 7. // Determine OS and execute the ping command. 8. if( stristr( php_uname( 's' ), 'Windows NT' ) ) { 9. // Windows 10. $cmd = shell_exec( 'ping ' . $target ); 11. } 12. else { 13. // *nix 14. $cmd = shell_exec( 'ping -c 4 ' . $target ); 15. } 16. // Feedback for the end user 17. echo "<pre>{$cmd}</pre>"; 18. } 19. 20. ?>
我們分析這個靶場的代碼可以看到$_REQUEST接受用戶傳過來的值 我們並沒有看到有什么過濾機制的代碼所以 可以輸入任何東西。
測試執行ping127.0.0.1沒問題 我們利用管道命令來 再加命令語句
我這邊環境時本地windows我們選用windows的管道符 來執行 OK
Medium
Medium Command Injection Source
繼續先分析代碼
1. <?php 2. if( isset( $_POST[ 'Submit' ] ) ) { 3. // Get input 4. $target = $_REQUEST[ 'ip' ]; 5. // Set blacklist 6. $substitutions = array( 7. '&&' => '', 8. ';' => '', 9. ); 10. // Remove any of the charactars in the array (blacklist). 11. $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); 12. // Determine OS and execute the ping command. 13. if( stristr( php_uname( 's' ), 'Windows NT' ) ) { 14. // Windows 15. $cmd = shell_exec( 'ping ' . $target ); 16. } 17. else { 18. // *nix 19. $cmd = shell_exec( 'ping -c 4 ' . $target ); 20. } 21. // Feedback for the end user 22. echo "<pre>{$cmd}</pre>"; 23. }
我們注意6-9行 這里是個黑名單過濾 我們可以想辦法繞過 這里雖然把&&和分號;加入了黑名單,但是我們還可以用邏輯或(||)、管道符(|)或(&)來命令執行 繞過