Skip to content

Instantly share code, notes, and snippets.

@bubba-h57
Created August 28, 2012 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bubba-h57/27d8515d2130ff84c9cb to your computer and use it in GitHub Desktop.
Save bubba-h57/27d8515d2130ff84c9cb to your computer and use it in GitHub Desktop.
Stripe CTF Level 1 - Solution Misuse of PHP Function on Untrusted Data
So let's step through the code and see what's happening:
creates $filename storing 'secret-combination.txt'
extract $_GET (all GET parameters supplied by the user)
if $attempt is set:
declare $combination with the trim()'d contents of $filename
if $attempt and $combination are equal
print contents of 'level02-password.txt'
else
print incorrect
So let's look at what extract() is actually doing...
int extract ( array &$var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix = NULL ]] )
Import variables from an array into the current symbol table.
Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table.
If extract_type is not specified, it is assumed to be EXTR_OVERWRITE.
Well, look at that, they didn't specify an extract_type, so by default it is EXTR_OVERWRITE, which is, "If there is a collision, overwrite the existing variable."
There was even a nice little warning for us,
Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.).
So now looking back at the code, we can see that they declare $filename before they use extract(), so this gives us the opportunity to create a collision and overwrite the existing variable with our GET parameters.
In simple terms, it will create variables depending on what you supply in your GET request. In this case we can see that our request /?attempt=SECRET creates a variable $attempt that stores the value "SECRET", so we could also send ”/?attempt=SECRET&filename=random_file.txt”. The extract() will now overwrite their original $filename with our supplied value, ”random_file.txt”.
So what can we do to make these match? You see how $combination is storing the result of file_get_contents() for the $filename, then using trim() on it. If file_get_contents() returns false due to a file not existing, trim() will then return an empty string. So if we supply a file that does not exist and an empty $attempt, they will match...
So let’s supply:
https://level01-x.stripe-ctf.com/user-xxxxxxxxxx/?filename=nada&attempt=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment