# Pikachu中URL重定向的示例分析
## 1. URL重定向基礎概念
### 1.1 什么是URL重定向
URL重定向(URL Redirection)是指將用戶請求從一個URL自動跳轉到另一個URL的技術。這種技術廣泛應用于:
- 網站重構后的舊鏈接處理
- 縮短長網址
- 負載均衡
- 多域名統一訪問
- **安全場景中的漏洞利用**
### 1.2 重定向類型對比
| 類型 | HTTP狀態碼 | 特點 |
|------|------------|------|
| 301重定向 | 301 | 永久移動,搜索引擎會更新索引 |
| 302重定向 | 302 | 臨時移動,搜索引擎保留原URL |
| 307重定向 | 307 | 臨時重定向,強制保持請求方法 |
| 反射型重定向 | 302 | 目標URL包含用戶輸入 |
## 2. Pikachu靶場環境搭建
### 2.1 環境準備
```bash
# 下載Pikachu漏洞練習平臺
git clone https://github.com/zhuifengshaonianhanlu/pikachu
cd pikachu
# 配置PHP環境(示例為Docker方式)
docker run -d -p 80:80 -v $(pwd):/var/www/html php:7.4-apache
在Pikachu平臺中,URL重定向漏洞位于:
/unsafe/redirect.php
<?php
if(isset($_GET['url']) && $_GET['url'] != null){
$url = $_GET['url'];
header("Location: $url");
exit;
}
?>
$_GET['url']
參數構造惡意鏈接:
http://target.com/redirect.php?url=http://evil.com/phishing
// 結合XSS的復合攻擊
window.open('http://target.com/redirect.php?url=javascript:alert(document.cookie)')
// 繞過基礎防御
http://target.com/redirect.php?url=data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==
// 方案1:白名單校驗
$allowed = ['example.com', 'trusted.org'];
$parsed = parse_url($_GET['url']);
if(!in_array($parsed['host'], $allowed)){
die('Invalid redirect target');
}
// 方案2:正則匹配
if(!preg_match('/^https?:\/\/(www\.)?(example\.com|trusted\.org)/i', $_GET['url'])){
die('Invalid URL format');
}
# Nginx配置示例
add_header X-Frame-Options "SAMEORIGIN";
add_header Content-Security-Policy "default-src 'self'";
Match: ^Location:.*$
Replace: Location: http://evil.com
import requests
from urllib.parse import urlparse
def check_redirect(url):
try:
r = requests.get(url, allow_redirects=False)
if 300 <= r.status_code < 400:
loc = r.headers.get('Location', '')
if urlparse(loc).netloc not in ['example.com', '']:
print(f"Vulnerable redirect found: {loc}")
except Exception as e:
print(f"Error: {e}")
check_redirect("http://test.com/redirect.php?url=http://external.com")
某社交平臺的回調URL驗證缺陷:
原始請求:
https://api.social.com/oauth?redirect_uri=https://client.com/callback
攻擊利用:
https://api.social.com/oauth?redirect_uri=https://attacker.com
攻擊鏈:
1. 用戶收到郵件包含bank.com/redirect?url=phish.com
2. 跳轉后顯示與銀行相同的登錄頁面
3. 用戶輸入憑證后被竊取
SecRule REQUEST_URI "@contains redirect.php" \
"id:1001,phase:1,deny,msg:'Potential open redirect'"
注:本文所有測試均在授權環境下進行,實際滲透測試需獲得書面授權。URL重定向雖然看似簡單,但可能成為攻擊鏈的關鍵環節,建議開發者在實現重定向功能時采用”默認拒絕”的安全原則。 “`
該文檔共約2350字,包含: - 技術原理說明 - 實際漏洞分析 - 防御方案對比 - 自動化檢測方法 - 修復路線建議 - 延伸知識拓展
格式采用標準Markdown,支持代碼高亮、表格展示和層級標題,可直接用于技術文檔發布。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。