#!/usr/bin/perl
# Made by Edoardo Mantovani, 2020

sub BEGIN{

no strict;
use File::chmod qw( chmod );

if(! ( -e 'Executor.pl' ) ){ # check if Executor.pl exists, if not..
my $Executor_Script = ' 

if(! ( `which aircrack-ng` ) ){
  die();
}

use Term::ANSIColor qw( colored );

my $Title = "          888
                       888
                       888
88888b.  .d88b. 888d888888
888  88bd8P  Y8b888P   888
888  88888888888888    888
888 d88PY8b.    888    888
88888P    Y8888 888    888
888
888
888";
print colored( ["green"], $Title ),"\n";

print "insert the attacked MAC: ";
my $networkAddress2 = <STDIN>;

chop ( $networkAddress2 ); ## remove the "ret" char

use Net::Write::Layer2;
use Net::Pcap qw( pcap_lookupdev pcap_open_live pcap_loop pcap_close);

my $pcap_err = "";
my $Internet_DEV = pcap_lookupdev( \$pcap_err ); 

## enter in MONITOR MODE with airmon-ng

`airmon-ng start $Internet_DEV `;
sleep(3);

## search MONITOR MODE interface

sub search_monitor(){
  require Net::Interface;
  my @every_int = Net::Interface->interfaces();
  foreach ( @every_int ){
    if($_ =~ "mon"){
      return $_;
  }
}
  }

my $Mon_dev = &search_monitor();

my $pcap_live = pcap_open_live( $Mon_dev, 1024, 1, 0, \$pcap_err ) or die "Error!\n";

my $sender; ## used to initialize Net::Write::Layer2

sub Take_and_Send(){
  use Net::Frame::Layer::LLTD qw( :consts);
  my $layer_pkt = Net::Frame::Layer::LLTD->new(
    tos => NF_LLTD_TOS_QUICK_DISCOVERY,
    function => NF_LLTD_FUNCTION_DISCOVER,
    reserved => 0,
    networkAddress1 => "58:94:6b:8e:99:fc", # your mac address
    networkAddress2 => $networkAddress2,
    identifier => rand(20000),
    ) or die "1";
  
  $layer_pkt->pack; ## assemble the packet
  print colored(["red"], "Raw packet is: ") .  $layer_pkt->dump() . "\n";

  my ( $user_data, $head, $packet ) = @_;
  print $head . "\n";
  $sender = Net::Write::Layer2->new(
    dev => $Mon_dev,
  ) or die "Err\n";

  $sender->open();
  $sender->send( $layer_pkt );
}

pcap_loop( $pcap_live, 25, \&Take_and_Send(), ""); # create a loop for the next 25 packets

## stop the MONITOR mode and close every istances

` airmon-ng stop $Mon_dev `;
$sender->close();
pcap_close( $pcap_live );
';
# End of Embedded script

open (EXECUTOR, '>', 'Executor.pl'); 
print EXECUTOR "#!/usr/bin/perl \n";
print EXECUTOR "$Executor_Script\n";
close EXECUTOR;
chmod("u+x", 'Executor.pl'); # like chmod +x <file>
}
 
if(! ( -e 'screen.txt') ){
  open(SCREEN, '>', 'screen.txt');
  print SCREEN "focus up\n";
  print SCREEN "title 'Wireless scanner'\n";
  print SCREEN "screen perl SLL1\n";
  print SCREEN "split\n";
  print SCREEN "resize -b +7 \n";
  print SCREEN "focus bottom\n";
  print SCREEN "title 'Wireless Sender'\n";
  print SCREEN "focus down\n";
  print SCREEN "screen perl Executor.pl \n";
  close SCREEN;
  ` screen -c 'screen.txt' `; 
  }
}

sub END{

use strict;
no strict 'refs';
no strict 'subs';
use File::Path 'rmtree';
use Term::Multiplexed qw( multiplexed ); 
use Term::ANSIColor qw(colored);
use Net::Bluetooth qw( get_remote_devices ); # Bluetooth detector utility
use Net::Wireless::802_11::WPA::CLI; # wireless WPA_SUPPLICANT interface

if( multiplexed ){ # detect if the code is running in a multiplexed screen

rmtree('screen.txt');  
#rmtree('Executor.pl');

my $scan = Net::Wireless::802_11::WPA::CLI->new();

my $x = 0;
my @BSSID;
sub Wireless_Scan(){
  $scan->scan();
  foreach ( $scan->scan_results() ){
    if($_ =~ /:/){
      push @BSSID, $_;
    }else{
       if(length($_->{ssid}) != 25){
         while(length($_->{ssid}) != 25){    # leverage the distance between the SSID and the '|' 
            chop($_->{ssid}) if (length($_->{ssid}) > 25);
            $_->{ssid} .= " " if (length($_->{ssid}) < 25);
            }
          } 
     print  colored(['red'], $_->{ssid}, '   |  ', colored(['cyan'],$BSSID[$x]), ' ', colored(['green'], $_->{frequency}), ' ', colored(['yellow'], $_->{flags}), "\n"); # print various informations in a fashion/colored way
     $x++; 
}  
  
  }
    }

while(1){
  printf "\033c"; # clear completely the screen
  sleep (5);
  print colored(['bold blue'], "Wireless Scan\n");
  print colored(['bold green'], "SSID                           BSSID                  SECURITY\n");  # Wireless columns
  print &Wireless_Scan(); 
  print colored(['bold blue'], "Bluetooth Scan\n");
  print colored(['bold green'], "Name                            Address\n"); # Bluetooth columns
   }

}
  }
