DVWA—命令执行练习

admin 2024年6月8日22:48:42评论2 views字数 3821阅读12分44秒阅读模式

Low

首先进来就看到了一个ping命令的输入框

DVWA—命令执行练习

先输入127.0.0.1来测试一下

DVWA—命令执行练习

正常回显,再尝试通过|符号来进行命令执行DVWA—命令执行练习

可以看到能够进行命令执行。

这里说一下一些常用的命令拼接符,Windows或Linux下:

command1 ; command2 : 先执行command1后执行comnand2
command1 & command2 : 先执行comnand2后执行command1
command1 && command2 : 先执行command1后执行comnand2
command1 | command2 : 只执行command2
command1 || command2 : command1执行失败, 再执行command2(若command1执行成功,就不再执行command2)

下面来分析一下源码

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

?>

首先就是通过POST来进行一个提交,用REQUEST来接受一个ip,之后就是使用stristr(),php_uname()函数来判断该系统是否为Windows系统。下面简单介绍一下这两个函数

stristr(a,b):查找函数,它会搜索b字符串在a中的第一次出现。

php_uname():返回运行php的操作系统的有关信息,具体介绍请参考这篇文章(https://www.php.net/manual/zh/function.php-uname.php)

再回到正轨上,如果判断是Windows系统,则会将“ping”与输入的ip进行拼接,然后由shell_exec()函数来执行,并将执行结果返回到cmd变量中,否则将以Linux系统来执行ping命令。这里之所以要进行判断,主要是因为Linux系统如果不带上-c 参数的话会一直ping下去。最后将ping的返回值打印出来。

这里再介绍一下shell_exec()函数,它与exec()、system()、passthru()、backquote()函数均为命令执行函数,只是system与passthru函数在执行时会直接打印出结果,而其他三个则需要用打印函数打印出来,不同的是,exec()函数返回的是一个数组,需要用var_dump()进行打印,否则只能打印最后一行,而backquote()函数需要使用反引号“ ”。

Medium等级

一样的,先看一看源码

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

?>

与low等级差不多,只是多加了几个过滤,可以看到,这次把&&和;给过滤了,但是他没有过滤‘|’,所以我们依然可以使用‘ | ’来进行命令执行

DVWA—命令执行练习可以看到能够正常执行

High等级

老样子,先看源码

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

?>

这次他几乎把能使用的特殊字符都给过滤了,但是当你仔细分析源码的时候就会发现,他在过滤‘ | ’时多加了一个空格,导致过滤的是'| ',而不是'|'。DVWA—命令执行练习所以我们不用空格,直接用‘|’进行拼接DVWA—命令执行练习成功执行

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 "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}

// Generate Anti-CSRF token
generateSessionToken();

?>

这一关与前几关不同的是,这一关限制了用户的输入格式,而不是过滤用户输入的字符

$octet = explode( ".", $target );

这句话以为分割符,将$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];
}
else {
// Ops. Let the user name theres a mistake
echo '<pre>ERROR: You have entered an invalid IP.</pre>';
}

这段话则是分别对上面经过 .  分隔的元素进行判断是否为数字,如果都为数字,则将他们再次用 进行连接,否则就会报错。

来源:【DVWA—命令执行练习 - FreeBuf网络安全行业门户

原文始发于微信公众号(船山信安):DVWA—命令执行练习

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年6月8日22:48:42
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   DVWA—命令执行练习https://cn-sec.com/archives/2816159.html

发表评论

匿名网友 填写信息