: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
php_value auto_prepend_file ~/wp-content/plugins/wordfence/waf/bootstrap.php
if (!defined('WFWAF_RUN_COMPLETE')) {
if (!defined('WFWAF_AUTO_PREPEND')) {
define('WFWAF_AUTO_PREPEND', true);
if (!defined('WF_IS_WP_ENGINE')) {
define('WF_IS_WP_ENGINE', isset($_SERVER['IS_WPE']));
if (!defined('WF_IS_FLYWHEEL')) {
define('WF_IS_FLYWHEEL', isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Flywheel/') === 0);
if (!defined('WF_IS_PRESSABLE')) {
define('WF_IS_PRESSABLE', (defined('IS_ATOMIC') && IS_ATOMIC) || (defined('IS_PRESSABLE') && IS_PRESSABLE));
require(dirname(__FILE__) . '/../lib/wfVersionSupport.php');
* @var string $wfPHPDeprecatingVersion
* @var string $wfPHPMinimumVersion
if (!defined('WF_PHP_UNSUPPORTED')) {
define('WF_PHP_UNSUPPORTED', version_compare(PHP_VERSION, $wfPHPMinimumVersion, '<'));
if (WF_PHP_UNSUPPORTED) {
require_once(dirname(__FILE__) . '/wfWAFUserIPRange.php');
require_once(dirname(__FILE__) . '/wfWAFIPBlocksController.php');
require_once(dirname(__FILE__) . '/../vendor/wordfence/wf-waf/src/init.php');
class wfWAFWordPressRequest extends wfWAFRequest {
* @param wfWAFRequest|null $request
public static function createFromGlobals($request = null) {
if (version_compare(phpversion(), '5.3.0') >= 0) {
$class = get_called_class();
return parent::createFromGlobals($request);
public function getIP() {
$howGet = wfWAF::getInstance()->getStorageEngine()->getConfig('howGetIPs', null, 'synced');
if (is_string($howGet) && is_array($_SERVER) && array_key_exists($howGet, $_SERVER)) {
$ips[] = array($_SERVER[$howGet], $howGet);
if ($howGet != 'REMOTE_ADDR') {
$ips[] = array((is_array($_SERVER) && array_key_exists('REMOTE_ADDR', $_SERVER)) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1', 'REMOTE_ADDR');
$recommendedField = wfWAF::getInstance()->getStorageEngine()->getConfig('detectProxyRecommendation', null, 'synced');
if (!empty($recommendedField) && $recommendedField != 'UNKNOWN' && $recommendedField != 'DEFERRED') {
if (isset($_SERVER[$recommendedField])) {
$ips[] = array($_SERVER[$recommendedField], $recommendedField);
$ips[] = array((is_array($_SERVER) && array_key_exists('REMOTE_ADDR', $_SERVER)) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1', 'REMOTE_ADDR');
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips[] = array($_SERVER['HTTP_X_FORWARDED_FOR'], 'HTTP_X_FORWARDED_FOR');
if (isset($_SERVER['HTTP_X_REAL_IP'])) {
$ips[] = array($_SERVER['HTTP_X_REAL_IP'], 'HTTP_X_REAL_IP');
$cleanedIP = $this->_getCleanIPAndServerVar($ips);
if (is_array($cleanedIP)) {
list($ip, $variable) = $cleanedIP;
* Expects an array of items. The items are either IPs or IPs separated by comma, space or tab. Or an array of IP's.
* We then examine all IP's looking for a public IP and storing private IP's in an array. If we find no public IPs we return the first private addr we found.
private function _getCleanIPAndServerVar($arr) {
$privates = array(); //Store private addrs until end as last resort.
foreach ($arr as $entry) {
list($item, $var) = $entry;
// try verifying the IP is valid before stripping the port off
if (!$this->_isValidIP($j)) {
$j = preg_replace('/:\d+$/', '', $j); //Strip off port
if ($this->_isValidIP($j)) {
if ($this->_isIPv6MappedIPv4($j)) {
$j = wfWAFUtils::inet_ntop(wfWAFUtils::inet_pton($j));
if ($this->_isPrivateIP($j)) {
$privates[] = array($j, $var);
continue; //This was an array so we can skip to the next item
$trustedProxyConfig = wfWAF::getInstance()->getStorageEngine()->getConfig('howGetIPs_trusted_proxies_unified', null, 'synced');
$trustedProxies = $trustedProxyConfig === null ? array() : explode("\n", $trustedProxyConfig);
foreach (array(',', ' ', "\t") as $char) {
if (strpos($item, $char) !== false) {
$sp = explode($char, $item);
$sp = array_reverse($sp);
foreach ($sp as $index => $j) {
if (!$this->_isValidIP($j)) {
$j = preg_replace('/:\d+$/', '', $j); //Strip off port
if ($this->_isValidIP($j)) {
if ($this->_isIPv6MappedIPv4($j)) {
$j = wfWAFUtils::inet_ntop(wfWAFUtils::inet_pton($j));
foreach ($trustedProxies as $proxy) {
if (wfWAFUtils::subnetContainsIP($proxy, $j) && $index < count($sp) - 1) {
if ($this->_isPrivateIP($j)) {
$privates[] = array($j, $var);
if ($skipToNext){ continue; } //Skip to next item because this one had a comma, space or tab so was delimited and we didn't find anything.
if (!$this->_isValidIP($item)) {
$item = preg_replace('/:\d+$/', '', $item); //Strip off port
if ($this->_isValidIP($item)) {
if ($this->_isIPv6MappedIPv4($item)) {
$item = wfWAFUtils::inet_ntop(wfWAFUtils::inet_pton($item));
if ($this->_isPrivateIP($item)) {
$privates[] = array($item, $var);
return array($item, $var);
if (sizeof($privates) > 0) {
return $privates[0]; //Return the first private we found so that we respect the order the IP's were passed to this function.
private function _isValidIP($ip) {
return filter_var($ip, FILTER_VALIDATE_IP) !== false;
private function _isIPv6MappedIPv4($ip) {
return preg_match('/^(?:\:(?:\:0{1,4}){0,4}\:|(?:0{1,4}\:){5})ffff\:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/i', $ip) > 0;
* @param string $addr Should be in dot or colon notation (127.0.0.1 or ::1)
private function _isPrivateIP($ip) {
// Run this through the preset list for IPv4 addresses.
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
$wordfenceLib = realpath(dirname(__FILE__) . '/../lib');
include($wordfenceLib . '/wfIPWhitelist.php'); // defines $wfIPWhitelist
$private = $wfIPWhitelist['private'];
foreach ($private as $a) {
if (wfWAFUtils::subnetContainsIP($a, $ip)) {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6) !== false
&& filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false;
class wfWAFWordPressObserver extends wfWAFBaseObserver {
public function __construct($waf){
public function beforeRunRules() {
// Whitelisted URLs (in WAF config)
$whitelistedURLs = wfWAF::getInstance()->getStorageEngine()->getConfig('whitelistedURLs', null, 'livewaf');
foreach ($whitelistedURLs as $whitelistedURL) {
$whitelistPattern .= preg_replace('/\\\\\*/', '.*?', preg_quote($whitelistedURL, '/')) . '|';
$whitelistPattern = '/^(?:' . wfWAFUtils::substr($whitelistPattern, 0, -1) . ')$/i';
wfWAFRule::create(wfWAF::getInstance(), 0x8000000, 'rule', 'whitelist', 0, 'User Supplied Allowlisted URL', 'allow',
new wfWAFRuleComparisonGroup(
new wfWAFRuleComparison(wfWAF::getInstance(), 'match', $whitelistPattern, array(
// Whitelisted IPs (Wordfence config)
$whitelistedIPs = wfWAF::getInstance()->getStorageEngine()->getConfig('whitelistedIPs', null, 'synced');
if (!is_array($whitelistedIPs)) {
$whitelistedIPs = explode(',', $whitelistedIPs);
foreach ($whitelistedIPs as $whitelistedIP) {
$ipRange = new wfWAFUserIPRange($whitelistedIP);
if ($ipRange->isIPInRange(wfWAF::getInstance()->getRequest()->getIP())) {
throw new wfWAFAllowException('Wordfence allowlisted IP.');
if ($result = wfWAF::getInstance()->willPerformFinalAction(wfWAF::getInstance()->getRequest())) {
if ($result === true) { $result = 'Not available'; } // Should not happen but can if the reason in the blocks table is empty
wfWAF::getInstance()->getRequest()->setMetadata(array_merge(wfWAF::getInstance()->getRequest()->getMetadata(), array('finalAction' => $result)));
public function afterRunRules()
if (!wfWAF::getInstance()->getStorageEngine()->getConfig('disableWAFBlacklistBlocking')) {
$blockedPrefixes = wfWAF::getInstance()->getStorageEngine()->getConfig('blockedPrefixes', null, 'transient');
if ($blockedPrefixes && wfWAF::getInstance()->getStorageEngine()->getConfig('isPaid', null, 'synced')) {
$blockedPrefixes = base64_decode($blockedPrefixes);
if ($this->_prefixListContainsIP($blockedPrefixes, wfWAF::getInstance()->getRequest()->getIP()) !== false) {
$allowedCacheJSON = wfWAF::getInstance()->getStorageEngine()->getConfig('blacklistAllowedCache', '', 'transient');
$allowedCache = @json_decode($allowedCacheJSON, true);
if (!is_array($allowedCache)) {
$cacheTest = base64_encode(wfWAFUtils::inet_pton(wfWAF::getInstance()->getRequest()->getIP()));
if (!in_array($cacheTest, $allowedCache)) {
$guessSiteURL = sprintf('%s://%s/', wfWAF::getInstance()->getRequest()->getProtocol(), wfWAF::getInstance()->getRequest()->getHost());
$request = new wfWAFHTTP();
$response = wfWAFHTTP::get(WFWAF_API_URL_SEC . "?" . http_build_query(array(
'action' => 'is_ip_blacklisted',
'ip' => wfWAF::getInstance()->getRequest()->getIP(),
'k' => wfWAF::getInstance()->getStorageEngine()->getConfig('apiKey', null, 'synced'),
's' => wfWAF::getInstance()->getStorageEngine()->getConfig('siteURL', null, 'synced') ? wfWAF::getInstance()->getStorageEngine()->getConfig('siteURL', null, 'synced') : $guessSiteURL,
'h' => wfWAF::getInstance()->getStorageEngine()->getConfig('homeURL', null, 'synced') ? wfWAF::getInstance()->getStorageEngine()->getConfig('homeURL', null, 'synced') : $guessSiteURL,
'lang' => wfWAF::getInstance()->getStorageEngine()->getConfig('WPLANG', null, 'synced'),
if ($response instanceof wfWAFHTTPResponse && $response->getBody()) {
$jsonData = wfWAFUtils::json_decode($response->getBody(), true);
if (is_array($jsonData) && array_key_exists('data', $jsonData)) {
if (preg_match('/^block:(\d+)$/i', $jsonData['data'], $matches)) {
wfWAF::getInstance()->getStorageEngine()->blockIP((int)$matches[1] + time(), wfWAF::getInstance()->getRequest()->getIP(), wfWAFStorageInterface::IP_BLOCKS_BLACKLIST);
$e = new wfWAFBlockException();
$e->setFailedRules(array('blocked'));
$e->setRequest(wfWAF::getInstance()->getRequest());
else { //Allowed, cache until the next prefix list refresh
$allowedCache[] = $cacheTest;
wfWAF::getInstance()->getStorageEngine()->setConfig('blacklistAllowedCache', json_encode($allowedCache), 'transient');
} catch (wfWAFHTTPTransportException $e) {
error_log($e->getMessage());
$watchedIPs = wfWAF::getInstance()->getStorageEngine()->getConfig('watchedIPs', null, 'transient');
if (!is_array($watchedIPs)) {
$watchedIPs = explode(',', $watchedIPs);
foreach ($watchedIPs as $watchedIP) {
$ipRange = new wfWAFUserIPRange($watchedIP);
if ($ipRange->isIPInRange(wfWAF::getInstance()->getRequest()->getIP())) {
$this->waf->recordLogEvent(new wfWAFLogEvent());
if ($reason = wfWAF::getInstance()->getRequest()->getMetadata('finalAction')) {
$e = new wfWAFBlockException($reason['action']);
$e->setRequest(wfWAF::getInstance()->getRequest());
private function _prefixListContainsIP($prefixList, $ip) {
$size = ord(wfWAFUtils::substr($prefixList, 0, 1));
$sha256 = hash('sha256', wfWAFUtils::inet_pton($ip), true);
$p = wfWAFUtils::substr($sha256, 0, $size);
$count = ceil((wfWAFUtils::strlen($prefixList) - 1) / $size);
$mid = (int) (($high + $low) / 2);
$val = wfWAFUtils::substr($prefixList, 1 + $mid * $size, $size);
class wfWAFWordPress extends wfWAF {
/** @var wfWAFRunException */
private $learningModeAttackException;
* @param wfWAFBlockException $e
public function blockAction($e, $httpCode = 403, $redirect = false, $template = null) {
$failedRules = $e->getFailedRules();
if (!is_array($failedRules)) {
if ($this->isInLearningMode() && !$e->getRequest()->getMetadata('finalAction') && !in_array('blocked', $failedRules)) {
register_shutdown_function(array(
$this, 'whitelistFailedRulesIfNot404',
$this->getStorageEngine()->logAttack($e->getFailedRules(), $e->getParamKey(), $e->getParamValue(), $e->getRequest());
$this->setLearningModeAttackException($e);
if (empty($failedRules)) {
$finalAction = $e->getRequest()->getMetadata('finalAction');
if (is_array($finalAction)) {
$isLockedOut = isset($finalAction['lockout']) && $finalAction['lockout'];
$finalAction = $finalAction['action'];
if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_COUNTRY_REDIR) {
$redirect = wfWAFIPBlocksController::currentController()->countryRedirURL();
else if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_COUNTRY_BYPASS_REDIR) {
$redirect = wfWAFIPBlocksController::currentController()->countryBypassRedirURL();
else if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_UAREFIPRANGE) {
wfWAF::getInstance()->getRequest()->setMetadata(array_merge(wfWAF::getInstance()->getRequest()->getMetadata(), array('503Reason' => 'Advanced blocking in effect.', '503Time' => 3600)));
else if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_COUNTRY) {
wfWAF::getInstance()->getRequest()->setMetadata(array_merge(wfWAF::getInstance()->getRequest()->getMetadata(), array('503Reason' => 'Access from your area has been temporarily limited for security reasons.', '503Time' => 3600)));
else if (is_string($finalAction) && strlen($finalAction) > 0) {
wfWAF::getInstance()->getRequest()->setMetadata(array_merge(wfWAF::getInstance()->getRequest()->getMetadata(), array('503Reason' => $finalAction, '503Time' => 3600)));
parent::blockAction($e, $httpCode, $redirect, '503-lockout'); //exits
else if (array_search('blocked', $failedRules) !== false) {
parent::blockAction($e, $httpCode, $redirect, '403-blacklist'); //exits
parent::blockAction($e, $httpCode, $redirect, $template);
* @param wfWAFBlockXSSException $e
public function blockXSSAction($e, $httpCode = 403, $redirect = false) {
if ($this->isInLearningMode() && !$e->getRequest()->getMetadata('finalAction')) {
register_shutdown_function(array(
$this, 'whitelistFailedRulesIfNot404',
$this->getStorageEngine()->logAttack($e->getFailedRules(), $e->getParamKey(), $e->getParamValue(), $e->getRequest());
$this->setLearningModeAttackException($e);
$failedRules = $e->getFailedRules();
if (empty($failedRules)) {
$finalAction = $e->getRequest()->getMetadata('finalAction');
if (is_array($finalAction)) {
$finalAction = $finalAction['action'];
if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_COUNTRY_REDIR) {
$redirect = wfWAFIPBlocksController::currentController()->countryRedirURL();
else if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_COUNTRY_BYPASS_REDIR) {
$redirect = wfWAFIPBlocksController::currentController()->countryBypassRedirURL();
else if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_UAREFIPRANGE) {
wfWAF::getInstance()->getRequest()->setMetadata(array_merge(wfWAF::getInstance()->getRequest()->getMetadata(), array('503Reason' => 'Advanced blocking in effect.', '503Time' => 3600)));
else if ($finalAction == wfWAFIPBlocksController::WFWAF_BLOCK_COUNTRY) {
wfWAF::getInstance()->getRequest()->setMetadata(array_merge(wfWAF::getInstance()->getRequest()->getMetadata(), array('503Reason' => 'Access from your area has been temporarily limited for security reasons.', '503Time' => 3600)));
else if (is_string($finalAction) && strlen($finalAction) > 0) {
wfWAF::getInstance()->getRequest()->setMetadata(array_merge(wfWAF::getInstance()->getRequest()->getMetadata(), array('503Reason' => $finalAction, '503Time' => 3600)));
parent::blockXSSAction($e, $httpCode, $redirect);
private function isCli() {
return (php_sapi_name()==='cli') || !array_key_exists('REQUEST_METHOD', $_SERVER);
public function runCron() {
* Removed sending attack data. Attack data is sent in @see wordfence::veryFirstAction