命令注入_渗透测试新手入门之DVWA(Command Injection命令行注入)篇

命令行注入(Command Injection):

是指在某些需要输入数据的位置,还构造了恶意的代码破坏了原先的语句结构。而系统缺少有效的过滤,最终达到破坏数据、信息泄露甚至掌控电脑的目的。许多内容管理系统CMS存在命令注入漏洞。

0a2367aaf8de6a2470012521436d0838.png

命令连接符:

&&:代表首先执行命令a在执行命令b,但是前提条件是命令a执行正确才会执行命令b,在a执行失败的情况下不会执行b命令。所以又被称为短路运算符。

&:代表首先执行命令a在执行命令b,如果a执行失败,还是会继续执行命令b。也就是说命令b的执行不会受到命令a的干扰,在执行效率上来说“&&”更加高效。

||:代表首先执行a命令在执行b命令,如果a命令执行成功,就不会执行b命令,相反,如果a命令执行不成功,就会执行b命令。

|:代表首先执行a命令,在执行b命令,不管a命令成功与否,都会去执行b命令

解决乱码问题:

当我们输入文本时候后显示乱码,

e6c8a981d4eccf18ed29bd4460862b02.png

在DVWA-masterdvwaincludes目录下找到dvwaPage.inc.php文件中所有的”charset=utf-8”,修改”charset=gb2312”,即可。

110d688bd6410c958a00116508215c94.png

low级:

源代码:

<?phpif ( 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    echo "
{$cmd}
";}?>

我们通过看源代码发现,没有进行任何过滤,我们只要使用“&&”、“&”、“|”即可完成注入。

eaee3a625785a54fa7e5fb609a7fae67.png

1.使用ping 192.168.35.132&&dir,得到结果是(前面是我的主机地址)

0938cf5b16f1a99b8eae4930c52cb5eb.png

2.使用ping 192.168.35.132&net user,得到结果是

7c6b8565752d356d545f11c7718305f8.png

3.使用ping 192.168.35.132|dir,192.168.35.132|net user得到结果是

804abeab126fe2a72750c08a1476e807.png
c29d186e5531780db74b5c3018042241.png

Medium级:

源代码:

<?phpif ( 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    echo "
{$cmd}";}?> 

我们通过观察代码发现,medium等级的命令行注入增加了一些过滤,通过查看源码可以看到,他将&&和:过滤成了空字符,所以我们依然可以进行注入。

78dd1f9004cb9378a6e7e0b1f3f28c71.png

输入”192.168.35.132 & dir”时,同样可以攻击,表明没有对”&”过滤。

675592783cf478e22e97b8d3ced0a4b2.png

但是”&&”和”&”是有区别的,”&&”是短路运算符,只有前一步执行成功才会执行后一步,而”&”则两个表达式都会执行。

我们输入”192.168.35.132&;& net view”时,也是可以的,因为过滤一次后相当于”192.168.35.132&& net view”。

37d19ac821e29d56e37842b4e6b03ce6.png

还有很多方法可以进行命令行输入,比如:“|”、“||”等等

High级:

<?phpif ( 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    echo "
{$cmd}";}?> 

这关看似没什么漏洞了,但是也是有一个小bug的。。引号下多了个空格,所以“|”还是可以用的。

1b2fe6abc55fc45ed55230b48f61e342.png

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         echo "
{$cmd}
"; } else { // Ops. Let the user name theres a mistake echo '
ERROR: You have entered an invalid IP.';     } } // Generate Anti-CSRF token generateSessionToken(); ?> 

可以看到,Impossible级别的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如“数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

原文链接地址:https://www.cnblogs.com/qi-yuan/p/12401736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值