DirectAdminde Saldırı Önleme Yolları
Sunucularınıza yönelik aşırı trafik veya şüpheli aktiviteler, kaynaklarınızın tükenmesine ve hizmet kesintilerine neden olabilir. DirectAdmin’de bu tür saldırılara karşı etkili önlemler alabilir ve sunucunuzun güvenliğini artırabilirsiniz. Bu makalede, gerçek bir senaryo üzerinden şüpheli trafik kaynağını nasıl tespit edeceğimizi, analiz edeceğimizi ve bu trafiği nasıl engelleyeceğimizi detaylı olarak ele alacağız.
Burada yazan kodlar tamamen öneri amaçlıdır. Lütfen sunucuyla ve sitenizle ilgili bir işlem yapmadan önce yedek aldığınızdan emin olun. Konfigrasyonlar sunucunuza veya sitenize uymayabilir. Örnek ip’ler KVKK gereği ** ile gizlenmiştir.
Şüpheli Trafik Tespiti
İlk adım, sunucunuzdaki aşırı trafiğin nereden geldiğini belirlemektir. Bunun için Apache’nin erişim loglarını analiz edebilirsiniz. DirectAdmin genellikle Apache veya Nginx web sunucusu ile çalışır ve log dosyaları şu konumda bulunur:
cat /var/log/httpd/access_log | awk '{print $1}' | sort | uniq -c | sort -nr | head
Örnek Çıktı:
KVKK için son 2 hane gizlenmiştir. Yukarıdaki sonuçlarda, 172.172.130.** IP adresinin diğerlerine kıyasla çok daha fazla trafik oluşturduğu görülmektedir. Bu IP’yi daha detaylı analiz etmemiz gerekiyor.
Şüpheli aktiviteleri belirlemek için en çok istek gönderen IP adresi: 159.89.10.**
Bir sonraki adımda bu IP’ye ait aktiviteleri görmek için şu komutu çalıştırabilirsiniz:
grep "159.89.10.12" /var/log/httpd/access_log
Çıktı Örneği:
Bu loglardan, 159.89.10.12 IP adresinden gelen isteklerin büyük olasılıkla kötü niyetli bir bot tarafından yapıldığı anlaşılabilir. İşte gözlemlenen durumlar:
Anormal İstekler:
- Standart olmayan HTTP metodları kullanılmış: OPTIONS, HELP, GET ile birlikte kötü amaçlı yollar (ör. /nice%20ports%2C/Tri%6Eity.txt%2ebak, /odinhttpcall1733302056).
- SIP/2.0 gibi protokoller kullanılarak tarama denemeleri yapılmış.
Hatalı Yanıt Kodları:
- 403 (Forbidden): Sunucu, bu isteklere erişimi reddetmiş.
- 406 (Not Acceptable): Sunucu, isteklerin içeriğini kabul etmemiş.
- 400 (Bad Request): Geçersiz istekler tespit edilmiş.
Bot Kullanımı:
- “Mozilla/5.0 (compatible; Odin; https://docs.getodin.com/)” gibi sahte bir User-Agent kullanılarak taramalar yapılmış.
IP’yi engelleyerek geçici bir çözüm elde edebilirsiniz fakat aşağıdaki örnekte olduğu gibi saldırılar devam edecektir:
Şimdi başka bir örnek yapalım.
cat /var/log/httpd/access_log | awk '{print $1}' | sort | uniq -c | sort -nr | head
Bu sefer net bir saldırgan yakaladık:
172.176.249.164 ip’si 172 defa tarama yapmış ne yaptığına bakalım:
grep "172.176.249.164" /var/log/httpd/access_log
Bu arkadaş kendi evi gidibi bir WordPress sitesindeki hemen her yere girmiş.
Bu IP’nin logları, özellikle WordPress hedefli kötü niyetli tarama girişimlerini gösteriyor. Tarama yapılan yolların birçoğu yaygın güvenlik açıkları veya zafiyetler içeren dosya ve dizinlere yönelik. Bu tür saldırıları önlemek için genel koruma önlemleri ve özel WordPress güvenliği sağlayabilirsiniz.
1. .htaccess ile Kötü Amaçlı Yolları Engelleme
Bu tür botların sıkça denediği yolları ve dosyaları engellemek için aşağıdaki kuralları .htaccess dosyanıza ekleyin:
<IfModule mod_rewrite.c>
RewriteEngine On
# phpMyAdmin and other access restrictions
RewriteCond %{REQUEST_URI} ^/phpMyAdmin$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/(about|admin|config|setup|test|xmlrpc)\.php$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/(cache|backup|tmp|logs)/ [NC,OR]
RewriteCond %{REQUEST_URI} \.(bak|tmp|old|log)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/\.env$ [NC,OR]
# PHPUnit and CGI-bin access restrictions
RewriteCond %{REQUEST_URI} ^/vendor/phpunit [NC,OR]
RewriteCond %{REQUEST_URI} ^/cgi-bin/ [NC,OR]
# Allow 127.0.0.1 server-status access
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.1$
RewriteCond %{REQUEST_URI} ^/server-status$ [NC]
RewriteRule .* - [L]
# Suspicious query string checks
RewriteCond %{QUERY_STRING} (allow_url_include|auto_prepend_file|eval-stdin|exec|phpinfo|system) [NC]
RewriteRule .* - [F,L]
# Block XML-RPC access completely
RewriteRule ^xmlrpc\.php$ - [F,L]
# Restrict access to .git and hidden files
RewriteCond %{REQUEST_URI} ^/(.git|.svn|.hg|.bzr|.cvs|.DS_Store) [NC]
RewriteRule .* - [F,L]
# WordPress critical file protections
RewriteRule ^wp-config\.php$ - [F,L]
RewriteRule ^readme\.html$ - [F,L]
RewriteRule ^license\.txt$ - [F,L]
# Block known shell and malicious file scans
RewriteCond %{REQUEST_URI} ^/(a|admin|autoload|classwithtostring|drop|eval|function|hack|license|phpinfo|shell|test|tmp)\.php$ [NC]
RewriteRule .* - [F,L]
# Referrer protection (allow CSS/JS)
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com/ [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|jpeg|png|gif|ico|svg|webp)$ [NC]
RewriteRule \.(jpg|jpeg|png|gif|svg|webp)$ - [F,L]
# Allow trusted search engine bots
RewriteCond %{HTTP_USER_AGENT} (Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot|facebot|ia_archiver) [NC]
RewriteRule .* - [L]
# Block specific bad bots
RewriteCond %{HTTP_USER_AGENT} (curl|wget|python|libwww-perl|nikto|bot|scanner|crawl|spider|httpclient|masscan|zgrab) [NC]
RewriteRule .* - [F,L]
</IfModule>
# Disable directory listing
Options -Indexes
# Restrict HTTP methods
<LimitExcept GET POST HEAD>
Deny from all
</LimitExcept>
# Restrict specific file types
<FilesMatch "\.(bak|old|ini|log|sql|sh|py|pl|cgi|env)$">
Order allow,deny
Deny from all
</FilesMatch>
# Protect critical files
<FilesMatch "^(\.htaccess|\.htpasswd|\.git|\.env|\.svn|composer\.json|composer\.lock)$">
Order allow,deny
Deny from all
</FilesMatch>
# Allow static files (CSS/JS/Images)
<FilesMatch "\.(css|js|jpg|jpeg|png|gif|ico|svg|webp|ttf|woff|woff2|eot|txt)$">
Order allow,deny
Allow from all
</FilesMatch>
# Add security headers
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>
Bunu 1 site için yaptık sunucudaki tüm siteler için yapmak istiyoruz.
DİKKAT AŞAĞIDAKİ İŞLEM TEHLİKELİDİR. LüTFEN YEDEKLİ ÇALIŞIN SİTELERİNİZ ÇALIŞMAYABİLİR. TEK SİTE İLE DENEME YAPARAK BAŞLAYINIZ!!!!
2. Tüm sunucu kullanıcıları için tekrarlama
DirectAdmin’deki tüm kullanıcıların .htaccess dosyalarına SSH üzerinden yukarıdaki önlemleri eklemek için şu adımları izleyebilirsiniz:
3. Örnek .htaccess Kuralları
Aşağıdaki içeriği bir dosyaya kaydedin. Bu dosyayı, tüm kullanıcıların .htaccess dosyalarına ekleyeceğiz.
cat <<EOF > /root/htaccess_security_rules
<IfModule mod_rewrite.c>
RewriteEngine On
# phpMyAdmin and other access restrictions
RewriteCond %{REQUEST_URI} ^/phpMyAdmin$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/(about|admin|config|setup|test|xmlrpc)\.php$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/(cache|backup|tmp|logs)/ [NC,OR]
RewriteCond %{REQUEST_URI} \.(bak|tmp|old|log)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/\.env$ [NC,OR]
# PHPUnit and CGI-bin access restrictions
RewriteCond %{REQUEST_URI} ^/vendor/phpunit [NC,OR]
RewriteCond %{REQUEST_URI} ^/cgi-bin/ [NC,OR]
# Allow 127.0.0.1 server-status access
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.1$
RewriteCond %{REQUEST_URI} ^/server-status$ [NC]
RewriteRule .* - [L]
# Suspicious query string checks
RewriteCond %{QUERY_STRING} (allow_url_include|auto_prepend_file|eval-stdin|exec|phpinfo|system) [NC]
RewriteRule .* - [F,L]
# Block XML-RPC access completely
RewriteRule ^xmlrpc\.php$ - [F,L]
# Restrict access to .git and hidden files
RewriteCond %{REQUEST_URI} ^/(.git|.svn|.hg|.bzr|.cvs|.DS_Store) [NC]
RewriteRule .* - [F,L]
# WordPress critical file protections
RewriteRule ^wp-config\.php$ - [F,L]
RewriteRule ^readme\.html$ - [F,L]
RewriteRule ^license\.txt$ - [F,L]
# Block known shell and malicious file scans
RewriteCond %{REQUEST_URI} ^/(a|admin|autoload|classwithtostring|drop|eval|function|hack|license|phpinfo|shell|test|tmp)\.php$ [NC]
RewriteRule .* - [F,L]
# Referrer protection (allow CSS/JS)
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com/ [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|jpeg|png|gif|ico|svg|webp)$ [NC]
RewriteRule \.(jpg|jpeg|png|gif|svg|webp)$ - [F,L]
# Allow trusted search engine bots
RewriteCond %{HTTP_USER_AGENT} (Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot|facebot|ia_archiver) [NC]
RewriteRule .* - [L]
# Block specific bad bots
RewriteCond %{HTTP_USER_AGENT} (curl|wget|python|libwww-perl|nikto|bot|scanner|crawl|spider|httpclient|masscan|zgrab) [NC]
RewriteRule .* - [F,L]
</IfModule>
# Disable directory listing
Options -Indexes
# Restrict HTTP methods
<LimitExcept GET POST HEAD>
Deny from all
</LimitExcept>
# Restrict specific file types
<FilesMatch "\.(bak|old|ini|log|sql|sh|py|pl|cgi|env)$">
Order allow,deny
Deny from all
</FilesMatch>
# Protect critical files
<FilesMatch "^(\.htaccess|\.htpasswd|\.git|\.env|\.svn|composer\.json|composer\.lock)$">
Order allow,deny
Deny from all
</FilesMatch>
# Allow static files (CSS/JS/Images)
<FilesMatch "\.(css|js|jpg|jpeg|png|gif|ico|svg|webp|ttf|woff|woff2|eot|txt)$">
Order allow,deny
Allow from all
</FilesMatch>
# Add security headers
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>
EOF
4. Tüm Kullanıcıların Web Dizinlerini Tespit Edin
DirectAdmin’de kullanıcıların web dizinleri genellikle /home/{kullanıcı_adı}/domains/{alan_adı}/public_html yolunda bulunur.
Tüm kullanıcıların .htaccess dosyalarını bulmak için:
find /home -type d -name public_html
5. Güvenlik Kurallarını Tüm Kullanıcıların .htaccess Dosyalarına Ekleyin
Şimdi güvenlik kurallarını otomatik olarak tüm public_html dizinlerine uygulayabilirsiniz.
for dir in $(find /home -type d -name public_html); do
if [ -f "$dir/.htaccess" ]; then
# Eğer .htaccess dosyası varsa yeni bir satır ekle ve ardından kuralları ekle
echo "" >> "$dir/.htaccess"
cat /root/htaccess_security_rules >> "$dir/.htaccess"
else
# Eğer .htaccess dosyası yoksa oluştur ve kuralları ekle
cp /root/htaccess_security_rules "$dir/.htaccess"
fi
done
6. Web Sunucusunu Yeniden Başlat:
Değişikliklerin etkili olması için web sunucusunu yeniden başlat:
# Apache için
systemctl restart httpd
# LiteSpeed için
/usr/local/lsws/bin/lswsctrl restart
7. Tarama işlemini tekrar başlat
Gelen kullanıcıların azaldığını görüyoruz
Eğer Tek Seferde Tüm htaccess’i değiştirmek istiyorsanız:
for user in $(ls /home); do
if [ -d "/home/$user/public_html" ]; then
cat <<EOF > /home/$user/public_html/.htaccess
# BEGIN WordPress
# "BEGIN WordPress" ve "END WordPress" arasındaki yönergeler (satırlar)
# dinamik olarak oluşturulmuştur ve yalnızca WordPress süzgeçleri ile düzenlenmelidir.
# Bu imler arasındaki yönergelerde yapılan değişikliklerin üzerine yazılır.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
# phpMyAdmin and other access restrictions
RewriteCond %{REQUEST_URI} ^/phpMyAdmin$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/(about|admin|config|setup|test|xmlrpc)\.php$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/(cache|backup|tmp|logs)/ [NC,OR]
RewriteCond %{REQUEST_URI} \.(bak|tmp|old|log)$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/\.env$ [NC,OR]
# PHPUnit and CGI-bin access restrictions
RewriteCond %{REQUEST_URI} ^/vendor/phpunit [NC,OR]
RewriteCond %{REQUEST_URI} ^/cgi-bin/ [NC,OR]
# Allow 127.0.0.1 server-status access
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.1$
RewriteCond %{REQUEST_URI} ^/server-status$ [NC]
RewriteRule .* - [L]
# Suspicious query string checks
RewriteCond %{QUERY_STRING} (allow_url_include|auto_prepend_file|eval-stdin|exec|phpinfo|system) [NC]
RewriteRule .* - [F,L]
# Block XML-RPC access completely
RewriteRule ^xmlrpc\.php$ - [F,L]
# Restrict access to .git and hidden files
RewriteCond %{REQUEST_URI} ^/(.git|.svn|.hg|.bzr|.cvs|.DS_Store) [NC]
RewriteRule .* - [F,L]
# WordPress critical file protections
RewriteRule ^wp-config\.php$ - [F,L]
RewriteRule ^readme\.html$ - [F,L]
RewriteRule ^license\.txt$ - [F,L]
# Block known shell and malicious file scans
RewriteCond %{REQUEST_URI} ^/(a|admin|autoload|classwithtostring|drop|eval|function|hack|license|phpinfo|shell|test|tmp)\.php$ [NC]
RewriteRule .* - [F,L]
# Referrer protection (allow CSS/JS)
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?yourdomain\.com/ [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|jpeg|png|gif|ico|svg|webp)$ [NC]
RewriteRule \.(jpg|jpeg|png|gif|svg|webp)$ - [F,L]
# Allow trusted search engine bots
RewriteCond %{HTTP_USER_AGENT} (Googlebot|Bingbot|Slurp|DuckDuckBot|Baiduspider|YandexBot|Sogou|Exabot|facebot|ia_archiver) [NC]
RewriteRule .* - [L]
# Block specific bad bots
RewriteCond %{HTTP_USER_AGENT} (curl|wget|python|libwww-perl|nikto|bot|scanner|crawl|spider|httpclient|masscan|zgrab) [NC]
RewriteRule .* - [F,L]
</IfModule>
# Disable directory listing
Options -Indexes
# Restrict HTTP methods
<LimitExcept GET POST HEAD>
Deny from all
</LimitExcept>
# Restrict specific file types
<FilesMatch "\.(bak|old|ini|log|sql|sh|py|pl|cgi|env)$">
Order allow,deny
Deny from all
</FilesMatch>
# Protect critical files
<FilesMatch "^(\.htaccess|\.htpasswd|\.git|\.env|\.svn|composer\.json|composer\.lock)$">
Order allow,deny
Deny from all
</FilesMatch>
# Allow static files (CSS/JS/Images)
<FilesMatch "\.(css|js|jpg|jpeg|png|gif|ico|svg|webp|ttf|woff|woff2|eot|txt)$">
Order allow,deny
Allow from all
</FilesMatch>
# Add security headers
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>
EOF
chown $user:$user /home/$user/public_html/.htaccess
chmod 644 /home/$user/public_html/.htaccess
fi
done
Sonra tekrar restart yapın
# Apache için
systemctl restart httpd
# LiteSpeed için
/usr/local/lsws/bin/lswsctrl restart
Tüm soru, öneri ve görüşleriniz için İletişim linkini kullanabilirsiniz.