__( 'Open', 'ai-guardwall' ), 'ignored' => __( 'Ignored', 'ai-guardwall' ), 'false_positive' => __( 'False positive', 'ai-guardwall' ), 'quarantined' => __( 'Quarantined', 'ai-guardwall' ), 'repaired' => __( 'Repaired', 'ai-guardwall' ), 'deleted' => __( 'Deleted', 'ai-guardwall' ), 'cleaned' => __( 'Cleaned', 'ai-guardwall' ), 'resolved' => __( 'Resolved (no longer detected)', 'ai-guardwall' ), ); } public static function categories() { return array( 'core' => __( 'WordPress core', 'ai-guardwall' ), 'plugin' => __( 'Plugin', 'ai-guardwall' ), 'theme' => __( 'Theme', 'ai-guardwall' ), 'mu' => __( 'MU plugin', 'ai-guardwall' ), 'dropin' => __( 'Drop-in', 'ai-guardwall' ), 'upload' => __( 'Uploads', 'ai-guardwall' ), 'root' => __( 'Site root', 'ai-guardwall' ), 'config' => __( 'Configuration', 'ai-guardwall' ), 'other' => __( 'Other file', 'ai-guardwall' ), 'db' => __( 'Database', 'ai-guardwall' ), 'user' => __( 'Users', 'ai-guardwall' ), 'cron' => __( 'Cron', 'ai-guardwall' ), ); } public static function remediations() { return array( 'delete' => __( 'Delete the file', 'ai-guardwall' ), 'replace_official_file' => __( 'Replace with the official WordPress file', 'ai-guardwall' ), 'replace_plugin' => __( 'Replace plugin from the official source', 'ai-guardwall' ), 'replace_theme' => __( 'Replace theme from the official source', 'ai-guardwall' ), 'remove_db_record' => __( 'Remove malicious database record', 'ai-guardwall' ), 'remove_cron' => __( 'Remove malicious cron job', 'ai-guardwall' ), 'remove_js' => __( 'Remove injected JavaScript', 'ai-guardwall' ), 'remove_spam' => __( 'Remove spam content', 'ai-guardwall' ), 'remove_user' => __( 'Remove rogue user', 'ai-guardwall' ), 'fix_config' => __( 'Fix configuration / apply hardening', 'ai-guardwall' ), 'update' => __( 'Update to the latest version', 'ai-guardwall' ), 'manual_review' => __( 'Manual review required', 'ai-guardwall' ), 'no_action' => __( 'No action needed', 'ai-guardwall' ), ); } /** * Inserts or refreshes a finding keyed by fingerprint. Ignored/false-positive findings keep * their status; everything else is re-opened with the new data. * * @return int finding id */ public static function upsert( array $f ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); $now = gmdate( 'Y-m-d H:i:s' ); $fingerprint = isset( $f['fingerprint'] ) ? $f['fingerprint'] : md5( $f['category'] . '|' . $f['location'] . '|' . ( isset( $f['signature'] ) ? $f['signature'] : '' ) ); $snippet = isset( $f['snippet'] ) ? (string) $f['snippet'] : null; $meta = isset( $f['meta'] ) ? $f['meta'] : null; self::without_secrets( $snippet, $meta ); $row = array( 'scan_id' => (int) $f['scan_id'], 'fingerprint' => $fingerprint, 'category' => substr( $f['category'], 0, 32 ), 'location' => (string) $f['location'], 'line' => isset( $f['line'] ) ? (int) $f['line'] : 0, 'severity' => isset( $f['severity'] ) ? $f['severity'] : 'medium', 'confidence' => isset( $f['confidence'] ) ? max( 0, min( 100, (int) $f['confidence'] ) ) : 50, 'signature' => isset( $f['signature'] ) ? substr( $f['signature'], 0, 64 ) : '', 'title' => substr( (string) $f['title'], 0, 255 ), 'description' => (string) $f['description'], 'snippet' => $snippet, 'verdict' => isset( $f['verdict'] ) ? $f['verdict'] : 'suspicious', 'remediation' => isset( $f['remediation'] ) ? substr( $f['remediation'], 0, 48 ) : '', 'meta' => null !== $meta ? Softvolt_Ai_Bodyguard_Helpers::esc_json( $meta ) : null, 'file_hash' => isset( $f['file_hash'] ) ? $f['file_hash'] : '', 'updated_at' => $now, ); $existing = $wpdb->get_row( $wpdb->prepare( "SELECT id, status, file_hash, ai_verdict FROM %i WHERE fingerprint = %s ORDER BY id DESC LIMIT 1", $table, $fingerprint ), ARRAY_A ); if ( $existing ) { $keep = in_array( $existing['status'], array( 'ignored', 'false_positive' ), true ); if ( $keep && $existing['file_hash'] === $row['file_hash'] ) { $row['status'] = $existing['status']; } else { $row['status'] = 'open'; } if ( $existing['file_hash'] !== $row['file_hash'] ) { $row['ai_verdict'] = ''; $row['ai_confidence'] = 0; $row['ai_analysis'] = null; } $wpdb->update( $table, $row, array( 'id' => (int) $existing['id'] ) ); return (int) $existing['id']; } $row['status'] = 'open'; $row['created_at'] = $now; $wpdb->insert( $table, $row ); return (int) $wpdb->insert_id; } public static function get( $id ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %i WHERE id = %d", $table, (int) $id ), ARRAY_A ); return $row ? self::hydrate( $row ) : null; } public static function hydrate( $row ) { $row['meta_arr'] = ! empty( $row['meta'] ) ? json_decode( $row['meta'], true ) : array(); $row['ai_arr'] = ! empty( $row['ai_analysis'] ) ? json_decode( $row['ai_analysis'], true ) : array(); if ( ! is_array( $row['meta_arr'] ) ) { $row['meta_arr'] = array(); } if ( ! is_array( $row['ai_arr'] ) ) { $row['ai_arr'] = array(); } // Rows saved before 1.5.2 kept their code as it was in the file. if ( array_key_exists( 'snippet', $row ) ) { self::without_secrets( $row['snippet'], $row['meta_arr'] ); if ( ! empty( $row['meta'] ) ) { $row['meta'] = Softvolt_Ai_Bodyguard_Helpers::esc_json( $row['meta_arr'] ); } } return $row; } /** * The code kept with a finding never holds a secret: database logins, security keys and salts, passwords, * tokens, private keys and password hashes are taken out of the snippet, the indicators' excerpts and * copied database content (Helpers::redact()). The finding still shows the code that matched. * * @since 1.5.2 * @param string|null $snippet Code around the match (changed in place). * @param array|null $meta Finding meta (changed in place). */ private static function without_secrets( &$snippet, &$meta ) { if ( is_string( $snippet ) && '' !== $snippet ) { $snippet = Softvolt_Ai_Bodyguard_Helpers::redact( $snippet, false ); } if ( ! is_array( $meta ) ) { return; } if ( isset( $meta['content'] ) && is_string( $meta['content'] ) ) { $meta['content'] = Softvolt_Ai_Bodyguard_Helpers::redact( $meta['content'], false ); } // In files that hold credentials by nature (wp-config.php, .env, …) an excerpt can start in the middle of // a key, so there only the snippet's whole lines are kept. $sensitive = ! empty( $meta['rel'] ) && class_exists( 'Softvolt_Ai_Bodyguard_Analyst' ) && Softvolt_Ai_Bodyguard_Analyst::is_sensitive_file( (string) $meta['rel'] ); if ( ! empty( $meta['hits'] ) && is_array( $meta['hits'] ) ) { foreach ( $meta['hits'] as $k => $hit ) { if ( is_array( $hit ) && isset( $hit['excerpt'] ) && is_string( $hit['excerpt'] ) ) { $meta['hits'][ $k ]['excerpt'] = $sensitive ? '' : Softvolt_Ai_Bodyguard_Helpers::redact( $hit['excerpt'], false ); } } } } public static function set_status( $id, $status, $note = '' ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); $data = array( 'status' => $status, 'updated_at' => gmdate( 'Y-m-d H:i:s' ), ); if ( '' !== $note ) { $row = self::get( $id ); if ( $row ) { $meta = $row['meta_arr']; $meta['notes'][] = array( 'time' => gmdate( 'Y-m-d H:i:s' ), 'note' => $note, ); $data['meta'] = Softvolt_Ai_Bodyguard_Helpers::esc_json( $meta ); } } return false !== $wpdb->update( $table, $data, array( 'id' => (int) $id ) ); } public static function set_ai( $id, $verdict, $confidence, array $analysis ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); return false !== $wpdb->update( $table, array( 'ai_verdict' => substr( (string) $verdict, 0, 32 ), 'ai_confidence' => max( 0, min( 100, (int) $confidence ) ), 'ai_analysis' => Softvolt_Ai_Bodyguard_Helpers::esc_json( $analysis ), 'updated_at' => gmdate( 'Y-m-d H:i:s' ), ), array( 'id' => (int) $id ) ); } public static function query( $args = array() ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); $defaults = array( 'scan_id' => 0, 'status' => 'open', 'severity' => '', 'category' => '', 'search' => '', 'verdict' => '', 'ids' => array(), 'per_page' => 50, 'page' => 1, 'orderby' => 'severity', ); $a = wp_parse_args( $args, $defaults ); $where = array( '1=1' ); $vals = array(); if ( $a['scan_id'] ) { $where[] = 'scan_id = %d'; $vals[] = (int) $a['scan_id']; } if ( 'all' !== $a['status'] && '' !== $a['status'] ) { if ( 'actionable' === $a['status'] ) { $where[] = "status IN ('open')"; } elseif ( 'closed' === $a['status'] ) { $where[] = "status <> 'open'"; } else { $where[] = 'status = %s'; $vals[] = $a['status']; } } if ( '' !== $a['severity'] ) { $where[] = 'severity = %s'; $vals[] = $a['severity']; } if ( '' !== $a['category'] ) { $where[] = 'category = %s'; $vals[] = $a['category']; } if ( '' !== $a['verdict'] ) { if ( 'ai_confirmed' === $a['verdict'] ) { $where[] = "ai_verdict = 'confirmed_malware'"; } elseif ( 'ai_none' === $a['verdict'] ) { $where[] = "( ai_verdict IS NULL OR ai_verdict = '' )"; } else { $where[] = 'verdict = %s'; $vals[] = $a['verdict']; } } if ( '' !== $a['search'] ) { $where[] = '(location LIKE %s OR title LIKE %s)'; $like = '%' . $wpdb->esc_like( $a['search'] ) . '%'; $vals[] = $like; $vals[] = $like; } if ( ! empty( $a['ids'] ) ) { $ids = array_map( 'intval', (array) $a['ids'] ); $where[] = 'id IN (' . implode( ',', array_fill( 0, count( $ids ), '%d' ) ) . ')'; $vals = array_merge( $vals, $ids ); } $order = "FIELD(severity,'critical','high','medium','low','info'), confidence DESC, id DESC"; if ( 'newest' === $a['orderby'] ) { $order = 'id DESC'; } elseif ( 'location' === $a['orderby'] ) { $order = 'location ASC'; } $where_sql = implode( ' AND ', $where ); $per_page = max( 1, min( 1000, (int) $a['per_page'] ) ); $offset = max( 0, ( (int) $a['page'] - 1 ) * $per_page ); $count_sql = $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE {$where_sql}", array_merge( array( $table ), $vals ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The interpolated part holds fixed column names and placeholders only; every value goes through prepare(). $rows_sql = $wpdb->prepare( "SELECT * FROM %i WHERE {$where_sql} ORDER BY {$order} LIMIT %d, %d", array_merge( array( $table ), $vals, array( $offset, $per_page ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The interpolated part holds fixed column names, a whitelisted ORDER BY and placeholders only; every value goes through prepare(). $rows = $wpdb->get_results( $rows_sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared return array( 'total' => (int) $wpdb->get_var( $count_sql ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared 'rows' => array_map( array( __CLASS__, 'hydrate' ), $rows ? $rows : array() ), ); } /** * Severity counts for open findings of a scan (or all open findings when scan_id = 0). */ public static function counts( $scan_id = 0, $status = 'open' ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); $sql = "SELECT severity, COUNT(*) AS c FROM %i WHERE status = %s"; $vals = array( $table, $status ); if ( $scan_id ) { $sql .= ' AND scan_id = %d'; $vals[] = (int) $scan_id; } $sql .= ' GROUP BY severity'; $rows = $wpdb->get_results( $wpdb->prepare( $sql, $vals ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $out = array_fill_keys( Softvolt_Ai_Bodyguard_Helpers::severities(), 0 ); foreach ( (array) $rows as $r ) { $out[ $r['severity'] ] = (int) $r['c']; } $out['total'] = array_sum( $out ); return $out; } public static function count_by( $field, $scan_id = 0 ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); $field = in_array( $field, array( 'category', 'status', 'verdict', 'ai_verdict' ), true ) ? $field : 'category'; $sql = 'SELECT %i AS k, COUNT(*) AS c FROM %i WHERE status = %s'; $vals = array( $field, $table, 'open' ); if ( $scan_id ) { $sql .= ' AND scan_id = %d'; $vals[] = (int) $scan_id; } $sql .= ' GROUP BY %i'; $vals[] = $field; $rows = $wpdb->get_results( $wpdb->prepare( $sql, $vals ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared -- $sql is a fixed string with placeholders only. $out = array(); foreach ( (array) $rows as $r ) { $out[ $r['k'] ] = (int) $r['c']; } return $out; } /** * After a scan: open findings not refreshed by this scan are marked resolved. */ public static function resolve_stale( $scan_id ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); return $wpdb->query( $wpdb->prepare( "UPDATE %i SET status = 'resolved', updated_at = %s WHERE status = 'open' AND scan_id <> %d", $table, gmdate( 'Y-m-d H:i:s' ), (int) $scan_id ) ); } public static function delete( $id ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'findings' ); return $wpdb->delete( $table, array( 'id' => (int) $id ) ); } /** * Human-friendly rows for reports/exports. */ public static function export_rows( $scan_id = 0, $status = 'open' ) { $res = self::query( array( 'scan_id' => $scan_id, 'status' => $status, 'per_page' => 1000, ) ); $rows = array(); foreach ( $res['rows'] as $f ) { $ai = $f['ai_arr']; $rows[] = array( 'file_or_table' => $f['location'], 'line' => $f['line'] ? (int) $f['line'] : '', 'category' => $f['category'], 'severity' => $f['severity'], 'confidence' => (int) $f['confidence'], 'verdict' => $f['verdict'], 'title' => $f['title'], 'why_suspicious' => $f['description'], 'recommendation' => $f['remediation'], 'ai_verdict' => $f['ai_verdict'], 'ai_confidence' => (int) $f['ai_confidence'], 'ai_reason' => isset( $ai['why_malicious'] ) ? $ai['why_malicious'] : '', 'ai_remediation' => isset( $ai['remediation'] ) ? $ai['remediation'] : '', 'how_to_fix' => Softvolt_Ai_Bodyguard_Fixes::short( $f ), 'status' => $f['status'], ); } return $rows; } }