$count, 'read' => 0, 'cached' => 0, 'files' => 0, 'skipped' => 0, 'leads' => 0, 'partial' => 0, 'errors' => 0, ], (array) ( $pos['stats'] ?? [] ) ); for ( $i = (int) ( $pos['i'] ?? 0 ); $i < $count; $i++ ) { try { $entry = self::analyze( $c, $targets[ $origins[ $i ] ], false ); ++$stats[ $entry['cached'] ? 'cached' : 'read' ]; $stats['files'] += (int) $entry['files']; $stats['skipped'] += (int) $entry['skipped']; $stats['leads'] += count( (array) $entry['leads'] ); $stats['partial'] += $entry['partial'] ? 1 : 0; } catch ( \Throwable $e ) { // The folder keeps the leads it had; the scan goes on. ++$stats['errors']; } if ( $i + 1 < $count && $c->budget()->exhausted() ) { return [ 'i' => $i + 1, 'stats' => $stats, ]; } } Store::keep( $origins ); $c->note( 'code', $stats ); return null; } /** * The folders of custom code: plugins and themes classed custom (§14.1), and must-use plugins. * * @return array */ public static function targets( Context $c ): array { $out = []; // ShieldXen itself isn't the site's own code, even when it was installed from a zip and so // isn't known to core's update check yet. Its files still get the malware and file checks. $self = Paths::real( SHIELDXEN_DIR ); foreach ( $c->inventory()['plugins'] as $plugin ) { $origin = 'plugin:' . $plugin['slug']; $dir = Paths::real( WP_PLUGIN_DIR . '/' . $plugin['slug'] ); if ( false !== strpos( (string) $plugin['file'], '/' ) && null !== $dir && $dir !== $self && 'custom' === Classes::of( $origin ) ) { $out[ $origin ] = [ 'origin' => $origin, 'type' => 'plugin', 'slug' => (string) $plugin['slug'], 'name' => (string) $plugin['name'], 'dir' => $dir, ]; } } foreach ( $c->inventory()['themes'] as $theme ) { $origin = 'theme:' . $theme['slug']; $dir = Paths::real( get_theme_root( (string) $theme['slug'] ) . '/' . $theme['slug'] ); if ( null !== $dir && 'custom' === Classes::of( $origin ) ) { $out[ $origin ] = [ 'origin' => $origin, 'type' => 'theme', 'slug' => (string) $theme['slug'], 'name' => (string) $theme['name'], 'dir' => $dir, ]; } } $mu = $c->origins()->mu(); if ( is_dir( $mu ) ) { $out['mu-plugin'] = [ 'origin' => 'mu-plugin', 'type' => 'code', 'slug' => 'mu-plugins', 'name' => __( 'Must-use plugins', 'shieldxen' ), 'dir' => $mu, ]; } return $out; } /** * Reads one folder, unless its files and the rules are what the stored leads came from. * * @param array{origin: string, type: string, slug: string, name: string, dir: string} $target The folder. * @return array The stored entry, with "cached". */ public static function analyze( Context $c, array $target, bool $fresh ): array { list( $files, $digest, $too_large ) = self::files( $c, $target ); $stored = Store::get( $target['origin'] ); if ( ! $fresh && null !== $stored && ( $stored['digest'] ?? null ) === $digest ) { return array_merge( $stored, [ 'cached' => true ] ); } $result = Analyzer::run( $files, static function ( string $path ): ?string { return PhpSource::read( $path ); } ); $entry = [ 'digest' => $digest, 'name' => $target['name'], 'type' => $target['type'], 'slug' => $target['slug'], 'at' => gmdate( 'Y-m-d H:i:s' ), 'files' => $result['files'], 'skipped' => $result['skipped'] + $too_large, 'partial' => $result['partial'], 'leads' => $result['leads'], ]; Store::put( $target['origin'], $entry ); return array_merge( $entry, [ 'cached' => false ] ); } /** * The folder's PHP and JS files from the index, and their digest. * * @param array{origin: string, type: string, slug: string, name: string, dir: string} $target The folder. * @return array{0: array, 1: string, 2: int} */ private static function files( Context $c, array $target ): array { $rows = ( new Files() )->where( 'origin = %s AND present = 1 AND kind IN (%s, %s)', [ $target['origin'], FileKind::PHP, FileKind::JS ] ); $set = $c->rules()->signatures(); // A must-use plugin the bundled data knows (a host's) is left out, with the folder named after it. $known = []; if ( 'mu-plugin' === $target['origin'] && null !== $set ) { foreach ( $rows as $row ) { $rel = Paths::relative( (string) $row['path'], $target['dir'] ); if ( null !== $rel && false === strpos( $rel, '/' ) && null !== $row['sha256'] && $set->is_known_mu( (string) $row['sha256'] ) ) { $known[ $rel ] = true; $known[ basename( $rel, '.php' ) . '/' ] = true; } } } $files = []; $lines = []; $too_large = 0; foreach ( $rows as $row ) { $path = (string) $row['path']; $rel = Paths::relative( $path, $target['dir'] ); if ( null === $rel || 1 === preg_match( '#(?:^|/)(?:vendor|node_modules)/#', $rel ) || isset( $known[ $rel ] ) ) { continue; } $slash = strpos( $rel, '/' ); if ( false !== $slash && isset( $known[ substr( $rel, 0, $slash + 1 ) ] ) ) { continue; } if ( (int) $row['size'] > PhpSource::MAX_BYTES ) { ++$too_large; continue; } $files[] = [ 'path' => $path, 'display' => Paths::display( $path ), 'kind' => FileKind::JS === $row['kind'] ? 'js' : 'php', ]; $lines[] = $rel . ':' . ( $row['sha256'] ?? ( $row['size'] . '/' . $row['mtime'] ) ); } usort( $files, static function ( array $a, array $b ): int { return strcmp( $a['path'], $b['path'] ); } ); sort( $lines ); return [ $files, sha1( Analyzer::ENGINE . "\n" . implode( "\n", $lines ) ), $too_large ]; } }