#!/usr/bin/perl
# $Date: 2015-06-12 $
# $Revision: 2.5.1 $
# $Source: /root/bin/submitsh $
# $Author: Matthew Harris $
# Submit shells to David Naraghi for Updating Scan
# http://git.toolbox.hostgator.com/submitsh
# Please submit all bug reports at jira.endurance.com
# (C) 2014 - HostGator.com, LLC
use strict;
use warnings;
use version; our $VERSION = qv('2.5');
use Carp;
use CPAN;
use Getopt::Long;
use Digest::MD5;
use Sys::Hostname;
use File::Copy;
use File::Basename;
use File::Path qw{ make_path }; 
use English qw{ -no_match_vars };

BEGIN {
    eval { require Net::SMTP::SSL };
    if ($EVAL_ERROR) {
#        croak 'Please install perl module: Net::SMTP::SSL' . "\n\n";
         CPAN::Shell->install("Net::SMTP::SSL");
         print "\n\n[+] Perl Module Installed. Please Run Again\n\n";
         exit;
    }
}

my ( $host, $sample, $user, $false, $name, $path);

GetOptions( 'false|f' => \$false, );

my $input = $ARGV[0];
($name,$path) = fileparse($input);
if ( !defined $input ) {
    usage();
}
else {
    do_submit($input);
}

sub do_submit {
    my $md5   = md5sum($input);
    my @stats = stat $input;
    $user = getpwuid $stats['4'];
    my $size = $stats['7'];
    $host = hostname;
    getsample($input);
    my $ruser = $ENV{'RUSER'};

    if ( !defined $ruser ) {
        $ruser = 'clam-sample';
    }

    my $output = <<"EOF";
Submitter: $ruser
User: $user
Server: $host
Hash: $md5:$size:$input

$sample
EOF
    my $hostname = open_port($host);
    sendmail($output);
    if ( $hostname !~ /\.(hostgator\.(com(\.(tr|br))?|in)|(websitewelcome|webhostsunucusu|ehost(s)?|ideahost|hostclear)\.com|websitedns\.in|prodns\.com\.br)$/ ) {
        remove_rules();
    }
    if ( -d "/home/$user/.security/shells" ) {
        copy($path.$name, "/home/$user/.security/shells/$name");
    } else {
        make_path "/home/$user/.security/shells";
        copy($path.$name, "/home/$user/.security/shells/$name");
    }
    return;
}

sub open_port {
    my $hostname = shift;
    if ( $hostname =~ /\.(hostgator\.(com(\.(tr|br))?|in)|(websitewelcome|webhostsunucusu|ehost(s)?|ideahost|hostclear)\.com|websitedns\.in|prodns\.com\.br)$/ ) {
        return $hostname;
    } else {
        print "[*] Opening port 465 temporarily\n";
        # VPS / Dedi Not Allowing Outbound 465
        my $cmd1 = '/sbin/iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT';
        my $cmd2 = '/sbin/iptables -I OUTPUT -p tcp --dport 465 -j ACCEPT';
        system($cmd1);
        system($cmd2);
        return $hostname;
    }
}

sub remove_rules {
    print "[*] Removing temporary port 465 rules\n";
    my $cmd1 = '/sbin/iptables -D INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT';
    my $cmd2 = '/sbin/iptables -D OUTPUT -p tcp --dport 465 -j ACCEPT';
    system($cmd1);
    system($cmd2);
}

sub md5sum {
    my $file = shift;
    my $digest;
    open my $fh, '<', $file or croak "[!] Could not find file: $file\n";
    my $fmd5 = Digest::MD5->new;
    $fmd5->addfile($fh);
    $digest = $fmd5->hexdigest;
    close $fh or croak 'Cannot close file handle';
    return $digest;
}

sub getsample {
    my $file = shift;
    my $shell;
    open my $fh, '<', $file or croak "[!] Could not find file: $file\n";
    my @contents = <$fh>;
    close $fh or croak 'Cannot close file handle';
    foreach (@contents) {
        $shell .= $_;
    }
    if ( !defined $shell ) {
        print "[!] Empty File\n" or croak 'Cannot print';
        exit;
    }
    $sample = substr $shell, 0, '3000';
    return;
}

sub sendmail {
    my $body     = shift;
    my $username = 'clam-sample';
    my $smtp     = Net::SMTP::SSL->new( 'zimbra.hostgator.com', Port => 465 );
    $smtp->mail("$username\@hostgator.com");
    $smtp->recipient('dnaraghi@hostgator.com');
    $smtp->recipient('pmarcotte@hostgator.com');
    $smtp->recipient('dbarker@hostgator.com');
    $smtp->recipient('securitymanagers\@hostgator.com');
    $smtp->data();
    $smtp->datasend("From: $username\@hostgator.com\n");
    $smtp->datasend("To: dnaraghi\@hostgator.com\n");
    $smtp->datasent("To: pmarcotte\@hostgator.com\n");
    $smtp->datasent("To: dbarker\@hostgator.com\n");
    $smtp->datasend("To: securitymanagers\@hostgator.com\n");
    if ($false) {
        $smtp->datasend(
            "Subject: ClamAV Sample - $host - $user - *False Positive*\n");
    }
    else {
        $smtp->datasend("Subject: ClamAV Sample - $host - $user\n");
    }
    $smtp->datasend("\n");
    $smtp->datasend("$body");
    $smtp->dataend;
    $smtp->quit;
    print "\n[+] Shell sample sent.\n\n" or croak 'Cannot print';
    return;
}

sub usage {
    my $usage = <<'END_USAGE';
 
  Usage: submitsh FILENAME
  Options:
    --false, -f     Mark as false positive
    --help, -h      Show this usage

END_USAGE
    print $usage or croak 'Cannot print usage';
    exit;
}
