LoginSignup
0
0

ディレクトリトラバーサル

Last updated at Posted at 2024-05-18

はじめに

ディレクトリバーサルについてまとめる

ディレクトリトラバーサルとは

本来想定していたパスを遡って自由にファイルを読み書きされてしまう脆弱性のこと。

クエリ情報経由でパスを受け取る以下のコードがあったとき、

download.php
$fl = $_GET['path'] ?? "default.jpg";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename= "' . $fl . '"');
print file_get_contents("./doc/{$fl}");

ユーザーが
http://localhost/download.php?path=../../../../apache2/conf/httpd.conf
のようなアドレスを指定するとhttpd.confを入手できてしまう。
(file_get_contents()が相対パスも受け入れ可能なため)

本来は/docフォルダの配下だけにアクセスさせるつもりだったのが想定している公開フォルダを超えて、公開すべきでない上位フォルダのファイルが勝手に参照できてしまう。

対策

リクエスト情報としてディレクトリ情報を受け渡ししないこと。
ファイル名のみを指定する。

download.php
$fl = basename($_GET['file_name']) ?? 'default.jpg';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename= "' . $fl . '"');
print file_get_contents("./doc/{$fl}");
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0