#!/usr/bin/perl -w
#########################################################################
##
##	FrontPage Xploiter Command Line Version (CL)
##
##	Frontpage Security scanner built entirely in perl. This tool will detect Microsoft web servers providing
##	Frontpage Server extensions with no password or a weak password as defined by the sample or user provided
##	password list. 
##
##	This tool was created for educational purposes only, and should only be used to scan networks and devices you
##	have already obtained permission to assess.
##
##
##	Copyright 2003 Matthew M Shannon (mshannon@fpxploiter.org)
##    	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 2 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, write to the Free Software
##    	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use Getopt::Long;	#Why anyone would do opts another way, I just don't know
use LWP::UserAgent;     # Define the User Agent
use Authen::NTLM;	# Use the NTLM Modules
use Class::Struct;
# Globals
my $I;
my $II; 
my $III; 
my $IVS; 
my $IVE; 
my $false = 0;
my $true = 1;
my @Targets;
my $filename ="/usr/share/fpxploiter/passlst"; #Default password list, came with the application
my $username ="Administrator"; #Default User name, comes with application ;)
my $hosts; # For Command line processing
my $help =''; #Getopts for help, rather lame.
my $auth_mess = 'method=open+service%3a4%2e0%2e22e3406&service%5fname=%2f';
my $verbose = $false;
struct( Results => {
	IP => '$',	   # IP Address
        WebServer => '$',  # Boolean Web Server
	Fpenabled => '$',  # Boolean Frontpage Enabled
	Fpnopass => '$',   # Boolean Frontpage No Pass
	Fpweakpass => '$' , #Boolean Frontpage Weak Pass
	Password => '$',    # Weak Pass if found
	Platform => '$',    # Version Information
    	Servername => '$'
    });

my @Results_ary = new Results;


#Summary variables to hold final report.

my $webservercnt =0;
my $frontpagecnt =0;
my $frontpagenopasscnt=0;
my $frontpageweakpasscnt=0;
my $NumOfRows=0;


# Command line processing

   
GetOptions ('u|username=s' => \$username,'t|targets=s' => \$hosts,'p|passlst=s' => \$filename, "h|help" =>\$help, "l|logfile=s" =>\$logfile, "v|verbose" =>\$verbose);
    
	if ($help){
		&HText;

	}
	if ($filename){
		#check that file exists
	}
	if ($logfile){
		$printlog = $true;
	}
	if ($hosts){
		
		# Build the UserAgent for future access.
	$ua = LWP::UserAgent->new(keep_alive=>1);
	$ua->agent("fpxploiter/1.00");


	# Build the header for future web requests
	my $vti_header = HTTP::Headers->new(
		Date 	=> scalar localtime,
		Content_Type => 'application/x-www-form-urlencoded',
		Accept => "auth/sicily",
		Connection => "Keep-Alive",
		'MIME-Version' => '1.0',
		'X-Vermeer-Content-Type' => 'application/x-www-form-urlencoded'	
		);
	
	&Scan;
	
	}
	else {    
		&HText;
	}






sub Handle_Log
{

# Open the file, if we can't let's die with dignity

if ($printlog == $true){
	# Print header information

	open (OUT, ">$logfile") or die "Can't write to $logfile - $!";

	# Print header information

	print OUT "fpXploiter Log\n\n\n";

	print OUT "Number of Hosts scanned: $NumOfRows\n";
	print OUT "Number of Web Servers Detected: $webservercnt\n";
	print OUT "Number of Web Servers running Frontpage: $frontpagecnt\n";
	print OUT "Number of Frontpage Servers with NO PASSWORD: $frontpagenopasscnt\n";
	print OUT "Number of Frontpage Servers with WEAK PASSWORDS: $frontpageweakpasscnt\n\n";

	print OUT "IP Address\tWeb Server\tFrontpage Enabled\tServer Name\txploitable\tWeak Password\n\n";
 
	# Put together a for loop to print the results

	for ($cnt=0;$cnt<=($NumOfRows-1);$cnt++)
	{
		print OUT "$Results_ary[$cnt]->{'IP'}\t$Results_ary[$cnt]->{'WebServer'}\t$Results_ary[$cnt]->{'Fpenabled'}\t$Results_ary[$cnt]->{'ServerName'}\t$Results_ary[$cnt]->{'Fpnopass'}\t$Results_ary[$cnt]->{'Password'}\n"; 
		print OUT "\n";
	} 
	
	close OUT;

}
	
	print  "fpXploiter Log\n\n\n";

	print  "Number of Hosts scanned: $NumOfRows\n";
	print  "Number of Web Servers Detected: $webservercnt\n";
	print  "Number of Web Servers running Frontpage: $frontpagecnt\n";
	print  "Number of Frontpage Servers with NO PASSWORD: $frontpagenopasscnt\n";
	print  "Number of Frontpage Servers with WEAK PASSWORDS: $frontpageweakpasscnt\n\n";

	print  "IP Address\tWeb Server\tFrontpage Enabled\tServer Name\txploitable\tWeak Password\n\n";
 
	# Put together a for loop to print the results

	for ($cnt=0;$cnt<=($NumOfRows-1);$cnt++)
	{
	print "$Results_ary[$cnt]->{'IP'}\t$Results_ary[$cnt]->{'WebServer'}\t$Results_ary[$cnt]->{'Fpenabled'}\t$Results_ary[$cnt]->{'ServerName'}\t$Results_ary[$cnt]->{'Fpnopass'}\t$Results_ary[$cnt]->{'Password'}\n"; 
	print  "\n";
	} 
}

sub ResetHeaders
{
	$vti_header = HTTP::Headers->new(
	Date 	=> scalar localtime,
	Content_Type => 'application/x-www-form-urlencoded',
	Accept => "auth/sicily",
	Connection => "Keep-Alive",
	'MIME-Version' => '1.0',
	'X-Vermeer-Content-Type' => 'application/x-www-form-urlencoded');
}


sub checkNet
{
	
for $_ (@_){
	if (/[A-Z][a-z][1-254]/)
	{
		print "All Octats must be a number between 1 and 254\n";
		exit;
		}
		
		
	}
	return $true;
}
  


sub checkMath
{
# Let's hope this doesn't fail, come'on guys, do the math!

if ($_[0] > $_[1]){
	# You've failed basic math
		print "Start must be before end, doh!\n";
		exit;
	}
}

sub HandleWindow
{
	print "\n\nfpxploiter - Frontpage Vulnerability Scanner\n\n";
	print "Starting....\n\n";
}
### Callback function to login to FrontPage Server 
sub GetnCheck
{
		# Split hosts data up into Octets and continue
		@octets = split(/\./,$hosts);
		@endpoints = split(/\-/,$octets[3]); 
		$I = $octets[0];
		$II = $octets[1];
		$III = $octets[2];
		$IVS = $endpoints[0];
		$IVE = $endpoints[1];
	
		
	if (checkNet($I,$II,$III,$IVS,$IVE) == $false)
	{
		return $false;
	}

	if (checkMath($IVS, $IVE) == $false)
	{
		return $false;
	}

	return $true;
}	

sub HText{
		
# Output some help text for weenies, then exit.
	print "\nHelp\nfpxploiter commandline tool argument syntax...\n";
	print " -h -> This message.\n";
	print " -t -> Target in the form aaa.bbb.ccc.ddd-eee\n";
	print " -u -> Username, if blank it will default to Administrator.\n";
	print " -p -> Password List, /path/to/file/passlst.lst.\n";
	print " -l -> Log File, /path/to/logfile/logfile.name.\n";
	print " -v -> Verbose\n";
	print "\n\n";
      	exit;
}
	
	
#Build Array Table of IPs to test
sub GetIPAdds
{	
	$NumOfRows = 0;
	$tot = ($IVE - $IVS); 
	for ($cnt = 0; $cnt <= $tot; $cnt++)
	{
		$IV = $IVS + $cnt;
		$IP = "$I" . '.' . "$II" . '.' . "$III" . '.' . "$IV";
		$Targets[$cnt]=$IP;
	}
	
	$cnt = 0;
	
	for $_ (@Targets){
		if (/[1-9]/){
			$cnt++;
			$NumOfRows++;
		}
	}
	return $true;
}

#Test for webserver
sub WebServerTest
{	
	
	
	$tot = ($IVE - $IVS);
	for ($cnt = 0; $cnt <= $tot; $cnt++)
	{
		print "Testing for Webserver at:$Targets[$cnt]....\n";
  		# Create a request
 		my $req = HTTP::Request->new(HEAD => "http://$Targets[$cnt]/");
	
  		# Pass request to the user agent and get a response back
		my $res = $ua->request($req);

  		# Check the outcome of the response
  		if ($res->is_success) {
		
			$Results_ary[$cnt]->{"IP"}= $Targets[$cnt];
			$Results_ary[$cnt]->{"WebServer"} = "Yes";
			$Results_ary[$cnt]->{"Fpenabled"} = "No";
			$Results_ary[$cnt]->{"Fpnopass"} = "No";
			$Results_ary[$cnt]->{"Fpweakpass"} = "No";
			$Results_ary[$cnt]->{"Password"} = "NA";  
			$Results_ary[$cnt]->{"Platform"} = "NA";
			$Results_ary[$cnt]->{"ServerName"} = "NA";
			UpdateResultsWeb($Targets[$cnt],$true);
			&FrontpageTest;
		}
		else {
			$code = $res->code();
			if ($code =~ /403/){
				$Results_ary[$cnt]->{"IP"}= $Targets[$cnt];
				$Results_ary[$cnt]->{"WebServer"} = "Yes";
				$Results_ary[$cnt]->{"Fpenabled"} = "";
				$Results_ary[$cnt]->{"Fpnopass"} = "";
				$Results_ary[$cnt]->{"Fpweakpass"} = "";
				$Results_ary[$cnt]->{"Password"} = "";  
				$Results_ary[$cnt]->{"Platform"} = "";
				$Results_ary[$cnt]->{"ServerName"} = "";
				UpdateResultsWeb($Targets[$cnt],$true);
				&FrontpageTest;
			}
			else{
				$Results_ary[$cnt]->{"IP"}= $Targets[$cnt];
				$Results_ary[$cnt]->{"WebServer"} = "No";
				$Results_ary[$cnt]->{"Fpenabled"} = "";
				$Results_ary[$cnt]->{"Fpnopass"} = "";
				$Results_ary[$cnt]->{"Fpweakpass"} = "";
				$Results_ary[$cnt]->{"Password"} = "";  
				$Results_ary[$cnt]->{"Platform"} = "";
				$Results_ary[$cnt]->{"ServerName"} = "";
				UpdateResultsWeb($Targets[$cnt],$false);
			}
		}
	}
	$NumOfRows=$cnt;
	return $true;
}

			
#Test all IPs for active FP _vti_inf.html file
sub FrontpageTest
{	
			
  		# Create a request
 		my $req = HTTP::Request->new(GET => "http://$Targets[$cnt]/_vti_inf.html");

  		# Pass request to the user agent and get a response back
		my $res = $ua->request($req);

  		# Check the outcome of the response
  		if ($res->is_success) {
		
			# Has Frontpage Server Extensions running
			$logindata = $res->content;
			$logindata =~ /FPVersion=(.*)\n/g;
			$platform = $1;
			if ($logindata =~ /.exe/g) {
				$Results_ary[$cnt]->{"Fpenabled"}= "Yes, Apache, Unsupported";
				UpdateResultsFrontPage($Targets[$cnt],$false);
				return $true;
			}
			$Results_ary[$cnt]->{"Fpenabled"}= "Yes";
			$Results_ary[$cnt]->{"Platform"}= $platform;

			UpdateResultsFrontPage($Targets[$cnt],$true);
			&FPnoPasswd;
		}
		else {
			UpdateResultsFrontPage($Targets[$cnt],$false);
		}

	return $true;
}

			
#Successful IPs, test for FP w/no password
	
sub FPnoPasswd
{

		# Create a request
		$login_req_mess = 'method=server+version%3a4%2e0%2e2%2e4715.';
		my $login_req = HTTP::Request->new(POST=> "http://$Targets[$cnt]/_vti_bin/shtml.dll/_vti_rpc",$vti_header,$login_req_mess);
		my $login_res = $ua->request($login_req);
		
  		# Check the outcome of the response
  		if ($login_res->is_success) {
			
			$login_req = HTTP::Request->new(POST => "http://$Targets[$cnt]/_vti_bin/_vti_aut/author.dll",$vti_header,$auth_mess);
			my $login_res = $ua->request($login_req);
			
			if ($login_res->is_success){
			
				#Frontpage, no password required.
				
				$logindata = $login_res->content;
				$logindata =~ /SR\|http:(.*)\n/;
				$servername = "http:$1";
				$Results_ary[$cnt]->{"Fpnopass"}= "No Pass!";
				$Results_ary[$cnt]->{"ServerName"}= $servername;

				UpdateResultsNoPasswd($Targets[$cnt],$true);
			}
			else {
				#Try with pass
				$Results_ary[$cnt]->{"Fpnopass"}= "No";
				UpdateResultsNoPasswd($Targets[$cnt],$false);
				&FPweakPasswd;
			}
			
		return $true;
		}	
	$Results_ary[$cnt]->{"Fpnopass"}= "No";
	UpdateResultsNoPasswd($Targets[$cnt],$false);
	&FPweakPasswd;
}
	
	


	

	
#Remaining IPs, test for FP w/weak password
sub FPweakPasswd
{
	open (PWLIST, "<$filename") or die "Can't open file $filename\n";
	
		# Handle password requests
		while (<PWLIST>) {
			$password=$_;
			chop ($password);
			ntlm_user("$username");
			ntlm_password("$password");
			$initial_str = ntlm;
			&ResetHeaders;
			
  			# Create a request
			$login_req_mess = 'method=server+version%3a4%2e0%2e2%2e4715.';
			my $login_req = HTTP::Request->new(POST => "http://$Targets[$cnt]/_vti_bin/shtml.dll/_vti_rpc",$vti_header,$login_req_mess);
			my $login_res = $ua->request($login_req);

  			# Check the outcome of the response
  			if ($login_res->is_success) {
			
			$vti_header->header(Authorization => "Negotiate $initial_str");
			
			$login_req = HTTP::Request->new(POST => "http://$Targets[$cnt]/_vti_bin/_vti_aut/author.dll",$vti_header,$auth_mess);
			
			my $login_res = $ua->request($login_req);
			
				if ($login_res->is_error){
							
					$login_res->header('WWW-Authenticate')=~ /Negotiate\s(.*)/;
					$login_res->header('Connection' => 'Keep-Alive');
					$auth_hash=$1;
			
					$final_str=ntlm($auth_hash);
					$vti_header = HTTP::Headers->new(
						Date => scalar localtime,
						Content_Type => 'application/x-www-form-urlencoded',
						Accept =>'auth/sicily',
						Authorization => "Negotiate $final_str",
						'MIME-Version' => '1.0',
						Connection => 'Keep-Alive',
						'X-Vermeer-Content-Type' => 'application/x-www-form-urlencoded');
					$login_req = HTTP::Request->new(POST => "http://$Targets[$cnt]/_vti_bin/_vti_aut/author.dll",$vti_header,$auth_mess);
					$login_res = $ua->request($login_req);
					ntlm_reset;
					if ($login_res->is_success){
						$Results_ary[$cnt]->{"Fpweakpass"}= "Yes";
						$Results_ary[$cnt]->{"Fpnopass"}= "Weak Password";
						$Results_ary[$cnt]->{"Password"}= $password;

						UpdateResultsWeakPasswd($Targets[$cnt],$true,$password);
						return $true;
					}
					else {
						$Results_ary[$cnt]->{"Fpweakpass"} = "No";
						UpdateResultsWeakPasswd($Targets[$cnt],$false, $password);
					}
				}
			}
		}
	
	return $true;
}
	

	
#Use HASH Table for results
sub UpdateResultsWeb
{
	if ($_[1] == $true) {
	$webservercnt++;
		if ($verbose == $true) {
		print "Webserver detected at $_[0]\n";
		}
	return $true;
	}
	else{
		if ($verbose == $true){
		print "No Webserver detected at $_[0]\n";
		}
	return $false;
	}
}
sub UpdateResultsFrontPage
{
	if ($_[1] == $true) {
		$frontpagecnt++;
		if ($verbose == $true) {
		print "Frontpage Server detected at $_[0]\n";
		}
	return $true;
	}
	else{
		if ($verbose == $true){
		print "No Frontpage Server detected at $_[0], scanning will cease against that server\n";
		}
	return $false;
	}
}
sub UpdateResultsNoPasswd
{
	if ($_[1] == $true) {
		$frontpagenopasscnt++;
		if ($verbose == $true) {
		print "xploitable Frontpage Server detected at $_[0], No Password\n";
		}
	return $true;
	}
	else{
		if ($verbose == $true){
		print "Frontpage Server at $_[0], has a password\n";
		}
	return $false;
	}
}
sub UpdateResultsWeakPasswd
{
	if ($_[1] == $true) {
		$frontpageweakpasscnt++;
		if ($verbose == $true){
		print "Frontpage Server at $_[0], has a weak password, password is $_[2]\n";
		}
		return $true;
	}
	else{
		if ($verbose == $true){
		print "Frontpage Server at $_[0], password $_[2] failed.\n";
		}
	return $false;
	}
}
	

sub Scan
{
	# Old Get and check to make sure we're dealing with the good input.
	&GetnCheck;
	
	# Handle the Window processing, make sure we've got love.
	&HandleWindow;
	
	# Processes the Octets into IPs.
	&GetIPAdds;
	
	# Test to see if the IP is running a web server.
	&WebServerTest;

	&Handle_Log;
	
}


# END 

