Hi everyone,
In this article I will be doing Canape machine on Hack the Box.
Lets start..
Enumeration
First we need to know which ports are open. So lets start with port-knocking
1 |
|
As you can see there is a git repo, probably contains source code of the app. I wanna look at that web page first.
Hmm, CouchDB looks like interesting. At this point I examined all pages on this site.
And decided to enumerate the directories on this site, so I ran gobuster, but didn’t find anything. Because if I go to the page which there isn’t on the site, the app gives me 200 ok and shows me a weird random code. Must has a custom error handler.
So I decided to take a look at that git repo I found earlier.
Edited my /etc/hosts
file to access the repo.
1 |
|
There it is, __init__.py
Other files are not important. Just some html and js files.
Now we can examine the source code of the app. I’m using Visual Studio Code as my text editor.
CouchDB running on http://localhost:5984/ noted. Also there is a custom error handler I talked about it.
But the important one is cPickle. Its a built-in python module that allows you to serialize & deserialize objects.
Here is the full gist of __init__.py
https://git.io/fAiTB
Inside of the check function item = cPickle.loads(data)
This line will help us to get RCE (Remote Code Execution). If we post something to http://canape.htb/submit with character and quote parameters that function will create a file in /tmp folder. And write our parameters in that file.
outfile = write(char + quote)
And than if we post to http://canape.htb/check with parameter id that function will load our data.
If our data contains malicious code, also will be executed.
Now we can build our payload. But first, character parameter must contain the one of items on the whitelist, don’t forget that.
Otherwise we will get 500 Internet Server Error.
Exploitation
WHITELIST = [ "homer", "marge", "bart", "lisa", "maggie", "moe", "carl", "krusty" ]
I wrote a simple script to test it if it works..
So I added “homer” at the end of my payload. I added at the begining before but when I tried to execute my test script, it didn’t worked.
Ran it and boooom, the command was executed.
Than I build a little script to get a reverse-shell.
Here is the gist link. https://git.io/fAiTU
Now we are in as www-data user. We can start to enumerate from the inside.
But first lets quickly upgrade our shell.
CTRL + Z
stty raw -echo
fg
Further Enumeration
First I want it to check couchdb. I didn’t know how to query that database, so I searched on google.
1 |
|
Tip: You can prettify that values in vscode by pressing CTRL + K F
Hmmm thats interesting. There is passwords table, probably with usefull content.
I wanna check it out, but it gives me an error that says I’m not authorized.
At this point, I know the version of couchdb, so I searched for common exploits and vulnerabilities. And I came across with these:
https://www.exploit-db.com/exploits/44498/
https://serverfault.com/questions/742184/couchdb-user-creation-without-authentication-standard-behavior
1 |
|
Yea, that worked, we added an admin user with name and password pwn .
1 |
|
Lets have a look at passwords database:
1 |
|
At this point, I spent my time to understand that values. And I spent to much time to figure it out.
Searched on google how to read those.
Than I understand, I didn’t know that base64 values are the actual id values like sql table id’s.
Tip: http://docs.couchdb.org/en/stable/api/index.html
1 |
|
We got a password.
Lets cat the /etc/passwd for enumerating users on system.
1 |
|
Maybe that password we found belongs to homer ?
1 |
|
We got homer now. user.txt is in this home folder.
Privilege Escalation
First lets check sudo -l
1 |
|
We can install pip packages as root, great.
I can grab the flag easily, let me show you.
1 |
|
Why it worked ? -r flag means take all requipments from given txt file. And I gave it root.txt
Then pip want it to collect the first line and boooom. (first-line was the flag)
And this is other technique for reverse-shell. Create empty folder and name it this little script as setup.py and run sudo pip install .
1 |
|
1 |
|
Listening machine:
1 |
|
Done, now we are root.
It think it was an easy box, but getting the user is a bit hard. Because most of us didn’t knew about couchdb.
I used pouchdb and couchdb together one of my projects but even I didn’t knew how to query it using http api ?!
This box, pushes us to learn some.
See you guys later.