首页 > 解决方案 > preg_replace 如何成为 RCE?

问题描述

我有一段代码导致代码变成远程代码执行。这个漏洞利用如何进行以及如何运行它?还有如何修复代码。我已经尝试了很多次,但它不会成功。谢谢!

这是代码:

<?php

error_reporting(E_WARNING | E_DEPRECATED);

require_once 'inc.php';

function getip(){
	if(getenv('HTTP_X_FORWARDED_FOR')){
	$ip = $_SERVER['REMOTE_ADDR'];
	if(preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", getenv('HTTP_X_FORWARDED_FOR'), $ip3)){
		$ip2 = array('/^0\./', '/^127\.0\.0\.1/', '/^192\.168\..*/', '/^172\.16\..*/', '/^10..*/', '/^176..*/', '/^206..*/');
		$ip = preg_replace($ip2, $ip, $ip3[1]);
	}
	}else{
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	if($ip == ""){ $ip = "x.x.x.x"; }
	return $ip;
}
$ipnya = getip();
$arrip = array("192.168.10.10","192.168.11.11");
if(!in_array($ipnya,$arrip)) {
    header( "HTTP/1.1 301 Moved Permanently" );
	header( "Status: 301 Moved Permanently" );
    header( "Location: https://www.google.com" );
	exit(0);
}

if(class_exists(PHPIDS_SUSPECTIP)) {
    $susp_ip = new PHPIDS_SUSPECTIP;
    
    if($susp_ip->__add())

    echo "<br>";
    
    $susp_list = json_decode($susp_ip->__read())->ip;    

    if(in_array($ipnya,$susp_list)) {
        header( "Status: 404 Page Not Found",true,404 );
    } else {
    }
}

?>

标签: php

解决方案


代码本身运行良好。某些 URL 链接或重定向存在问题。


推荐阅读