#!/usr/bin/perl -w # proxy.pl - phProxy v0.1a # Coded by kGen, http://own-the.net (C) 2008. # This is free code. Use it in any way you want, but don't remove the credits. use strict; use IO::Socket::INET; $|++; #Auto flush $SIG{CHLD} = 'IGNORE'; #Detach child processes my $authToken = 'SOME_SECRET_VALUE'; #Authenticate with the proxy my $proxyServ = 'SOME.DOMAIN.COM'; my $proxyPath = '/PATH/proxy.php'; my $localPort = 13268; #This will go to the browser settings my $servSock = IO::Socket::INET->new( Proto=>'tcp', LocalPort=>$localPort, Listen=>1, Reuse=>1 ) or die "[-] Can't listen on local socket. $!\n"; print "[+] Server started.\n"; while ($servSock){ my $data; my $buf; my $packet; my $req; my $resp; #Large random value to act as a multipart/form-data boundary my $rndBoundary = int(rand(4294967296)). int(rand(4294967296)). int(rand(4294967296)). int(rand(4294967296)); #Obtain client socket. This comes from the browser my $clientSock = $servSock->accept() or die "[-] Accept failed. $!\n"; #F**k off and continue if(fork() != 0){ next; #This is the parent process, continue back to the accept() thig. } #If we're here after the fork(), this is the child process. $req = ''; #Read lines of data from the local socket while(defined ($buf = <$clientSock>)){ $req .= $buf; if(index($req, "\r\n\r\n") != -1){ #Stop the loop on the first CRLF last; } } #Extra content with the request? Read this too $req =~ /content\-length: (.*)\n/i; if (defined $1){ read ($clientSock, $buf, $1); $req .= $buf; } # $data and $packet represent a multipart/form-data request that is sent to proxy.php $data = "--$rndBoundary\r\n". "content-disposition: form-data; name=\"auth\"\r\n". "\r\n". "$authToken\r\n". "--$rndBoundary\r\n". "content-disposition: form-data; name=\"data\"\r\n". "Content-Transfer-Encoding: binary\r\n". "\r\n". "$req\r\n". "--$rndBoundary--\r\n"; $packet = "POST $proxyPath HTTP/1.0\r\n". "Host: $proxyServ\r\n". "Content-type: multipart/form-data, boundary=$rndBoundary\r\n". "Content-Length: ". length ($data). "\r\n". "\r\n". "$data"; #Socket to proxy server my $proxySock = IO::Socket::INET->new( Proto=>'tcp', PeerAddr=>$proxyServ, PeerPort=>80 ) or die "[-] Can't connect to proxy. $!\n"; print $proxySock $packet; #Send the request to the proxy.php script my $flag = 0; $resp = ''; #Read the response from proxy.php while(defined ($buf = <$proxySock>)){ if($flag == 0 && index($resp, "\r\n\r\n") != -1){ #Wait for CRLF $flag = 1; $resp .= $buf; }elsif($flag == 0){ #No CRLF yet $resp .= $buf; } if ($flag == 1){ #Past the first CRLF? if (!$clientSock){ last; #If the client closed the socket, what's the point living? }else{ print $clientSock $buf; #Forward the stream to the client } } } close($proxySock); close($clientSock); exit 0; #Finally, end the execution of the current process } #close ($servSock); #This won't happen.