delete( $table, array( 'scan_id' => $scan_id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $buffer = array(); $failed = false; $add = function ( $path ) use ( &$buffer, &$failed, $wpdb, $table, $scan_id ) { if ( null !== $path ) { $buffer[] = $path; } if ( ! $buffer || ( null !== $path && count( $buffer ) < 300 ) ) { return; } $args = array(); foreach ( $buffer as $p ) { $args[] = $scan_id; $args[] = $p; } $values = implode( ', ', array_fill( 0, count( $buffer ), '(%d, %s)' ) ); if ( false === $wpdb->query( $wpdb->prepare( "INSERT INTO %i (scan_id, path) VALUES {$values}", array_merge( array( $table ), $args ) ) ) ) { // 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 (%d, %s) placeholder groups only; every value goes through prepare(). $failed = true; } $buffer = array(); }; $skip = self::skip_dirs(); // WordPress itself and every folder it may have moved elsewhere (content, plugins, must-use plugins, // themes, uploads). A folder inside another one is reached only once (realpath() below). $roots = array_values( array_unique( array_filter( array_reverse( Softvolt_Ai_Bodyguard_Helpers::site_dirs() ) ) ) ); if ( function_exists( 'search_theme_directories' ) && ! empty( $GLOBALS['wp_theme_directories'] ) ) { foreach ( (array) $GLOBALS['wp_theme_directories'] as $theme_dir ) { $roots[] = untrailingslashit( wp_normalize_path( $theme_dir ) ); } $roots = array_values( array_unique( $roots ) ); } $count = 0; $visited = array(); foreach ( $roots as $root ) { $stack = array( $root ); while ( $stack ) { $dir = array_pop( $stack ); $real = realpath( $dir ); if ( false === $real || isset( $visited[ $real ] ) ) { continue; } $visited[ $real ] = true; $rel_dir = Softvolt_Ai_Bodyguard_Helpers::rel_path( $dir ); foreach ( $skip as $pattern ) { if ( self::dir_matches( $rel_dir, $pattern ) ) { continue 2; } } $entries = @scandir( $dir ); if ( ! is_array( $entries ) ) { continue; } foreach ( $entries as $entry ) { if ( '.' === $entry || '..' === $entry ) { continue; } $path = $dir . '/' . $entry; if ( is_dir( $path ) ) { $stack[] = $path; } elseif ( is_file( $path ) ) { $add( Softvolt_Ai_Bodyguard_Helpers::rel_path( $path ) ); $count++; } } } } // wp-config.php one level up is common; include it. $site = Softvolt_Ai_Bodyguard_Helpers::site_dirs()['']; $up = dirname( $site ) . '/wp-config.php'; if ( file_exists( $up ) && ! file_exists( $site . '/wp-config.php' ) ) { $add( $up ); $count++; } $add( null ); if ( $failed ) { return new WP_Error( 'svabg_list', sprintf( /* translators: %s: database error */ __( 'The list of files to scan could not be saved in the database (%s).', 'ai-guardwall' ), $wpdb->last_error ) ); } return $count; } /** * Files the owner excluded (Scan settings › Exclude): wildcards on the site-relative path, * or on the file name when the pattern has no slash. * * @since 1.4.0 */ public static function excluded( $rel ) { static $patterns = null; if ( null === $patterns ) { $patterns = array(); foreach ( Softvolt_Ai_Bodyguard_Settings::list_to_array( Softvolt_Ai_Bodyguard_Settings::get( 'scan_exclude', '' ) ) as $p ) { $p = ltrim( wp_normalize_path( trim( $p ) ), '/' ); if ( '' !== $p ) { $patterns[] = $p; } } } $rel = ltrim( wp_normalize_path( $rel ), '/' ); foreach ( $patterns as $p ) { $subject = false !== strpos( $p, '/' ) ? $rel : basename( $rel ); if ( $subject === $p || fnmatch( $p, $subject, FNM_CASEFOLD ) || ( '/' === substr( $p, -1 ) && 0 === strpos( $rel, $p ) ) ) { return true; } } return false; } /** * The owner's own signatures (Scan settings): text found anywhere, or /regular expressions/. * * @since 1.4.0 */ public static function custom_hits( $content ) { static $rules = null; if ( null === $rules ) { $rules = array(); foreach ( preg_split( '/\r\n|\r|\n/', (string) Softvolt_Ai_Bodyguard_Settings::get( 'scan_custom_signatures', '' ) ) as $line ) { $line = trim( $line ); if ( '' === $line ) { continue; } $regex = strlen( $line ) > 2 && '/' === $line[0] && preg_match( '#^/.+/[imsxu]*$#', $line ) && false !== @preg_match( $line, '' ); $rules[] = array( $line, $regex ); } } $out = array(); foreach ( $rules as $r ) { $offset = false; $count = 0; if ( $r[1] ) { $count = (int) @preg_match_all( $r[0], $content, $m, PREG_OFFSET_CAPTURE ); $offset = $count ? (int) $m[0][0][1] : false; } else { $offset = stripos( $content, $r[0] ); $count = false === $offset ? 0 : substr_count( strtolower( $content ), strtolower( $r[0] ) ); } if ( false === $offset || ! $count ) { continue; } $out[] = array( 'id' => 'custom_' . substr( md5( $r[0] ), 0, 10 ), 'title' => sprintf( /* translators: %s: value */ __( 'Your own signature matched: %s', 'ai-guardwall' ), Softvolt_Ai_Bodyguard_Helpers::trim_text( $r[0], 60 ) ), 'severity' => 'high', 'confidence' => 75, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => __( 'A pattern from Malware Cleanup › Scan settings › Your own signatures was found in this file.', 'ai-guardwall' ), 'line' => Softvolt_Ai_Bodyguard_Helpers::line_of_offset( $content, $offset ), 'count' => $count, 'excerpt' => Softvolt_Ai_Bodyguard_Helpers::excerpt( $content, $offset ), ); } return $out; } private static function skip_dirs() { $out = array(); foreach ( Softvolt_Ai_Bodyguard_Settings::list_to_array( Softvolt_Ai_Bodyguard_Settings::get( 'scan_skip_dirs', '' ) ) as $p ) { $out[] = trim( wp_normalize_path( $p ), '/' ); } // The plugin's own working folder (database backups). $out[] = 'wp-content/uploads/' . Softvolt_Ai_Bodyguard_Helpers::WORK_FOLDER; $out[] = 'wp-content/uploads/' . Softvolt_Ai_Bodyguard_Helpers::LEGACY_FOLDER; return $out; } private static function dir_matches( $rel_dir, $pattern ) { if ( '' === $pattern ) { return false; } if ( false !== strpos( $pattern, '/' ) ) { return 0 === strpos( $rel_dir . '/', $pattern . '/' ) || fnmatch( $pattern, $rel_dir ); } return basename( $rel_dir ) === $pattern || fnmatch( $pattern, basename( $rel_dir ) ); } /** * core | plugin | theme | mu | dropin | upload | root | config | other */ public static function categorize( $rel ) { $rel = ltrim( wp_normalize_path( $rel ), '/' ); if ( 'wp-config.php' === basename( $rel ) ) { return 'config'; } if ( 0 === strpos( $rel, 'wp-admin/' ) || 0 === strpos( $rel, 'wp-includes/' ) ) { return 'core'; } if ( 0 === strpos( $rel, 'wp-content/plugins/' ) ) { return 'plugin'; } if ( 0 === strpos( $rel, 'wp-content/themes/' ) ) { return 'theme'; } if ( 0 === strpos( $rel, 'wp-content/mu-plugins/' ) ) { return 'mu'; } if ( 0 === strpos( $rel, 'wp-content/uploads/' ) ) { return 'upload'; } if ( preg_match( '#^wp-content/[^/]+\.php$#', $rel ) ) { return 'dropin'; } if ( false === strpos( $rel, '/' ) ) { return Softvolt_Ai_Bodyguard_Integrity::is_core_path( $rel ) ? 'core' : 'root'; } return 'other'; } /** * Scan one file and record findings / hashes. * * @since 1.0.0 * @param string $rel Site-relative path (or absolute for wp-config.php outside the root). * @param array $state Scan state (by reference: changes, counters). */ public static function scan_file( $rel, array &$state ) { $abs = ( 0 === strpos( $rel, '/' ) || preg_match( '#^[A-Za-z]:/#', $rel ) ) ? $rel : Softvolt_Ai_Bodyguard_Helpers::abs_path( $rel ); if ( ! is_file( $abs ) ) { return; } $scan_id = (int) $state['scan_id']; $size = (int) @filesize( $abs ); $mtime = (int) @filemtime( $abs ); $ext = Softvolt_Ai_Bodyguard_Helpers::ext( $rel ); $base = basename( $rel ); $category = self::categorize( $rel ); $type = Softvolt_Ai_Bodyguard_Signatures::type_for_ext( $ext, $base ); $max = max( 1, (int) Softvolt_Ai_Bodyguard_Settings::get( 'scan_max_file_size', 5 ) ) * 1024 * 1024; $deep = (bool) Softvolt_Ai_Bodyguard_Settings::get( 'scan_deep_media', 0 ); $state['files_scanned']++; $state['current'] = $rel; if ( self::excluded( $rel ) ) { return; } // What we read: code-like files fully (up to the size limit), media only when asked (first 512 KB). // Core and plugin files always get a real hash (they are compared with WordPress.org); other // media gets a cheap size+mtime stamp for change tracking. $content = null; $md5 = ''; $verifiable = in_array( $category, array( 'core', 'plugin' ), true ); if ( 'media' === $type ) { if ( $deep || $size <= 262144 ) { $content = (string) @file_get_contents( $abs, false, null, 0, 524288 ); } $md5 = $verifiable ? (string) @md5_file( $abs ) : 'm:' . $size . ':' . $mtime; } elseif ( $size <= $max ) { $content = (string) @file_get_contents( $abs ); $md5 = md5( $content ); } else { $md5 = (string) @md5_file( $abs ); } $hits = array(); $integrity = ''; $primary = null; $slug = ''; if ( 'core' === $category ) { $integrity = Softvolt_Ai_Bodyguard_Integrity::core_status( $rel, $md5 ); if ( 'verified' === $integrity ) { self::record_hash( $rel, $md5, $size, $mtime, $scan_id, $state, $category, '', true ); return; } if ( 'modified' === $integrity ) { $primary = array( 'id' => 'core_modified', 'title' => __( 'Modified WordPress core file', 'ai-guardwall' ), 'severity' => 'critical', 'confidence' => 90, 'verdict' => 'suspicious', 'remediation' => 'replace_official_file', 'description' => __( 'The file differs from the official copy for this WordPress version. Core files are never edited legitimately; a change here usually means injected code.', 'ai-guardwall' ), ); } elseif ( 'unknown' === $integrity ) { $primary = array( 'id' => 'core_unknown', 'title' => __( 'Unknown file inside WordPress core folders', 'ai-guardwall' ), 'severity' => Softvolt_Ai_Bodyguard_Helpers::is_php_ext( $ext ) ? 'high' : 'medium', 'confidence' => Softvolt_Ai_Bodyguard_Helpers::is_php_ext( $ext ) ? 75 : 45, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => __( 'WordPress.org does not ship this file. Attackers hide backdoors among core files because owners rarely look there. Leftovers from old versions also land here.', 'ai-guardwall' ), ); } } elseif ( 'plugin' === $category ) { $slug = Softvolt_Ai_Bodyguard_Helpers::plugin_slug_from_rel( $rel ); $inner = substr( $rel, strlen( 'wp-content/plugins/' . $slug . '/' ) ); if ( in_array( $slug, array( basename( dirname( SOFTVOLT_AI_BODYGUARD_FILE ) ), 'softvoltai-ai-bodyguard', 'softvoltai-ai-bodyguard-pro', 'ai-guardwall', 'ai-bodyguard-scanning-security' ), true ) ) { // Our own files (free or Pro edition) hold the malware signatures themselves; hash them for change tracking only. self::record_hash( $rel, $md5, $size, $mtime, $scan_id, $state, $category, $slug ); return; } if ( '' !== $slug ) { $integrity = Softvolt_Ai_Bodyguard_Integrity::plugin_file_status( $slug, $inner, $md5, ( null !== $content && 'media' !== $type && $size <= $max ) ? hash( 'sha256', $content ) : '' ); if ( 'verified' === $integrity ) { self::record_hash( $rel, $md5, $size, $mtime, $scan_id, $state, $category, $slug, true ); return; } if ( 'modified' === $integrity ) { $primary = array( 'id' => 'plugin_modified', 'title' => __( 'Plugin file differs from the WordPress.org version', 'ai-guardwall' ), 'severity' => 'high', 'confidence' => 75, 'verdict' => 'suspicious', 'remediation' => 'replace_plugin', 'description' => __( 'The file is not identical to the copy in the WordPress.org repository for this plugin version. Injected code, a nulled copy or a manual edit are the usual causes.', 'ai-guardwall' ), ); } elseif ( 'unknown' === $integrity && Softvolt_Ai_Bodyguard_Helpers::is_php_ext( $ext ) ) { $primary = array( 'id' => 'plugin_unknown_file', 'title' => __( 'PHP file not part of the WordPress.org plugin package', 'ai-guardwall' ), 'severity' => 'medium', 'confidence' => 45, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => __( 'The repository package for this plugin version does not contain this file. Attackers drop shells inside plugin folders; some plugins also create files at runtime.', 'ai-guardwall' ), ); } } } elseif ( 'theme' === $category ) { $slug = Softvolt_Ai_Bodyguard_Helpers::theme_slug_from_rel( $rel ); } // Firewall data that other security plugins generate is full of attack strings by design // (Wordfence's wflogs/rules.php lists shell names in its rules): track changes only. if ( self::is_security_plugin_data( $rel, $content ) ) { self::record_hash( $rel, $md5, $size, $mtime, $scan_id, $state, $category, '' ); return; } // Signature scan of what cannot be verified. if ( null !== $content && '' !== $content ) { $hits = Softvolt_Ai_Bodyguard_Signatures::scan( $content, array( 'type' => $type, 'category' => $category ) ); $own = self::custom_hits( $content ); if ( $own ) { $hits = array_merge( $hits, $own ); usort( $hits, array( 'Softvolt_Ai_Bodyguard_Signatures', 'sort_hits' ) ); } } // Location rules. if ( 'upload' === $category && Softvolt_Ai_Bodyguard_Helpers::is_php_ext( $ext ) && ! self::is_silence( $content ) && ! self::is_inert( $content ) ) { $primary = array( 'id' => 'upload_php', 'title' => __( 'PHP file inside the uploads folder', 'ai-guardwall' ), 'severity' => 'critical', 'confidence' => $hits ? 97 : 85, 'verdict' => $hits ? 'confirmed' : 'suspicious', 'remediation' => 'delete', 'description' => __( 'The uploads folder holds media only. Executable PHP here is almost always a planted backdoor or a dropper.', 'ai-guardwall' ), ); } elseif ( 'root' === $category && Softvolt_Ai_Bodyguard_Helpers::is_php_ext( $ext ) && ! in_array( $base, array( 'wp-config-sample.php', '.maintenance' ), true ) && ! $primary ) { $primary = array( 'id' => 'root_unknown_php', 'title' => __( 'Unknown PHP file in the site root', 'ai-guardwall' ), 'severity' => $hits ? 'critical' : 'high', 'confidence' => $hits ? 90 : 55, 'verdict' => $hits ? 'confirmed' : 'suspicious', 'remediation' => $hits ? 'delete' : 'manual_review', 'description' => __( 'WordPress does not ship this file in the site root. Hosting tools (adminer, info.php) or old installers land here legitimately; backdoors are also commonly dropped here.', 'ai-guardwall' ), ); } elseif ( in_array( $category, array( 'root', 'other', 'upload' ), true ) && preg_match( '/\.(sql|sql\.gz|zip|tar|tar\.gz|tgz|bak|old|orig|save|swp)$/i', $base ) && preg_match( '/wp-config|\.sql|backup|dump|wordpress|public_html/i', $base ) && ! $primary ) { $primary = array( 'id' => 'exposed_backup', 'title' => __( 'Backup or database dump inside the web root', 'ai-guardwall' ), 'severity' => 'high', 'confidence' => 60, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => __( 'Backup archives and SQL dumps inside public folders can be downloaded by anyone who guesses the name, leaking passwords and the whole database. Move them outside the web root.', 'ai-guardwall' ), ); } elseif ( 'mu' === $category && 1 === substr_count( substr( $rel, strlen( 'wp-content/mu-plugins/' ) ), '/' ) + 0 && Softvolt_Ai_Bodyguard_Helpers::is_php_ext( $ext ) && ! $primary && ! $hits ) { $primary = array( 'id' => 'mu_plugin_present', 'title' => __( 'Must-use plugin (loads on every request, cannot be deactivated)', 'ai-guardwall' ), 'severity' => 'info', 'confidence' => 20, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => __( 'Hosts and site builders install MU plugins legitimately; attackers love them because they run everywhere and never show as "active". Confirm you know where this one came from.', 'ai-guardwall' ), ); } elseif ( 'dropin' === $category && ! $primary && ! $hits ) { $primary = array( 'id' => 'dropin_present', 'title' => __( 'WordPress drop-in file present', 'ai-guardwall' ), 'severity' => 'info', 'confidence' => 20, 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'description' => __( 'Drop-ins (advanced-cache.php, db.php, object-cache.php...) replace core behaviour and load before plugins. Caching plugins create them; so does malware.', 'ai-guardwall' ), ); } self::record_hash( $rel, $md5, $size, $mtime, $scan_id, $state, $category, $slug, $primary || $hits ); if ( ! $primary && ! $hits ) { return; } // Build one finding per file: the strongest indicator leads, the rest is listed. $top = $primary ? $primary : $hits[0]; if ( $primary && $hits ) { $h = $hits[0]; if ( Softvolt_Ai_Bodyguard_Helpers::severity_rank( $h['severity'] ) > Softvolt_Ai_Bodyguard_Helpers::severity_rank( $top['severity'] ) || ( $h['severity'] === $top['severity'] && $h['confidence'] > $top['confidence'] ) ) { $top = array_merge( $primary, array( 'severity' => $h['severity'], 'confidence' => max( $h['confidence'], $primary['confidence'] ), 'verdict' => $h['verdict'], 'title' => $primary['title'] . ' — ' . $h['title'], 'remediation' => 'confirmed' === $h['verdict'] && in_array( $primary['remediation'], array( 'manual_review' ), true ) ? $h['remediation'] : $primary['remediation'], ) ); } elseif ( 'confirmed' === $h['verdict'] && $h['confidence'] >= 85 ) { $top['verdict'] = 'confirmed'; $top['confidence'] = max( $top['confidence'], $h['confidence'] ); } } $confirmed = 'confirmed' === $top['verdict'] && $top['confidence'] >= 85; foreach ( $hits as $h ) { if ( 'confirmed' === $h['verdict'] && $h['confidence'] >= 85 ) { $confirmed = true; } } $desc = $top['description']; if ( $hits ) { $more = array(); foreach ( array_slice( $hits, 0, 8 ) as $h ) { if ( $primary || $h['id'] !== $top['id'] ) { $more[] = sprintf( '%s (line %d%s)', $h['title'], $h['line'], $h['count'] > 1 ? ', ' . $h['count'] . 'x' : '' ); } } if ( $more ) { $desc .= ' ' . __( 'Indicators:', 'ai-guardwall' ) . ' ' . implode( '; ', $more ) . '.'; } } $line = $hits ? (int) $hits[0]['line'] : 0; $snippet = ( null !== $content && $line ) ? Softvolt_Ai_Bodyguard_Helpers::lines_around( $content, $line, 8, 2500 ) : ( $hits && ! empty( $hits[0]['excerpt'] ) ? $hits[0]['excerpt'] : '' ); $id = Softvolt_Ai_Bodyguard_Findings::upsert( array( 'scan_id' => $scan_id, 'fingerprint' => md5( 'file|' . $rel ), 'category' => $category, 'location' => $rel, 'line' => $line, 'severity' => $top['severity'], 'confidence' => (int) $top['confidence'], 'signature' => $top['id'], 'title' => $top['title'], 'description' => $desc, 'snippet' => $snippet, 'verdict' => $confirmed ? 'confirmed' : 'suspicious', 'remediation' => $top['remediation'], 'file_hash' => $md5, 'meta' => array( 'rel' => $rel, 'size' => $size, 'mtime' => $mtime, 'mtime_human' => $mtime ? gmdate( 'Y-m-d H:i:s', $mtime ) . ' UTC' : '', 'type' => $type, 'integrity' => $integrity, 'plugin_slug' => 'plugin' === $category ? $slug : '', 'theme_slug' => 'theme' === $category ? $slug : '', 'official_url' => Softvolt_Ai_Bodyguard_Integrity::official_url( $rel ), 'hits' => array_slice( $hits, 0, 15 ), ), ) ); $state['findings']++; if ( 'critical' === $top['severity'] ) { $state['critical']++; } unset( $id ); } /** * A PHP file whose first statement stops PHP (__halt_compiler, exit or die) cannot run * anything after it: plugins use this to keep data (settings, rules) in .php files. */ private static function is_inert( $content ) { return is_string( $content ) && (bool) preg_match( '/^\s*<\?php\s+(?:__halt_compiler\s*\(\s*\)|exit|die)\s*(?:\(\s*(?:\'[^\'\n]*\'|"[^"\n]*"|\d+)?\s*\))?\s*;/i', $content ); } /** * Wordfence's firewall data files in wp-content/wflogs, recognised by the guard every one of them starts with. */ private static function is_security_plugin_data( $rel, $content ) { if ( ! is_string( $content ) || ! preg_match( '#^wp-content/wflogs/[a-z0-9_-]+\.php$#i', $rel ) ) { return false; } $head = substr( $content, 0, 300 ); return (bool) preg_match( '/^<\?php\s+if\s*\(\s*!\s*defined\s*\(\s*[\'"]WFWAF_VERSION[\'"]\s*\)/', $head ) && false !== strpos( $head, 'exit(' ); } private static function is_silence( $content ) { return is_string( $content ) && (bool) preg_match( '/^\s*<\?php\s*(?:\/\/|#)\s*silence is golden\.?\s*(?:\?>)?\s*$/i', $content ); } /** * Write the pending hash rows. * * @since 1.0.0 * @param array $state Scan state (by reference). */ public static function flush_hashes( array &$state ) { global $wpdb; if ( empty( $state['hash_batch'] ) ) { return; } $table = Softvolt_Ai_Bodyguard_Db::table( 'filehash' ); $rows = array_values( array_filter( (array) $state['hash_batch'], 'is_array' ) ); if ( $rows ) { $values = implode( ', ', array_fill( 0, count( $rows ), '(%s, %s, %s, %d, %d, %d)' ) ); $wpdb->query( $wpdb->prepare( "REPLACE INTO %i (path_hash, path, md5, size, mtime, scan_id) VALUES {$values}", array_merge( array( $table ), ...$rows ) ) ); // 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 placeholder groups only; every value goes through prepare(). } $state['hash_batch'] = array(); } /** * Store the file hash and note changes since the previous scan. */ private static function record_hash( $rel, $md5, $size, $mtime, $scan_id, array &$state, $category, $slug = '', $trusted = false ) { global $wpdb; $table = Softvolt_Ai_Bodyguard_Db::table( 'filehash' ); $path_hash = md5( $rel ); $previous = isset( $state['prev_hashes'][ $path_hash ] ) ? $state['prev_hashes'][ $path_hash ] : null; if ( null === $previous && ! empty( $state['baseline'] ) ) { $previous = $wpdb->get_var( $wpdb->prepare( "SELECT md5 FROM %i WHERE path_hash = %s", $table, $path_hash ) ); } // Hashes are written in batches of 200 rows: one query per file was the slowest part of a scan. $state['hash_batch'][] = array( $path_hash, $rel, $md5, (int) $size, (int) $mtime, (int) $scan_id ); if ( count( $state['hash_batch'] ) >= 200 ) { self::flush_hashes( $state ); } // Verified official files and files that already have a finding of their own never raise a "changed" notice. if ( $trusted || empty( $state['baseline'] ) || ! Softvolt_Ai_Bodyguard_Settings::get( 'scan_file_changes', 1 ) ) { return; } $code = in_array( $category, array( 'core', 'plugin', 'theme', 'mu', 'dropin', 'root', 'config', 'other' ), true ); if ( ! $code ) { return; } $changed = null !== $previous ? ( $previous !== $md5 ) : true; if ( ! $changed ) { return; } $type = null === $previous ? 'new' : 'modified'; if ( count( $state['changes'] ) < 500 ) { $state['changes'][] = array( 'type' => $type, 'path' => $rel, ); } // Changes explained by an update (or a flagged file, which already has a finding) are not reported again. $updated = isset( $state['updated_slugs'] ) ? (array) $state['updated_slugs'] : array(); if ( ( '' !== $slug && in_array( $slug, $updated, true ) ) || ( 'core' === $category && ! empty( $state['core_updated'] ) ) ) { return; } if ( 'modified' === $type || Softvolt_Ai_Bodyguard_Helpers::is_php_ext( Softvolt_Ai_Bodyguard_Helpers::ext( $rel ) ) ) { Softvolt_Ai_Bodyguard_Findings::upsert( array( 'scan_id' => $scan_id, 'fingerprint' => md5( 'change|' . $rel ), 'category' => $category, 'location' => $rel, 'line' => 0, 'severity' => 'low', 'confidence' => 35, 'signature' => 'file_' . $type, 'title' => 'new' === $type ? __( 'New file since the last scan', 'ai-guardwall' ) : __( 'File changed since the last scan', 'ai-guardwall' ), 'description' => __( 'The file was added or changed since the previous scan and no update of its plugin, theme or WordPress explains the change. Expected after you edited it; otherwise worth a look.', 'ai-guardwall' ), 'snippet' => '', 'verdict' => 'suspicious', 'remediation' => 'manual_review', 'file_hash' => $md5, 'meta' => array( 'rel' => $rel, 'size' => $size, 'mtime' => $mtime, 'mtime_human' => $mtime ? gmdate( 'Y-m-d H:i:s', $mtime ) . ' UTC' : '', 'change' => $type, 'plugin_slug' => 'plugin' === $category ? $slug : '', 'theme_slug' => 'theme' === $category ? $slug : '', ), ) ); $state['findings']++; } } }