perl<<'EOF'
use strict;
use Sys::Hostname;

my $service = 'service_placeholder';
my $action = 'action_placeholder';
my $ticket = 'ticket_placeholder';

my $hostname = hostname();
if ( $hostname =~ /(hostgator|websitedns|prodns|ehost|ideahost|hostclear|websitewelcome|webhostsunucusu|minidedicated)/ ) {
	die "[!] Do NOT run this on shared/reseller servers!\n";
}

if ( $action !~ /^(enable|disable)$/ ) {
	die "[!] Action not specified\n";
}

if (( $action eq 'disable' ) && ( -f '/root/.lockdown' )) {
	open(my $DAT, '<', '/root/.lockdown');
		my $ticket = <$DAT>;
	close($DAT);
	if ( $ticket ) {
		die "[!] Server is already locked down. See ticket $ticket\n";
	} else {
		die "[!] Server is already locked down. No ticket found\n";
	}
}

my %ports = (
	'http' => [
		80,
		81,
		443,
		8080,
		8000
	],
	'exim' => [
		25,
		26,
		587,
		110,
		143,
		993
	],
);


my $job;
if ( $action eq 'disable' ) {
	$job = '-I';
	open(my $WAT, '>', '/root/.lockdown');
		print $WAT $ticket;
	close($WAT);
} elsif ( $action eq 'enable' ) {
	$job = '-D';
	unlink('/root/.lockdown');
}

foreach my $port ( @{$ports{$service}} ) {
	system("iptables $job INPUT -p tcp --dport $port -j DROP");
	system("iptables $job OUTPUT -p tcp --dport $port -j DROP");
}

if ( $action eq 'disable' ) {
	print "[*] The server $hostname has had $service disabled via iptables:\n";
	foreach my $port ( @{$ports{$service}} ) {
		system("iptables -L -vn |grep DROP |grep -P \"(s|d)pt:$port\"");
	}
} elsif ( $action eq 'enable' ) {
	print "[*] The server $hostname has had $service unblocked via iptables:\n";
	foreach my $port ( @{$ports{$service}} ) {
		system("iptables -L -vn |grep -P \"(s|d)pt:$port\"");
	}
}
	
EOF
