allowed reverse-DNS suffixes. */ const BOTS = array( // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- host names used to verify crawlers by reverse DNS; nothing is loaded. 'googlebot|google-inspectiontool|googleother|storebot-google|adsbot-google|mediapartners-google|apis-google|feedfetcher-google' => array( '.googlebot.com', '.google.com', '.googleusercontent.com' ), 'bingbot|msnbot|bingpreview|adidxbot' => array( '.search.msn.com' ), 'applebot' => array( '.applebot.apple.com' ), 'yandex(bot|images|mobilebot|metrika)' => array( '.yandex.ru', '.yandex.net', '.yandex.com' ), 'baiduspider' => array( '.baidu.com', '.baidu.jp' ), 'duckduckbot' => array( '.duckduckgo.com' ), ); const CRAWLER = '#bot\b|bot/|crawl|spider|slurp|fetch|scrap|archiver|facebookexternalhit|bingpreview|headless|python-requests|curl/|wget/|go-http-client|okhttp|java/|libwww|httpclient|ahrefs|semrush|mj12|dotbot|petalbot|bytespider|gptbot|claudebot|ccbot|amazonbot#i'; private static $verified = array(); /** * Run the pattern checks (called by the firewall's early checks). * * @param string $ip IP. */ public static function check( $ip ) { $s = array( 'Softvolt_Ai_Bodyguard_Settings', 'get' ); $method = Softvolt_Ai_Bodyguard_Helpers::request_method(); $ua = Softvolt_Ai_Bodyguard_Helpers::user_agent(); $path = Softvolt_Ai_Bodyguard_Helpers::request_path(); if ( $s( 'firewall_block_methods', 1 ) && in_array( $method, array( 'TRACE', 'TRACK', 'DEBUG', 'CONNECT', 'MOVE' ), true ) ) { Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'method' ), __( 'This request method is not allowed.', 'ai-guardwall' ), 405, true ); } // Honeypot URLs: nobody but a scanner asks for them. $traps = Softvolt_Ai_Bodyguard_Settings::list_to_array( $s( 'firewall_trap_urls', '' ) ); if ( $traps && self::path_matches( $path, $traps ) ) { $minutes = (int) $s( 'firewall_trap_minutes', 1440 ); Softvolt_Ai_Bodyguard_Lockouts::block( $ip, 'trap', sprintf( /* translators: %s: path */ __( 'Requested the honeypot URL %s', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::trim_text( $path, 120 ) ), $minutes ); Softvolt_Ai_Bodyguard_Firewall::bump_stats( 'lockouts' ); Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'trap' ), __( 'Your IP address has been blocked.', 'ai-guardwall' ), 403, false ); } $uas = Softvolt_Ai_Bodyguard_Settings::list_to_array( $s( 'firewall_block_uas', '' ) ); if ( $uas && '' !== $ua && self::text_matches( $ua, $uas ) ) { Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'user_agent' ), __( 'Your browser or tool is blocked on this site.', 'ai-guardwall' ), 403, true ); } $refs = Softvolt_Ai_Bodyguard_Settings::list_to_array( $s( 'firewall_block_referrers', '' ) ); $ref = Softvolt_Ai_Bodyguard_Helpers::referer(); if ( $refs && '' !== $ref && self::text_matches( $ref, $refs ) ) { Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'referrer' ), __( 'Visits from that website are blocked.', 'ai-guardwall' ), 403, true ); } $hosts = Softvolt_Ai_Bodyguard_Settings::list_to_array( $s( 'firewall_block_hosts', '' ) ); if ( $hosts ) { $host = self::hostname( $ip ); if ( '' !== $host && self::host_matches( $host, $hosts ) ) { Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'hostname' ), __( 'Your network is blocked on this site.', 'ai-guardwall' ), 403, true ); } } if ( $s( 'firewall_fake_bots', 1 ) && self::claimed_bot( $ua ) && ! self::verified_bot( $ip, $ua ) ) { Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'fake_bot' ), __( 'This visitor claims to be a search engine but is not.', 'ai-guardwall' ), 403, true ); } if ( $s( 'firewall_block_blank_post', 0 ) && 'POST' === $method && '' === trim( $ua ) && '' === $ref ) { Softvolt_Ai_Bodyguard_Firewall::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'blank_post' ), __( 'Requests without a user agent and a referrer are not accepted.', 'ai-guardwall' ), 403, true ); } } /* ----------------------------- matching ----------------------------- */ /** * A line is a regular expression when wrapped in slashes, a wildcard pattern with *, or else a case-insensitive substring. */ public static function text_matches( $text, array $patterns ) { foreach ( $patterns as $p ) { $p = trim( $p ); if ( '' === $p ) { continue; } if ( strlen( $p ) > 2 && '/' === $p[0] && preg_match( '#^/.+/[imsx]*$#', $p ) ) { if ( @preg_match( $p, $text ) ) { return true; } continue; } if ( false !== strpos( $p, '*' ) ) { if ( preg_match( '#^' . str_replace( '\*', '.*', preg_quote( $p, '#' ) ) . '$#i', $text ) ) { return true; } continue; } if ( false !== stripos( $text, $p ) ) { return true; } } return false; } public static function path_matches( $path, array $patterns ) { $path = strtolower( $path ); foreach ( $patterns as $p ) { $p = strtolower( trim( $p ) ); if ( '' === $p ) { continue; } if ( '/' !== $p[0] && '*' !== $p[0] ) { $p = '/' . $p; } if ( false !== strpos( $p, '*' ) ? fnmatch( $p, $path ) : ( $path === $p || $path === rtrim( $p, '/' ) || ( '/' === substr( $p, -1 ) && 0 === strpos( $path, $p ) ) ) ) { return true; } } return false; } public static function host_matches( $host, array $patterns ) { $host = strtolower( $host ); foreach ( $patterns as $p ) { $p = strtolower( trim( $p ) ); if ( '' === $p ) { continue; } if ( false !== strpos( $p, '*' ) ? fnmatch( $p, $host ) : ( $host === $p || substr( $host, -strlen( '.' . ltrim( $p, '.' ) ) ) === '.' . ltrim( $p, '.' ) ) ) { return true; } } return false; } /* ------------------------- crawlers and bots ------------------------- */ public static function is_crawler( $ua ) { return '' === trim( (string) $ua ) || (bool) preg_match( self::CRAWLER, (string) $ua ); } /** * The search engine a user agent claims to be, or ''. */ public static function claimed_bot( $ua ) { foreach ( self::BOTS as $pattern => $suffixes ) { if ( preg_match( '#' . $pattern . '#i', (string) $ua ) ) { return $pattern; } } return ''; } /** * A real search engine crawler: the reverse DNS name ends in the engine's * domain and that name resolves back to the same IP. Cached for a day. */ public static function verified_bot( $ip, $ua ) { $claim = self::claimed_bot( $ua ); if ( '' === $claim ) { return false; } $key = $ip . '|' . $claim; if ( isset( self::$verified[ $key ] ) ) { return self::$verified[ $key ]; } $tkey = 'svabg_bot_' . md5( $key ); $cached = get_transient( $tkey ); if ( false !== $cached ) { self::$verified[ $key ] = ( 'yes' === $cached ); return self::$verified[ $key ]; } $ok = false; $host = self::hostname( $ip ); if ( '' !== $host ) { foreach ( self::BOTS[ $claim ] as $suffix ) { if ( substr( strtolower( $host ), -strlen( $suffix ) ) === $suffix ) { $ok = self::resolves_to( $host, $ip ); break; } } } set_transient( $tkey, $ok ? 'yes' : 'no', DAY_IN_SECONDS ); self::$verified[ $key ] = $ok; return $ok; } /** * Reverse DNS name of an IP (cached for a day), '' when none. */ public static function hostname( $ip ) { if ( ! Softvolt_Ai_Bodyguard_Helpers::is_valid_ip( $ip ) || Softvolt_Ai_Bodyguard_Helpers::is_private_ip( $ip ) ) { return ''; } $key = 'svabg_rdns_' . md5( $ip ); $cached = get_transient( $key ); if ( false !== $cached ) { return (string) $cached; } $host = @gethostbyaddr( $ip ); $host = ( ! $host || $host === $ip ) ? '' : strtolower( (string) $host ); set_transient( $key, $host, DAY_IN_SECONDS ); return $host; } private static function resolves_to( $host, $ip ) { $v6 = false !== strpos( $ip, ':' ); if ( $v6 ) { $records = @dns_get_record( $host, DNS_AAAA ); foreach ( is_array( $records ) ? $records : array() as $r ) { if ( isset( $r['ipv6'] ) && @inet_pton( $r['ipv6'] ) === @inet_pton( $ip ) ) { return true; } } return false; } $ips = @gethostbynamel( $host ); return is_array( $ips ) && in_array( $ip, $ips, true ); } /* --------------------------- unlock by email -------------------------- */ /** * Handle the unlock form and the unlock link. Runs before the lockout check, * once WordPress's mail functions exist. * * @param string $ip IP. */ public static function maybe_unlock( $ip ) { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'login_unlock_requests', 1 ) ) { return; } if ( ! empty( $_GET['svabg_unlock'] ) || ! empty( $_POST['svabg_unlock_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- only decides whether strings are about to be translated. Softvolt_Ai_Bodyguard_Firewall::early_l10n(); } // phpcs:disable WordPress.Security.NonceVerification -- the token and the rate limit protect these. if ( ! empty( $_GET['svabg_unlock'] ) ) { $token = preg_replace( '/[^A-Za-z0-9]/', '', sanitize_text_field( wp_unslash( $_GET['svabg_unlock'] ) ) ); $data = get_transient( 'svabg_unlock_' . hash( 'sha256', $token ) ); if ( is_array( $data ) && ! empty( $data['ip'] ) ) { delete_transient( 'svabg_unlock_' . hash( 'sha256', $token ) ); Softvolt_Ai_Bodyguard_Lockouts::unblock( $data['ip'] ); set_transient( 'svabg_tmp_allow_' . md5( $data['ip'] ), 1, HOUR_IN_SECONDS ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'ip_blocked', sprintf( /* translators: 1: IP address, 2: email */ __( 'IP %1$s unlocked by the emailed link (requested for %2$s).', 'ai-guardwall' ), $data['ip'], $data['email'] ) ); if ( function_exists( 'wp_safe_redirect' ) ) { wp_safe_redirect( wp_login_url() ); exit; } } return; } if ( empty( $_POST['svabg_unlock_email'] ) || 'POST' !== Softvolt_Ai_Bodyguard_Helpers::request_method() ) { return; } $email = sanitize_email( (string) wp_unslash( $_POST['svabg_unlock_email'] ) ); // phpcs:enable if ( Softvolt_Ai_Bodyguard_Firewall::count( 'svabg_unl_' . md5( $ip ), HOUR_IN_SECONDS ) > 3 ) { return; } $user = $email ? get_user_by( 'email', $email ) : false; if ( $user && user_can( $user, 'manage_options' ) && function_exists( 'wp_mail' ) ) { $token = wp_generate_password( 32, false, false ); set_transient( 'svabg_unlock_' . hash( 'sha256', $token ), array( 'ip' => $ip, 'email' => $email ), HOUR_IN_SECONDS ); $link = add_query_arg( 'svabg_unlock', $token, home_url( '/' ) ); wp_mail( $email, sprintf( /* translators: %s: setting */ __( '[%s] Unlock your IP address', 'ai-guardwall' ), wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ) ), sprintf( __( "Someone at IP %1\$s asked to unlock it on %2\$s.\n\nIf that was you, open this link within an hour to remove the block and let this address in for one hour:\n%3\$s\n\nIf it was not you, ignore this email: nothing changes.", 'ai-guardwall' ), $ip, home_url( '/' ), $link ) ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'ip_blocked', sprintf( /* translators: 1: email, 2: IP address */ __( 'Unlock link sent to %1$s for IP %2$s.', 'ai-guardwall' ), $email, $ip ) ); } define( 'SVABG_UNLOCK_SENT', true ); } /** * Allowed for an hour after an unlock link was used. */ public static function temporarily_allowed( $ip ) { return (bool) get_transient( 'svabg_tmp_allow_' . md5( $ip ) ); } /** * The unlock form shown on lockout pages. */ public static function unlock_form() { if ( ! Softvolt_Ai_Bodyguard_Settings::get( 'login_unlock_requests', 1 ) ) { return ''; } if ( defined( 'SVABG_UNLOCK_SENT' ) ) { return '

' . esc_html__( 'If that address belongs to an administrator of this site, an unlock link is on its way.', 'ai-guardwall' ) . '

'; } return '
'; } }