$hooks ) { if ( ! is_array( $hooks ) ) { continue; } foreach ( $hooks as $hook => $instances ) { $signals = array(); $hook_string = (string) $hook; if ( preg_match( '/(?:^|[_-])[a-f0-9]{8,}(?:$|[_-])/i', $hook_string ) ) { $signals[] = 'random-or-hex hook component'; } $known_core_hook = $this->is_known_core_hook( $hook_string ); if ( ! $known_core_hook && preg_match( '/^(?:wp|wordpress)[_-](?:core|cron|cache|health|update)[_-].+/i', $hook_string ) ) { $signals[] = 'core-imitating hook name'; } $ioc_hits = ACHWPA_IOC::matches( $hook_string ); if ( ! empty( $ioc_hits ) ) { $signals[] = 'known indicator'; } $no_callback = ! has_action( $hook_string ); if ( $no_callback && ! $known_core_hook ) { $signals[] = 'no callback registered at audit time'; } foreach ( (array) $instances as $instance ) { $schedule = isset( $instance['schedule'] ) ? (string) $instance['schedule'] : ''; $interval = isset( $instance['interval'] ) ? (int) $instance['interval'] : 0; $args = isset( $instance['args'] ) ? $instance['args'] : array(); $instance_signals = $signals; if ( $interval > 0 && $interval < 300 ) { $instance_signals[] = 'very frequent recurrence'; } if ( empty( $instance_signals ) ) { continue; } $level = 'Info'; if ( count( $instance_signals ) >= 2 || ! empty( $ioc_hits ) ) { $level = 'Review'; } $preview = sprintf( "Hook: %s\nSchedule: %s\nInterval: %s seconds\nNext run: %s\nArguments: %s", $hook_string, '' !== $schedule ? $schedule : 'single event', $interval > 0 ? (string) $interval : 'n/a', gmdate( 'c', (int) $timestamp ), wp_json_encode( $args ) ); $findings[] = new ACHWPA_Finding( array( 'category' => 'Cron', 'level' => $level, 'location' => $hook_string, 'indicator' => implode( ', ', array_unique( array_merge( $instance_signals, $ioc_hits ) ) ), 'explanation' => __( 'The scheduled event contains characteristics relevant to persistence analysis. A missing callback may also be caused by normal plugin lifecycle behavior, so it is treated as contextual evidence.', 'aiutocomputerhelp-persistence-audit' ), 'preview' => ACHWPA_Preview::sanitize( $preview, 1200 ), 'artifacts' => array_merge( array( 'HOOK:' . $hook_string ), $this->artifacts_from_args( $args ) ), 'identifiers' => $this->extract_identifiers( $hook_string . ' ' . wp_json_encode( $args ) ), ) ); } } } return $findings; } private function artifacts_from_args( $args ) { $text = wp_json_encode( $args ); if ( ! is_string( $text ) ) { return array(); } $out = array(); if ( preg_match_all( '/[A-Za-z0-9_.-]{3,}\.php\b/', $text, $matches ) ) { foreach ( $matches[0] as $file ) { $out[] = 'FILE:' . basename( $file ); } } if ( preg_match_all( '/(?:_site_transient_|_transient_|wp_cache_|wp_compat_|_wp_)[A-Za-z0-9_.-]{3,}/i', $text, $matches ) ) { foreach ( $matches[0] as $name ) { $out[] = 'OPTION:' . $name; } } return array_slice( array_values( array_unique( $out ) ), 0, 30 ); } private function extract_identifiers( $text ) { $ids = array(); if ( preg_match_all( '/\b[a-f0-9]{6,32}\b/i', $text, $matches ) ) { foreach ( $matches[0] as $candidate ) { if ( ! ctype_digit( $candidate ) ) { $ids[] = strtolower( $candidate ); } } } return array_slice( array_values( array_unique( $ids ) ), 0, 30 ); } private function is_known_core_hook( $hook ) { static $core_hooks = array( 'wp_version_check', 'wp_update_plugins', 'wp_update_themes', 'wp_maybe_auto_update', 'wp_scheduled_delete', 'delete_expired_transients', 'wp_scheduled_auto_draft_delete', 'recovery_mode_clean_expired_keys', 'wp_site_health_scheduled_check', 'wp_privacy_delete_old_export_files', 'wp_update_user_counts', 'wp_delete_temp_updater_backups', 'wp_scheduled_update_network_counts', ); return in_array( (string) $hook, $core_hooks, true ); } }