远程命令执行+ 远程代码执行 RCE漏洞

RCE漏洞 远程系统命令执行

提供远程命令操作的接口地方,能执行其他的命令的话就存在 命令执行漏洞
在一个固定接受命令的位置,通过命令链接符 执行多个系统命令

image-20220805084410790

总结来说
系统解释器
php解释器
python解释器

都是去解释一段字符串的语法工具,只要按照解释器的语法去拼接字符串就一定能都被执行。

A网站 提供了多种文字、语音、视频的交流互动,
在搭建这套服务中,会提供多种多样的数据接口,取得数据,
如果没有对获取数据的方式进行授权或者限制的话,那么就是存在的漏洞。

理论上将提供数据交互的地方,就一定存在漏洞,只是如何让解释器去执行你的命令而已。


渗透的工作就是检测 服务器提供的数据接口的限制或者授权是否有效\


# RCE漏洞中
 看代码中 对命令检测的复杂性的定义
在系统shell 中  
有多条命令一起执行的连接符 比如  
windows中 & and     &&  or的意思   

image-20220805145901592

低等防护
   没有对命令的的字符串进行过滤

image-20220805150119292

中等防护
   对 && 进行了str_replace 进行了替换
   没有对 & 进行替换
   
   substitutions = array( 
        '&&' => '', 
        ';'  => '', 
    ); 

    // Remove any of the charactars in the array (blacklist). 
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); 

image-20220805150500986

image-20220805150526363

高等防护
$substitutions = array( 
        '&'  => '', 
        ';'  => '', 
        '| ' => '', 
        '-'  => '', 
        '$'  => '', 
        '('  => '', 
        ')'  => '', 
        '`'  => '', 
        '||' => '', 
    ); 

    // Remove any of the charactars in the array (blacklist). 
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); 
    # 如何突破替换的str_replace 的正则过滤
    // Determine OS and execute the ping command. 
    if( stristr( php_uname( 's' ), 'Windows NT' ) ) { 
        // Windows 
        $cmd = shell_exec( 'ping  ' . $target ); 
        
        
'''
已经过滤了
& 
&&
netstat -an 
echo $PATH
echo $(cat /etc/passwd)
echo `cat /etc/fstab`
pwd || pwd
'''
唯独有一个 |空格 这个特殊符号过滤了,,|  这个特殊符号没有过滤

image-20220805151755577

最高级防护

<?php 

if( isset( $_POST[ 'Submit' ]  ) ) { 
    // Check Anti-CSRF token 
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' ); 
  
# 检查前端请求的 user-token  前端做的加密函数 hash  等等 ,防csrf   跨站伪造请求
    
    // Get input 
    $target = $_REQUEST[ 'ip' ]; 
    $target = stripslashes( $target ); 

    // Split the IP into 4 octects 
    $octet = explode( ".", $target ); 
    # 把输入的IP 地址 以. 进行分割
    // 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 ) ) {
        # 判断每个字符串 是否是数字
        # 在拼接成IP的形式
        // 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(); 

?> 

远程代码命令执行


posted @ 2022-08-08 15:17  mmszxc  阅读(485)  评论(0)    收藏  举报