]*(?:php|phtml|phar)/i', $trim ) ) { $signals[] = 'PHP-related FilesMatch rule'; $suspicious_lines[] = $trim; } if ( preg_match( '/RewriteRule\s+.*\s+[^\s]*[A-Za-z0-9_.-]+\.php(?:\?|\s|$)/i', $trim ) && false === stripos( $trim, 'index.php' ) ) { $signals[] = 'rewrite to non-index PHP file'; $suspicious_lines[] = $trim; } if ( preg_match( '/[A-Za-z0-9+\/]{100,}={0,2}/', $trim ) ) { $signals[] = 'highly encoded-looking block'; $suspicious_lines[] = $trim; } } $ioc_hits = ACHWPA_IOC::matches( $content ); if ( ! empty( $ioc_hits ) ) { $signals[] = 'known indicator'; } if ( empty( $signals ) ) { return array(); } $level = count( array_unique( $signals ) ) >= 2 ? 'Review' : 'Info'; if ( ! empty( $ioc_hits ) ) { $level = 'Review'; } $preview = implode( "\n", array_slice( array_unique( $suspicious_lines ), 0, 30 ) ); if ( '' === $preview ) { $preview = $content; } return array( new ACHWPA_Finding( array( 'category' => '.htaccess', 'level' => $level, 'location' => '.htaccess', 'indicator' => implode( ', ', array_unique( array_merge( $signals, $ioc_hits ) ) ), 'explanation' => __( 'One or more rewrite or access-control patterns may affect how PHP code is reached. Legitimate security, caching and hosting rules can produce similar syntax, so manual review is required.', 'aiutocomputerhelp-persistence-audit' ), 'preview' => ACHWPA_Preview::sanitize( $preview, 1500 ), 'artifacts' => $this->extract_artifacts( $content ), 'identifiers' => $this->extract_identifiers( $content ), ) ), ); } private function extract_artifacts( $text ) { $out = array(); if ( preg_match_all( '/[A-Za-z0-9_.-]{3,}\.php\b/', $text, $matches ) ) { foreach ( $matches[0] as $file ) { $out[] = 'FILE:' . basename( $file ); } } return array_slice( array_values( array_unique( $out ) ), 0, 40 ); } private function extract_identifiers( $text ) { $ids = array(); if ( preg_match_all( '/\b[a-f0-9]{6,32}\b/i', $text, $matches ) ) { foreach ( $matches[0] as $candidate ) { if ( ! ctype_digit( $candidate ) ) { $ids[] = strtolower( $candidate ); } } } return array_slice( array_values( array_unique( $ids ) ), 0, 30 ); } }