DVWA——Command Injection 命令注入

命令注入(Command Injection)

乱码问题

打开/DVWA/dvwa/includes/dvwaPage.inc.php文件,

277 行修改,将 UTF-8 改为GBK或者 GB2312。

在这里插入图片描述

命令注入的原理

  • OS命令注入:使用脚本语言开发应用程序,会调用一些外部程序(系统命令或者 exe 等可执行文件),当应用需要调用一些外部程序时就会用到一些系统命令的函数。

  • 比如服务器程序通过 system、eval、exec 等函数直接或者间接地调用 cmd.exe

  • 当在调用系统命令函数时,如果将用户的输入作为系统命令的参数拼接到命令中,在没有过滤用户输入的情况下,就会造成命令执行漏洞。

系统中的管道符应用

windows系统支持的管道符:

示例用法
a | b直接执行命令b
a || ba为假 ,才执行b
a & ba可真可假,若为假则直接执行b
a &&ba为真,b才能执行
0x0a换行回车

linux系统支持的管道符:

示例用法
a;b执行完再执行b
a | b直接显示b的执行结果
a || ba错,b才执行
a & ba可真可假,若为假则直接执行b
a && ba为真才能执行b
0x0a换行回车

Low

源码

<?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>";
}
?>

代码理解

​ 接收一个输入的 IP 地址,在前面连接一个 ping 命令。

​ 执行的代码 ping 输入的内容

漏洞利用

  • 当输入的内容只有 IP 时,执行的代码为 ping IP
  • 而当输入的内容不只有 IP 时,利用管道符可以执行输入的其他内容

当输入的内容为 | net user时,提交给系统执行的命令为 ping | net user

根据 windows 的管道符原则可知,直接回显| 后的指令执行结果,所以只执行命令 net user

利用此漏洞,可以进行一些其他操作

| net user

image-20231121162309369

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>";
}

?>

区别

​ 可以看到,medium 级别的命令注入源码在 low 级别的基础上增加了对 &&; 管道符的过滤,使用其他管道符拼接命令依旧可以完成注入

// 增加的过滤代码
$substitutions = array(
    '&&' => '',
    ';'  => '',
);

漏洞利用

| whoami

image-20231121162436701

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>";
}

?>

函数解析

trim()

trim(): 用于去除字符串两端的空格或其他字符。

函数举例

<?php
    
// 定义字符串 “test123”,长度为7,前后加空格之后长度变为14
$str="  test123     ";

// 未使用 trim() 函数去除首尾空格,输出内容和长度
echo "without_trim()<br>$str,length=".strlen($str)."<hr>";

// 使用 trim() 函数去除字符串首尾空格
$str2=trim($str);

// 输出使用 trim() 函数去除首尾空格后的内容和长度
echo "use_trim()<br>".$str2.",length=".strlen($str2);
?>

image-20231121164357134

str_replace

str_replace(): 用于在字符串中替换指定的字符或字符串。

str_replace(a,b,c):在字符串 c 中查找 a,将其替换为 b

函数举例

<?php
    // 定义字符串
    $str="Zhangsan love study";
    echo $str."<br>";
    // 将 $str 中的 “love” 替换为 “hate”
    $str=str_replace("love","hate",$str);
    echo $str;
?>

image-20231121164902579

array_keys

array_keys(): 用于获取数组中的所有键名,并返回一个包含键名的数组。

函数举例

<?php
    // 定义数组
    $a=array(
    "sex"=>True,
    "name"=>"zhangsan",
        "age"=>"18"
);
    // 输出数组
    print_r($a);
    echo "<hr>";
    // 使用 array_keys() 函数获取数组 $a 中所有键名并返回到 $key 数组中
    $key=array_keys($a);
    print_r($key);
?>

image-20231121165858562

php_uname()

php_uname(): 用于获取操作系统的信息。

php_uname('s'): 获取操作系统名称

php_uname('n'):获取主机名

<?php
    // 获取操作系统的信息。
    echo php_uname()."<hr>";
    // 获取操作系统名称
    echo php_uname('s')."<hr>";
    // 获取主机名
    echo php_uname('n');
?>

image-20231121171356754

stristr

stristr(): 用于在字符串中查找指定的子字符串(不区分大小写),匹配不到则返回 False,匹配成功则默认返回匹配成功后的字符串

image-20231121171837476

区别

high 级别在 medium 的级别上新增了以下绕过

'| ' => '',
'-'  => '',
'$'  => '',
'('  => '',
')'  => '',
'`'  => '',
'||' => '',

漏洞利用

| 后不加空格

|dir

image-20231121162816260

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();

?>

函数解析

stripslashes():过滤字符串中的反斜杠

explode():将所有的字符串打散成为数组

is_numeric() :用于检测变量是否为数字或数字字符串

sizeof(): 用于返回数组的长度

防御解析

名单,对输入进行过滤,只允许输入ip地址。
将输入根据点分成 4 段,每一段都判断一下是不是数字,总共是不是 4 段,如果是的话,再用点把 4 段数字拼接起来,因此不存在命令注入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gjl_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值