*/ public static function patterns() { $input = '\$_(?:GET|POST|REQUEST|COOKIE|SERVER)\b'; return array( // eval(base64_decode(...)), eval(gzinflate(...)) und Verwandte. 'eval_decode' => '/\b(?:eval|assert)\s*\(\s*(?:@\s*)?(?:base64_decode|gzinflate|gzuncompress|gzdecode|str_rot13|strrev|rawurldecode|hex2bin)\s*\(/i', // Entpacken eines base64-Blocks: gzinflate(base64_decode(...)). 'decode_chain' => '/\b(?:gzinflate|gzuncompress|gzdecode)\s*\(\s*(?:@\s*)?base64_decode\s*\(/i', // Anfragedaten direkt in eine ausfuehrende Funktion. 'request_exec' => '/\b(?:eval|assert|system|shell_exec|passthru|exec|popen|proc_open|pcntl_exec)\s*\(\s*(?:@\s*)?(?:stripslashes\s*\(\s*)?' . $input . '/i', // Anfragedaten als Funktionsname: $_POST['x']($_POST['y']). 'request_call' => '/' . $input . '\s*\[[^\]]{1,40}\]\s*\(/i', // Datei aus dem Netz holen und schreiben, Muster vieler Nachlader. 'remote_dropper' => '/\bfile_put_contents\s*\([^;]{0,200}\b(?:file_get_contents|base64_decode)\s*\(\s*[\'"]https?:\/\//i', ); } /** * Regeln, die im Inhalt einer Datei anschlagen. * * @return string[] Regel-Kennungen. */ public static function check_contents( $code ) { $code = (string) $code; $hits = array(); foreach ( self::patterns() as $rule => $pattern ) { if ( preg_match( $pattern, $code ) ) { $hits[] = $rule; } } // Sehr langer base64-Block zusammen mit einer Dekodier- oder // Ausfuehrfunktion: typische Verschleierung. Allein ist ein langer // Block harmlos (eingebettete Bilder, Schriften). if ( preg_match( '/[A-Za-z0-9+\/]{3000,}={0,2}/', $code ) && preg_match( '/\b(?:eval|assert|base64_decode|gzinflate|create_function)\s*\(/i', $code ) ) { $hits[] = 'obfuscated_blob'; } return $hits; } /** Ob ein Dateiname vom Webserver als PHP ausgefuehrt werden kann. */ public static function is_executable_name( $name ) { $parts = explode( '.', strtolower( basename( (string) $name ) ) ); array_shift( $parts ); // Auch doppelte Endungen wie bild.php.jpg: Manche Server fuehren sie aus. foreach ( $parts as $ext ) { if ( in_array( $ext, self::EXECUTABLE, true ) ) { return true; } } return false; } /** Bezeichnung einer Regel fuer die Oberflaeche und die E-Mail. */ public static function rule_label( $rule ) { $labels = array( 'php_in_uploads' => __( 'Executable PHP file in the uploads folder', '360-orbit-security-digest' ), 'eval_decode' => __( 'Runs decoded code (eval with base64/gzinflate)', '360-orbit-security-digest' ), 'decode_chain' => __( 'Unpacks a hidden code block', '360-orbit-security-digest' ), 'request_exec' => __( 'Runs data from the request as code or command', '360-orbit-security-digest' ), 'request_call' => __( 'Calls a function named in the request', '360-orbit-security-digest' ), 'remote_dropper' => __( 'Downloads code from the internet and saves it', '360-orbit-security-digest' ), 'obfuscated_blob' => __( 'Long encoded block together with a decoder', '360-orbit-security-digest' ), ); return $labels[ $rule ] ?? $rule; } /** * Verzeichnisse, die durchsucht werden. * * @return array Art => absoluter Pfad. */ public static function roots() { $roots = array(); $uploads = wp_get_upload_dir(); if ( ! empty( $uploads['basedir'] ) && is_dir( $uploads['basedir'] ) ) { $roots['uploads'] = $uploads['basedir']; } if ( is_dir( get_theme_root() ) ) { $roots['themes'] = get_theme_root(); } if ( defined( 'WPMU_PLUGIN_DIR' ) && is_dir( WPMU_PLUGIN_DIR ) ) { $roots['mu-plugins'] = WPMU_PLUGIN_DIR; } return $roots; } /** * Durchsucht die Verzeichnisse und speichert das Ergebnis. * * @return array Ergebnis wie {@see self::result()}. */ public static function scan() { $started = microtime( true ); $findings = array(); $checked = 0; $complete = true; foreach ( self::roots() as $kind => $root ) { $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $root, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::LEAVES_ONLY, RecursiveIteratorIterator::CATCH_GET_CHILD ); foreach ( $iterator as $file ) { if ( ! $file->isFile() ) { continue; } if ( $checked >= self::MAX_FILES || microtime( true ) - $started > self::MAX_SECONDS ) { $complete = false; break 2; } $path = $file->getPathname(); $executable = self::is_executable_name( $path ); // Im Upload-Ordner nur PHP-faehige Dateien lesen; Bilder und // Dokumente nicht anfassen, das spart den Grossteil der Zeit. $is_php = 'php' === strtolower( $file->getExtension() ); if ( 'uploads' === $kind && ! $executable ) { continue; } if ( 'uploads' !== $kind && ! $is_php && ! $executable ) { continue; } ++$checked; $rules = array(); if ( 'uploads' === $kind ) { // index.php mit "Silence is golden" legen viele Plugins an. $rules[] = 'php_in_uploads'; } if ( $file->getSize() <= self::MAX_BYTES && is_readable( $path ) ) { $rules = array_merge( $rules, self::check_contents( (string) file_get_contents( $path ) ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- lokale Datei. } if ( 'uploads' === $kind && array( 'php_in_uploads' ) === $rules && self::is_silent_index( $path ) ) { continue; } if ( ! $rules ) { continue; } $findings[] = array( 'path' => self::relative( $path ), 'rules' => array_values( array_unique( $rules ) ), 'hash' => (string) sha1_file( $path ), ); if ( count( $findings ) >= self::MAX_FINDINGS ) { $complete = false; break 2; } } } $result = array( 'checked_at' => time(), 'files' => $checked, 'complete' => $complete, 'findings' => $findings, ); update_option( self::RESULT_OPTION, $result, false ); return $result; } /** Leere Schutzdatei, wie sie viele Plugins in ihre Upload-Ordner legen. */ private static function is_silent_index( $path ) { if ( 'index.php' !== strtolower( basename( $path ) ) || filesize( $path ) > 200 ) { return false; } $code = (string) file_get_contents( $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- lokale Datei. $code = trim( preg_replace( '#/\*.*?\*/|//[^\n]*|\#[^\n]*#s', '', $code ) ); return in_array( $code, array( '', '' ), true ); } private static function relative( $path ) { $path = wp_normalize_path( $path ); $base = wp_normalize_path( ABSPATH ); return 0 === strpos( $path, $base ) ? substr( $path, strlen( $base ) ) : $path; } /** * Letztes Ergebnis ohne ausgeblendete Treffer. * * @return array{checked_at:int,files:int,complete:bool,findings:array}|null */ public static function result() { $result = get_option( self::RESULT_OPTION ); if ( ! is_array( $result ) || ! isset( $result['findings'] ) ) { return null; } $result['findings'] = self::apply_ignores( (array) $result['findings'], self::ignores() ); return $result; } /** * Entfernt ausgeblendete Treffer. Ein Treffer bleibt ausgeblendet, solange * sich die Datei nicht aendert (Pfad und Pruefsumme). * * @param array $findings Treffer. * @param array $ignores Pfad => Pruefsumme. */ public static function apply_ignores( array $findings, array $ignores ) { return array_values( array_filter( $findings, function ( $finding ) use ( $ignores ) { return ! isset( $ignores[ $finding['path'] ] ) || $ignores[ $finding['path'] ] !== $finding['hash']; } ) ); } /** * Treffer, die beim vorigen Lauf noch nicht da waren (neue Datei oder * geaenderter Inhalt). */ public static function new_findings( array $findings, array $previous ) { $known = array(); foreach ( $previous as $finding ) { $known[ $finding['path'] . '|' . $finding['hash'] ] = true; } return array_values( array_filter( $findings, function ( $finding ) use ( $known ) { return ! isset( $known[ $finding['path'] . '|' . $finding['hash'] ] ); } ) ); } /** @return array */ public static function ignores() { $ignores = get_option( self::IGNORE_OPTION, array() ); return is_array( $ignores ) ? $ignores : array(); } /** Nach einer vollstaendigen Datei-Pruefung mitlaufen. */ public static function on_integrity_done( $result, $previous ) { if ( ! is_array( $result ) || ! empty( $result['partial'] ) ) { return; } $before = self::result(); $after = self::scan(); } /** Formular im Reiter „Dateien“: jetzt suchen, Treffer aus- oder einblenden. */ public static function handle() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to do this.', '360-orbit-security-digest' ), '', array( 'response' => 403 ) ); } check_admin_referer( self::ACTION ); $do = isset( $_POST['do'] ) ? sanitize_key( wp_unslash( $_POST['do'] ) ) : ''; $path = isset( $_POST['path'] ) ? sanitize_text_field( wp_unslash( $_POST['path'] ) ) : ''; if ( 'scan' === $do && Wp360_Security_Digest_Integrity::enabled() ) { self::scan(); } elseif ( 'ignore' === $do && '' !== $path ) { $stored = get_option( self::RESULT_OPTION ); foreach ( is_array( $stored ) ? (array) $stored['findings'] : array() as $finding ) { if ( $finding['path'] === $path ) { $ignores = self::ignores(); $ignores[ $path ] = $finding['hash']; update_option( self::IGNORE_OPTION, $ignores, false ); } } } elseif ( 'unignore_all' === $do ) { delete_option( self::IGNORE_OPTION ); } wp_safe_redirect( admin_url( 'admin.php?page=wp360-security-digest&tab=files#wp360-sd-heuristic' ) ); exit; } /** Kasten im Reiter „Dateien“. */ public static function render_box() { $result = self::result(); $ignored = count( self::ignores() ); $action = admin_url( 'admin-post.php' ); ?>

0 ) { ?>

$result ? (int) $result['checked_at'] : 0, 'count' => $result ? count( $result['findings'] ) : 0, ); } }