#! /usr/bin/perl 

#
# My crappy perl script to find access points on the wired network
#
# Version 0.01 BETA (Because everything is BETA!)
#
# Author: Paul Asadoorian (paul@pauldotcom.com)
# Web: http://pauldotcom.com
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Large portions of this script taken from the example in Nmap Network Scanning
# Link: http://nmap.org/book/output-formats-xml-with-perl.html
#

use Getopt::Std;
use Nmap::Parser;

#
# Do the usage stuff and get options
# 
print "rogueapdetect.pl v0.01 - ( paul\@pauldotcom.com )\n",('-'x50),"\n\n";
	
getopts('hi:');

die "Usage: $0 [-i <hosts>]\n"
	unless ($opt_i);

#
# Send Email Function
#
# Simple Email Function
# ($to, $from, $subject, $message)
sub sendEmail
{
	my ($to, $from, $subject, $message) = @_;
	my $sendmail = '/usr/sbin/sendmail';
	open(MAIL, "|$sendmail -oi -t");
	print MAIL "From: $from\n";
	print MAIL "To: $to\n";
	print MAIL "Subject: $subject\n\n";
	print MAIL "$message\n";
	close(MAIL);
} 

#
# Create the parser object
#
my $np = new Nmap::Parser;

#
# Execute Nmap Scan
#
$nmap_exe = '/usr/local/bin/nmap';
$np->parsescan($nmap_exe,'-n -PN -pT:80,443,23,22,21,U:161,1900 -sU -sV -sS -oA rogueap -O -T4', $opt_i);

#$np->parsefile('osfinger.xml'); #using filenames

#GETTING SCAN INFORMATION

print "Scan Information:\n";
$si = $np->get_session();
#get scan information by calling methods
print
'Number of services scanned: '.$si->numservices()."\n",
'Start Time: '.$si->start_time()."\n",
'Scan Types: ',(join ' ',$si->scan_types())."\n";

#GETTING HOST INFORMATION

print "Hosts scanned:\n";
for my $host_obj ($np->all_hosts()){
	$os = $host_obj->os_sig;

	if ($os->type() eq "WAP") {
	  print
	  'Address   : '.$host_obj->ipv4_addr()."\n",
	  'OS match  : '.$os->name().$os->family()."\n",
	  'Device Type: '.$os->type()."\n";
	}
}

#
# Send Email (Coming Soon)
#
#sendEmail("toemail\@mydomain.com", "fromemail\@mydomain.com", "Simple email.", "This is a test of the email function."); 

#frees memory--helpful when dealing with memory intensive scripts
$np->purge();
