LoginSignup
2
2

More than 3 years have passed since last update.

AppGoatで学ぶディレクトリトラバーサル

Last updated at Posted at 2019-09-22

ディレクトリトラバーサルの概要

traverseという単語は横断するという意味であり、悪意のある利用者がパラメータに「 ../」といったディレクトリを横断するキーワードを含め、
サービス提供者が予期していないディレクトリへのアクセスを目論む攻撃をディレクトリトラバーサルと呼びます。

AppGoat_attack.PNG
※出典:AppGoat

AppGoatの例がWindows Server 2003で大変な歴史を感じます。

ディレクトリトラバーサルの対策

対策自体は簡単で、パラメータにディレクトリトラバーサルを引き起こしそうなものが含まれていたら弾く処理をいれるだけです。
いくつかの言語毎の対策を並べます。

C++の場合

DirectoryTravarse.cpp
 if( NULL != strstr(urlPath, "../") || 
          NULL != strstr(urlPath, "..\\") )
          return -1;

C#の場合(ファイル名だけ抜き取ってこちらの想定外のディレクトリにアクセスさせない)

DirectoryTravarse.cs
      const string DIRECTORY_PATH = @"C:\Files\";

      //ファイル名取得
      string fileName = Path.GetFileName(path);

      //パス結合
      string path = Path.Combine(DIRECTORY_PATH, fileName);

Pythonの場合(ディレクトリトラバーサルを引き起こすかどうか判定するコード)

DirectoryTravarse.py
import os 

def is_directory_traversal(file_name):
    current_directory = os.path.abspath(os.curdir)
    requested_path = os.path.relpath(file_name, start=current_directory)
    requested_path = os.path.abspath(requested_path)
    common_prefix = os.path.commonprefix([requested_path, current_directory])
    return common_prefix != current_directory

Pythonは以下から引用したのですが、単純にディレクトリを横断されなきゃ良いと考えてしまっては防げない問題の指摘もあり、勉強になります。
https://codeday.me/jp/qa/20190222/272788.html

言語ごとに最適な方法があるのできちんと意識する事がまず一歩ですね。
AppGoatだけでは内容が古く、別途調べて知識を補完する必要があると強く感じた章でした。

2
2
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
2
2