Linux中的sudo和su


首先,实地看看sudo和su命令的位置和相关属性:

[email protected]:~# whereis sudo
sudo: /usr/bin/sudo /usr/lib/sudo /usr/bin/X11/sudo /usr/share/man/man8/sudo.8.gz

[email protected]:~# whereis su
su: /bin/su /usr/share/man/man1/su.1.gz

[email protected]:~# ls -l /usr/bin/sudo
-rwsr-xr-x 1 root root 156708 Mar 13 00:27 /usr/bin/sudo

[email protected]:~# ls -l /bin/su
-rwsr-xr-x 1 root root 35300 Feb 17  2014 /bin/su

注意最后的”ls -l”命令中看到的’s’字符,setuid位,参考学习:Linux 文件系统安全攻略。然后进入正文:

搜索关键字:

sudo su 环境变量

==

当前用户的环境变量不会应用到sudo启动的程序,除非使用-E选项

-E, –preserve-env

Indicates to the security policy that the user wishes to preserve their existing environment variables.  The security policy may return an error if the user does not have permission to preserve the environment.

$ sudo -E pacman -Syu

如果经常需要这样做,可以在~/.bashrc(或其他shell配置文件)中加入命令别名:

alias sudo="sudo -E"

在/etc/sudoers中添加以下内容作用相同:

Defaults !env_reset

可以把需要传递环境变量的命令设置到env_keep:

Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"

==

su 与 su – 的区别
$ su username

shell会切换到username的身份,但是shell的环境变量还是和切换前的一样,注意,这样可能会运行不了username的某些程序——因为某些环境变量的问题。

$ su - username

shell会切换到username的身份,但是shell的环境变量会变成username身份下的环境变量。所以建议在切换用户时使用后一种用法。

==

sudo命令的-i选项

-i, –login

Run the shell specified by the target user’s password database entry as a login shell.  This means that login-specific resource files such as .profile or .login will be read by the shell.  If a command is specified, it is passed to the shell for execution via the shell’s -c option.  If no command is specified, an interactive shell is executed.  sudo attempts to change to that user’s home directory before running the shell.  The command is run with an environment similar to the one a user would receive at log in.  The Command Environment section in the sudoers(5) manual documents how the -i option affects the environment in which a command is run when the sudoers policy is in use.

看上去类似于上面的 su – ,都是使用被切换用户自己的环境变量,不过暂未测试两者之间的区别,可以参考后面给出的几个链接。

==

参考链接:
更多参考链接:
, ,

《“Linux中的sudo和su”》 有 10 条评论

  1. 滥用 sudo 命令绕过受限 shell 进行权限提升的方法
    http://touhidshaikh.com/blog/?p=790
    `
    What is SUDO?
    Sudoer FIle Syntax.
    Exploiting SUDO user:
    /usr/bin/find
    /usr/bin/nano
    /usr/bin/vim
    /usr/bin/man
    /usr/bin/awk
    /usr/bin/less
    /usr/bin/nmap ( –interactive and –script method)
    /bin/more
    /usr/bin/wget
    /usr/sbin/apache2
    `

  2. CVE-2019-14287:linux sudo bug
    https://www.4hou.com/vulnerable/20881.html
    `
    研究人员在Linux SUDO命令中发现了一个bug,非授权的用户可以利用该bug以root权限运行命令。幸好该漏洞不是以标准配置下运行,因此大多数Linux服务器不受影响。

    在Linux操作系统下执行命令时,非特权的用户可以用sudo命令来以root权限来运行命令,前提是要知道root用户密码或提前被授予权限。

    可以通过在/etc/sudoers配置文件中加入特殊命令来配置Sudo命令为允许用户以其他用户运行命令。比如,下面的命令就可以让用户test以其他非root用户来运行/usr/bin/vim和 /usr/bin/id命令。
    test ALL = (ALL, !root) /usr/bin/vim
    test ALL = (ALL, !root) /usr/bin/id

    Test用户要运行上面的命令,就可以用sudo -u来指定要运行的用户。比如:
    sudo -u bleeping-test vim

    在Linux中创建用户时,每个用户都会有一个UID。

    用户在使用sudo命令时还可以用UID来替代用户名。比如,下面的命令就用bleeping-test用户的UID来替代用户名了:
    sudo -u#1002 vim

    苹果安全研究人员 Joe Vennix 发现sudo命令中存在一个安全漏洞,用户利用该漏洞可以通过在sudo命令中将UID 设为-1或4294967295来以root权限运行sudo命令。

    比如,下面的命令就利用该漏洞来以root权限启动 /usr/bin/id ,但是test用户的 /etc/sudoers文件中是没有这样的权限的。
    sudo -u#-1 id

    虽然该bug很强大,但是只有通过sudoers配置文件授予用户这样的权限该漏洞才可能运行。如果没有配置的话,该漏洞是无法利用的,而大多数Linux发行版中默认是不配置的。

    # 攻击演示

    为了利用该漏洞,用户需要为命令配置sudoer指令来启动其他命令。

    在下面的例子中,研究人员使用的是VIM命令:
    test ALL = (ALL, !root) /usr/bin/vim

    在VIM中,用户可以用:!命令启动其他程序。比如,在VIM中,可以用!ls 来在当前文件夹中执行ls命令。

    如果使用 sudo -u#-1 vim命令来利用该漏洞,VIM就会以root启动。用户可以执行 !whoami 命令来进行确认。
    `
    https://www.sudo.ws/alerts/minus_1_uid.html
    https://www.bleepingcomputer.com/news/linux/linux-sudo-bug-lets-you-run-commands-as-root-most-installs-unaffected/

  3. Impost3r — A linux password thief
    https://github.com/ph4ntonn/Impost3r
    `
    Impost3r是一个利用C语言编写,用来窃取linux下各类密码(ssh,su,sudo)的工具。用户可使用此程序在普通用户权限下,制造水坑,窃取合法用户的密码

    # 特性
    自动擦除行为痕迹
    通过DNS协议传输结果
    用户无感

    # 依赖
    gcc
    `

  4. `
    mark@lunchbox:~$ cat /etc/passwd

    www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
    syslog:x:101:104::/home/syslog:/bin/false

    mark@lunchbox:~$ sudo su www-data
    This account is currently not available.
    mark@lunchbox:~$ sudo su syslog
    mark@lunchbox:~$

    mark@lunchbox:~$ sudo -u www-data /bin/bash
    www-data@lunchbox:~$

    (ixyzero)$ sudo -u tomcat7 /bin/bash

    We trust you have received the usual lecture from the local System
    Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

    [sudo] password for ixyzero:
    (ixyzero)$
    (ixyzero)$ sudo su –
    # sudo -u tomcat7
    usage: sudo -h | -K | -k | -V
    usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
    usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
    usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-u user] [VAR=value] [-i|-s] []
    usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-u user] file …
    # sudo -u tomcat7 /bin/bash
    (tomcat7)$ du -sh /var/lib/tomcat7/* 2>/dev/null

    `

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注