'block', 'firewall_learning_until' => '' ) ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'hardening', __( 'Learning mode ended as planned: the firewall now blocks attacks.', 'ai-guardwall' ) ); $mode = 'block'; } } if ( 'off' === $mode ) { return; } $has_login_cookie = false; foreach ( array_keys( $_COOKIE ) as $name ) { if ( 0 === strpos( (string) $name, 'wordpress_logged_in_' ) ) { $has_login_cookie = true; break; } } if ( $early && $has_login_cookie ) { return; // Decided at plugins_loaded. } if ( ! $early && $has_login_cookie && ! Softvolt_Ai_Bodyguard_Settings::get( 'firewall_inspect_logged_in', 0 ) && function_exists( 'wp_validate_auth_cookie' ) ) { $uid = wp_validate_auth_cookie( '', 'logged_in' ); if ( $uid && user_can( $uid, 'edit_posts' ) ) { define( 'SVABG_FIREWALL_INSPECTED', true ); return; } } define( 'SVABG_FIREWALL_INSPECTED', true ); self::inspect( $ip, $mode ); } /** * Requests the firewall never touches. */ private static function is_exempt( $ip ) { $path = Softvolt_Ai_Bodyguard_Helpers::request_path(); if ( preg_match( '#/wp-cron\.php$#', $path ) ) { return true; } $ua = Softvolt_Ai_Bodyguard_Helpers::user_agent(); if ( false !== strpos( $ua, 'AI-GuardWall/' ) && false !== strpos( $ua, '(self-check)' ) ) { return true; } if ( Softvolt_Ai_Bodyguard_Helpers::ip_in_list( $ip, Softvolt_Ai_Bodyguard_Settings::get( 'firewall_allowlist_ips', '' ) ) ) { return true; } if ( Softvolt_Ai_Bodyguard_Blocking::temporarily_allowed( $ip ) ) { return true; } foreach ( Softvolt_Ai_Bodyguard_Settings::list_to_array( Softvolt_Ai_Bodyguard_Settings::get( 'firewall_allowlist_urls', '' ) ) as $allow ) { $allow = trim( $allow ); if ( '' !== $allow && ( 0 === strpos( $path, $allow ) || fnmatch( $allow, $path ) ) ) { return true; } } return false; } private static function early_checks( $ip, $early = false ) { // Manual block list. if ( Softvolt_Ai_Bodyguard_Helpers::ip_in_list( $ip, Softvolt_Ai_Bodyguard_Settings::get( 'firewall_blocklist_ips', '' ) ) ) { self::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'blocklist' ), __( 'Your IP address is on this site\'s block list.', 'ai-guardwall' ), 403, false ); } // Lockouts. $block = Softvolt_Ai_Bodyguard_Lockouts::is_blocked( $ip ); if ( $block ) { $until = ! empty( $block['until'] ) ? sprintf( /* translators: %s: time span */ __( 'Try again in %s.', 'ai-guardwall' ), human_time_diff( time(), (int) $block['until'] ) ) : ''; self::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'lockout' ), trim( __( 'Your IP address has been temporarily blocked after suspicious activity.', 'ai-guardwall' ) . ' ' . $until ), 403, false ); } // Threat intelligence: networks run by criminals. if ( Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'threat_feed' ) && Softvolt_Ai_Bodyguard_Threat_Feed::listed( $ip ) ) { Softvolt_Ai_Bodyguard_Threat_Feed::count_block(); self::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'threat_feed' ), __( 'Your network is on a list of known malicious networks.', 'ai-guardwall' ), 403, true ); } // Methods, user agents, referrers, hostnames, honeypot URLs, fake search engines. Softvolt_Ai_Bodyguard_Blocking::check( $ip ); // Rate limits: people and crawlers can have different limits; real search engines are never limited. if ( Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'rate_limiting' ) ) { $ua = Softvolt_Ai_Bodyguard_Helpers::user_agent(); $limit = (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_requests', 0 ); $crawlers = (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_crawlers', 0 ); $crawler = Softvolt_Ai_Bodyguard_Blocking::is_crawler( $ua ); if ( $crawler && $crawlers > 0 ) { $limit = $crawlers; } if ( $limit > 0 && Softvolt_Ai_Bodyguard_Settings::get( 'firewall_verified_bots_exempt', 1 ) && Softvolt_Ai_Bodyguard_Blocking::claimed_bot( $ua ) && Softvolt_Ai_Bodyguard_Blocking::verified_bot( $ip, $ua ) ) { $limit = 0; } if ( $limit > 0 && self::count( 'svabg_rl_' . md5( $ip ), (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_window', 60 ) ) > $limit ) { Softvolt_Ai_Bodyguard_Lockouts::block( $ip, 'rate', sprintf( $crawler ? /* translators: %d: number */ __( 'Crawler: more than %d requests per window', 'ai-guardwall' ) : /* translators: %d: number */ __( 'More than %d requests per window', 'ai-guardwall' ), $limit ), (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_block_minutes', 15 ) ); self::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'rate' ), __( 'Too many requests. Please slow down.', 'ai-guardwall' ), 429, false ); } } // Empty user agent on sensitive POSTs. if ( Softvolt_Ai_Bodyguard_Settings::get( 'firewall_block_empty_ua', 1 ) && 'POST' === Softvolt_Ai_Bodyguard_Helpers::request_method() && '' === trim( Softvolt_Ai_Bodyguard_Helpers::user_agent() ) && preg_match( '#/(?:wp-login\.php|wp-comments-post\.php|xmlrpc\.php)$#', Softvolt_Ai_Bodyguard_Helpers::request_path() ) ) { self::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'empty_ua' ), __( 'Requests without a user agent are not accepted here.', 'ai-guardwall' ), 403, true ); } } /** * The login page, wp-admin (not the AJAX/post handlers), XML-RPC or the hidden login slug. */ public static function is_login_path() { $path = strtolower( Softvolt_Ai_Bodyguard_Helpers::request_path() ); if ( preg_match( '#/(?:wp-login\.php|xmlrpc\.php)$#', $path ) ) { return true; } if ( preg_match( '#/wp-admin(?:/|$)#', $path ) && ! preg_match( '#/wp-admin/(?:admin-ajax|admin-post|async-upload)\.php$#', $path ) ) { return true; } $slug = (string) Softvolt_Ai_Bodyguard_Settings::get( 'login_slug', '' ); return '' !== $slug && Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'custom_login_url' ) && rtrim( $path, '/' ) === '/' . strtolower( $slug ); } private static function inspect( $ip, $mode ) { $rules = Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rules', array() ); if ( ! Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'block_scanner_ua' ) ) { $rules['scanner_ua'] = 0; } $uri = Softvolt_Ai_Bodyguard_Helpers::raw_request_uri(); $files = array(); foreach ( (array) $_FILES as $field ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- the firewall reads the names of uploaded files to block dangerous ones; it never handles the upload. if ( isset( $field['name'] ) ) { foreach ( (array) $field['name'] as $n ) { if ( is_scalar( $n ) ) { $files[] = (string) $n; } } } } $body = ''; if ( ! empty( $rules['xmlrpc_multicall'] ) && preg_match( '#/xmlrpc\.php#', $uri ) && 'POST' === Softvolt_Ai_Bodyguard_Helpers::request_method() ) { $body = (string) file_get_contents( 'php://input', false, null, 0, 65536 ); } // WordPress's login and session cookies carry signed tokens, and password fields carry passwords: they // are never inspected, so no part of them can end up in the activity log as a "sample". $cookies = array(); foreach ( (array) $_COOKIE as $name => $value ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- only matched against the attack patterns, never stored or printed. if ( ! preg_match( '/^(wordpress_|wp-settings-|wp_woocommerce_session_|woocommerce_|comment_author_|wp-postpass_|svabg_)/i', (string) $name ) ) { $cookies[ $name ] = $value; } } $secret_fields = array_flip( array( 'pwd', 'pass', 'pass1', 'pass2', 'pass1-text', 'password', 'user_pass', 'post_password', 'current_password', 'new_password', 'confirm_password' ) ); $matches = Softvolt_Ai_Bodyguard_Firewall_Rules::inspect( array( 'uri' => $uri, 'get' => array_diff_key( $_GET, $secret_fields ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended 'post' => array_diff_key( $_POST, $secret_fields ), // phpcs:ignore WordPress.Security.NonceVerification.Missing 'cookies' => $cookies, 'ua' => Softvolt_Ai_Bodyguard_Helpers::raw_user_agent(), 'files' => $files, 'body' => $body, ), $rules ); if ( ! $matches ) { return; } $labels = Softvolt_Ai_Bodyguard_Firewall_Rules::labels(); $first = $matches[0]; $reason = ( isset( $labels[ $first['rule'] ] ) ? $labels[ $first['rule'] ] : $first['rule'] ) . ' (' . $first['where'] . ')'; $data = array( 'uri' => Softvolt_Ai_Bodyguard_Helpers::trim_text( Softvolt_Ai_Bodyguard_Helpers::loggable_url( Softvolt_Ai_Bodyguard_Helpers::request_uri() ), 500 ), 'rule' => $first['rule'], 'where' => sanitize_text_field( $first['where'] ), 'sample' => Softvolt_Ai_Bodyguard_Helpers::sample_text( $first['sample'] ), 'method' => Softvolt_Ai_Bodyguard_Helpers::request_method(), 'ua' => Softvolt_Ai_Bodyguard_Helpers::trim_text( Softvolt_Ai_Bodyguard_Helpers::user_agent(), 200 ), 'matches' => count( $matches ), ); self::bump_stats( 'block' === $mode ? 'blocked' : 'logged' ); Softvolt_Ai_Bodyguard_Logger::log_throttled( 'fw' . $ip . $first['rule'], 5, 'warning', 'block' === $mode ? 'firewall_block' : 'firewall_log', sprintf( /* translators: 1: reason, 2: IP address, 3: uri */ __( '%1$s from %2$s on %3$s', 'ai-guardwall' ), $reason, $ip, Softvolt_Ai_Bodyguard_Helpers::trim_text( Softvolt_Ai_Bodyguard_Helpers::request_uri(), 120 ) ), $data ); // Repeat offenders get locked out. $hits = self::count( 'svabg_fh_' . md5( $ip ), (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_hits_window', 10 ) * MINUTE_IN_SECONDS ); if ( 'block' === $mode && $hits >= (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_hits_to_block', 5 ) ) { Softvolt_Ai_Bodyguard_Lockouts::block( $ip, 'firewall', sprintf( /* translators: 1: hits, 2: number, 3: reason */ __( '%1$d blocked requests in %2$d minutes (%3$s)', 'ai-guardwall' ), $hits, (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_hits_window', 10 ), $reason ), (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_block_minutes', 60 ) ); self::bump_stats( 'lockouts' ); } if ( 'block' === $mode ) { self::deny( Softvolt_Ai_Bodyguard_Firewall::rule( $first['rule'] ), __( 'This request was blocked by the site\'s firewall.', 'ai-guardwall' ), 403, false ); } } /** * Count an event per key within a sliding window (transient based). */ public static function count( $key, $window ) { $data = get_transient( $key ); $now = time(); if ( ! is_array( $data ) || $now - (int) $data['start'] > $window ) { $data = array( 'start' => $now, 'count' => 0 ); } $data['count']++; set_transient( $key, $data, max( 60, (int) $window ) ); return (int) $data['count']; } public static function init_late_hooks() { if ( Softvolt_Ai_Bodyguard_Settings::hardening_enabled( 'rate_limiting' ) && ( (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_404', 40 ) > 0 || (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_404_crawlers', 0 ) > 0 ) ) { add_action( 'template_redirect', array( __CLASS__, 'count_404' ), 1 ); } } public static function count_404() { if ( ! is_404() || is_user_logged_in() ) { return; } $ip = Softvolt_Ai_Bodyguard_Helpers::client_ip(); if ( self::is_exempt( $ip ) ) { return; } $ua = Softvolt_Ai_Bodyguard_Helpers::user_agent(); if ( Softvolt_Ai_Bodyguard_Settings::get( 'firewall_verified_bots_exempt', 1 ) && Softvolt_Ai_Bodyguard_Blocking::claimed_bot( $ua ) && Softvolt_Ai_Bodyguard_Blocking::verified_bot( $ip, $ua ) ) { return; } $limit = (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_404', 40 ); $crawlers = (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_404_crawlers', 0 ); if ( $crawlers > 0 && Softvolt_Ai_Bodyguard_Blocking::is_crawler( $ua ) ) { $limit = $crawlers; } if ( $limit > 0 && self::count( 'svabg_404_' . md5( $ip ), (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_window', 60 ) ) > $limit ) { Softvolt_Ai_Bodyguard_Lockouts::block( $ip, 'rate', sprintf( /* translators: %d: limit */ __( 'More than %d missing pages requested a minute (scanner)', 'ai-guardwall' ), $limit ), (int) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_rate_block_minutes', 15 ) ); self::bump_stats( 'lockouts' ); self::deny( Softvolt_Ai_Bodyguard_Firewall::rule( 'rate_404' ), __( 'Too many requests. Please slow down.', 'ai-guardwall' ), 429, false ); } } /** * A rule id for deny(), with this plugin's translations loaded first. The firewall stops requests * before `init`, where WordPress 6.7+ would otherwise load them "too early" and log a notice; used * as deny()'s first argument, so it runs before the message is translated. * * @since 1.5.0 * @param string $rule Rule id. * @return string */ public static function rule( $rule ) { self::early_l10n(); return $rule; } /** * Make this plugin's strings safe to translate before `init`, where WordPress 6.7+ logs a notice * when it loads them itself. Loads the site language's translations if there are any; otherwise * gives the domain an empty translation until after_setup_theme and then hands it back, so later * strings in the same request still load the normal way. * * @since 1.5.0 */ public static function early_l10n() { static $done = false; global $wp_textdomain_registry, $l10n; if ( $done || did_action( 'after_setup_theme' ) || ! function_exists( 'get_file_data' ) || ! defined( 'SOFTVOLT_AI_BODYGUARD_PATH' ) ) { return; } $done = true; // The main file keeps its name in every edition. $header = get_file_data( SOFTVOLT_AI_BODYGUARD_PATH . 'softvoltai-ai-bodyguard.php', array( 'domain' => 'Text Domain' ) ); $domain = isset( $header['domain'] ) ? (string) $header['domain'] : ''; if ( '' === $domain || isset( $l10n[ $domain ] ) ) { return; } $locale = determine_locale(); $loaded = false; if ( $wp_textdomain_registry instanceof WP_Textdomain_Registry && $wp_textdomain_registry->has( $domain ) ) { $path = $wp_textdomain_registry->get( $domain, $locale ); $loaded = $path && load_textdomain( $domain, trailingslashit( $path ) . $domain . '-' . $locale . '.mo', $locale ); } if ( ! $loaded ) { $l10n[ $domain ] = new NOOP_Translations(); } // Hand the domain back: from after_setup_theme on WordPress loads it itself, in the signed-in // user's language when that differs from the site's. add_action( 'after_setup_theme', function () use ( $domain ) { unload_textdomain( $domain, true ); }, 0 ); } /** * Stop the request with a plain page. * * @since 1.0.0 * @param string $rule Rule id (shown as a reference). * @param string $message Message. * @param int $code HTTP status. * @param bool $log Whether to log. */ public static function deny( $rule, $message, $code = 403, $log = true ) { if ( $log ) { self::bump_stats( 'blocked' ); Softvolt_Ai_Bodyguard_Logger::log_throttled( 'deny' . Softvolt_Ai_Bodyguard_Helpers::client_ip() . $rule, 10, 'warning', 'firewall_block', sprintf( /* translators: 1: rule, 2: IP address, 3: request path */ __( 'Blocked (%1$s) from %2$s on %3$s', 'ai-guardwall' ), $rule, Softvolt_Ai_Bodyguard_Helpers::client_ip(), Softvolt_Ai_Bodyguard_Helpers::trim_text( Softvolt_Ai_Bodyguard_Helpers::request_path(), 120 ) ) ); } /** * Lets an add-on change or replace the blocked page. * * @param string $message Message. * @param string $rule Rule id. * @param int $code HTTP status. */ if ( class_exists( 'Softvolt_Ai_Bodyguard_Traffic' ) ) { Softvolt_Ai_Bodyguard_Traffic::blocked( $rule, $code ); } $message = apply_filters( 'svabg_deny_message', $message, $rule, $code ); $custom = trim( (string) Softvolt_Ai_Bodyguard_Settings::get( 'firewall_block_message', '' ) ); $unlock = in_array( $rule, array( 'lockout', 'trap', 'rate', 'rate_404', 'blocklist' ), true ) && class_exists( 'Softvolt_Ai_Bodyguard_Blocking' ) ? Softvolt_Ai_Bodyguard_Blocking::unlock_form() : ''; if ( ! headers_sent() ) { status_header( $code ); nocache_headers(); header( 'Content-Type: text/html; charset=utf-8' ); header( 'X-Robots-Tag: noindex, nofollow' ); } $site = esc_html( wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ) ); $ip = esc_html( Softvolt_Ai_Bodyguard_Helpers::client_ip() ); $ref = esc_html( $rule . '-' . substr( md5( $ip . gmdate( 'YmdH' ) ), 0, 6 ) ); echo '' . esc_html__( 'Access denied', 'ai-guardwall' ) . '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- a style tag printed by WP_Styles; the CSS is a fixed string. . Softvolt_Ai_Bodyguard_Helpers::page_style( 'svabg-block-page', 'body{margin:0;font:16px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;background:#f4f6f2;color:#151a14;display:flex;min-height:100vh;align-items:center;justify-content:center}.box{max-width:520px;margin:24px;padding:32px;background:#fff;border:1px solid #e1e4dd;border-radius:14px;box-shadow:0 6px 20px rgba(16,20,15,.05)}h1{margin:0 0 10px;font-size:22px}p{margin:0 0 12px}.meta{color:#5f675d;font-size:13px}.badge{display:inline-block;margin-bottom:14px;padding:3px 10px;border-radius:999px;background:#10140f;color:#65f545;font-size:11px;font-weight:700;letter-spacing:.06em;text-transform:uppercase}.unlock{margin:18px 0 12px;padding:14px;border-radius:10px;background:#f4f6f2;font-size:14px}.unlock span{display:flex;gap:8px;margin-top:8px}.unlock input{flex:1;min-width:0;padding:8px 10px;border:1px solid #c9cec5;border-radius:8px;font:inherit}.unlock button{padding:8px 14px;border:0;border-radius:8px;background:#10140f;color:#fff;font:inherit;cursor:pointer}' ) . '
' . 'AI GuardWall

' . esc_html__( 'Access denied', 'ai-guardwall' ) . '

' . esc_html( $message ) . '

' . ( '' !== $custom ? '

' . nl2br( esc_html( $custom ) ) . '

' : '' ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- every part was escaped when it was built above. . '

' . $site . ' · ' . esc_html__( 'Your IP:', 'ai-guardwall' ) . ' ' . $ip . ' · ' . esc_html__( 'Reference:', 'ai-guardwall' ) . ' ' . $ref . '

' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- every part was escaped when it was built above. . $unlock . '

' . esc_html__( 'If you are the site owner, unblock your IP from AI GuardWall › Firewall, or add it to the allowlist.', 'ai-guardwall' ) . '

'; exit; } /* ----------------------------- stats ------------------------------ */ public static function bump_stats( $key ) { $stats = get_option( 'svabg_fw_stats', array() ); $stats = is_array( $stats ) ? $stats : array(); $day = gmdate( 'Y-m-d' ); if ( ! isset( $stats[ $day ] ) ) { $stats[ $day ] = array( 'blocked' => 0, 'logged' => 0, 'lockouts' => 0 ); ksort( $stats ); $stats = array_slice( $stats, -31, 31, true ); } $stats[ $day ][ $key ] = ( isset( $stats[ $day ][ $key ] ) ? (int) $stats[ $day ][ $key ] : 0 ) + 1; update_option( 'svabg_fw_stats', $stats, false ); } public static function stats( $days = 7 ) { $stats = get_option( 'svabg_fw_stats', array() ); $out = array(); for ( $i = $days - 1; $i >= 0; $i-- ) { $day = gmdate( 'Y-m-d', time() - $i * DAY_IN_SECONDS ); $out[ $day ] = isset( $stats[ $day ] ) ? $stats[ $day ] : array( 'blocked' => 0, 'logged' => 0, 'lockouts' => 0 ); } return $out; } }