current_time( 'mysql' ), 'issues' => $issues, ); update_option( 'syslock_last_audit', $report, false ); return $report; } /** * Verify WordPress core files against official WordPress.org MD5 checksums. */ private static function check_core_checksums(): array { global $wp_version; $locale = get_locale(); $url = sprintf( 'https://api.wordpress.org/core/checksums/1.0/?version=%s&locale=%s', rawurlencode( $wp_version ), rawurlencode( $locale ) ); $response = wp_remote_get( $url, array( 'timeout' => 10 ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { return array( array( 'category' => 'Core Checksums', 'target' => 'WordPress.org API', 'message' => 'Could not fetch official checksums manifest.', ), ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['checksums'] ) || ! is_array( $body['checksums'] ) ) { return array(); } $issues = array(); foreach ( $body['checksums'] as $rel_path => $expected_md5 ) { // Skip wp-content sample themes/plugins that admins legitimately remove. if ( 0 === strpos( $rel_path, 'wp-content/' ) ) { continue; } $full_path = ABSPATH . $rel_path; if ( ! file_exists( $full_path ) ) { $issues[] = array( 'category' => 'Core Integrity', 'target' => $rel_path, 'message' => 'Core file is missing.', ); continue; } $actual_md5 = md5_file( $full_path ); if ( false !== $actual_md5 && ! hash_equals( $expected_md5, $actual_md5 ) ) { $issues[] = array( 'category' => 'Core Integrity', 'target' => $rel_path, 'message' => 'Checksum mismatch (file modified).', ); } } return $issues; } /** * Detect executable PHP files hidden inside wp-content/uploads. */ private static function check_uploads_php(): array { $upload_dir = wp_upload_dir(); $base_dir = $upload_dir['basedir'] ?? ''; if ( '' === $base_dir || ! is_dir( $base_dir ) ) { return array(); } $issues = array(); try { $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_dir, FilesystemIterator::SKIP_DOTS ) ); foreach ( $iterator as $file ) { if ( ! $file->isFile() ) { continue; } $ext = strtolower( $file->getExtension() ); if ( in_array( $ext, array( 'php', 'phtml', 'phar', 'php5', 'php7' ), true ) ) { $rel = str_replace( ABSPATH, '', $file->getPathname() ); $issues[] = array( 'category' => 'Malware / Uploads', 'target' => $rel, 'message' => 'Executable PHP file found inside uploads directory.', ); } } } catch ( Exception $e ) { // Ignore unreadable subdirectories gracefully. } return $issues; } /** * Audit POSIX permission bitmasks on critical paths (read-only diagnostic). */ private static function check_posix_permissions(): array { $issues = array(); $upload_dir = wp_upload_dir(); $wp_config = file_exists( ABSPATH . 'wp-config.php' ) ? ABSPATH . 'wp-config.php' : dirname( ABSPATH ) . '/wp-config.php'; $targets = array( 'wp-config.php' => $wp_config, '.htaccess' => ABSPATH . '.htaccess', 'ABSPATH' => ABSPATH, 'wp-content' => WP_CONTENT_DIR, 'wp-content/plugins' => defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins', 'wp-content/themes' => function_exists( 'get_theme_root' ) ? get_theme_root() : WP_CONTENT_DIR . '/themes', 'uploads' => $upload_dir['basedir'] ?? '', ); foreach ( $targets as $label => $path ) { if ( '' === $path || ! file_exists( $path ) ) { continue; } clearstatcache( true, $path ); $perms = fileperms( $path ); if ( false === $perms ) { continue; } $octal = substr( sprintf( '%o', $perms ), -4 ); // Check world-writable bit (0x0002). if ( $perms & 0x0002 ) { $issues[] = array( 'category' => 'POSIX Permissions', 'target' => $label, 'message' => "World-writable permissions ({$octal}). Recommended: 0755 for dirs, 0644/0640 for files.", ); } // Check world-readable bit (0x0004) specifically on wp-config.php. if ( 'wp-config.php' === $label && ( $perms & 0x0004 ) ) { $issues[] = array( 'category' => 'POSIX Permissions', 'target' => 'wp-config.php', 'message' => "World-readable permissions ({$octal}). Recommended: 0640 or 0600.", ); } } return $issues; } /** * Check for publicly exposed residual logs, backups, or git metadata. */ private static function check_exposed_files(): array { $candidates = array( 'wp-content/debug.log' => WP_CONTENT_DIR . '/debug.log', '.git/HEAD' => ABSPATH . '.git/HEAD', 'wp-config.php.bak' => ABSPATH . 'wp-config.php.bak', 'wp-config.php~' => ABSPATH . 'wp-config.php~', '.wp-config.php.swp' => ABSPATH . '.wp-config.php.swp', ); $issues = array(); foreach ( $candidates as $label => $path ) { if ( file_exists( $path ) ) { $issues[] = array( 'category' => 'Exposed File', 'target' => $label, 'message' => 'Sensitive residual file exists inside webroot.', ); } } // Check root for stray database/archive dumps (.sql, .zip, .gz). foreach ( array( 'sql', 'zip', 'gz' ) as $ext ) { $dumps = glob( ABSPATH . '*.' . $ext ); if ( is_array( $dumps ) ) { foreach ( $dumps as $dump ) { $issues[] = array( 'category' => 'Exposed Archive', 'target' => basename( $dump ), 'message' => 'Archive or SQL dump found in webroot.', ); } } } return $issues; } }