#!/usr/bin/perl -w
#################################################################
# security.pl - Michael Karr
# The Mechanical Security Administrator
# Investigates source of, and cleans found shells and malware.
#
# Git: http://git.toolbox.hostgator.com/security
# Installer: http://git.hgfix.net/security/dev/install_security.sh
# Wiki: https://gatorwiki.hostgator.com/Security/SecurityScript
# 
# Please submit all bug reports at http://bugs.hostgator.com
#
# (c) 2012 - HostGator.com, LLC.
#################################################################

BEGIN { # begin package Security::Config
package Security::Config;

use strict;
use warnings;

our $range = 60; # fuzzy search range (in seconds) for HF
our $trimwidth = 120; # length to trim matches to

# utilities we need to run

our $utilities = { 
    nscan => ['/root/bin/autoclean', 'http://git.hgfix.net/autoclean/autoclean'],
    scan => ['/root/bin/scan','http://scripts.hostgator.com/~sec/scan'],
    vdetect => ['/root/bin/vdetect','http://git.hgfix.net/vdetect/vdetect'],
    hf => ['/root/bin/hf','http://git.hgfix.net/hf/hf.pl'],
    encfind => ['/root/bin/encfind','http://git.hgfix.net/encfind/encfind.pl'],
    addedbyip => ['/root/bin/added_by_ip','http://git.hgfix.net/iphistory/added_by_ip'],
};

our $notifyemail = "mkarr\@hostgator.com"; # email address to send review notifications to
our $sendnotice = 0; # should we send a notice by default?

our $datelimit = 1298959200; # date before which we should discard results (01 Mar 2011 00:00:00 -0600)
our $use_interactive_hf = 1; # do we want to use the interactive version of the hf code?

1;
} # end package Security::Config


{ # begin package main
package main;

use strict;
use warnings;

use Getopt::Long;

use Cwd;
use FileHandle;
use File::stat;
use File::Find;
use File::Path qw/mkpath/;
use File::Basename qw/dirname/;

use DateTime;
use Date::Parse;
use Date::Format;

use MIME::Lite;
use Sys::Hostname;
use JSON;

use Term::ANSIColor;
use Text::Wrap;

# globals

my %userlogs; # will store the file handles for the user logs
my %userlogsn; # will store the filenames for the user logs
my %userstatslogs; # will store the file handles for the stats logs
my %backupdir; # stores the current backup dir for the user

# grab arguments

my %args = (
    interactive => $Security::Config::use_interactive_hf,
);

GetOptions (
    'list|l' => \$args{list},
    'user|u=s' => \$args{user},
    'debug' => \$args{debug},
    'interactive|i=s' => \$args{interactive},
    'reseller|r=s' => \$args{reseller},
    'noclean|n' => \$args{noclean},
);

# main routine

## LOCkE Test :: Ensuring  tabula rasa
    my $lockuser;
    if (defined($args{reseller})) {
        $lockuser=$args{reseller};
    } elsif (defined($args{user})) {
        $lockuser=$args{user};
    } elsif (my $user = dir_to_user(cwd())) {
        $lockuser=$user;
    }
    loguser($lockuser,'');
    my $PROGRAM = 'security';
    my $LOCKDIR = File::Spec->catdir(user_to_homedir($lockuser), ".security");
    my $LOCKFILE = "$LOCKDIR/$PROGRAM.lock";
    loguser($lockuser,'');
    # use a lockfile to prevent spawning duplicate processes
    unless ( sysopen(my $fh, $LOCKFILE, O_CREAT | O_EXCL | O_RDWR, 0600) )
        {
            close $fh;
        # the lockfile already exists
        print "Lockfile $LOCKFILE aready exists.\nChecking for running $PROGRAM process...\n";
        # sleep for a couple of seconds to make sure we don't race
        # by this time the process should be in the process table
        sleep 2;
        my @ps = grep{ m/(\d+).*?security/ } `ps --no-headers axo pid,args`;
        # we expect 1 copy (us) running
        die "\nThere is already a copy of $PROGRAM running!\n" if @ps > 1;
    }
    $SIG{INT} = $SIG{TERM} = sub {
        unlink $LOCKFILE or warn "Could not unlink $LOCKFILE for $PROGRAM\n";
        syswrite(STDERR, "Caught ^C or KILL. Exiting after lockfile removal.\n", 55 );
        exit;
    };


if (defined($args{list})) {
    if (exists $ARGV[0]) {
        procfile($ARGV[0]); # use the file were were given
    } else {
        procfile("-"); # use stdin if we were provided with nothing else
    }
} else {
    if (defined($args{reseller})) {
        scanreseller($args{reseller});
    } elsif (defined($args{user})) {
        scanuser($args{user});
    } elsif (my $user = dir_to_user(cwd())) {
        scanuser($user);
    } else {
        print "No user given, and I do not appear to be in a home directory.\n";
        print "Please use '-u' to specify a user or '-l' to specify a list of files.\n";
    }
}

cleanup();

exit;

# end main routine

sub cleanup {
    # clean up opened log files
        
    for my $user (keys %userlogs) {
        if (defined($userlogs{$user})) {
            $userlogs{$user}->close();
        }
    }
    
    for my $user (keys %userstatslogs) {
        if (defined($userlogs{$user})) {
            $userlogs{$user}->close();
        }
    }
    unlink $LOCKFILE;
}

sub scanuser {
    my ($user) = @_;
    
    init();
    
    loguser($user, "Scanning for shell scripts on user '$user'.\n\n");
    
    my $files_r = scan($user);
    
    if (scalar(@{$files_r})) {
        loguser($user, "Shells found.\n\n");
    } else {
        loguser($user, "No shells found.\n\n");
    }
    
    procuser($user, $files_r);
}

sub scanreseller {
    my ($reseller) = @_;
    my $users_r = MK::Utility::cPanel::ownerusers($reseller);
    
    if (scalar(@{$users_r})) {
        $args{interactive} = 0;
        
        for my $user (sort {$a cmp $b} @{$users_r}) {
            scanuser($user);
            cleanup();
        }
    } else {
        print "No sub-acccounts found for the given user. Is it actually a reseller?\n";
    }
}

sub procfile {
    my ($file) = @_;
    
    init();
    
    if (defined(my $fh = FileHandle->new("<$file"))) {
        my %malware; # hash of arrays, user -> list of malware files
        
        while (my $line = <$fh>) {
            chomp $line;
            
            if (my $user = dir_to_user($line)) {                
                if (-f $line) { # make sure the file exists
                    push(@{$malware{$user}}, $line);
                }
            }
        }
        
        $fh->close();
        
        for my $user (keys %malware) {
            procuser($user, \@{$malware{$user}});
        }
    } else {
        print STDERR "Opening '$file' for reading failed!\n";
    }
}

sub procuser {
    my ($user, $list_r) = @_;
    my $creationdate = MK::Utility::cPanel::creationdate($user);
    
    loguser($user, "Processing user '$user': \n");

    if ($args{noclean}) {
        loguser($user, "No clean mode enabled.\n");
    }

    loguser($user, "\n");

    # check for enabled modsec rules
    
    loguser($user, "Checking for enabled mod_security rules.\n\n", 1);
    checkmodsec($user);
    
    # push file stats into array for later
        
    my @file_stat = map {
        my $fs = stat($_);
        {
            file => $_,
            size => $fs->size,
            ctime => $fs->ctime,
            mtime => $fs->mtime,
        };
    } @{$list_r};
        
    # if we don't have a list of files, don't bother doing anything associated with it
    
    if (scalar(@{$list_r}) > 0) {
        loguser($user, "Shells found under user '$user': \n\n");
            
        for my $file (@{$list_r}) {
            loguser($user, "$file\n");
        }
    
        loguser($user, "\n");
        
        # remove the list of files
        
        unless($args{noclean}) {
            loguser($user, "Removing shells from account.\n\n", 1);
            removefiles($user, $list_r);
        }   
    }
    
    # run autoclean on the account
    
    loguser($user, "Scanning for malware.\n\n", 1);
    my $malware = autoclean($user);
    
    # add autoclean malware stats to list
    
    @file_stat = (@file_stat, @{$malware});
    
    # dump the stats list
    
    for my $file (@file_stat) {
        dumpstat($user, $file->{file}, $file->{ctime}, $file->{mtime})
    }

    # re-run clamav scan on files detected with autoclean

    loguser($user, "Rechecking detected files for shells.\n\n", 1);
    my $newshells_r = scanfiles($user, $malware);

    if (scalar(@{$newshells_r}) > 0) {
        loguser($user, "Shells found under user '$user': \n\n");
            
        for my $file (@{$newshells_r}) {
            loguser($user, "$file\n");
        }
    
        loguser($user, "\n");
        
        # remove the list of files

        unless($args{noclean}) {
            loguser($user, "Removing shells from account.\n\n", 1);
            removefiles($user, $newshells_r);
        }
    }

    # run hf checks
    
    if (scalar(@file_stat) > 0) {
        # 1. Generate two lists: files sorted by ctime, and files sorted by mtime.
        
        my @oldest_mtime = sort {$a->{mtime} <=> $b->{mtime}} @file_stat;
        my @oldest_ctime = sort {$a->{ctime} <=> $b->{ctime}} @file_stat;
        
        # 2. Discard mtime entries from before account creation
        
        @oldest_mtime = grep {($_->{mtime} > $creationdate)} @oldest_mtime;
        
        # 3. Check if there are logs in '/home/$user/logs' pre-dating date limit.
        
        my $log_times = scalar (grep {$_ < $Security::Config::datelimit} user_log_times($user));
        
        # 4. Check to see if there are any entries in the list post-dating date limit.
        
        my $post_entries = scalar(grep {$_->{mtime} >= $Security::Config::datelimit} @oldest_mtime) + scalar(grep {$_->{ctime} >= $Security::Config::datelimit} @oldest_ctime);
        
        # 5. If 3 evaluates as false, and 4 evaluates as true, then discard entries before date limit from both lists.
        
        if ((!$log_times) && $post_entries) {
            @oldest_mtime = grep {($_->{mtime} > $Security::Config::datelimit)} @oldest_mtime;
            @oldest_ctime = grep {($_->{ctime} > $Security::Config::datelimit)} @oldest_ctime
        }
        
        # 5a. If both 3 and 4 evaluate as false, then we probably don't have logs of the oldest file's modification. Emit warning.
        
        if ((!$log_times) && (!$post_entries)) {
            loguser($user, color('bold')."Warning: Modification times appear to predate archived log availablilty!\n\n".color('reset'));
        }
        
        # 6. If there are any mtime entries remaining, run HF on oldest entry from mtime list.
        
        if (scalar(@oldest_mtime) > 0) {
            loguser($user, "Oldest file (based on mtime):\n\n");
            
            # 6a. If the oldest mtime entry is within a day of the account creation date, display a warning.
            
            if (($oldest_mtime[0]->{mtime} < ($creationdate + 86400)) && ($oldest_mtime[0]->{mtime} > ($creationdate - 86400))) {
                loguser($user, color('bold')."Warning: Modification times appear to coincide with account creation!\n\n".color('reset'));
            }
            
            ($args{interactive}) ? findhack_interactive($user, $oldest_mtime[0], \@file_stat) : findhack($user, $oldest_mtime[0], \@file_stat);
        }    
        
        # 7. If there are any ctime entries left, and if oldest entry is different than oldest mtime entry, Run HF on oldest ctime entry.
     
        if (scalar((@oldest_ctime) > 0) && ((!(scalar(@oldest_mtime) > 0)) || ($oldest_ctime[0]->{file} ne $oldest_mtime[0]->{file}))) {
            loguser($user, "Oldest file (based on ctime):\n\n");
            
            # 7a. If the oldest ctime entry is within a day of the account creation date, display a warning.
            
            if (($oldest_ctime[0]->{ctime} < ($creationdate + 86400)) && ($oldest_ctime[0]->{ctime} > ($creationdate - 86400))) {
                loguser($user, color('bold')."Warning: Change times appear to coincide with account creation!\n\n".color('reset'));
            }
            
            ($args{interactive}) ? findhack_interactive($user, $oldest_ctime[0], \@file_stat) : findhack($user, $oldest_ctime[0], \@file_stat);
        }
        
        # 8. Find suspicious encoded files modified after our oldest file (if it exists)
    
        if (scalar(@oldest_mtime) || scalar(@oldest_ctime)) {
            # scan recently changed files for possible unknown encoded malware
            my $oldest;
            
            if (defined($oldest_ctime[0] && !defined($oldest_mtime[0]))) {
                $oldest = $oldest_ctime[0]->{ctime};
            } elsif (defined($oldest_mtime[0] && !defined($oldest_ctime[0]))) {
                $oldest = $oldest_mtime[0]->{mtime};
            } else {
                $oldest = ( $oldest_ctime[0]->{ctime} < $oldest_mtime[0]->{mtime} ? $oldest_ctime[0]->{ctime} : $oldest_mtime[0]->{mtime});
            }

            loguser($user, "Searching for suspicious files.\n\n", 1);
            findencfiles($user, $oldest);
        }
    }
    
    loguser($user, "Searching for outdated software.\n\n", 1);
    vdetect($user);
    
    if ($Security::Config::sendnotice) {
        loguser($user, "\nSending notification.\n\n", 1);
        sendnotice($user);
    }
}

sub loguser {
    my ($user, $msg, $debug) = @_;
    
    if ($debug) {
        return unless $args{debug};        
    }
    
    if ($args{debug}) {
        $msg = time.': '.$msg;
    }
    
    print $msg; # output to standard out too
    
    my $secdir = File::Spec->catdir(user_to_homedir($user), ".security");
    
    unless (-d $secdir) {
        mkpath($secdir);
    }
    
    unless (exists($userlogs{$user})) { # if we do not have a log opened for the user, open it
        $userlogsn{$user} = File::Spec->catfile($secdir, "securitylog.".time);
        $userlogs{$user} = FileHandle->new(">$userlogsn{$user}");
        
        unless (defined($userlogs{$user})) {
            print STDERR "Failed to open log file in home directory for user '$user'.\n";
            $userlogs{$user} = undef; # we want to continue anyway and will just forgo writing to the log
        }
    }
    
    if (defined($userlogs{$user})) { # don't bother writing to an undefined file handle
        print {$userlogs{$user}} $msg;
    }
}

sub removefiles {
    my ($user, $files_r) = @_;
    
    for my $file (@{$files_r}) {
        if (-f $file) { # make sure it exists. first
            if (backupfile($user, $file) == 0) {
                if ($file =~ /^.*?\/(index|main|default|header|define_[^\.]*|application_(top|bottom))\.([a-z]?html?|php[0-9]?)$/) { # if the file is on the whitelist, just blank it
                    if (my $fh = FileHandle->new(">$file")) {
                        print $fh "\n"; # just output a single newline
                        $fh->close();
                        loguser($user, "Cleared malicious file: $file\n");
                    } else {
                        loguser($user, "Failed to clear file: $file\n");
                    }
                } else { 
                    if (unlink($file)) {
                        loguser($user, "Removed malicious file: $file\n");
                    } else {
                        loguser($user, "Failed to remove file: $file\n");
                    }
                }
            } else {
                loguser($user, "Failed to backup file '$file'. Aborting removal.\n");
            }
        }
    }

    loguser($user, "\n");
}

sub backupfile {
    my ($user, $file) = @_;
  
    unless (defined ($backupdir{$user})) {
        my $secdir = File::Spec->catdir(user_to_homedir($user), ".security");
        
        unless (-d $secdir) {
            mkpath($secdir);
        }
        
        $backupdir{$user} = File::Spec->catdir($secdir, "securitybkup.".time);
        mkpath($backupdir{$user});
    }
    
    my $destfile = File::Spec->catfile($backupdir{$user}, $file);
    my $path = dirname($destfile);

    unless (-d $path) {
        mkpath($path);
    }
    
    return system("cp", "-a", $file, $destfile);
}

sub trimtowidth {
    my ($text, $wchar) = @_;

    if ((length($text)) > $wchar) {
        return substr($text,0,(($wchar-3)/2))."...".substr($text,-(($wchar-3)/2));
    }
    
    return $text;
}   

sub sendnotice {
    my ($user) = @_;
    my $host = hostname;
    $host =~ s/^([^\.]*)\..*$/$1/;
    
    my $msg = MIME::Lite->new(
        To => $Security::Config::notifyemail,
        Subject => "[SECURITY] $user\@$host - Susipicious files found.",
        Type => "TEXT",
        Data => "Suspicious files have been found under the user $user on the server ".hostname.".\n".
                "The report is attached.\n"
    );
    
    $msg->attach(
        Type => "text/plain",
        Path => $userlogsn{$user},
        Filename => "report.$user.$host.txt"
    );
    
    $msg->send;
}

sub getdomains {
    my ($user) = @_;
    my $userpath = "/etc/userdomains";
    my %domains; # hash domains for the given user

    if (defined(my $fh = FileHandle->new("</etc/userdomains"))) {  
        while (my $line = <$fh>) {
            chomp($line); # Kill trailing whitespace/newline
            my @temp = split(": ", $line); # userdomains format is "domain.com: user"
            
            if ($temp[1] eq $user) { # if it's our user, add it
                $domains{$temp[0]} = undef;
            }
        }
        $fh->close();
    } else {
        print STDERR "Opening '/etc/userdomains' for reading failed!\n";
    }
    
    return \%domains;
}

sub checkmodsec {
    my ($user) = @_;
    
    my %secrules = %{getmodsec($user)}; # grab modsec rules for user's domains
    
    if (scalar(keys %secrules) > 0) {
        loguser($user, "Whitelisted mod_security rules detected.\n\n");
        
        for my $domain (keys %secrules) {
            loguser($user, "$domain:\n");
            
            for my $rule (@{$secrules{$domain}}) {
                loguser($user, "\t$rule\n");
            }
        }
        
        loguser($user, "\n");
    }
}
sub getmodsec {
    my ($user) = @_;
    my %domains = %{getdomains($user)};
    my %rules; # domain -> rules map
    
    if (defined(my $fh = FileHandle->new("</opt/mod_security/whitelist.conf"))) {  
        while (my $line = <$fh>) {
            if ($line =~ /^SecRule\sSERVER_NAME\s\"([^\"]*)"\s.*?ruleRemoveByID=([0-9]+)/) {
                if (exists($domains{$1})) {
                    push (@{$rules{$1}}, $2);
                }
            }
        }
        $fh->close();
    } else {
        print STDERR "Opening '/opt/mod_security/whitelist.conf' for reading failed!\n";
    }
    
    return \%rules;
}

sub init {
    my $sane = 1;
    
    # check to ensure that all needed utilities are present
    
    for my $utility (keys %{$Security::Config::utilities}) {
        if (!(-f ${$Security::Config::utilities->{$utility}}[0])) {
            print STDERR "Utility $utility not found.\n";
            $sane = 0;
        }
    }
    
    if (!$sane) {
        print STDERR "Environment not sane. Exiting\n";
        exit;
    }
}

sub autoclean {
    my ($user) = @_;
    
    # normal run
    
    loguser($user, "Scanning via Autoclean for normal malware.\n", 1);
    
    my $command_n;

    if ($args{noclean}) {
        $command_n = ${$Security::Config::utilities->{nscan}}[0]." -S -q -d ".user_to_homedir($user);
    } else {    
        $command_n = ${$Security::Config::utilities->{nscan}}[0]." -S -q -c -d ".user_to_homedir($user);
    }

    my @out_n = `$command_n`;
    my $ac_out_n;
    
    for my $line (@out_n) {
        if ($line =~ /^\{.*\}$/) { # we only want valid json, or things will blow up
            $ac_out_n = decode_json($line);
            last;
        }
    }

    my @matches_n = map {{
        file => $_,
        ctime => $ac_out_n->{$_}->{ctime},
        mtime => $ac_out_n->{$_}->{mtime},
        size => $ac_out_n->{$_}->{size},
    }} keys %{$ac_out_n};
    
    # htaccess run
    
    loguser($user, "Scanning via Autoclean for .htaccess malware.\n", 1);
    
    my $command_ht;

    if ($args{noclean}) {
        $command_ht = ${$Security::Config::utilities->{nscan}}[0]." -S -q -l htaccess -d ".user_to_homedir($user);
    } else {
        $command_ht = ${$Security::Config::utilities->{nscan}}[0]." -S -q -c -l htaccess -d ".user_to_homedir($user);
    }

    my @out_ht = `$command_ht`;
    my $ac_out_ht;
    
    for my $line (@out_ht) {
        if ($line =~ /^\{.*\}$/) { # we only want valid json, or things will blow up
            $ac_out_ht = decode_json($line);;
            last;
        }
    }
    
    my @matches_ht = map {{
        file => $_,
        ctime => $ac_out_ht->{$_}->{ctime},
        mtime => $ac_out_ht->{$_}->{mtime},
        size => $ac_out_ht->{$_}->{size},
    }} keys %{$ac_out_ht};
    
    # combine the two
    
    my @matches = (@matches_n, @matches_ht);
    
    if (scalar(@matches) > 0) {
        if ($args{noclean}) {
            loguser($user, "Malware has been detected:\n\n");
        } else {
            loguser($user, "Malware has been detected and cleaned in files:\n\n");
        }

        for my $match (@matches) {
            loguser($user, $match->{file}."\n");
        }
        
        loguser($user, "\n");
    }
    
    return \@matches;
}

sub scan {
    my ($user) = @_;
    
    my $command = ${$Security::Config::utilities->{scan}}[0]." -s $user";
    my @matches = map {/(^.*?):\s.*?FOUND$/ ? $1 : ()} `$command`;
    
    return \@matches;
}

sub scanfiles {
    my ($user, $malware) = @_;

    # ensure .security dir exists

    my $secdir = File::Spec->catdir(user_to_homedir($user), ".security");        
    unless (-d $secdir) {
        mkpath($secdir);
    }

    my $tmpfile = File::Spec->catfile($secdir, "scanfiles.tmp".time);
    my $tfh = FileHandle->new(">$tmpfile");

    unless (defined($tfh)) {
        loguser($user, "Failed to open temporary file for clamav scan.\n\n");
        return [];
    }

    for my $file (@{$malware}) {
        print $tfh $file->{file}."\n";
    }

    my $command = ${$Security::Config::utilities->{scan}}[0]." -l $tmpfile";
    my @matches = map {/(^.*?):\s.*?FOUND$/ ? $1 : ()} `$command`;
    
    return \@matches;
}

sub vdetect {
    my ($user) = @_;

    my $command = ${$Security::Config::utilities->{vdetect}}[0]." --maxdepth 9 --plain --json --user $user";
    my $apps = decode_json(`$command`);
    my @vmatches = sort {${$a}[1] cmp ${$b}[1]} @{$apps};
    
    my %seen;
    my @outdated;
    
    for my $match (@vmatches) {
        my ($severity, $script, $location, $version) = @{$match};
        
        if (($severity > 1 || $severity < 0) && !(exists($seen{$location}))) { # filter based on severity
            $seen{$location} = undef;
            push(@outdated, $match);
        }
    }
    

    if (scalar(@outdated) > 0) {
        loguser($user, "Outdated software has been detected:\n\n");
    
        for my $match (@outdated) {
            my ($severity, $script, $location, $version) = @{$match};
            
            loguser($user, sprintf(" % -16s :: % 8s :: % -60s\n",$script,$version,$location));
        }
    
        loguser($user, "\n");
        
        $Security::Config::sendnotice = 1;
    } else {
        loguser($user, "No outdated software has been detected:\n\n");
    }
}

sub findencfiles {
    my ($user, $ctime) = @_;
    
    my $command = ${$Security::Config::utilities->{encfind}}[0]." --directory ".user_to_homedir($user)." --modified-after $ctime --json";
    my $encfiles = decode_json(`$command`);
    
    if (scalar(keys %{$encfiles}) > 0) {
        loguser($user, "Found suspicious encoded files:\n\n");
        
        for my $file (keys  %{$encfiles}) {
            loguser($user, "$file\n");
        }
        
        loguser($user, "\n");
        
        for my $file (keys  %{$encfiles}) {
            loguser($user, "Matches for file '$file':\n");
            
            for my $match (@{$encfiles->{$file}}) {
                my $output = $match->{match};
                $output =~ s/[^[:punct:]a-zA-Z0-9 ]//g;
                loguser($user, trimtowidth("Match: $output\n", $Security::Config::trimwidth));
                
                for my $decoded (@{$match->{decoded}}) {
                    my $output = $decoded;
                    $output =~ s/[^[:punct:]a-zA-Z0-9 ]//g;
                    
                    loguser($user, trimtowidth("Decoded: $output\n", $Security::Config::trimwidth));
                }
            }
            
            loguser($user, "\n");
        }
        
        $Security::Config::sendnotice = 1;
    } else {
        loguser($user, "No suspicious encoded files found.\n\n");
    }
}

sub findhack {
    my ($user, $file, $filelist_r) = @_;
    
    loguser($user, 'File: '.$file->{file}."\n");
    
    my $mtime = DateTime->from_epoch(epoch => $file->{mtime});
    my $ctime = DateTime->from_epoch(epoch => $file->{ctime});
    
    my $tz = DateTime::TimeZone->new(name =>'local');
    
    $mtime->set_time_zone($tz);
    $ctime->set_time_zone($tz);
    
    loguser($user, "Modify: ".$mtime->strftime("%a, %d %b %Y %H:%M:%S %z")."\n");
    loguser($user, "Change: ".$ctime->strftime("%a, %d %b %Y %H:%M:%S %z")."\n\n");
    
    $mtime->set_time_zone('UTC');
    $ctime->set_time_zone('UTC');
    
    # loguser($user, "Logs for Modify Time:\n\n");
    my $entries = run_hf($user, $file->{file}, $file->{size}, $file->{mtime});
    
    my $logcnt = 0;
    my $found = 0;
    
    my %ips;
    
    for my $service (keys %{$entries}) {
        $logcnt += $entries->{$service}->{log_count};
        
        if (scalar(@{$entries->{$service}->{matches}}) > 0) {
            for my $match (@{$entries->{$service}->{matches}}) {
                loguser($user, $match->{match}->{log}.": ".$match->{match}->{entry}."\n");
                $found++;
                
                if ($service =~ /^ftp_/) {
                    if ($match->{match}->{entry} =~ /(([01]\s)|(@))(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})((\)\s)|(\s\d+\s))/) { # extract ip
                        $ips{$4}++;
                    }
                }
            }
            
            loguser($user, "\n");
        }
    }
    
    if ($file->{ctime} != $file->{mtime}) {
        # loguser($user, "Logs for Change Time:\n\n");
        $entries = run_hf($user, $file->{file}, $file->{size}, $file->{ctime});
        
        for my $service (keys %{$entries}) {
            $logcnt += $entries->{$service}->{log_count};
            
            if (scalar(@{$entries->{$service}->{matches}}) > 0) {
                for my $match (@{$entries->{$service}->{matches}}) {
                    loguser($user, $match->{match}->{log}.": ".$match->{match}->{entry}."\n");
                    $found++;
                    
                    if ($service =~ /^ftp_/) {
                        if ($match->{match}->{entry} =~ /(([01]\s)|(@))(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})((\)\s)|(\s\d+\s))/) { # extract ip
                            $ips{$4}++;
                        }
                    }
                }
                
                loguser($user, "\n");
            }
        }
    }
    
    unless ($found > 0) {
        loguser($user, "No matches found in the available logs!\n\n");
    }
    
    unless ($logcnt > 0) {
        loguser($user, "No logs found for the referenced time!\n\n");
    }
    
    if (scalar(keys %ips) > 0) {
        loguser($user, "The following IP addresses uploaded files via FTP:\n\n");
        
        for my $ip (keys %ips) {
            loguser($user, "$ip\n");
        }
        
        loguser($user, "\n");
        
        for my $ip (keys %ips) {
            filesbyip($user, $ip, $filelist_r);
        }
        
        loguser($user, "\n");
    }
}

sub run_hf {
    my ($user, $filename, $filesize, $time) = @_;
    
    my $command = ${$Security::Config::utilities->{hf}}[0]." -u $user -r ".$Security::Config::range." --filename=\"".$filename."\" --filesize=$filesize -j \"$time\"";
    return decode_json(`$command`);
}

sub dumpstat {
    my ($user, $path, $ctime, $mtime) = @_;
    
    my $secdir = File::Spec->catdir(user_to_homedir($user), ".security");
    
    unless (-d $secdir) {
        mkpath($secdir);
    }

    unless (exists($userstatslogs{$user})) { # if we do not have a log opened for the user, open it
        my $file = File::Spec->catfile($secdir, "statlog.".time);
        $userstatslogs{$user} = FileHandle->new(">$file");
        
        unless (defined($userstatslogs{$user})) {
            print STDERR "Failed to open stat log file in home directory for user '$user'.\n";
            $userstatslogs{$user} = undef; # we want to continue anyway and will just forgo writing to the log
        }
    }
    
    if (defined($userstatslogs{$user})) { # don't bother writing to an undefined file handle
        my $tz = DateTime::TimeZone->new(name =>'local');

        my $dctime = DateTime->from_epoch(epoch => $ctime);
        $dctime->set_time_zone($tz);
    
        my $dmtime = DateTime->from_epoch(epoch => $mtime);
        $dmtime->set_time_zone($tz);

        print {$userstatslogs{$user}} "File: $path, Change Time: ".$dctime->strftime("%m/%d/%Y %T %z")." ($ctime), Modify Time: ".$dmtime->strftime("%m/%d/%Y %T %z")." ($mtime)\n";
    }
}

sub findhack_interactive {
    my ($user, $file, $filelist_r) = @_;
    
    my $done = 0;
    my %logs_matches;
    my @files = ($file);
    
    while (!$done) {
        my $cur_file = pop(@files);        
        dump_file_info($user, $cur_file);
        
        if (!exists($logs_matches{$cur_file})) {
            $logs_matches{$cur_file} = compile_hf_info($user, $cur_file);
        }
        
        my $log_matches_r = $logs_matches{$cur_file};
        
        if (scalar(@{$log_matches_r}) > 0) {
            my $selection = selection_menu($user, $log_matches_r);
            
            if ((!defined($selection)) || ($selection eq '')) {
                loguser($user, "\nContinuing without selection.\n");
                
                my $ips_r = extract_ips($log_matches_r);
                files_by_ip_list($user, $ips_r, $filelist_r);
                $done = 1;
            } else {
                push(@files, $cur_file);
                
                loguser($user, "\nSelected log entry '$selection'.\n");
                
                if (my $newfile = re_file($user, $log_matches_r->[$selection])) {                
                    if (my $fs = stat($newfile)) {
                        push(@files, {
                            file => $newfile,
                            ctime => $fs->ctime,
                            mtime => $fs->mtime,
                            size => $fs->size,
                        });
                    } else {
                        loguser($user, "File does not appear to exist any longer.\n\n");
                    }
                } else {
                    loguser($user, "Log entry does not appear to correspond to a valid file location.\n\n");
                }
            }
        } else {
            loguser($user, "No matches found.\n\n");
            
            if (scalar(@files) <= 0) {
                $done = 1;
            }   
        }
    }
    
    loguser($user, "\n");
}

sub dump_file_info {
    my ($user, $file) = @_;    
    loguser($user, 'File: '.$file->{file}."\n");
    
    my $mtime = DateTime->from_epoch(epoch => $file->{mtime});
    my $ctime = DateTime->from_epoch(epoch => $file->{ctime});
    
    my $tz = DateTime::TimeZone->new(name =>'local');
    
    $mtime->set_time_zone($tz);
    $ctime->set_time_zone($tz);
    
    loguser($user, "Size: ".$file->{size}." bytes \n");
    loguser($user, "Modify: ".$mtime->strftime("%a, %d %b %Y %H:%M:%S %z")." (".$file->{mtime}.")\n");
    loguser($user, "Change: ".$ctime->strftime("%a, %d %b %Y %H:%M:%S %z")." (".$file->{ctime}.")\n\n");
}

sub compile_hf_info {
    my ($user, $file) = @_;
    my (@log_data, @log_matches);
    
    push (@log_data, run_hf($user, $file->{file}, $file->{size}, $file->{mtime}));
    
    if ($file->{ctime} != $file->{mtime}) {
        push (@log_data, run_hf($user, $file->{file}, $file->{size}, $file->{ctime}));
    }
    
    for my $match_set (@log_data) {
        for my $service (keys %{$match_set}) {
            for my $match (@{$match_set->{$service}->{matches}}) {
                push (@log_matches, {
                    service => $service,
                    time => $match->{match}->{time},
                    log => $match->{match}->{log},
                    entry => $match->{match}->{entry},
                    flag => $match->{flag},
                });
            }
        }
    }
    
    return \@log_matches;
}

sub selection_menu {
    my ($user, $log_matches_r) = @_;
    
    $Text::Wrap::columns = $Security::Config::trimwidth;
    $Text::Wrap::unexpand = 0;
    
    my $i; for ($i = 0; $i < scalar(@{$log_matches_r}); $i++) {
        my $label = "    $i) ";
        loguser($user, wrap('', ' ' x length($label), color('bold').$label.color('reset').$log_matches_r->[$i]->{log}.": ".$log_matches_r->[$i]->{entry})."\n\n");
    }
        
    my $selection; 

    while (!defined($selection) || !((($selection =~ /^\d+?$/) && ($selection >= 0) && ($selection < scalar(@{$log_matches_r}))) || ($selection =~ /^$/))) {
        loguser($user, "Please select the log entry you wish to investigate further (no selection to continue/exit): ");
        
        if (-t STDIN) {
            $selection = <STDIN>;
        } else {
            last;
        }
    }
        
    chomp($selection);
    return $selection;
}

sub extract_ips {
    my ($log_matches_r) = @_;
    my %ips;
                
    for my $match (@{$log_matches_r}) {
        if ($match->{service} =~ /^ftp_/) {
            if ($match->{entry} =~ /(([01]\s)|(@))(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})((\)\s)|(\s\d+\s))/) { # extract ip
                $ips{$4}++;
            }
        }
    }
    
    return \%ips;
}

sub files_by_ip_list {
    my ($user, $ips_r, $filelist_r) = @_;
    
    if (scalar(keys %{$ips_r}) > 0) {
        loguser($user, "The following IP addresses uploaded files via FTP:\n\n");
        
        for my $ip (keys %{$ips_r}) {
            loguser($user, "$ip\n");
        }
        
        loguser($user, "\n");
        
        for my $ip (keys %{$ips_r}) {
            filesbyip($user, $ip, $filelist_r);
        }
    }
}

sub re_file {
    my ($user, $match) = @_;
    
    if ($match->{service} eq 'apache_dom') {
        if ($match->{log} =~ /\/([^\/]+?)(?:-\w{3}-\d{4}\.gz)?$/) {
            my $domain = $1;
            
            if (my $docroot = MK::Utility::cPanel::get_docroot($domain)) {
                
                if ($match->{entry} =~ /"(?:GET|POST)\s\/([^\s]+)\sHTTP\/\d+\.\d+"/) {
                    my $file = $1;
                    $file =~ s/^([^?]+)\?.*$/$1/; # strip query vars out
                    return  $docroot.'/'.$file;;
                }
            }
        }
    }
    
    return undef;
}

sub filesbyip {
    my ($user, $ip, $filelist_r) = @_;
    
    my $command = 'perl '.${$Security::Config::utilities->{addedbyip}}[0]." -u $user -m -x -j $ip";
    
    my $ipdata; eval { $ipdata = decode_json(`$command`); };
    
    if ($@) {
        loguser($user, "Added_By_IP returned invalid data.\n");
    } else {
        my %filescleaned = map {$_->{file} => undef} @{$filelist_r};
        
        loguser($user, "Files uploaded by IP '$ip' that have not yet been removed/cleaned:\n\n");
        
        for my $file (sort {$a cmp $b} (keys %{$ipdata->{uploaded_only}}, keys %{$ipdata->{both_directions}})) {
            if ((-f $file) && (!exists($filescleaned{$file}))) {
                loguser($user, "$file\n");
            }
        }
        
        loguser($user, "\n");
    }
}

sub user_log_times {
    my ($user) = @_;
    
    my $dir = File::Spec->catdir(user_to_homedir($user), "logs");

    my $date_pattern = '([0-3][0-9]/[A-Z][a-z]{2}/[0-9]{4}:[0-2][0-9]:[0-5][0-9]:[0-6][0-9]\s[+\-][0-9]{4})';
    my @log_times;
    
    if (-d $dir) {
        opendir(my $dh, $dir);
        
        while(my $file = readdir($dh)) {
            if (my $time = time_start(File::Spec->catfile($dir, $file), $date_pattern)) {
                push(@log_times, $time);
            }
        }
        
        closedir($dh);
    }
    
    return @log_times;
}

sub parse_time {
    my ($time, $format, $tz) = @_;
    
    my $dtp = new DateTime::Format::Strptime(pattern => $format);
    my $dt = $dtp->parse_datetime($time);
    
    # fall back to Date::Parse if DateTime::Format::Strptime fails
    
    if (!$dt) {
        $dt = DateTime->from_epoch(epoch => str2time($time));
    }
    
    if ($tz) {
        $dt->set_time_zone($tz);
    }
    
    return $dt;
}

sub time_start {
    my ($file, $timerex) = @_;
   
    if (my $time = MK::Utility::Regex::regex_file_match($file, "($timerex)")) {
        return str2time($time);
    } else {
        return undef;
    }
}

sub dir_to_user {
    my ($dir) = @_;
    my %home_dirs;
    
    while(my @ent = getpwent()) {
        $home_dirs{File::Spec->canonpath($ent[7])} = $ent[0];
    }
    
    endpwent();
    
    my @dp = File::Spec->splitdir($dir);
    
    while (scalar(@dp)) {
        my $nd = File::Spec->canonpath(File::Spec->catdir(@dp));
        
        if (exists($home_dirs{$nd})) {
            if (-e '/var/cpanel/users/'.$home_dirs{$nd}) {
                return $home_dirs{$nd};
            }
        }
        
        pop(@dp);
    }
    
    return undef;
}

sub user_to_homedir {
    my ($user) = @_;
    my @pwd = getpwnam($user);
    return $pwd[7];    
}

} # end package main


BEGIN { # start package MK::Utility::cPanel
package MK::Utility::cPanel;

use strict;
use warnings;
use Carp;

use YAML::Syck qw(LoadFile);
use File::ReadBackwards;
use Date::Parse;

# module subroutines

sub get_docroot {
    my ($domain) = @_;
    
    # normalize domain
    
    $domain = lc $domain;
    $domain =~ s/(?:^\s+)|(?:\s+$)//g; # trim whitespace
    $domain =~ s/(?:^www\.)//; # remove preceeding 'www.'
    
    if (my $username = MK::Utility::Regex::regex_file_match('/etc/userdomains', '^'.$domain.':\s(\w+)$')) {
        my ($userdata_ref, $maindata_ref) = load_userdata($username, $domain);
        
        if ($userdata_ref) {
            return $userdata_ref->{documentroot};
        } else {
            return undef;
        }
    } else {
        return undef;
    }
}

sub load_userdata {
    my ($user, $domain) = @_;    
    my $real_domain;
    
    my $mainpath = '/var/cpanel/userdata/'.$user.'/main';
    my $main_ref =  LoadFile($mainpath);
    
    # if we are a sub/addon domain, load the associated subdomain file
    
    if ((ref($main_ref->{addon_domains}) eq 'HASH') && $main_ref->{addon_domains}->{$domain}) {
        $real_domain = $main_ref->{addon_domains}->{$domain};
    } elsif ((ref($main_ref->{sub_domains}) eq 'ARRAY') && (grep {$_ eq $domain} @{$main_ref->{sub_domains}})) {
        $real_domain = $domain;
    } else {
        $real_domain = $main_ref->{main_domain};
    }
    
    my $userpath = '/var/cpanel/userdata/'.$user.'/'.$real_domain;
    my $userdata_ref = LoadFile($userpath);
    
    return $userdata_ref, $main_ref;
}

sub creationdate {
    my ($user) = @_;

    my $pattern = '^(\w{3}\s\w{3}\s{1,2}\d{1,2}\s\d\d:\d\d:\d\d\s\d{4}):CREATE:[^:]*:[^:]*:[^:]*:[^:]*:'.$user.'$';
    my $date = 0;

    if (my $fh = File::ReadBackwards->new('/var/cpanel/accounting.log')) {
        while (defined(my $line = $fh->readline)) {
            if ($line =~ /$pattern/){
                $date = str2time($1);
                last;
            }
        }
        $fh->close;
    }

    return $date;
}

sub ownerusers {
    my ($owner) = @_;
    my @users;
    
    open(my $fh, '/etc/trueuserowners') or die "Failed to open '/etc/trueuserowners': $!";
    
    while (my $line = <$fh>) {
        if ($line =~ /^([^:]+):\s$owner$/) {
            push(@users, $1);
        }
    }
    
    close($fh);
    
    return \@users;
}


1;    
} # end package MK::Utility::cPanel


BEGIN { # start package MK::Utility::Regex
package MK::Utility::Regex;

use strict;
use warnings;
use Carp;

use FileHandle; 

# module subroutines

sub regex_file_match {
    my ($file, $rex) = @_;
    my $fh;
    
    if ($file =~ /^.*?\.gz$/) {
        open($fh, "-|", "zcat " . $file); # total hack, but super fast
    } else {
        $fh = FileHandle->new("<$file");
    }
    
    if ($fh) {
        my @matches;
        
        while(my $line = <$fh>) {
            if ($line =~ /$rex/) {
                push (@matches, $1);
                
                unless (wantarray()) {
                    last;
                }
            }       
        }
        
        $fh->close();
        
        if (wantarray()) {
            return @matches;
        } else {
            return shift @matches;
        }
    } else {
        return undef;
    }
}

1;    
} # end package MK::Utility::Regex
