Sun, May 10, 2021 7:11 PM
CTF
web
get-parameter-length-bypass
nodejs
spawn
I did not solve the challenge in time
I found the solution on discord, later
This write-up helps you understand the detailed solution
n
n
as an array like n[]
. Now, how big the input is, array length, that is no. of elements in an array is going to stay 1.Take a look at following javascript code
Array is concatenated with a string -
It's like js tries to convert the elemnts in array into strings and performs the concatenation. If multiple arguments are specifies, it adds a ,
(comma) between the elements. So it a single element is provided the 'hello'
and ['hello']
are treated same in certain scenario. So let's make use of this later.
In line 45
in above source code, there length check of n
using length
attribute. This attribute can also be used with the array to give number of elements in the array.
Look the code below -
Notice the difference!!
So, if the parameter is passed as an array rather a string, that would bypass the length check. And same time the array will be treated as a string by JS and it is passed as an argument to a binary.
The request to get the primes count -
Trying larger number (more than 8 digits) as input -
Send the same number as an array -
Cool. We didn't hit the check now. But the binary doesn't accept this number.
As mentioned in the description, about memory issues
, an ideal thought would be memory corruption
, with a hypothesis, may be length of input is not checked in the binary but only has been checked in the JS.
With this in mind, trying a larger number to overflow the buffer may cause a Segmentation Fault
.
Trying larger input -
Bingo!. We got the flag.
Wait, what ??
How could a Segmentation Fault would give the flag ?
The binary uses SIGSEGV
signal and sigsegv_handler
which are usually used for handling segmentation faults (Probably a bad idea! :). Here, in this case, the seg fault generates a SIGSEGV
which evokes the sigsegv_handler
which is just a some function to do something. Here the handler just prints the flag. Authour probably wanted to make the binary part of challenge, simple.
sdctf{B3$T_0f-b0TH_w0rLds}
Happy Hacking!