'; $lines[] = ' RewriteEngine On'; // Ohne Herkunftsseite (direkter Aufruf, Feed-Leser, CDN) bleibt alles erlaubt. $lines[] = ' RewriteCond %{HTTP_REFERER} !^$'; $lines[] = ' RewriteCond %{HTTP_REFERER} !^https?://([^/]+\.)?(' . implode( '|', $quoted ) . ')(:[0-9]+)?(/|$) [NC]'; $lines[] = ' RewriteRule \.(jpe?g|png|gif|webp|avif|svg)$ - [F,NC]'; $lines[] = ''; } if ( ! empty( $settings['harden_files'] ) ) { $lines[] = ''; $lines[] = ' '; $lines[] = ' Require all denied'; $lines[] = ' '; $lines[] = ' '; $lines[] = ' Order allow,deny'; $lines[] = ' Deny from all'; $lines[] = ' '; $lines[] = ''; } if ( ! empty( $settings['harden_indexes'] ) ) { $lines[] = 'Options -Indexes'; } if ( ! empty( $settings['harden_signature'] ) ) { $lines[] = 'ServerSignature Off'; } return $lines; } /** Wertet der Webserver `.htaccess` aus (Apache, LiteSpeed)? */ public static function server_reads_htaccess() { $software = isset( $_SERVER['SERVER_SOFTWARE'] ) ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) ) : ''; return false !== strpos( $software, 'apache' ) || false !== strpos( $software, 'litespeed' ); } /** * Die .htaccess im Stammverzeichnis gilt auf Multisite fuer das ganze * Netzwerk: Dort darf nur ein Super-Admin sie aendern. */ public static function current_user_may_write() { if ( ! is_multisite() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return true; } return is_super_admin(); } private static function htaccess_path() { require_once ABSPATH . 'wp-admin/includes/file.php'; return get_home_path() . '.htaccess'; } public static function htaccess_writable() { $path = self::htaccess_path(); return is_file( $path ) ? wp_is_writable( $path ) : wp_is_writable( dirname( $path ) ); } /** Zeilen, die gerade im eigenen Block stehen. */ public static function current_lines() { require_once ABSPATH . 'wp-admin/includes/misc.php'; $path = self::htaccess_path(); return is_file( $path ) ? (array) extract_from_markers( $path, self::MARKER ) : array(); } /** * Nach dem Speichern: Block schreiben, wenn sich etwas geaendert hat. * * @param array $settings Bereinigte Einstellungen. */ public static function on_settings_saved( $settings ) { if ( ! is_array( $settings ) || ! self::current_user_may_write() ) { return; } $lines = self::build_block( $settings, self::site_host() ); if ( self::current_lines() === $lines ) { return; } self::write( $lines ); } public static function on_deactivate() { if ( self::current_lines() && self::htaccess_writable() ) { self::insert_block( self::htaccess_path(), array() ); } } /** * Schreibt den Block, prueft die Startseite und legt bei einem * Serverfehler die vorherige Fassung zurueck. * * @param string[] $lines Neue Zeilen. * @return array{ok:bool,message:string,time:int,rolled_back?:bool,effective?:string} */ public static function write( array $lines ) { if ( ! self::htaccess_writable() ) { $test = array( 'ok' => false, 'message' => __( 'The .htaccess file is not writable. Please adjust the file permissions.', '360-orbit-security-digest' ), 'time' => time(), ); update_option( self::OPTION_TEST, $test, false ); return $test; } $previous = self::current_lines(); update_option( self::OPTION_BACKUP, array( 'content' => $previous, 'time' => time(), ), false ); $path = self::htaccess_path(); self::insert_block( $path, $lines ); $test = self::self_test( $lines ); $test['hash'] = self::lines_hash( $lines ); if ( ! $test['ok'] ) { self::insert_block( $path, $previous ); $test['rolled_back'] = true; } update_option( self::OPTION_TEST, $test, false ); return $test; } /** * Ruft die Startseite ab; bei gesperrten Dateien zusaetzlich readme.html, * um zu sehen, ob der Webserver die Regel auch anwendet. * * @param string[] $lines Geschriebene Zeilen. */ public static function self_test( array $lines ) { $args = array( 'timeout' => 10, 'redirection' => 0, 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress-Core-Filter. 'headers' => array( 'Cache-Control' => 'no-cache' ), ); $response = wp_remote_get( home_url( '/' ), $args ); if ( is_wp_error( $response ) ) { return array( 'ok' => false, /* translators: %s: error message */ 'message' => sprintf( __( 'The homepage could not be reached: %s', '360-orbit-security-digest' ), $response->get_error_message() ), 'time' => time(), ); } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code >= 500 ) { return array( 'ok' => false, /* translators: %d: HTTP status code */ 'message' => sprintf( __( 'The homepage answered with HTTP %d after the change. The previous rules were restored; your server probably does not allow one of the rules in .htaccess.', '360-orbit-security-digest' ), $code ), 'time' => time(), ); } $test = array( 'ok' => true, /* translators: %d: HTTP status code */ 'message' => sprintf( __( 'The homepage answered with HTTP %d.', '360-orbit-security-digest' ), $code ), 'time' => time(), ); // Wirkt die Sperre? Nur pruefbar, wenn readme.html vorhanden ist. if ( in_array( '', $lines, true ) && is_file( ABSPATH . 'readme.html' ) ) { $probe = wp_remote_head( site_url( '/readme.html' ), $args ); $probe_code = is_wp_error( $probe ) ? 0 : (int) wp_remote_retrieve_response_code( $probe ); $test['effective'] = 403 === $probe_code ? 'yes' : ( 200 === $probe_code ? 'no' : 'unknown' ); } // Hotlink-Schutz: ein Bild von WordPress mit fremder Herkunftsseite abrufen. $image = ABSPATH . WPINC . '/images/w-logo-blue.png'; if ( preg_grep( '/^\tRewriteRule .*\[F,NC\]$/', $lines ) && is_file( $image ) ) { $foreign = $args; $foreign['headers'] += array( 'Referer' => 'https://hotlink-test.example/' ); $probe = wp_remote_head( includes_url( 'images/w-logo-blue.png' ), $foreign ); $probe_code = is_wp_error( $probe ) ? 0 : (int) wp_remote_retrieve_response_code( $probe ); $test['hotlink_effective'] = 403 === $probe_code ? 'yes' : ( 200 === $probe_code ? 'no' : 'unknown' ); } return $test; } /** * insert_with_markers() laesst am Dateiende den Zeilenumbruch weg; ein * spaeter angehaengter Block klebte sonst an unserer END-Marke. * * @param string $path Datei. * @param string[] $lines Zeilen. */ private static function insert_block( $path, array $lines ) { require_once ABSPATH . 'wp-admin/includes/misc.php'; $result = insert_with_markers( $path, self::MARKER, $lines ); if ( $result && is_readable( $path ) ) { $contents = (string) file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- lokale .htaccess. if ( '' !== $contents && "\n" !== substr( $contents, -1 ) ) { file_put_contents( $path, $contents . "\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- lokale .htaccess. } } return $result; } /* * --------------------------------------------------------------------- * Reiter „Haertung“ * --------------------------------------------------------------------- */ public static function render_tab( array $settings ) { $test = get_option( self::OPTION_TEST, array() ); $test = is_array( $test ) ? $test : array(); $reads = self::server_reads_htaccess(); $may_write = self::current_user_may_write(); $in_config = self::editor_off_in_config(); $wants_rule = (bool) self::build_block( $settings, self::site_host() ); $has_rule = (bool) self::current_lines(); ?>

*/ public static function permissions() { if ( '\\' === DIRECTORY_SEPARATOR ) { return array(); } require_once ABSPATH . 'wp-admin/includes/file.php'; $config = is_file( ABSPATH . 'wp-config.php' ) ? ABSPATH . 'wp-config.php' : dirname( ABSPATH ) . '/wp-config.php'; $upload = wp_upload_dir( null, false ); $paths = array( 'wp-config.php' => array( $config, 'config' ), '.htaccess' => array( get_home_path() . '.htaccess', 'htaccess' ), 'wp-content' => array( WP_CONTENT_DIR, 'dir' ), 'wp-content/uploads' => array( $upload['basedir'], 'dir' ), ); $out = array(); foreach ( $paths as $label => $entry ) { if ( ! file_exists( $entry[0] ) ) { continue; } $mode = (int) @fileperms( $entry[0] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- fehlende Rechte = keine Angabe. $out[] = array( 'label' => $label, 'mode' => substr( sprintf( '%o', $mode ), -3 ), 'state' => self::permission_state( $entry[1], $mode ), ); } return $out; } /** Abschnitt „Dateirechte“ im Reiter. */ private static function render_permissions() { $rows = self::permissions(); ?>

' . esc_html__( 'Other users on the server can change it', '360-orbit-security-digest' ) . ''; } elseif ( 'info' === $row['state'] ) { esc_html_e( 'Readable for everyone on the server. On shared hosting 640 or 600 is safer, if your host allows it.', '360-orbit-security-digest' ); } else { echo '' . esc_html__( 'In order', '360-orbit-security-digest' ) . ''; } ?>

'warn', /* translators: %s: list of files and folders */ 'label' => sprintf( __( 'Other users on the server can change: %s', '360-orbit-security-digest' ), implode( ', ', $bad ) ), 'hint' => __( 'Ask your host or correct the permissions with your FTP program.', '360-orbit-security-digest' ), ); } /** * Punkt fuer „Sicherheit auf einen Blick“. * * @param array $settings Einstellungen. * @return array{status:string,label:string,hint:string} */ public static function check( array $settings ) { $editor = self::editor_off_in_config() || ! empty( $settings['harden_file_editor'] ); $test = get_option( self::OPTION_TEST, array() ); $files = ! empty( $settings['harden_files'] ) && self::current_lines(); if ( $files && is_array( $test ) && 'no' === ( $test['effective'] ?? '' ) ) { return array( 'status' => 'warn', 'label' => __( 'The block of sensitive files has no effect', '360-orbit-security-digest' ), 'hint' => __( 'readme.html can still be opened. Your server seems to ignore the rule; ask your host.', '360-orbit-security-digest' ), ); } if ( $editor && $files ) { return array( 'status' => 'ok', 'label' => __( 'File editor off, sensitive files blocked', '360-orbit-security-digest' ), 'hint' => '', ); } return array( 'status' => 'info', 'label' => $editor ? __( 'Sensitive files are not blocked', '360-orbit-security-digest' ) : __( 'The file editor in the admin area is on', '360-orbit-security-digest' ), 'hint' => __( 'Switch it off on the Hardening tab.', '360-orbit-security-digest' ), ); } }