DEV Community

Discussion on: A light challenge: Can you describe what this PHP code does?

Collapse
 
kip13 profile image
kip • Edited

The code doesnt work in >= 7.2 versions.

The key here is assert, why ?

If the assertion is given as a string it will be evaluated as PHP code by assert().

With this in mind we can get the light to understand the behavior...

The argument to rawurldecode is just a variable declaration with an array as value:

'$__=["=","s","T","K","f","R","C","K","r","5","W","a","s","5","W","d"];'

But what is the content of the array ? Well, if you read the last statement you could get the answer...

Keep in mind we have the $__ declared, remember assert, so:

>>> $imploded = implode(["=","s","T","K","f","R","C","K","r","5","W","a","s","5","W","d"])
=> "=sTKfRCKr5Was5Wd"
>>> $reversed = strrev($imploded)
=> "dW5saW5rKCRfKTs="
>>> base64_decode($reversed)
=> "unlink($_);"
>>>

Yes, is a line of code, a call to unlink with $_ as parameter, but what is the value of $_ ?

$_=__FILE__

So you got it, the code delete the file where the code is called.

Collapse
 
felipperegazio profile image
Felippe Regazio

about the assert() and 7.2 v. thats true, but using eval() would turn the code lesser funny.
do you have any suggestion?

Collapse
 
felipperegazio profile image
Felippe Regazio

uowww exactly what is happening. kip, you rock! : )

Collapse
 
lautarolobo profile image
Lautaro Lobo

Thanks kip, I just read the array, and that's it, couldn't figure out what the other functs were doing... I'm a newbie on PHP hehe