Tropical API
JavaScript Regex Shenanigans
Challenge
Web, 23 Solves
This internal API was accidentally exposed to the public. Fortunately, the developer left a backup of the backend source code for us.
User input appears to be properly validated. Can you find a way to get the flag?
Solution
The premise of this challenge was simple - we had "SSRF-as-a-service", and the flag is in one of the request headers. We need to control fqdn
to make a request to an arbitrary URL, where we are listening for a request.
The problem is that there is a very restrictive regex check that only allows us to use hexadecimal characters in the fqdn
.
If we look at the documantation for RegExp.prototype.test()
, however, we would notice a very interesting behaviour when test()
is used with a regex containing the global flag.
This means that if the regex is being tested multiple times for bad characters, each time the string is only searched from the previously-found index onwards.
This, combined with the fact that we are allowed to provide multiple fqdn
s, means that we can bypass the restrictions by simply submitting the same payload multiple times. For instance, if we use the following:
The first time the regex is tested, lastIndex
is set to 32 since the disallowed character, x
, was found at the end of the string. The second time the regex is tested, no match would be found and test()
would return false
.
Great! We can bypass the regex restriction. The next problem is that when converted into a Buffer
from hex, the length of the Buffer
must be 16. This means we need a minimum of 32 characters in our fqdn
.
Luckily, the Buffer
stops when the first non-hexadecimal character is encountered, so it's fine to have non-hexadecimal characters after the first 32 bytes.
But how do we provide a URL that starts with 32 bytes of hexadecimal characters? My teammate Enyei found this very helpful article that describes the various ways that IP addresses can be represented.
In this case, the octal notation proved very helpful. We could lead with as many 0
s as we want, which is a hexadecimal character. Then, we can use any octal IP address converter to convert our public IP address to octal form. For example:
The ending #
will turn the trailing .ping-proxy/ping
into a URL fragment, making the final URL simply that of our public IP address.
This allows us to receive the request and get our flag.
Last updated