Level06
PHP Command execution
Once we login as level06 we get 2 files in our new home directory :
one binary file called level06
and one php script called level06.php
when we execute the binary file we get the following :
It looks like the binary file is executing.
if we provide an argument to this binary it returns us the following :
From this we can guess that the command line arguments passed to the binary file level06
is passed to the level06.php and the level06.php reads the content of the file and prints it to the standard output.
In the level06.php file we have the following :
What does the code means ?
It seems the code contain 2 functions and it prints the result returned by the function x
.
The code first :
Calls the function
x
with 2 parameters, In our case the first parameter is the file name.The function x calls
file_get_contents
which returns the content of the file asString
It then calls the
preg_replace
which uses RegEx to find and replace specific string patterns.In this case it searches for the pattern
([x ...])
Searches for the character
[
and replaces with(
Searches for the character
]
and replaces with)
once all the placements are done we return the result which is then used to print
to the standard output.
While we are at it we can see that the function Y also dows basic RegEx replacements.
What can we exploit ?
There are some thing we must notice.
The version of PHP in the ISO provided for Snow Crash project
What happens when we copy the code to a machine with an up to date version of php and execute it.
The version of php in the Virtual Machine (ISO) is 5.3.10.
When we execute the code using an up to date version of PHP we get the folloing error:
It says that the /e
modifier is no longer supported. We can see the /e
in the above code on line 11 :
In the official php dock they says
When preg_replace() is called with the /e modifier, the interpreter must parse the replacement string into PHP code once for every replacement made
So this means that whatever is parsed is transformed into php code.
An other interesting this we can find in the official dock is the definition of \\2
in the second parameter. It says under the replacement
section :
The string or an array with strings to replace. If this parameter is a string and the
pattern
parameter is an array, all patterns will be replaced by that string. If bothpattern
andreplacement
parameters are arrays, eachpattern
will be replaced by thereplacement
counterpart. If there are fewer elements in thereplacement
array than in thepattern
array, any extrapattern
s will be replaced by an empty string.
So from this we understand that any pattern found by the first parameter, the send group of that pattenr will be passed to the function y
Here is an exemple using the regex101.com
So from the pattern from the file level06.php (line 11) we know we have to gave something that start with [x
and end with ]
and whetever inside will be treated as php code.
But this is not as simple as puting anything you want because if we put the followiing code :
It seems to treat our command as a string. After a little bit of research and trial, i have found that the complex syntax
works.
We can use the execution operators
( ` ) to execute shell commands. So to execute the getflag program we can use the following command :
And this reveals the flag!
Password for next level
So the Password to connect to the account level07 is wiok45aaoguiboiki2tuin6ub
Command summery
Last updated
Was this helpful?