Cinnamon Dynamics, an innovative technology company, provides a service for the public to execute short scripts to query some limited information about the company. To combat abuse, they’ve instated a requirement for all scripts to be approved by a company employee before they can be executed. Approved scripts are granted a “script token” that allows them to be executed an indefinite amount of times, so long as the script is not modified. Unfortunately, it seems that malicious actors have managed to circumvent the security system…
if (typeof token !== 'string' || typeof script !== 'string') { return res.render('execute', { error: 'Token and script must be provided and must be strings.' }) }
if (!script.trim().length) { return res.render('execute', { error: 'Please provide a script to execute.' }) }
if (!safeCompare(hash.digest('hex'), token)) { return res.render('execute', { error: 'Script token is invalid! ' + 'Contact a Cinnamon Dynamics employee to get your script ' + 'approved and receive a valid token for it.' }) }
app.listen(PORT, () =>console.log(`Server listening on port ${PORT}`))
Basically, the server takes the script, prepends a secret of unknown length to the script, and hashes it. If the hash doesn’t match the user-inputted token, it doesn’t execute the script.
If you navigate to the site, the user is presented with 4 different scripts and corresponding tokens to execute. Most notably, there is one titled “Unfinished Script”:
1 2 3 4 5 6 7
const file = await Deno.readTextFile('sales.txt')
const sales = file.split('\n')
console.log('Number of sales:', sales.length)
// TODO: finish this script
Notably, there is a comment at the end of this script, which would allow any bytes we put there to be ignored when the script is executed.
This, in fact, is an instance of a well-known attack – a hash length extension attack. A hash length extension attack works on various hashes, including the SHA family, and essentially allows you to “extend” a given plaintext by a user-chosen string and calculate the corresponding hash if the provided hash is equal to hash(secret + plaintext).
I won’t explain it, but there are a lot of good resources online explaining how it works. This is what I used to solve it though.
Also note that we have to brute-force the length of the hash, which is what I do in the solve script. Also also I spent an hour trying to figure out how to send the payload properly until I realized the answer was just to do .decode('latin').