#!/usr/bin/perl
# Copyright Andrew Gavin 2009, 2010
#
# This file is part of OpenDLP.
#
# OpenDLP 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 3 of the License, or
# (at your option) any later version.
#
# OpenDLP 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 OpenDLP. If not, see .
use CGI qw/:standard/;
use DBI;
my $user = remote_user();
my $is_valid = 1;
my $MAX_LENGTH_PROFILE = 64;
my $MAX_LENGTH_SCAN = 64;
my $MAX_LENGTH_HOSTNAME = 64;
my( $scanname, $profile, $formurl, $systems, $concurrent ) = "";
my( $db_username, $db_password ) = "";
my $p = "";
my( $hostname, $results, $log, $filesscanned, $bytesscanned ) = "";
my( $filestotal, $bytestotal, $status, $tracker ) = "";
my( $scan, $profile ) = "";
my $db_admin_file = "../../etc/db_admin";
open( DB, $db_admin_file );
my $db_line = ;
close( DB );
chomp $db_line;
($db_username, $db_password) = split( ":", $db_line );
header();
if( request_method() ne "POST" )
{
$is_valid = 0;
print "Only POST requests are allowed.
\n";
}
foreach $p (param())
{
$form{$p} = param($p);
if( $p eq "hostname" )
{
$hostname = $form{$p};
if( length( $hostname ) > $MAX_LENGTH_HOSTNAME )
{
$is_valid = 0;
}
if( $hostname !~ /^[A-Z0-9\.\-]+$/i )
{
$is_valid = 0;
}
}
if( $p eq "filesscanned" )
{
$filesscanned = $form{$p};
if( $filesscanned !~ /^[0-9]+$/ )
{
$is_valid = 0;
}
}
if( $p eq "bytesscanned" )
{
$bytesscanned = $form{$p};
if( $bytesscanned !~ /^[0-9]+$/ )
{
$is_valid = 0;
}
}
if( $p eq "results" )
{
$results = $form{$p};
}
if( $p eq "log" )
{
$log = $form{$p};
}
if( $p eq "bytestotal" )
{
$bytestotal = $form{$p};
if( $bytestotal !~ /^[0-9]+$/ )
{
$is_valid = 0;
}
}
if( $p eq "filestotal" )
{
$filestotal = $form{$p};
if( $filestotal !~ /^[0-9]+$/ )
{
$is_valid = 0;
}
}
if( $p eq "status" )
{
$status = $form{$p};
if( $status !~ /^(0|1|2|3)$/ )
{
$is_valid = 0;
}
}
if( $p eq "tracker" )
{
$tracker = $form{$p};
if( $tracker !~ /[A-F0-9]{32}/i )
{
$is_valid = 0;
}
}
if( $p eq "scan" )
{
$scan = $form{$p};
if( length( $scan ) > $MAX_LENGTH_SCAN )
{
$is_valid = 0;
}
if( $scan !~ /^[a-z0-9\ \,\.\-\_]+$/i )
{
$is_valid = 0;
}
}
if( $p eq "profile" )
{
$profile = $form{$p};
if( $profile !~ /^[a-z0-9\ \,\.\-\_]+$/i )
{
$is_valid = 0;
}
if( length( $profile ) > $MAX_LENGTH_PROFILE )
{
$is_valid = 0;
}
}
}
if( $is_valid )
{
my $logstring = "";
foreach $p (param())
{
$form{$p} = param($p);
$logstring .= "$p\=$form{$p}\&";
}
open( LOG, ">>/tmp/log.txt" );
print LOG "$logstring\n";
close( LOG );
if( $status eq "2" )
{
my $localtime = time();
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
# update systems
my $string = "UPDATE systems SET system=?,status=?,updated=?,filesdone=?,bytesdone=? WHERE tracker=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $hostname, '2', $localtime, $filesscanned, $bytesscanned, $tracker );
# update results
while( my $line = <$results> )
{
$line =~ s/\r$//g;
$line =~ s/\n$//g;
my( $filename, $regex_name, $result, $offset, $md5 ) = split( "\t", $line );
my $string = "INSERT INTO results SET scan=?,system=?,type=?,pattern=?,file=?,offset=?,md5=?,tracker=?,is_false=\"0\"";
$sth = $dbh->prepare( $string );
$sth->execute( $scan,$hostname,$regex_name,$result,$filename,$offset,$md5,$tracker );
}
# update log
my $line_counter = 0;
while( my $line = <$log> )
{
$line =~ s/\r$//g;
$line =~ s/\n$//g;
chomp $line;
my $string = "INSERT INTO logs SET tracker=?,line=?,data=?,updated=?,scan=?,profile=?";
$sth = $dbh->prepare( $string );
$sth->execute( $tracker, $line_counter, $line, $localtime, $scan, $profile );
$line_counter++;
}
$sth->finish();
$dbh->disconnect();
}
elsif( $status eq "1" )
{
my $localtime = time();
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
# update systems
my $string = "UPDATE systems SET system=?,status=?,updated=?,filestotal=?,bytestotal=? WHERE tracker=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $hostname, '1', $localtime, $filestotal, $bytestotal, $tracker );
# update logs
my $line_counter = 0;
while( my $line = <$log> )
{
$line =~ s/\r$//g;
$line =~ s/\n$//g;
chomp $line;
my $string = "INSERT INTO logs SET tracker=?,line=?,data=?,updated=?,scan=?,profile=?";
$sth = $dbh->prepare( $string );
$sth->execute( $tracker, $line_counter, $line, $localtime, $scan, $profile );
$line_counter++;
}
$sth->finish();
$dbh->disconnect();
}
elsif( $status eq "0" )
{
my $localtime = time();
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
# update systems
my $string = "UPDATE systems SET system=?,status=?,updated=? WHERE tracker=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $hostname, '0', $localtime, $tracker );
# update logs
my $line_counter = 0;
while( my $line = <$log> )
{
$line =~ s/\r$//g;
$line =~ s/\n$//g;
my $string = "INSERT INTO logs SET tracker=?,line=?,data=?,updated=?,scan=?,profile=?";
$sth = $dbh->prepare( $string );
$sth->execute( $tracker, $line_counter, $line, $localtime, $scan, $profile );
$line_counter++;
}
$sth->finish();
$dbh->disconnect();
}
elsif( $status eq "3" )
{
my $localtime = time();
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
# update systems
my $string = "UPDATE systems SET system=?,status=?,updated=?,filesdone=?,bytesdone=?,control=? WHERE tracker=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $hostname, '3', $localtime, $filesscanned, $bytesscanned, 'finished', $tracker );
# update results
while( my $line = <$results> )
{
$line =~ s/\r$//g;
$line =~ s/\n$//g;
my( $filename, $regex_name, $result, $offset, $md5 ) = split( "\t", $line );
my $string = "INSERT INTO results SET scan=?,system=?,type=?,pattern=?,file=?,offset=?,md5=?,tracker=?,is_false=\"0\"";
$sth = $dbh->prepare( $string );
$sth->execute( $scan,$hostname,$regex_name,$result,$filename,$offset,$md5,$tracker );
}
# update logs
my $line_counter = 0;
while( my $line = <$log> )
{
$line =~ s/\r$//g;
$line =~ s/\n$//g;
my $string = "INSERT INTO logs SET tracker=?,line=?,data=?,updated=?,scan=?,profile=?";
$sth = $dbh->prepare( $string );
$sth->execute( $tracker, $line_counter, $line, $localtime, $scan, $profile );
$line_counter++;
}
# get OS credentials
my $string = "SELECT username,password,domain,path from profiles where profile=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $profile );
my $results = $sth->fetchrow_arrayref();
my $username = $$results[0];
my $password = $$results[1];
my $domain = $$results[2];
my $path = $$results[3];
# get IP address of system
my $string = "SELECT ip from systems where tracker=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $tracker );
my $results = $sth->fetchrow_arrayref();
my $system = $$results[0];
my $escape_user = "";
my $length_user = length( $username );
my $x = 0;
for( $x = 0; $x < $length_user; $x++ )
{
$escape_user .= "\\" . substr( $username, $x, 1 );
}
my $escape_pass = "";
my $length_pass = length( $password );
my $x = 0;
for( $x = 0; $x < $length_pass; $x++ )
{
$escape_pass .= "\\" . substr( $password, $x, 1 );
}
# stop service
my $command = "winexe --user=\'$domain\'\\$escape_user --password=$escape_pass //$system \'\"$path\\sc.exe\" stop OpenDLP\'";
`$command`;
# delete service
my $command = "winexe --user=\'$domain\'\\$escape_user --password=$escape_pass //$system \'\"$path\\sc.exe\" delete OpenDLP\'";
`$command`;
# delete contents of installation directory
my $command = "winexe --user=\'$domain\'\\$escape_user --password=$escape_pass //$system \'cmd.exe /c del /Q \"$path\\*.*\"\'";
`$command`;
# delete installation directory
my $command = "winexe --user=\'$domain\'\\$escape_user --password=$escape_pass //$system \'cmd.exe /c rmdir \"$path\"\'";
`$command`;
$sth->finish();
$dbh->disconnect();
}
}
footer();
sub header
{
print "Content-type: text/html\n\n";
print "\n\n";
print "Data Discovery Tool 1.0\n";
print "\n";
}
sub footer
{
print "\n";
}