200 ] ); } wp_safe_redirect( home_url() ); exit; } // Redirect unauthenticated wp-admin requests to home if ( is_admin() && ! is_user_logged_in() && ! defined( 'DOING_AJAX' ) ) { wp_safe_redirect( home_url() ); exit; } } add_filter( 'login_url', 'aio_custom_login_url', 10, 3 ); function aio_custom_login_url( $login_url, $redirect, $force_reauth ) { global $aio_hide_login_url; $login_url = home_url( $aio_hide_login_url ); if ( ! empty( $redirect ) ) { $login_url = add_query_arg( 'redirect_to', rawurlencode( $redirect ), $login_url ); } if ( $force_reauth ) { $login_url = add_query_arg( 'reauth', '1', $login_url ); } return $login_url; } add_filter( 'site_url', 'aio_custom_site_url_login', 10, 4 ); function aio_custom_site_url_login( $url, $path, $scheme, $blog_id ) { global $aio_hide_login_url; if ( $path === 'wp-login.php' || strpos( (string) $path, 'wp-login.php?' ) === 0 ) { return home_url( $aio_hide_login_url . substr( (string) $path, 12 ) ); } return $url; } } /* ========================================================================== 2. ANTI-SPAM (CAPTCHA: Cloudflare Turnstile & Google reCAPTCHA v2) ========================================================================== */ $aio_captcha_provider = aio_get_option( 'optCaptchaProvider', 'turnstile' ); $aio_captcha_site = aio_get_option( 'optCaptchaSiteKey', '' ); $aio_captcha_secret_raw = aio_get_option( 'optCaptchaSecretKey', '' ); $aio_captcha_secret = ! empty( $aio_captcha_secret_raw ) ? AIO_Crypto::decrypt( $aio_captcha_secret_raw ) : ''; $aio_captcha_login = ! empty( aio_get_option( 'optCaptchaForLogin' ) ); $aio_captcha_cf7 = ! empty( aio_get_option( 'optCaptchaForCF7' ) ); if ( ! empty( $aio_captcha_site ) && ! empty( $aio_captcha_secret ) ) { function aio_render_captcha_html() { global $aio_captcha_provider, $aio_captcha_site; if ( $aio_captcha_provider === 'turnstile' ) { // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent, WordPress.WP.EnqueuedResources.NonEnqueuedScript echo ''; echo '
'; } else { // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent, WordPress.WP.EnqueuedResources.NonEnqueuedScript echo ''; echo '
'; } } function aio_verify_captcha_response( $response_field ) { global $aio_captcha_provider, $aio_captcha_secret; $response = isset( $_POST[ $response_field ] ) ? sanitize_text_field( wp_unslash( $_POST[ $response_field ] ) ) : ''; if ( empty( $response ) ) { return false; } $verify_url = ( $aio_captcha_provider === 'turnstile' ) ? 'https://challenges.cloudflare.com/turnstile/v0/siteverify' : 'https://www.google.com/recaptcha/api/siteverify'; $api_response = wp_safe_remote_post( $verify_url, [ 'timeout' => 10, 'body' => [ 'secret' => $aio_captcha_secret, 'response' => $response, 'remoteip' => aio_get_client_ip(), ], ] ); if ( is_wp_error( $api_response ) ) { return false; } $result = json_decode( wp_remote_retrieve_body( $api_response ) ); return isset( $result->success ) && $result->success === true; } if ( $aio_captcha_login ) { add_action( 'login_form', 'aio_render_captcha_html' ); add_action( 'register_form', 'aio_render_captcha_html' ); add_action( 'lostpassword_form', 'aio_render_captcha_html' ); add_filter( 'wp_authenticate_user', 'aio_verify_captcha_login', 10, 2 ); function aio_verify_captcha_login( $user, $password ) { global $aio_captcha_provider; if ( ! isset( $_POST['log'] ) ) { return $user; } $field = ( $aio_captcha_provider === 'turnstile' ) ? 'cf-turnstile-response' : 'g-recaptcha-response'; if ( ! aio_verify_captcha_response( $field ) ) { return new WP_Error( 'captcha_error', '' . esc_html__( 'Error:', 'aio-tools' ) . ' ' . esc_html__( 'CAPTCHA verification failed.', 'aio-tools' ) ); } return $user; } add_filter( 'registration_errors', 'aio_verify_captcha_register', 10, 3 ); function aio_verify_captcha_register( $errors, $sanitized_user_login, $user_email ) { global $aio_captcha_provider; $field = ( $aio_captcha_provider === 'turnstile' ) ? 'cf-turnstile-response' : 'g-recaptcha-response'; if ( ! aio_verify_captcha_response( $field ) ) { $errors->add( 'captcha_error', '' . esc_html__( 'Error:', 'aio-tools' ) . ' ' . esc_html__( 'CAPTCHA verification failed.', 'aio-tools' ) ); } return $errors; } add_action( 'lostpassword_post', 'aio_verify_captcha_lostpassword', 10, 1 ); function aio_verify_captcha_lostpassword( $errors ) { global $aio_captcha_provider; $field = ( $aio_captcha_provider === 'turnstile' ) ? 'cf-turnstile-response' : 'g-recaptcha-response'; if ( ! aio_verify_captcha_response( $field ) ) { if ( is_wp_error( $errors ) ) { $errors->add( 'captcha_error', '' . esc_html__( 'Error:', 'aio-tools' ) . ' ' . esc_html__( 'CAPTCHA verification failed.', 'aio-tools' ) ); } else { wp_die( esc_html__( 'CAPTCHA verification failed.', 'aio-tools' ), esc_html__( 'Error', 'aio-tools' ), [ 'response' => 403, 'back_link' => true ] ); } } } } if ( $aio_captcha_cf7 && defined( 'WPCF7_VERSION' ) ) { add_filter( 'wpcf7_form_elements', 'aio_cf7_add_captcha' ); function aio_cf7_add_captcha( $elements ) { ob_start(); aio_render_captcha_html(); $captcha_html = ob_get_clean(); if ( preg_match( '/(<(?:input|button)[^>]+type=["\']submit["\'][^>]*>)/i', $elements ) ) { $elements = preg_replace( '/(<(?:input|button)[^>]+type=["\']submit["\'][^>]*>)/i', $captcha_html . '$1', $elements, 1 ); } else { $elements .= $captcha_html; } return $elements; } add_filter( 'wpcf7_validate', 'aio_cf7_verify_captcha', 20, 2 ); function aio_cf7_verify_captcha( $result, $tags ) { global $aio_captcha_provider; $field = ( $aio_captcha_provider === 'turnstile' ) ? 'cf-turnstile-response' : 'g-recaptcha-response'; if ( ! aio_verify_captcha_response( $field ) ) { $result->invalidate( new WPCF7_FormTag( [ 'name' => 'captcha', 'type' => 'captcha', 'basetype' => 'captcha' ] ), esc_html__( 'Please verify CAPTCHA.', 'aio-tools' ) ); } return $result; } } } /* ========================================================================== 3. TWO-FACTOR AUTHENTICATION (Post-Password 2-Step Challenge + Backup Codes) ========================================================================== */ if ( ! function_exists( 'aio_render_2fa_interstitial_page' ) ) { function aio_render_2fa_interstitial_page( $challenge_token, $error_msg = '' ) { if ( ! defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); } nocache_headers(); header( 'Content-Type: text/html; charset=UTF-8' ); ?> > <?php echo esc_html__( 'Two-Factor Authentication', 'aio-tools' ) . ' ‹ ' . esc_html( get_bloginfo( 'name' ) ); ?>

←
(int) $user->ID, 'remember' => (bool) $remember, 'redirect_to' => ! empty( $redirect_to ) ? esc_url_raw( $redirect_to ) : admin_url(), 'ip' => aio_get_client_ip(), 'attempts' => 0, ], 5 * MINUTE_IN_SECONDS ); aio_render_2fa_interstitial_page( $challenge_token ); } } add_filter( 'authenticate', 'aio_intercept_2fa_after_password', 50, 3 ); function aio_intercept_2fa_after_password( $user, $username, $password ) { if ( ! ( $user instanceof WP_User ) ) { return $user; } $is_2fa_enabled = get_user_meta( $user->ID, '_aio_2fa_enabled', true ); if ( ! $is_2fa_enabled ) { return $user; } $raw_secret = get_user_meta( $user->ID, '_aio_2fa_secret', true ); $secret = ! empty( $raw_secret ) ? AIO_Crypto::decrypt( (string) $raw_secret ) : ''; if ( empty( $secret ) ) { return $user; } // Block non-interactive XML-RPC / REST password logins when 2FA is active if ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return new WP_Error( 'aio_2fa_required', __( 'Two-Factor Authentication is enabled for this account.', 'aio-tools' ) ); } $remember = ! empty( $_POST['rememberme'] ); $redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_REQUEST['redirect_to'] ) ) : admin_url(); aio_start_2fa_challenge_for_user( $user, $redirect_to, $remember ); return new WP_Error( 'aio_2fa_pending', __( '2FA verification required.', 'aio-tools' ) ); } add_action( 'init', 'aio_handle_2fa_challenge_submission', 1 ); function aio_handle_2fa_challenge_submission() { if ( ! isset( $_POST['aio_2fa_action'] ) || $_POST['aio_2fa_action'] !== 'verify' ) { return; } if ( ! isset( $_POST['aio_2fa_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['aio_2fa_nonce'] ) ), 'aio_2fa_challenge_verify' ) ) { wp_die( esc_html__( 'Security session expired. Please log in again.', 'aio-tools' ), esc_html__( 'Error', 'aio-tools' ), [ 'response' => 403, 'back_link' => true ] ); } $challenge_token = isset( $_POST['aio_2fa_token'] ) ? sanitize_text_field( wp_unslash( $_POST['aio_2fa_token'] ) ) : ''; $code = isset( $_POST['aio_2fa_code'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['aio_2fa_code'] ) ) ) : ''; $transient_key = 'aio_2fa_challenge_' . hash( 'sha256', $challenge_token ); $challenge = get_transient( $transient_key ); if ( empty( $challenge_token ) || ! is_array( $challenge ) || empty( $challenge['user_id'] ) ) { wp_safe_redirect( wp_login_url() ); exit; } $user = get_user_by( 'id', (int) $challenge['user_id'] ); if ( ! $user ) { delete_transient( $transient_key ); wp_safe_redirect( wp_login_url() ); exit; } $raw_secret = get_user_meta( $user->ID, '_aio_2fa_secret', true ); $secret = ! empty( $raw_secret ) ? AIO_Crypto::decrypt( (string) $raw_secret ) : ''; $verified = false; $matched_slice = null; // 1. Check 6-digit TOTP with time-slice replay protection if ( AIO_TOTP::verify_code( $secret, $code, 1, $matched_slice ) ) { $last_slice = (int) get_user_meta( $user->ID, '_aio_2fa_last_slice', true ); if ( $matched_slice > $last_slice ) { update_user_meta( $user->ID, '_aio_2fa_last_slice', (int) $matched_slice ); $verified = true; } else { aio_render_2fa_interstitial_page( $challenge_token, __( 'This 2FA code was already used. Please wait for the next 6-digit code.', 'aio-tools' ) ); } } elseif ( AIO_TOTP::verify_and_consume_backup_code( $user->ID, $code ) ) { // 2. Fallback: One-time 8-character recovery code $verified = true; } if ( ! $verified ) { $challenge['attempts'] = (int) ( $challenge['attempts'] ?? 0 ) + 1; if ( $challenge['attempts'] >= 5 ) { delete_transient( $transient_key ); do_action( 'wp_login_failed', $user->user_login ); wp_safe_redirect( wp_login_url() ); exit; } set_transient( $transient_key, $challenge, 5 * MINUTE_IN_SECONDS ); aio_render_2fa_interstitial_page( $challenge_token, __( 'Invalid authentication or recovery code. Please try again.', 'aio-tools' ) ); } delete_transient( $transient_key ); wp_clear_auth_cookie(); wp_set_current_user( $user->ID ); wp_set_auth_cookie( $user->ID, ! empty( $challenge['remember'] ) ); do_action( 'wp_login', $user->user_login, $user ); wp_safe_redirect( ! empty( $challenge['redirect_to'] ) ? $challenge['redirect_to'] : admin_url() ); exit; } /* ========================================================================== 4. LIMIT LOGIN ATTEMPTS ========================================================================== */ if ( ! empty( aio_get_option( 'optEnableLimitLogin' ) ) ) { add_filter( 'authenticate', 'aio_check_login_limit', 30, 3 ); function aio_check_login_limit( $user, $username, $password ) { if ( empty( $username ) && empty( $password ) ) { return $user; } $ip = aio_get_client_ip(); $lockout_key = 'aio_lockout_' . md5( $ip ); if ( get_transient( $lockout_key ) ) { $duration = max( 1, (int) aio_get_option( 'optLimitLoginDuration', 15 ) ); /* translators: %d: lockout duration in minutes */ return new WP_Error( 'too_many_retries', sprintf( __( 'ERROR: Too many failed login attempts. Please try again in %d minutes.', 'aio-tools' ), $duration ) ); } return $user; } add_action( 'wp_login_failed', 'aio_login_failed' ); function aio_login_failed( $username ) { $ip = aio_get_client_ip(); $attempts_key = 'aio_login_attempts_' . md5( $ip ); $lockout_key = 'aio_lockout_' . md5( $ip ); $max_attempts = max( 1, (int) aio_get_option( 'optLimitLoginMax', 3 ) ); $duration = max( 1, (int) aio_get_option( 'optLimitLoginDuration', 15 ) ); $attempts = (int) get_transient( $attempts_key ) + 1; if ( $attempts >= $max_attempts ) { set_transient( $lockout_key, true, $duration * MINUTE_IN_SECONDS ); delete_transient( $attempts_key ); global $wpdb; $lockouts_table = $wpdb->prefix . 'aio_lockouts'; // Prune expired lockouts to keep table clean $wpdb->query( $wpdb->prepare( "DELETE FROM {$lockouts_table} WHERE expires_at < %s", current_time( 'mysql' ) ) ); $wpdb->insert( $lockouts_table, [ 'ip_address' => substr( $ip, 0, 45 ), 'reason' => 'Limit Login Attempts', 'blocked_at' => current_time( 'mysql' ), 'expires_at' => wp_date( 'Y-m-d H:i:s', time() + ( $duration * MINUTE_IN_SECONDS ) ), ], [ '%s', '%s', '%s', '%s' ] ); } else { set_transient( $attempts_key, $attempts, 12 * HOUR_IN_SECONDS ); } } add_action( 'wp_login', 'aio_login_success', 10, 2 ); function aio_login_success( $user_login, $user ) { $ip = aio_get_client_ip(); delete_transient( 'aio_login_attempts_' . md5( $ip ) ); } } /* ========================================================================== 5. AUDIT LOG (System Activity + Automatic 500-Row Retention) ========================================================================== */ if ( ! empty( aio_get_option( 'optEnableAuditLog' ) ) ) { class AIO_Audit_Log { public function __construct() { add_action( 'wp_login', [ $this, 'log_login' ], 10, 2 ); add_action( 'wp_login_failed', [ $this, 'log_login_failed' ] ); add_action( 'deleted_post', [ $this, 'log_deleted_post' ] ); add_action( 'activated_plugin', [ $this, 'log_activated_plugin' ] ); add_action( 'deactivated_plugin', [ $this, 'log_deactivated_plugin' ] ); } private function insert_log( $event, $details, $user_id = null, $user_login = null ) { global $wpdb; $table_name = $wpdb->prefix . 'aio_audit_logs'; if ( $user_id === null ) { $user = wp_get_current_user(); $user_id = $user->ID; $user_login = $user->user_login; } if ( empty( $user_login ) ) { $user_login = 'System/Guest'; } $ip = aio_get_client_ip(); $wpdb->insert( $table_name, [ 'time' => current_time( 'mysql' ), 'user_id' => (int) $user_id, 'user_login' => substr( sanitize_text_field( (string) $user_login ), 0, 60 ), 'event' => substr( sanitize_text_field( (string) $event ), 0, 100 ), 'details' => sanitize_text_field( (string) $details ), 'ip_address' => substr( $ip, 0, 45 ), ], [ '%s', '%d', '%s', '%s', '%s', '%s' ] ); // Keep only the latest 500 audit events to prevent unbounded DB bloat $cutoff_id = $wpdb->get_var( "SELECT id FROM {$table_name} ORDER BY id DESC LIMIT 1 OFFSET 500" ); if ( $cutoff_id ) { $wpdb->query( $wpdb->prepare( "DELETE FROM {$table_name} WHERE id <= %d", (int) $cutoff_id ) ); } } public function log_login( $user_login, $user ) { $this->insert_log( 'User Logged In', 'User logged in successfully.', $user->ID, $user_login ); } public function log_login_failed( $username ) { $clean_user = sanitize_user( (string) $username ); $this->insert_log( 'Failed Login', "Failed login attempt for username: {$clean_user}", 0, $clean_user ?: 'Unknown' ); } public function log_deleted_post( $post_id ) { $post = get_post( $post_id ); if ( $post && $post->post_type !== 'revision' ) { $this->insert_log( 'Post Deleted', "Deleted post #{$post_id}: {$post->post_title} ({$post->post_type})" ); } } public function log_activated_plugin( $plugin ) { $this->insert_log( 'Plugin Activated', "Activated plugin: {$plugin}" ); } public function log_deactivated_plugin( $plugin ) { $this->insert_log( 'Plugin Deactivated', "Deactivated plugin: {$plugin}" ); } } new AIO_Audit_Log(); } /* ========================================================================== 6. SYSTEM HARDENING (Disable XML-RPC & Disable File Editing) ========================================================================== */ if ( ! empty( aio_get_option( 'opt-perf-disable-xmlrpc' ) ) ) { add_filter( 'xmlrpc_enabled', '__return_false' ); add_filter( 'xmlrpc_methods', '__return_empty_array' ); remove_action( 'wp_head', 'rsd_link' ); } if ( ! empty( aio_get_option( 'optEnableDisableFileEdit' ) ) ) { if ( ! defined( 'DISALLOW_FILE_EDIT' ) ) { define( 'DISALLOW_FILE_EDIT', true ); } add_filter( 'map_meta_cap', 'aio_disable_file_edit_caps', 10, 2 ); function aio_disable_file_edit_caps( $caps, $cap ) { if ( in_array( $cap, [ 'edit_files', 'edit_plugins', 'edit_themes' ], true ) ) { $caps[] = 'do_not_allow'; } return $caps; } } /* ========================================================================== 7. FILE INTEGRITY MONITOR ========================================================================== */ if ( ! function_exists( 'aio_scan_core_files' ) ) { function aio_scan_core_files() { global $wp_version; $locale = get_locale(); $api_url = 'https://api.wordpress.org/core/checksums/1.0/?version=' . rawurlencode( $wp_version ) . '&locale=' . rawurlencode( $locale ); $response = wp_safe_remote_get( $api_url, [ 'timeout' => 15 ] ); if ( is_wp_error( $response ) ) { return [ 'success' => false, 'message' => __( 'Cannot connect to WordPress API.', 'aio-tools' ) ]; } $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $data['checksums'] ) || ! is_array( $data['checksums'] ) ) { $api_url = 'https://api.wordpress.org/core/checksums/1.0/?version=' . rawurlencode( $wp_version ) . '&locale=en_US'; $response = wp_safe_remote_get( $api_url, [ 'timeout' => 15 ] ); $data = json_decode( wp_remote_retrieve_body( $response ), true ); } if ( empty( $data['checksums'] ) || ! is_array( $data['checksums'] ) ) { return [ 'success' => false, 'message' => __( 'No checksum data found for this version.', 'aio-tools' ) ]; } $modified_files = []; foreach ( $data['checksums'] as $file => $expected_md5 ) { if ( strpos( $file, 'wp-content/' ) === 0 || $file === 'wp-config-sample.php' ) { continue; } $local_file = ABSPATH . $file; if ( ! file_exists( $local_file ) ) { $modified_files[] = [ 'file' => $file, 'status' => 'missing' ]; continue; } if ( md5_file( $local_file ) !== $expected_md5 ) { $modified_files[] = [ 'file' => $file, 'status' => 'modified' ]; } } return [ 'success' => true, 'modified' => $modified_files ]; } } /* ========================================================================== 8. UPLOADS DIRECTORY PROTECTION (.htaccess with non-destructive markers) ========================================================================== */ if ( ! function_exists( 'aio_sync_uploads_protection_htaccess' ) ) { function aio_sync_uploads_protection_htaccess( $enabled ) { if ( ! function_exists( 'insert_with_markers' ) ) { require_once ABSPATH . 'wp-admin/includes/misc.php'; } $upload_dir = wp_upload_dir(); if ( empty( $upload_dir['basedir'] ) || ! is_dir( $upload_dir['basedir'] ) ) { return; } $htaccess_file = trailingslashit( $upload_dir['basedir'] ) . '.htaccess'; // Clean up legacy un-markered file if created by v3.0.8 if ( file_exists( $htaccess_file ) ) { $existing = @file_get_contents( $htaccess_file ); if ( is_string( $existing ) && trim( $existing ) === "\ndeny from all\n" ) { @unlink( $htaccess_file ); } } if ( $enabled ) { $rules = [ '', ' ', ' Require all denied', ' ', ' ', ' Order allow,deny', ' Deny from all', ' ', '', ]; @insert_with_markers( $htaccess_file, 'AIO Tools Upload Protection', $rules ); } elseif ( file_exists( $htaccess_file ) ) { @insert_with_markers( $htaccess_file, 'AIO Tools Upload Protection', [] ); } } } add_action( 'aio_tools_settings_updated', function ( $new_settings ) { aio_sync_uploads_protection_htaccess( ! empty( $new_settings['optEnableUploadProtection'] ) ); }, 10, 1 ); add_action( 'admin_init', function () { if ( ! empty( aio_get_option( 'optEnableUploadProtection' ) ) ) { $upload_dir = wp_upload_dir(); $htaccess_file = ! empty( $upload_dir['basedir'] ) ? trailingslashit( $upload_dir['basedir'] ) . '.htaccess' : ''; if ( $htaccess_file && ( ! file_exists( $htaccess_file ) || strpos( (string) @file_get_contents( $htaccess_file ), 'BEGIN AIO Tools Upload Protection' ) === false ) ) { aio_sync_uploads_protection_htaccess( true ); } } } ); /* ========================================================================== 9. WEB APPLICATION FIREWALL (Lightweight Application-Layer WAF) ========================================================================== */ if ( ! empty( aio_get_option( 'optEnableWAF' ) ) ) { add_action( 'plugins_loaded', 'aio_waf_intercept', 0 ); function aio_waf_intercept() { if ( is_user_logged_in() && current_user_can( 'edit_posts' ) ) { return; } $patterns = [ '/\bunion\s+(?:all\s+)?select\b/i', '/\binformation_schema\b/i', '/(?:benchmark|sleep)\s*\(\s*\d+/i', '/<\s*script\b/i', '/javascript\s*:/i', '/\bon(?:error|load|mouseover|focus)\s*=/i', '/(?:base64_decode|eval|gzinflate)\s*\(/i', '/(?:\.\.\/|\.\.\\\\){2,}/', '/(?:\/etc\/passwd|wp-config\.php)/i', '/(?:php|expect|data):\/\//i', ]; $inspect = function ( $input ) use ( &$inspect, $patterns ) { if ( is_array( $input ) ) { foreach ( $input as $v ) { $inspect( $v ); } return; } $decoded = rawurldecode( rawurldecode( (string) $input ) ); foreach ( $patterns as $regex ) { if ( preg_match( $regex, $decoded ) ) { status_header( 403 ); nocache_headers(); die( 'AIO Tools WAF: Malicious request blocked.' ); } } }; if ( ! empty( $_GET ) ) { $inspect( $_GET ); } if ( ! empty( $_POST ) ) { $inspect( $_POST ); } if ( ! empty( $_COOKIE ) ) { foreach ( $_COOKIE as $c_key => $c_val ) { if ( strpos( (string) $c_key, 'wordpress_' ) === 0 || strpos( (string) $c_key, 'wp-settings-' ) === 0 ) { continue; } $inspect( $c_val ); } } } }