= 0 && (int) $bits <= $max; } public static function in_range($ip, $range) { if (!self::is_valid($ip)) { return false; } if (strpos($range, '/') === false) { return self::is_valid($range) && inet_pton($ip) === inet_pton($range); } list($subnet, $bits) = explode('/', $range, 2); $ip_bin = inet_pton($ip); $subnet_bin = @inet_pton($subnet); if ($subnet_bin === false || strlen($ip_bin) !== strlen($subnet_bin)) { return false; } $bits = (int) $bits; $bytes = intdiv($bits, 8); if ($bytes > 0 && substr($ip_bin, 0, $bytes) !== substr($subnet_bin, 0, $bytes)) { return false; } $remainder = $bits % 8; if ($remainder === 0) { return true; } $mask = chr((0xff << (8 - $remainder)) & 0xff); return (substr($ip_bin, $bytes, 1) & $mask) === (substr($subnet_bin, $bytes, 1) & $mask); } public static function in_any($ip, $ranges) { foreach ((array) $ranges as $range) { if ($range !== '' && self::in_range($ip, $range)) { return true; } } return false; } public static function is_private($ip) { return self::is_valid($ip) && filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false; } public static function cloudflare_ranges() { $stored = get_site_option('aowpcs_cloudflare_ranges'); if (is_array($stored) && !empty($stored['ranges'])) { return $stored['ranges']; } return self::$cloudflare; } public static function trusted_proxies() { $ranges = AOWPCS_Settings::list_value('trusted_proxies'); if (AOWPCS_Settings::get('proxy_cloudflare')) { $ranges = array_merge($ranges, self::cloudflare_ranges()); } return $ranges; } public static function remote_addr() { $remote = isset($_SERVER['REMOTE_ADDR']) ? trim(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; return self::is_valid($remote) ? $remote : ''; } /** * The visitor's address */ public static function client_ip() { if (self::$client !== null) { return self::$client; } $remote = self::remote_addr(); self::$client = $remote; if ($remote === '') { return self::$client; } $header = AOWPCS_Settings::get('proxy_cloudflare') ? 'cf-connecting-ip' : AOWPCS_Settings::get('proxy_header'); if ($header === 'none' || !self::in_any($remote, self::trusted_proxies())) { return self::$client; } $key = 'HTTP_' . strtoupper(str_replace('-', '_', $header)); $value = isset($_SERVER[$key]) ? trim(wp_unslash($_SERVER[$key])) : ''; if ($value === '') { return self::$client; } if ($header === 'x-forwarded-for') { // The right-most address that is not one of our trusted proxies is the client $hops = array_reverse(array_map('trim', explode(',', $value))); $trusted = self::trusted_proxies(); foreach ($hops as $hop) { if (!self::is_valid($hop)) { break; } if (!self::in_any($hop, $trusted)) { self::$client = $hop; break; } } return self::$client; } if (self::is_valid($value)) { self::$client = $value; } return self::$client; } /** * Headers proxies and CDNs use to pass the visitor address */ private static function forwarded_header() { $headers = array('HTTP_X_FORWARDED_FOR' => 'X-Forwarded-For', 'HTTP_CF_CONNECTING_IP' => 'CF-Connecting-IP', 'HTTP_X_REAL_IP' => 'X-Real-IP', 'HTTP_TRUE_CLIENT_IP' => 'True-Client-IP', 'HTTP_FASTLY_CLIENT_IP' => 'Fastly-Client-IP', 'HTTP_X_SUCURI_CLIENTIP' => 'X-Sucuri-ClientIP', 'HTTP_FORWARDED' => 'Forwarded'); foreach ($headers as $key => $name) { if (!empty($_SERVER[$key])) { return array($name, sanitize_text_field(wp_unslash($_SERVER[$key]))); } } return null; } /** * Whether the request seems to come through a proxy or CDN that has not been configured. * In that case every visitor may share the proxy's address, so automatic blocking is unsafe. */ public static function proxy_suspected() { $remote = self::remote_addr(); if ($remote === '' || self::in_any($remote, self::trusted_proxies())) { return false; } if (self::proxy_detected($remote)) { return true; } return self::forwarded_header() !== null && (self::is_private($remote) || self::in_any($remote, self::$cloudflare)); } /** * Addresses recorded as unconfigured proxies during the past week (address => UTC time) */ public static function detected_proxies() { if (AOWPCS_Settings::get('proxy_none')) { return array(); } $seen = AOWPCS_Settings::state('proxy_seen'); if (!is_array($seen) || empty($seen['addresses']) || !is_array($seen['addresses'])) { return array(); } $cutoff = time() - WEEK_IN_SECONDS; $trusted = self::trusted_proxies(); $proxies = array(); foreach ($seen['addresses'] as $address => $time) { if (strtotime($time . ' UTC') > $cutoff && !self::in_any($address, $trusted)) { $proxies[$address] = $time; } } return $proxies; } /** * Whether an address (or, without one, any address) was detected as an unconfigured proxy. * Automatic blocks of a detected proxy are refused: blocking it would lock out every visitor * behind it. This is per address, so forged headers can only exempt the sender itself, which * changing its address would achieve anyway. */ public static function proxy_detected($remote = null) { $proxies = self::detected_proxies(); return $remote === null ? $proxies !== array() : isset($proxies[$remote]); } /** * Watch for unconfigured proxies: an untrusted address that forwards requests for at least * three different visitors within a day is recorded as a proxy. Only requests that carry a * forwarding header do any work here, and the stored state is small and bounded. */ public static function observe_proxy() { $forwarded = self::forwarded_header(); if ($forwarded === null || AOWPCS_Settings::get('proxy_none')) { return; } $remote = self::remote_addr(); if ($remote === '' || self::in_any($remote, self::trusted_proxies())) { return; } $proxies = self::detected_proxies(); if (isset($proxies[$remote]) && strtotime($proxies[$remote] . ' UTC') > time() - DAY_IN_SECONDS) { return; } $first = trim(preg_replace('/^for=/i', '', explode(',', explode(';', $forwarded[1])[0])[0]), " \"[]"); if (!self::is_valid($first) || $first === $remote) { return; } $candidates = AOWPCS_Plugin::get_option('aowpcs_proxy_candidates', array()); if (!is_array($candidates)) { $candidates = array(); } $now = time(); foreach ($candidates as $address => $entry) { if ($entry['first'] < $now - DAY_IN_SECONDS) { unset($candidates[$address]); } } // Keyed hash: the visitor addresses themselves are not stored $hash = substr(self::hash($first, 'proxy'), 0, 12); if (!isset($candidates[$remote])) { if (count($candidates) >= 50) { array_shift($candidates); } $candidates[$remote] = array('first' => $now, 'clients' => array()); } if (in_array($hash, $candidates[$remote]['clients'], true)) { return; } $candidates[$remote]['clients'][] = $hash; if (count($candidates[$remote]['clients']) >= 3) { unset($candidates[$remote]); $seen = AOWPCS_Settings::state('proxy_seen'); $addresses = (is_array($seen) && !empty($seen['addresses']) && is_array($seen['addresses'])) ? $seen['addresses'] : array(); unset($addresses[$remote]); $addresses[$remote] = AOWPCS_Plugin::now(); AOWPCS_Settings::set_state('proxy_seen', array('time' => AOWPCS_Plugin::now(), 'remote' => $remote, 'header' => $forwarded[0], 'addresses' => array_slice($addresses, -20, null, true))); } AOWPCS_Plugin::update_option('aowpcs_proxy_candidates', $candidates, true); } /** * Key used for rate limits and automatic blocks: the address itself for IPv4, the /64 * network for IPv6 (one subscriber usually controls a whole /64 and can rotate within it) */ public static function block_key($ip) { if (!self::is_valid($ip) || strpos($ip, ':') === false) { return $ip; } return inet_ntop(substr(inet_pton($ip), 0, 8) . str_repeat("\0", 8)) . '/64'; } /** * Remove the host part of an address: IPv4 /24, IPv6 /48 */ public static function anonymize($ip) { if (!self::is_valid($ip)) { return ''; } $bin = inet_pton($ip); if (strlen($bin) === 4) { return inet_ntop(substr($bin, 0, 3) . "\0"); } return inet_ntop(substr($bin, 0, 6) . str_repeat("\0", 10)); } /** * The address as it is stored in logs (anonymised unless the administrator disabled it) */ public static function for_log($ip) { return AOWPCS_Settings::get('anonymize_ip') ? self::anonymize($ip) : (self::is_valid($ip) ? $ip : ''); } /** * Keyed hash of an address, used for rate limiting without storing the address */ public static function hash($ip, $purpose = '') { return hash_hmac('sha1', $purpose . '|' . $ip, AOWPCS_Plugin::secret()); } /** * Reset the cached client address (tests) */ public static function reset() { self::$client = null; } }