简介
命令执行:
即命令注入,通过提交恶意构造的参数破坏命令语句结构,达到执行恶意命令的目的
比如删除文件就可以用一个url请求
http://127.0.0.1/delete.php?filename=bob.txt;id
delete.php 的内容
<?php
$file=$_GET['filename']
system("rm $file");
?>
(基于linux系统)这样就能达到恶意删除bob.txt文件的目的同时用了;分隔符还能同时执行输出id的命令
现在用NVWA进行实战
low级别
输入127.0.0.1 && ipconfig
我们在输入的IP地址后面加了个&&分隔符返回ipconfig的命令返回内容即在他执行完ping 127.0.0.1后又执行了后面的命令
这里提供一些常用的注入
127.0.0.1 || ipconfig
127.0.0.1 & ipconfig
127.0.0.1 && ipconfig
127.0.0.1 | ipconfig
127.0.0.1 |ipconfig
127.0.0.1 ; ipconfig
适用于windows和linux系统
现在我们来看下源码
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
echo "<pre>{$cmd}</pre>";
}
?>
low级别没有任何guolv以上注入都可行(’;’ 是用于Linux系统window不适用)
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>";
}
?>
其中
$substitutions = array(
'&&' => '',
';' => '',
设置了黑名单有&&会被赋值为空,当然我们绕过就行啦
用一个上面的常用注入绕过例如
我使用127.0.0.1 | ipconfig
直接跳过ping执行了恶意命令ipconfig
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
echo "<pre>{$cmd}</pre>";
}
?>
这里黑名单就比较多了同样的方法绕过手工注入太麻烦我们直接用brupsuite
准备好字典
随便输入127.0.0.1打开代理brupsuite截断
抓包发往intruder
将其他目标clear,选择要用的ip add添加
选择刚刚的准备好的字典然后开始攻击
可以在Render看返回效果可以看出在high级别只有127.0.0.1 |ipconfig
可以绕过黑名单(注意 “|” 后没有空格)
impossible级别
观察分析源码
这里
这段代码限制了输入只能是纯数字
就无法注入了
总结
防范方法
1设置黑名单
2替换,转义关键字
3对于IP地址可以以‘ . ’为分界将数字分到数组判断是否为数字以保证输入内无连接符避免注入