黑客防线,在攻与防的对立统一中寻求突破!2001年创刊的黑客技术专业刊物!

设为首页
收藏本站
联系我们
网站导航


黑客论坛 - 公共交流区 - VoIP技术研究 - 浏览 - [分享]一个针对asterisk服务器的BOT
您是本贴第 185 位浏览者 本版版主
浏览: [分享]一个针对asterisk服务器的BOT
  • funhoo
  • 等级: 普通用户
  • 发贴: 9 贴
  • 货币: 0 金币
  • 积分: 180 分
  • 经验: 100 点
  • 体力: 430 点
  • 注册: 2008-06-17
[分享]一个针对asterisk服务器的BOT


其它有关技术交流可参与QQ群讨论:  5873047   56389932
                                  



use POE;
use POE::Component::IRC;
use Net::Telnet;         # Used for connecting to the asterisk manager port.
use DBI;

use warnings;
use strict;

my $count;
my $dbh;
my $outpt;

my $name      = "Telephreak PBX";       # Your PBX's named

############################################################################
## IRC server/nick/port information                                       ##
############################################################################

my $ircserver = "irc.telephreak.org";
my $nickname = "AstBot";
my $channel   = "#testing";
my $port      = "6667";
my $debug     = "1";                   # Set to 0 for no debug output

############################################################################
## Asterisk manager port authentication information                       ##
############################################################################

my $username = "myusername";
my $secret   = "mypassword";
my $serverip = "127.0.0.1";

############################################################################
## Asterisk MySQL information (used to retreive # of VMBs                 ##
############################################################################
my $myusername = "asterisk";
my $mysecret   = "mysecret";
my $myhost     = "127.0.0.1";
my $myvm       = "asterisk_vm";   # Asterisk Voicemail DB

############################################################################
## Where to look for help                                                 ##
############################################################################

my $help       = "Please see:http://www.telephreak.org/astbot.txt";

# Various var's that probably don't concern you.

my $dbase;
my @dataout;
my $channelpattern="Seconds:";
my @outarray;
my $outmsg;

my $i;
my $stripmsg;
my $nick;
my $ts;
my $kicker;
my $kicked;

my $tn = new Net::Telnet (Port => 5038,
                          Prompt =>  '/.*[\$%#>] $/',
                          Output_record_separator => '',
                          Errmode    => 'return'
                          );


sub CHANNEL () { "$channel" }

# Create the component that will represent an IRC network.

POE::Component::IRC->new("astbot");

# Create the bot session.  The new() call specifies the events the bot
# knows about and the functions that will handle those events.

POE::Session->create(
    inline_states => {
        _start     => \&bot_start,
        irc_001    => \&on_connect,
        irc_public => \&on_public,
    },
);

# The bot session has started.  Register this bot with the "astbot"
# IRC component.  Select a nickname.  Connect to a server.

sub bot_start {
    my $kernel  = $_[KERNEL];
    my $heap    = $_[HEAP];
    my $session = $_[SESSION];

    $kernel->post( astbot => register => "all");

    $kernel->post( astbot => connect =>
          { Nick     => $nickname,
            Username => $nickname,
            Ircname  => $nickname,
            Server   => $ircserver,
            Port     => $port,
            Debug    => $debug,
          }
    );

}

# The bot has successfully connected to a server.  Join a channel.

sub on_connect {
    $_[KERNEL]->post( astbot => join => CHANNEL );
}

# The bot has received a public message.  Parse it for commands, and
# respond to interesting things.

sub on_public {
   my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ];
   my $nick = &remove_unwanted(( split /!/, $who )[0]);
   my $channel = $where->[0];

   my $ts = scalar localtime;      # Time for output

############################################################################
## Get the number of active channels                                      ##
############################################################################

   if ( $msg eq ".channels" )
           {
        print " [$ts] <$nick:$channel> $msg\n";
        &connect_manager();
        $tn->print("Action: Status\n\n");
        @dataout = $tn->waitfor('/Event: StatusComplete/');
        $tn->print("Action: Logoff\n\n");
        $_ = $dataout[0];
        $count=s/($channelpattern)/$1/g;
        if ( $count eq "" )
                {
                $count="0";
                }
        $outmsg="There are currently $count active channels up and operational on the $name";
        $kernel->post( astbot => 'privmsg', $channel , $outmsg );
        }

############################################################################
## Get total number of voice mail boxes                                   ##
############################################################################


     if ( $msg eq ".voicemail" )
        {
        print " [$ts] <$nick:$channel> $msg\n";
        $dbase = "DBI:mysql:database=$myvm;host=$myhost";
        $dbh = DBI->connect($dbase, $myusername,  $mysecret) || die "Can't connect to asterisk_vm";
        $outpt = $dbh->prepare("select count(mailbox) from users");
        $outpt->execute || die "Can do query";
        $count=$outpt->fetchrow_array;
        $outmsg="There are currently $count voicemail boxes on the $name.";
        $kernel->post( astbot => 'privmsg', $channel, $outmsg);
        print "$outmsg \n";
        }

###########################################################################
## Get information about conferences                                     ##
###########################################################################

     if ( $msg eq ".meetme" )
        {
        print " [$ts] <$nick:$channel> $msg\n";
        &connect_manager();
        $tn->print("Action: Command\nCommand: meetme\n\n");
        @dataout = $tn->waitfor('/--END COMMAND--/');
        $tn->print("Action: Logoff\n\n");
        @outarray = split('\n', $dataout[0]);
        foreach $i (4,5,6,7)
                {
                       $kernel->post( astbot => 'privmsg', $channel , $outarray[$i] );
                print "$outarray[$i] \n";
                }
        }

###########################################################################
## Get system uptime (last reload,  etc)                                 ##
###########################################################################

     if ( $msg eq ".uptime" )
             {
         print " [$ts] <$nick:$channel> $msg\n";
         &connect_manager();
         $tn->print("Action: Command\nCommand: show uptime\n\n");
         @dataout = $tn->waitfor('/--END COMMAND--/');
         $tn->print("Action: Logoff\n\n");
         @outarray = split('\n', $dataout[0]);
         $outmsg = "$name Uptime:";
         $kernel->post( astbot => 'privmsg', $channel , $outmsg );
         foreach $i (4,5)
                        {
                 $kernel->post( astbot => 'privmsg', $channel, $outarray[$i] );
                 print "$outarray[$i] \n";
                 }
         }

############################################################################
## Get version of Asterisk thats running                                  ##
############################################################################

         if ( $msg eq ".version" )
         {
         print " [$ts] <$nick:$channel> $msg\n";
         &connect_manager();
         $tn->print("Action: Command\nCommand: show version\n\n");
         @dataout = $tn->waitfor('/--END COMMAND--/');
         $tn->print("Action: Logoff\n\n");
         @outarray = split('\n', $dataout[0]);
         foreach $i (4,5)
                 {
                   $kernel->post( astbot => 'privmsg', $channel, $outarray[$i] );
                   print "$outarray[$i] \n";
                 }
          }

############################################################################
## HALP!  HALP!                                                           ##
############################################################################

           if ( $msg eq ".help" )
             {
             print " [$ts] <$nick:$channel> $msg\n";
             $kernel->post( astbot => 'privmsg', $channel, $help );
             }

}

#############################################################################
## Trust nobody.  Ever.                                                    ##
#############################################################################

sub remove_unwanted
{
our $s;
local($s) = @_;
$s =~ s/[^A-Za-z0-9]//g if defined $s;
$s =~ tr/A-Z/a-z/;
return $s;
}

############################################################################
## Connect to the manager port,  and log in please.....                   ##
############################################################################

sub connect_manager
{
$tn->open("$serverip");
$tn->waitfor('/0\n$/');
$tn->print("Action: Login\nUsername: $username\nSecret: $secret\n\n");
$tn->waitfor('/Authentication accept*/') || die "Can't login in";
}


############################################################################
## Ready! Aim! FIRE!                                                      ##
############################################################################

$poe_kernel->run();
exit 0;


SUCK-O社团是progenic榜排名世界第一的黑客组织,总部设在德国,社区内相关技术水平处于国际领先地位,欢迎大家和我们进行技术交流。

交流群 5873047

2008-8-1 14:41:46