当前位置: 首页 > news >正文

DVWA中命令执行漏洞细说

在攻击中,命令注入是比较常见的方式,今天我们细说在软件开发中如何避免命令执行漏洞

我们通过DVWA中不同的安全等级来细说命令执行漏洞

1、先调整DVWA的安全等级为Lower,调整等级在DVWA Security页面调整

2、在Command Injection页面输入127.0.0.1&whoami 点击提交

这里返回是ping Ip的响应时间和当前登录系统的用户,说明后面的whoami命令已经执行了,这样就是存在命令执行漏洞,我们看看这个地方源码是怎么写的,

我们点击View Source,获取源代码,进行代码分析

代码分析:

<?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 );//shell_exec通过shell环境执行命令,并且将完整的输出以字符串的方式返回}// Feedback for the end userecho "<pre>{$cmd}</pre>";
}?>

3、在DVWA Security页面将安全等级调整为Medium ,我们在执行命令

执行127.0.0.1&&whoami出现信息不返回的情况

执行127.0.0.1&whoami信息还是可以返回,说明命令执行漏洞还在

代码分析:

<?phpif( isset( $_POST[ 'Submit' ]  ) ) {// Get input$target = $_REQUEST[ 'ip' ];// Set blacklist$substitutions = array('&&' => '',';'  => '',//这里只是将 && 和;这2种字符进行了删除);// Remove any of the characters 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 );//shell_exec通过shell环境执行命令,并且将完整的输出以字符串的方式返回}else {// *nix$cmd = shell_exec( 'ping  -c 4 ' . $target );}// Feedback for the end userecho "<pre>{$cmd}</pre>";
}?>

4、在DVWA Security页面将安全等级调整为High  ,我们在执行命令

执行127.0.0.1&whoami出现信息不返回的情况,说明&符合的漏洞已经修复

执行127.0.0.1|whoami信息还是可以返回,说明命令执行漏洞还在

代码分析:

<?phpif( isset( $_POST[ 'Submit' ]  ) ) {// Get input$target = trim($_REQUEST[ 'ip' ]);// Set blacklist$substitutions = array('&'  => '',';'  => '','| ' => '','-'  => '','$'  => '','('  => '',')'  => '','`'  => '','||' => '',//这里只是将上面9中字符进行拦截和删除。删除命令中的"| " 不是"|",多一个空格);// Remove any of the characters 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 );//shell_exec通过shell环境执行命令,并且将完整的输出以字符串的方式返回}else {// *nix$cmd = shell_exec( 'ping  -c 4 ' . $target );}// Feedback for the end userecho "<pre>{$cmd}</pre>";
}?>

5、在DVWA Security页面将安全等级调整为Impossible ,我们在执行命令

执行127.0.0.1|whoami将会被拦截。

代码分析:

<?phpif( isset( $_POST[ 'Submit' ]  ) ) {// Check Anti-CSRF tokencheckToken( $_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 );//把用户输入的数据根据.进行分开,比如127.0.0.1会分开成127 0 0 1// Check IF each octet is an integerif( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {//分别判断分开的4个部分是不是数字,如果有不是数字的直接不允许访问// If all 4 octets are int's put the IP back together.$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];//把验证完是数字的4部分在用.拼接起来// 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 userecho "<pre>{$cmd}</pre>";}else {// Ops. Let the user name theres a mistakeecho '<pre>ERROR: You have entered an invalid IP.</pre>';}
}// Generate Anti-CSRF token
generateSessionToken();?>

http://www.lryc.cn/news/408165.html

相关文章:

  • 【YOLOv5/v7改进系列】引入中心化特征金字塔的EVC模块
  • 【QT】常用控件(概述、QWidget核心属性、按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
  • 【Python】字母 Rangoli 图案
  • html+css 实现水波纹按钮
  • 科技与占星的融合:AI 智能占星师
  • 判断字符串,数组方法
  • SpringBoot Vue使用Jwt实现简单的权限管理
  • java中的多态
  • 【数据结构】:用Java实现链表
  • 前端开发知识(三)-javascript
  • Windows图形界面(GUI)-MFC-C/C++ - MFC绘图
  • 51单片机-第五节-串口通信
  • 【Linux常用命令】之df命令
  • 2024年起重信号司索工(建筑特殊工种)证模拟考试题库及起重信号司索工(建筑特殊工种)理论考试试题
  • AWS全服务历史年表:发布日期、GA和服务概述一览 (全)
  • Leetcode 2824. 统计和小于目标的下标对数目
  • TCP服务器主动断开客户端
  • 接口自动化中json.dumps()跟json.loads()区别详解
  • 计算机网络-配置双机三层互联(静态路由方式)
  • ES(Elasticsearch)常用的函数有哪些?
  • 【计算机网络】ICMP报文实验
  • transformers进行学习率调整lr_scheduler(warmup)
  • 智能优化算法之灰狼优化算法(GWO)
  • 昇思25天学习打卡营第17天|计算机视觉
  • Windows图形界面(GUI)-MFC-C/C++ - 键鼠操作
  • Angular 18.2.0 的新功能增强和创新
  • matlab 小数取余 rem 和 mod有 bug
  • Avalonia中的数据模板
  • Sqlmap中文使用手册 - Techniques模块参数使用
  • 科普文:kubernets原理