Clickjacking saldırılarından korunmak için hem client-side hem de server-side önlemler alınması gerekir. İşte en etkili korunma yöntemleri:
1. X-Frame-Options Header
Bu HTTP header, web sayfanızın iframe içinde yüklenmesini kontrol eder:
// X-Frame-Options header değerleri
// Hiç iframe'de yüklenmesin
X-Frame-Options: DENY
// Sadece aynı domain'de iframe'de yüklenebilsin
X-Frame-Options: SAMEORIGIN
// Belirtilen domain'de iframe'de yüklenebilsin
X-Frame-Options: ALLOW-FROM https://trustedsite.com
// Nginx konfigürasyonu
add_header X-Frame-Options "SAMEORIGIN" always;
// Apache konfigürasyonu
Header always set X-Frame-Options "SAMEORIGIN"
// PHP'de header set etme
header("X-Frame-Options: SAMEORIGIN");
2. Content Security Policy (CSP)
Modern ve daha esnek çözüm. Frame-ancestors directive kullanılır:
// CSP frame-ancestors directive
// Hiçbir yerden iframe edilemesin
Content-Security-Policy: frame-ancestors 'none';
// Sadece aynı origin'den iframe edilebilsin
Content-Security-Policy: frame-ancestors 'self';
// Belirtilen domain'lerden iframe edilebilsin
Content-Security-Policy: frame-ancestors 'self' https://partner.com;
// Meta tag ile CSP
<meta http-equiv="Content-Security-Policy"
content="frame-ancestors 'self'">
// Nginx'te CSP header
add_header Content-Security-Policy "frame-ancestors 'self';" always;
Browser Desteği Karşılaştırması:
• X-Frame-Options: Eski tarayıcıları destekler, basit kullanım
• CSP frame-ancestors: Modern yaklaşım, daha esnek kontrol
• Önerilen: Her ikisini birden kullanın (defense in depth)
3. Frame Busting Scripts
JavaScript tabanlı korunma (ikincil savunma olarak):
// Basit frame busting script
if (top !== self) {
top.location = self.location;
}
// Daha güvenli frame busting
(function() {
if (top !== self) {
try {
if (top.location.hostname !== self.location.hostname) {
throw new Error("Clickjacking detected!");
}
} catch (e) {
top.location = self.location;
}
}
})();
// Modern frame busting
if (window.top !== window.self) {
document.write('<div style="position:fixed;top:0;left:0;width:100%;height:100%;background:#fff;z-index:999999;">Bu sayfa frame içinde çalışmaz</div>');
throw new Error("Frame busting activated");
}
4. Sandbox Attribute Bypass Korunması
HTML5 iframe sandbox özelliği frame busting'i devre dışı bırakabilir:
// Saldırgan sandbox kullanımı
<iframe src="https://victim.com"
sandbox="allow-forms allow-scripts"></iframe>
// allow-top-navigation olmadığı için frame busting çalışmaz
// Korunma: Sandbox detection
if (window.frameElement && window.frameElement.hasAttribute('sandbox')) {
// Sandbox detect edildi, önlem al
document.body.innerHTML = '<h1>Güvenlik Uyarısı</h1>';
}
// Alternative: Parent frame kontrolü
try {
if (window.parent.location.href) {
// Erişim başarılıysa aynı origin
console.log("Same origin frame");
}
} catch (e) {
// Cross-origin frame detected
window.top.location = window.location;
}
Çok Katmanlı Savunma Stratejisi:
1. Server-side headers (X-Frame-Options + CSP)
2. JavaScript frame busting
3. Sandbox detection
4. User-agent ve referrer kontrolü
5. İşlem onayları için CAPTCHA