options; $limit = self::MAX_ROWS; $large = self::LARGE_VALUE_BYTES; $transient_like = $wpdb->esc_like( '_transient_' ) . '%'; $site_transient_like = $wpdb->esc_like( '_site_transient_' ) . '%'; /* * Read-only audit query. %i is supported by the minimum WordPress * version (6.2+) and safely represents the current installation's * options table identifier. LIKE wildcards are carried in %s values. * * Caching is intentionally not used here because the auditor needs the * current persistence state rather than a potentially stale snapshot. */ // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Intentional bounded, read-only persistence audit. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT option_name, option_value, LENGTH(option_value) AS value_size FROM %i WHERE option_name LIKE %s OR option_name LIKE %s OR LENGTH(option_value) >= %d ORDER BY value_size DESC LIMIT %d", $table, $transient_like, $site_transient_like, $large, $limit ), ARRAY_A ); if ( ! is_array( $rows ) ) { return $findings; } foreach ( $rows as $row ) { $name = isset( $row['option_name'] ) ? (string) $row['option_name'] : ''; $value = isset( $row['option_value'] ) ? (string) $row['option_value'] : ''; $size = isset( $row['value_size'] ) ? (int) $row['value_size'] : strlen( $value ); $signals = array(); $level = 'Info'; if ( $size >= self::LARGE_VALUE_BYTES ) { $signals[] = 'large option/transient (' . size_format( $size ) . ')'; } $base64_analysis = $this->inspect_base64( $value ); if ( $base64_analysis['suspicious'] ) { $signals[] = 'suspicious Base64 payload'; } if ( is_serialized( $value ) && preg_match( '/(?:<\?php|is_benign_feed_cache_size_only( $name, $value, $signals, $base64_analysis, $ioc_hits ) ) { continue; } if ( empty( $signals ) ) { continue; } if ( $base64_analysis['suspicious'] || in_array( 'serialized value with code-like content', $signals, true ) || in_array( 'code-like content', $signals, true ) ) { $level = 'Review'; } if ( ! empty( $ioc_hits ) && 'Info' === $level ) { $level = 'Review'; } $decoded_hint = $base64_analysis['preview']; $preview_source = "Option: {$name}\nSize: {$size} bytes\n\n" . $value; if ( '' !== $decoded_hint ) { $preview_source .= "\n\n[static Base64 inspection]\n" . $decoded_hint; } $findings[] = new ACHWPA_Finding( array( 'category' => 'Database', 'level' => $level, 'location' => $name, 'indicator' => implode( ', ', array_unique( array_merge( $signals, $ioc_hits ) ) ), 'explanation' => __( 'The option or transient contains characteristics that may be relevant to persistence analysis. Size or encoding alone is not treated as proof of malicious behavior.', 'aiutocomputerhelp-persistence-audit' ), 'preview' => ACHWPA_Preview::sanitize( $preview_source, 1800 ), 'artifacts' => array_merge( array( 'OPTION:' . $name ), $this->extract_artifacts( $value ) ), 'identifiers' => $this->extract_identifiers( $name . ' ' . $value ), ) ); } return $findings; } private function is_benign_feed_cache_size_only( $name, $value, array $signals, array $base64_analysis, array $ioc_hits ) { if ( ! preg_match( '/^_(?:site_)?transient_feed_[a-f0-9]{16,64}$/i', (string) $name ) ) { return false; } if ( ! empty( $base64_analysis['suspicious'] ) || ! empty( $ioc_hits ) ) { return false; } if ( 1 !== count( $signals ) || 0 !== strpos( $signals[0], 'large option/transient (' ) ) { return false; } if ( ! is_serialized( $value ) ) { return false; } $markers = 0; foreach ( array( 'channel', 'rss', 'atom', 'lastBuildDate', 'xml_base', 'generator', 'description', 'language' ) as $marker ) { if ( false !== stripos( $value, $marker ) ) { $markers++; } } if ( $markers < 3 ) { return false; } if ( preg_match( '/(?:<\?php| false, 'preview' => '', ); if ( strlen( $value ) > ACHWPA_Preview::ANALYSIS_LIMIT ) { $value = substr( $value, 0, ACHWPA_Preview::ANALYSIS_LIMIT ); } if ( ! preg_match_all( '/(? 65536 || 0 !== strlen( $candidate ) % 4 ) { continue; } $decoded = base64_decode( $candidate, true ); if ( false === $decoded || strlen( $decoded ) < 24 ) { continue; } $code_like = preg_match( '/(?:<\?php|