COMP6841 Something Awesome

z5418112 (Sam Zheng)

JSCalc

By: makelaris

30 minutes

Easy
JavaScript
RCE

This particular challenge was a simple one, but it was a good introduction to the concept of client-side attacks, and as my first CTF within this project, I figured it was a good starting point just to warm up.

JSCalc application

JSCalc application

So, from the little description that was given, it was clear from the get-go that this website was using an eval() function to actually run the calculator. Given that I was already very familiar with JavaScript, I knew that this was a very potential vector for remote code execution.

I then downloaded the source code and snooped around in then to look for more clues, and to confirm whether or not this attack vector was actually feasible.

Poking around, I see a file called calculatorHelper.js. Hmm, looks fishy. Let's take a look inside.

calculate(formula) {
try {
return eval(`(function() { return ${formula} ;}())`);

Wow, would you look at that. Un-filtered user input going directly into the eval() function. Interestingly, it took a little playing around for me to figure out what we could do. Given that the eval is calling an anonymous function that then returns the formula that was given, I figured that we actually needed to use a second eval to be able to do whatever I wanted.

From here, I jumped on a local Node instance just to ensure I was getting the syntax right, before I proceeded to try it on the website again. I got to this point:

Testing with listing files

Testing with listing files

Great, our payload is indeed working, but there's no flag file! Re-checking the overall structure of the source code, I noticed that the file.txt was likely located in the parent folder. No big deal, we can change our payload very easily.

Found the flag file!

Found the flag file!

The rest is history from here - we just change readdirSync to readFileSync instead.

Pwned :)

Pwned :)

Reflection and Learnings

Next Writeup

Baby Nginxatsu