攻擊者架設伺服器(如 evil.com),設定反向代理至你的站點(victim.com)。用戶訪問 evil.com 實際獲取的是你的內容,卻以攻擊者域名呈現。危害:盜用頻寬/資源、SEO 重複內容降權、廣告聯盟帳號風險、竊取用戶 Cookie 等。
<script>
(function() {
var allowed = ['victim.com', 'www.victim.com'];
var current = document.domain.toLowerCase();
if (!allowed.includes(current)) {
// 強制跳回官方域名,保留路徑
window.location.href = 'https://www.victim.com' + window.location.pathname + window.location.search;
}
})();
</script>
<?php
function isLegitimateHost(): bool {
$allowed = ['victim.com', 'www.victim.com'];
$host = $_SERVER['HTTP_HOST'] ?? '';
// 若反向代理正確轉發了 Host,直接比對
if (in_array($host, $allowed, true)) return true;
// 部分代理會將原始 Host 放在 X-Forwarded-Host
$forwardedHost = $_SERVER['HTTP_X_FORWARDED_HOST'] ?? '';
if (in_array($forwardedHost, $allowed, true)) return true;
return false;
}
if (!isLegitimateHost()) {
// 選項 1:直接拒絕
http_response_code(403);
exit('非法反向代理訪問');
// 選項 2:301 重導向至官方域名(保留 URI)
// $uri = $_SERVER['REQUEST_URI'];
// header('Location: https://www.victim.com' . $uri, true, 301);
// exit;
}
?>
server {
server_name victim.com www.victim.com;
# 拒絕非法 Host
if ($host !~* ^(victim\.com|www\.victim\.com)$) {
return 403 "Forbidden: Illegal reverse proxy";
}
# 或強制重導向
# if ($host != 'www.victim.com') {
# return 301 https://www.victim.com$request_uri;
# }
}
.htaccess (需 mod_rewrite)RewriteEngine On
# 條件:Host 非允許清單
RewriteCond %{HTTP_HOST} !^(victim\.com|www\.victim\.com)$ [NC]
# 動作:禁止存取 (或改寫為 RewriteRule ^ https://www.victim.com%{REQUEST_URI} [R=301,L])
RewriteRule ^ - [F,L]
<!-- HTTP 標頭方式(推薦) -->
<?php header('X-Frame-Options: SAMEORIGIN'); ?>
<!-- 或 CSP -->
<?php header("Content-Security-Policy: frame-ancestors 'self'"); ?>
<!-- JS 兜底(舊瀏覽器) -->
<script>if (top !== self) top.location = self.location;</script>
X-Frame-Options 與 CSP: frame-ancestors 防 iframe 嵌入。X-Forwarded-For、X-Forwarded-Host 信任清單,避免被偽造。