File: //remove_htaccess_hacked/remove_htaccess_debug_final.sh
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# TRACE=1 per debug shell
TRACE="${TRACE:-0}"
if [ "$TRACE" -eq 1 ]; then
set -x
fi
MODE="${1:-dry}" # dry (default) | apply
LOGFILE="${2:-./remove_htaccess_debug_final.log}"
TMPDIR="$(mktemp -d)"
BLOCK_MATCH_SHORT='\\.(py|exe|php|suspected)'
BLOCK_MATCH_LONG='wp-blog-header\.php'
# Trap generico che non si attiva per exit status trattati in modo non-fallibile
trap 'rc=$?; if [ $rc -ne 0 ] && [ $rc -ne 1 ]; then echo "ERROR: script terminated with code $rc" | tee -a "$LOGFILE"; tail -n 50 "$LOGFILE" || true; rm -rf "$TMPDIR"; exit $rc; else rm -rf "$TMPDIR"; fi' ERR INT TERM EXIT
echo "[$(date --iso-8601=seconds)] Start script (mode=$MODE). Log: $LOGFILE" | tee "$LOGFILE"
echo "Temp dir: $TMPDIR" | tee -a "$LOGFILE"
dump_snippet() {
local file="$1"; local start="$2"; local lines="${3:-20}"
echo "----- snippet from $file (line $start, $lines lines) -----" | tee -a "$LOGFILE"
# usa awk per stampare linee anche se ultimo record non termina con newline
awk "NR>=$start && NR<=$start+$lines-1 { printf(\"%4d: %s\n\", NR, \$0) }" "$file" | tee -a "$LOGFILE"
echo "----- hexdump (first 256 bytes) -----" | tee -a "$LOGFILE"
head -c 256 "$file" | hexdump -C | tee -a "$LOGFILE"
echo "--------------------------------------" | tee -a "$LOGFILE"
}
PERL_PROGRAM='
use strict;
use warnings;
local $/ = undef;
# leggiamo tutto anche se file non termina con newline
my $content = do { local $/; <STDIN> };
$content =~ s/\r\n?/\n/g;
my $orig = $content;
my $removed = 0;
while ($content =~ m{<FilesMatch\b([^>]*)>(.*?)</FilesMatch>}gis) {
my $attrs = $1;
my $inner = $2;
my $test = lc($attrs . " " . $inner);
if ($test =~ qr/\.\(py\|exe\|php\|suspected/ && $test =~ /deny from all/) {
$content =~ s{<FilesMatch\Q$attrs\E>\Q$inner\E</FilesMatch>}{}si;
$removed++;
next;
}
if ($test =~ /wp-blog-header\.php/ && $test =~ /allow from all/) {
$content =~ s{<FilesMatch\Q$attrs\E>\Q$inner\E</FilesMatch>}{}si;
$removed++;
next;
}
}
$content =~ s/\n{3,}/\n\n/g;
# assicurati che il file finisca sempre con newline per evitare messaggi "No newline at end of file"
if (length $content && substr($content,-1) ne "\n") { $content .= "\n"; }
if ($removed) {
print $content;
exit 0;
} else {
exit 1;
}
'
# raccogli .htaccess
mapfile -d '' FILES < <(find . -type f -name ".htaccess" -print0)
if [ "${#FILES[@]}" -eq 0 ]; then
echo "[$(date --iso-8601=seconds)] Nessun file .htaccess trovato." | tee -a "$LOGFILE"
rm -rf "$TMPDIR"
exit 0
fi
echo "[$(date --iso-8601=seconds)] Trovati ${#FILES[@]} file .htaccess" | tee -a "$LOGFILE"
modified_count=0
checked_count=0
for f in "${FILES[@]}"; do
checked_count=$((checked_count+1))
echo "Processing [$checked_count/${#FILES[@]}]: $f" | tee -a "$LOGFILE"
if ! grep -Eiq "$BLOCK_MATCH_SHORT|$BLOCK_MATCH_LONG" "$f"; then
echo " -> No quick pattern match (skip)." | tee -a "$LOGFILE"
continue
fi
echo " -> Quick match passato: mostro prime 40 righe per controllo" | tee -a "$LOGFILE"
# awk per mostrare anche l'ultima riga senza newline
awk 'NR<=40 { printf("%4d: %s\n", NR, $0) }' "$f" | tee -a "$LOGFILE"
out_tmp="$TMPDIR/out.$$.$(basename "$f")"
# chiamiamo perl leggendo il file via stdin per avere controllo sull'exit code
if perl -0777 -e "$PERL_PROGRAM" < "$f" > "$out_tmp" 2>>"$LOGFILE"; then
# perl ha rimosso almeno un blocco (exit 0)
if cmp -s "$out_tmp" "$f"; then
echo " -> Perl ha prodotto output ma file identico: mostro snippet diagnostico." | tee -a "$LOGFILE"
dump_snippet "$f" 1 80
rm -f "$out_tmp"
continue
fi
if [ "$MODE" = "dry" ]; then
echo " -> DRY RUN: il file verrebbe modificato. Mostro diff (prime 200 righe):" | tee -a "$LOGFILE"
diff -u "$f" "$out_tmp" | sed -n '1,200p' | tee -a "$LOGFILE"
modified_count=$((modified_count+1))
rm -f "$out_tmp"
continue
fi
cp --preserve=mode,timestamps "$f" "$f.bak"
mv "$out_tmp" "$f"
echo " -> APPLY: aggiornato. Backup: $f.bak" | tee -a "$LOGFILE"
modified_count=$((modified_count+1))
else
# perl ha exit != 0 (probabilmente 1 = nessuna modifica)
perl_rc=$?
if [ "$perl_rc" -eq 1 ]; then
echo " -> Perl: nessuna modifica necessaria (exit 1)." | tee -a "$LOGFILE"
else
echo " -> Perl terminato con codice $perl_rc. Controlla il log:" | tee -a "$LOGFILE"
tail -n 200 "$LOGFILE" | sed -n '1,200p' | tee -a "$LOGFILE"
fi
rm -f "$out_tmp"
fi
done
echo "[$(date --iso-8601=seconds)] Controllati: $checked_count. Modificati: $modified_count." | tee -a "$LOGFILE"
rm -rf "$TMPDIR"
echo "Fine." | tee -a "$LOGFILE"
exit 0