PHP的命令执⾏漏洞学习
⾸先我们来了解基础
基础知识来源于:<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
3echo 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
黑客必备cmd命令大全
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⾏这⾥是个⿊名单过滤我们可以想办法绕过这⾥虽然把&&和分号;加⼊了⿊名单,但是我们还可以⽤逻辑或(||)、管道符(|)或(&)来命令执⾏绕过