【小白靶场学习】DVWA靶场篇——Command Injection命令注入

前言

DVWA(Damn Vulnerable Web Application)是安全入门者们最为熟知的练习靶场,它能够帮助小白对常见的安全问题进行理解,并且随着理解问题的深入自主调整靶场的安全级别。

DVWA靶场共包含十四个练习模块,每个模块分为 Low,Medium,High,Impossible四种难度,而Impossible和High相对而言,对于后续在进行过基础安全建设的生产环境中的实战更加具有参考意义。

  • Brute Force——暴力破解
  • Command Injection——命令注入
  • CSRF——跨站请求伪造
  • File Inclusion——文件包含
  • File Upload——文件上传漏洞
  • Insecure CAPTCHA——不安全的验证
  • SQL Injection——sql注入
  • SQL Injection(Blind)——sql注入(盲注)
  • Weak Session IDs——不安全的SessionID
  • XSS(DOM)——DOM型XSS  
  • XSS(Reflected)——反射型XSS
  • XSS(Stored)——存储型XSS
  • CSP Bypass ——绕过内容安全策略
  • JavaScript——JS攻击

一、模块介绍

Command Injection,命令注入是指由于程序中对提交的数据过滤不严格,导致通过构造特殊字符串(将自己的输入作为系统命令的参数拼接到命令行中),被程序执行后可获取到敏感的数据或资源情况。

原理分析

什么是命令注入,命令注入如何避免?_llzhang_fly的博客-CSDN博客_命令注入1、什么是命令注入Command Injection,即命令注入攻击,是指由于嵌入式应用程序或者 web应用程序对用户提交的数据过滤不严格,导致黑客可以通过构造特殊命令字符串的方式,将数据提交至应用程序中,并利用该方式执行外部程序或系统命令实施攻击,非法获取数据或者网络资源等。在命令注入的漏洞中,最为常见的是PHP的命令注入。PHP命令注入攻击存在的主要原因是Web应用程序员在应用PHP语言中一些具有命令执行功能的函数时,对用户提交的数据内容没有进行严格的过滤就带入函...https://blog.csdn.net/llzhang_fly/article/details/119685242易混淆的拼接符对比

A & BA && BA | BA || B

AB间无制约关系

A执行成功后B执行A的输出是B的输入A失败后执行B

二、各级通关

命令注入的形成需要如下三个条件:

  1. 使用了内部调用shell的函数:system(),exec()等
  2. 将外界传入的参数没有足够的过滤,直接传递给内部调用shell的函数
  3. 参数中shell的元字符没有被转义

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

?>

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

?> 

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

?> 

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

?> 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值