Aug 28, 2008

[AS] Apache Tomcat Directory Traversal Vulnerability を再現してみる。

Apache Tomcat の Directory Traversal 脆弱性が報告されている。 報告だけではイマイチ分かりにくいので再現実験を行ってみた。

SecurityForcus - Apache Tomcat Directory Traversal Vulnerability
http://www.securityfocus.com/archive/1/archive/1/495318/100/0/threaded
CVE-2008-2938
http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-2938

脆弱性の内容

SecurityForcus によると、

As Apache Security Team, this problem occurs because of JAVA side. If your context.xml or server.xml allows 'allowLinking'and 'URIencoding' as 'UTF-8', an attacker can obtain your important system files.(e.g. /etc/passwd)

Exploit
If your webroot directory has three depth(e.g /usr/local/wwwroot), An attacker can access arbitrary files as below. (Proof-of-concept)
http://www.target.com/%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/foo/bar
とのこと。allowLinking が true で且つ URIEncoding が UTF-8 に設定されている Apache Tomcat(〜6.0.16) で再現するそうだ。

allowLinking と URIEncoding

allowLinking と URIEncoding は、Apache Tomcat のドキュメントによると

Apache Tomcat Configuration Reference - The HTTP Connector
http://tomcat.apache.org/tomcat-6.0-doc/config/http.html
URIEncoding
This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.
Apache Tomcat Configuration Reference - The Context Container
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
allowLinking
If the value of this flag is true, symlinks will be allowed inside the web application, pointing to resources outside the web application base path. If not specified, the default value of the flag is false.
NOTE: This flag MUST NOT be set to true on the Windows platform (or any other OS which does not have a case sensitive filesystem), as it will disable case sensitivity checks, allowing JSP source code disclosure, among other security problems.
というものだそうだ。

脆弱性の再現

Apache Tomcat 6.0.16 を使用して脆弱性の再現実験を行った。 手順は以下の通り。

  1. Apache Tomcat 6.0.16 をインストール
  2. $CATALINA_HOME/conf/server.xml を編集
  3. $CATALINA_HOME/conf/context.xml を編集
  4. Apache Tomcat 6.0.16 を起動
  5. Web ブラウザより Apache Tomcat 6.0.16 にアクセスし、脆弱性を再現
server.xml はこんな感じ
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" 
               URIEncoding="UTF-8" />
  </Service>
</Server>
context.xml はこんな感じ
$CATALINA_HOME/conf/context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context allowLinking="true">
</Context>
Web ブラウザで http://localhost:8080/%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd にアクセスしたところ、脆弱性が再現できた。 [脆弱性が再現できた]

もう少し調べてみる

allowLinking, URIEncodign がそれぞれ設定されていない場合はどうなるか試してみた。
[allowLinkngを設定しない場合] [URIEncodingを設定しない場合]
どちらも脆弱性は再現できなかった。

URIEncoding を UTF-16 にした場合はどうなるか試してみた。
[URIEncodingをUTF-16にした場合]
脆弱性は再現できなかった。

%c0%ae って何?

脆弱性が再現できたところで一つ気になるのは、%c0%ae が何者なのか、ということ。

System.out.println("" + URLDecoder.decode("%c0%ae", "UTF-8"));
.
まぁ想像通りの結果。 要するに、http://localhost:8080/%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/%c0%ae%c0%ae/etc/passwd は Apache Tomcat 内部で http://localhost:8080/../../../../etc/passwd と解釈されるわけだ。 で、何らかの原因で(「Java 側の問題」と書いてあるが、何だろう?)パスがチェックをすり抜けてファイルアクセスに使用されてしまう、ということだろう。

結論

とにかく Apache Tomcat を脆弱性が修正されている 6.0.18 以上にバージョンアップする必要がある。 急げ。急げ。