#!/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;
use POSIX ":sys_wait_h";
use Proc::Queue;
my $user = remote_user();
my $db_admin_file = "../etc/db_admin";
my $is_valid = 1;
my %profile_info = ();
open( DB, $db_admin_file );
my $db_line = ;
close( DB );
chomp $db_line;
my ($db_username, $db_password) = split( ":", $db_line );
header();
my $query = CGI->new;
my $action = $query->param('action');
if( $action !~ /^(pause|resume|uninstall)$/i )
{
$is_valid = 0;
print "Invalid action: Must be \"pause\", \"resume\", or \"uninstall\".
\n";
}
my $scanname = $query->param('scanname');
if( $scanname ne "" && $scanname !~ /^[a-z0-9\ \,\.\-\_]+$/i )
{
$is_valid = 0;
print "Invalid scan name
\n";
}
my $system = $query->param('system');
if( $system ne "" && $system !~ /^[A-Z0-9]{32}$/ )
{
$is_valid = 0;
print "Invalid system tracker
\n";
# if no arguments are given
if( $system eq "" && $scanname eq "" )
{
print "No action will be taken: You must at least specify a scan name.
\n";
}
elsif( $scanname ne "" && $system eq "" )
{
print "Attempting to pause agents...
\n";
get_profile();
# get all systems in "running" state
my @systems = ();
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
my $string = "SELECT ip FROM systems WHERE scan=? AND control=\"running\"";
my $sth = $dbh->prepare( $string );
$sth->execute( $scanname );
while( my $results = $sth->fetchrow_arrayref() )
{
push @systems, $$results[0];
}
my $length_of_systems = @systems;
if( $length_of_systems == 0 )
{
print "No agents are running, so nothing will be paused.
\n";
}
else
{
Proc::Queue::size( $profile_info{concurrent} );
foreach my $ip( @systems )
{
$length_of_systems--;
my $f = fork;
if( defined ($f) and $f == 0 )
{
print "Trying to pause agent on $ip ($length_of_systems systems remain in queue) \n";
my $escape_user = "";
my $length_user = length( $profile_info{username} );
my $x = 0;
for( $x = 0; $x < $length_user; $x++ )
{
$escape_user .= "\\" . substr( $profile_info{username}, $x, 1 );
}
my $escape_pass = "";
my $length_pass = length( $profile_info{password} );
my $x = 0;
for( $x = 0; $x < $length_pass; $x++ )
{
$escape_pass .= "\\" . substr( $profile_info{password}, $x, 1 );
}
# stop OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" stop OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /(STOP_PENDING|STOPPED)/ )
{
$is_successful = 1;
}
if( $is_successful )
{
print "$ip: OpenDLP paused \n";
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"stopped\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
}
else
{
print "$ip: Got unexpected response from command. The agent may be paused. Check the previous screen to verify. \n";
}
exit(0);
}
1 while waitpid(-1, WNOHANG) > 0;
}
}
$sth->finish();
$dbh->disconnect();
}
# pause specific system
elsif( $scanname ne "" && $system ne "" )
{
get_profile();
# get system's IP
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
my $string = "SELECT ip FROM systems WHERE scan=? AND control=\"running\" AND tracker=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $scanname, $system );
my $results = $sth->fetchrow_arrayref();
my $ip = $$results[0];
if( $ip eq "" )
{
print "Agent is not running, so nothing will be paused.
\n";
}
else
{
print "$ip: Attempting to pause agent...
\n";
my $escape_user = "";
my $length_user = length( $profile_info{username} );
my $x = 0;
for( $x = 0; $x < $length_user; $x++ )
{
$escape_user .= "\\" . substr( $profile_info{username}, $x, 1 );
}
my $escape_pass = "";
my $length_pass = length( $profile_info{password} );
my $x = 0;
for( $x = 0; $x < $length_pass; $x++ )
{
$escape_pass .= "\\" . substr( $profile_info{password}, $x, 1 );
}
# stop OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" stop OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /(STOP_PENDING|STOPPED)/ )
{
$is_successful = 1;
}
if( $is_successful )
{
print "$ip: OpenDLP paused \n";
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"stopped\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
}
else
{
print "$ip: Got unexpected response from command. The agent may be paused. Check the previous screen to verify. \n";
}
}
$sth->finish();
$dbh->disconnect();
}
}
elsif( $action eq "resume" )
{
print "Resume Agents
\n";
# if no arguments are given
if( $system eq "" && $scanname eq "" )
{
print "No action will be taken: You must at least specify a scan name.
\n";
}
elsif( $scanname ne "" && $system eq "" )
{
print "Attempting to resume agents...
\n";
get_profile();
# get all systems in "stopped" state
my @systems = ();
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
my $string = "SELECT ip FROM systems WHERE scan=? AND control=\"stopped\"";
my $sth = $dbh->prepare( $string );
$sth->execute( $scanname );
while( my $results = $sth->fetchrow_arrayref() )
{
push @systems, $$results[0];
}
my $length_of_systems = @systems;
if( $length_of_systems == 0 )
{
print "No agents are paused, so nothing will be resumed.
\n";
}
else
{
Proc::Queue::size( $profile_info{concurrent} );
foreach my $ip( @systems )
{
$length_of_systems--;
my $f = fork;
if( defined ($f) and $f == 0 )
{
print "Trying to resume agent on $ip ($length_of_systems systems remain in queue) \n";
my $escape_user = "";
my $length_user = length( $profile_info{username} );
my $x = 0;
for( $x = 0; $x < $length_user; $x++ )
{
$escape_user .= "\\" . substr( $profile_info{username}, $x, 1 );
}
my $escape_pass = "";
my $length_pass = length( $profile_info{password} );
my $x = 0;
for( $x = 0; $x < $length_pass; $x++ )
{
$escape_pass .= "\\" . substr( $profile_info{password}, $x, 1 );
}
# start OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" start OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /(START_PENDING|RUNNING)/ )
{
$is_successful = 1;
}
if( $is_successful )
{
print "$ip: OpenDLP resumed \n";
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"running\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
}
else
{
print "$ip: Got unexpected response from command. The agent may be resumed. Check the previous screen to verify. \n";
}
exit(0);
}
1 while waitpid(-1, WNOHANG) > 0;
}
}
$sth->finish();
$dbh->disconnect();
}
# resume specific system
elsif( $scanname ne "" && $system ne "" )
{
get_profile();
# get system's IP
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
my $string = "SELECT ip FROM systems WHERE scan=? AND control=\"stopped\" AND tracker=?";
my $sth = $dbh->prepare( $string );
$sth->execute( $scanname, $system );
my $results = $sth->fetchrow_arrayref();
my $ip = $$results[0];
if( $ip eq "" )
{
print "Agent is not running, so nothing will be paused.
\n";
}
else
{
print "$ip: Attempting to resume agent...
\n";
my $escape_user = "";
my $length_user = length( $profile_info{username} );
my $x = 0;
for( $x = 0; $x < $length_user; $x++ )
{
$escape_user .= "\\" . substr( $profile_info{username}, $x, 1 );
}
my $escape_pass = "";
my $length_pass = length( $profile_info{password} );
my $x = 0;
for( $x = 0; $x < $length_pass; $x++ )
{
$escape_pass .= "\\" . substr( $profile_info{password}, $x, 1 );
}
# start OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" start OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /(START_PENDING|RUNNING)/ )
{
$is_successful = 1;
}
if( $is_successful )
{
print "$ip: OpenDLP resumed \n";
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"running\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
}
else
{
print "$ip: Got unexpected response from command. The agent may be resumed. Check the previous screen to verify. \n";
}
}
$sth->finish();
$dbh->disconnect();
}
}
elsif( $action eq "uninstall" )
{
print "Uninstall Agents
\n";
# if no arguments are given
if( $system eq "" && $scanname eq "" )
{
print "No action will be taken: You must at least specify a scan name.
\n";
}
elsif( $scanname ne "" && $system eq "" )
{
print "Attempting to stop and uninstall agents...
\n";
get_profile();
# get all systems in "running" state
my @systems = ();
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
my $string = "SELECT ip FROM systems WHERE scan=? AND control=\"running\"";
my $sth = $dbh->prepare( $string );
$sth->execute( $scanname );
while( my $results = $sth->fetchrow_arrayref() )
{
push @systems, $$results[0];
}
# get all systems in "stopped" state
my $string = "SELECT ip FROM systems WHERE scan=? AND control=\"stopped\"";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname );
while( my $results = $sth->fetchrow_arrayref() )
{
push @systems, $$results[0];
}
# uniq @systems
my %seen = ();
@systems = grep {! $seen{$_}++} @systems;
my $length_of_systems = @systems;
if( $length_of_systems == 0 )
{
print "No agents are running or paused, so nothing will be stopped and uninstalled.
\n";
}
else
{
Proc::Queue::size( $profile_info{concurrent} );
foreach my $ip( @systems )
{
$length_of_systems--;
my $f = fork;
if( defined ($f) and $f == 0 )
{
print "Trying to stop and uninstall agent on $ip ($length_of_systems systems remain in queue) \n";
my $escape_user = "";
my $length_user = length( $profile_info{username} );
my $x = 0;
for( $x = 0; $x < $length_user; $x++ )
{
$escape_user .= "\\" . substr( $profile_info{username}, $x, 1 );
}
my $escape_pass = "";
my $length_pass = length( $profile_info{password} );
my $x = 0;
for( $x = 0; $x < $length_pass; $x++ )
{
$escape_pass .= "\\" . substr( $profile_info{password}, $x, 1 );
}
# stop OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" stop OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /(STOP_PENDING|STOPPED)/ )
{
$is_successful = 2;
}
elsif( $output =~ /The\ service\ has\ not\ been\ started/ )
{
$is_successful = 1;
}
if( $is_successful == 2 )
{
print "$ip: OpenDLP stopped \n";
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"stopped\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
}
elsif( $is_successful == 1 )
{
print "$ip: OpenDLP already stopped \n";
}
else
{
print "$ip: Got unexpected response from command. The agent may be stopped. Check the previous screen to verify. \n";
}
# delete OpenDLP service
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" delete OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /DeleteService\ SUCCESS/ )
{
$is_successful = 1;
}
if( $is_successful == 1 )
{
print "$ip: OpenDLP service deleted \n";
}
else
{
print "$ip: Got unexpected response from command. The agent may be stopped. Check the previous screen to verify. \n";
}
# delete all files in OpenDLP directory
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'cmd.exe /c del /Q \"$profile_info{path}\\*.*\"\'";
`$command`;
# rmdir OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'cmd.exe /c rmdir \"$profile_info{path}\"\'";
`$command`;
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"uninstalled\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
print "$ip: OpenDLP stopped and uninstalled \n";
exit(0);
}
1 while waitpid(-1, WNOHANG) > 0;
}
}
$sth->finish();
$dbh->disconnect();
}
# uninstall specific system
elsif( $scanname ne "" && $system ne "" )
{
get_profile();
# get system's IP
my $dbh = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
my $string = "SELECT ip FROM systems WHERE scan=? AND tracker=? AND (control=\"stopped\" or control=\"running\")";
my $sth = $dbh->prepare( $string );
$sth->execute( $scanname, $system );
my $results = $sth->fetchrow_arrayref();
my $ip = $$results[0];
if( $ip eq "" )
{
print "Agent is not running or paused, so nothing will be stopped and uninstalled.
\n";
}
else
{
print "$ip: Attempting to stop and uninstall agent...
\n";
my $escape_user = "";
my $length_user = length( $profile_info{username} );
my $x = 0;
for( $x = 0; $x < $length_user; $x++ )
{
$escape_user .= "\\" . substr( $profile_info{username}, $x, 1 );
}
my $escape_pass = "";
my $length_pass = length( $profile_info{password} );
my $x = 0;
for( $x = 0; $x < $length_pass; $x++ )
{
$escape_pass .= "\\" . substr( $profile_info{password}, $x, 1 );
}
# stop OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" stop OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /(STOP_PENDING|STOPPED)/ )
{
$is_successful = 2;
}
elsif( $output =~ /The\ service\ has\ not\ been\ started/ )
{
$is_successful = 1;
}
if( $is_successful == 2 )
{
print "$ip: OpenDLP stopped \n";
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"stopped\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
}
elsif( $is_successful == 1 )
{
print "$ip: OpenDLP already stopped \n";
}
else
{
print "$ip: Got unexpected response from command. The agent may be stopped. Check the previous screen to verify. \n";
}
# delete OpenDLP service
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'\"$profile_info{path}\\sc.exe\" delete OpenDLP\'";
my $output = `$command`;
my $is_successful = 0;
if( $output =~ /DeleteService\ SUCCESS/ )
{
$is_successful = 1;
}
if( $is_successful == 1 )
{
print "$ip: OpenDLP service deleted \n";
}
else
{
print "$ip: Got unexpected response from command. The agent may be stopped. Check the previous screen to verify. \n";
}
# delete all files in OpenDLP directory
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'cmd.exe /c del /Q \"$profile_info{path}\\*.*\"\'";
`$command`;
# rmdir OpenDLP
my $command = "winexe --user=\'$profile_info{domain}\'\\$escape_user --password=$escape_pass //$ip \'cmd.exe /c rmdir \"$profile_info{path}\"\'";
`$command`;
# update "control" in "systems" table
my $string = "UPDATE systems SET control=\"uninstalled\" WHERE scan=? AND ip=?";
$sth = $dbh->prepare( $string );
$sth->execute( $scanname, $ip );
print "$ip: OpenDLP stopped and uninstalled \n";
}
$sth->finish();
$dbh->disconnect();
}
}
}
#footer();
sub get_profile
{
my $dbh_profile = DBI->connect("DBI:mysql:database=OpenDLP;host=localhost",$db_username,$db_password);
# first get profile for scanname
my $string = "SELECT profile FROM systems WHERE scan=? LIMIT 1";
my $sth_profile = $dbh_profile->prepare( $string );
$sth_profile->execute( $scanname );
my $results = $sth_profile->fetchrow_arrayref();
my $profile = $$results[0];
my $string = "SELECT username,password,domain,path,concurrent from profiles where profile=?";
$sth_profile = $dbh_profile->prepare( $string );
$sth_profile->execute( $profile );
my $results = $sth_profile->fetchrow_arrayref();
$profile_info{username} = $$results[0];
$profile_info{password} = $$results[1];
$profile_info{domain} = $$results[2];
$profile_info{path} = $$results[3];
$profile_info{concurrent} = $$results[4];
$sth_profile->finish();
$dbh_profile->disconnect();
}
sub header
{
print "Content-type: text/html\n\n";
print "\n\n";
print "OpenDLP 0.1\n";
print "\n";
print "\n";
print "\n";
print "\n";
print "\n";
# make a fancy false positive interface in the next release
# print qq {
#
#
# };
print "\n";
print "\n";
print '