32
And god saw that it was good. And god blessed them, saying” Be fruitful and multiply. Genesis 1:21:22 VIRUS VIRUS

writing a virus program

Embed Size (px)

Citation preview

Page 1: writing a virus program

And god saw that it was good. And god blessed them, saying” Be fruitful and multiply.

Genesis 1:21:22

VIRUSVIRUS

Page 2: writing a virus program

Points of Discussion

• Other malicious programs.• What is a VIRUS. • Types of VIRUS.• Functional elements of a VIRUS.• Tools needed to write a VIRUS.• Construction of the demons.• Conclusion.

Page 3: writing a virus program

Trojan horses.Trojan horses. Logical bombs.Logical bombs. Spy waresSpy wares Computer worms.Computer worms.

Other Malicious programsOther Malicious programs

Page 4: writing a virus program

What is a VIRUSWhat is a VIRUS

A A computer viruscomputer virus is a is a computer programcomputer program that can copy itself and infect a that can copy itself and infect a computer without permission or knowledge of the user .computer without permission or knowledge of the user .All All computercomputer viruses is manmade. A simple virus that can make a viruses is manmade. A simple virus that can make a copycopy of itself over of itself over and over again is relatively easy to produce. Even such a simple virus is and over again is relatively easy to produce. Even such a simple virus is dangerous because it will quickly use all available dangerous because it will quickly use all available memorymemory and bring the and bring the systemsystem to a halt. to a halt.

It can reproduce. When executed, it may makes one or more copies of It can reproduce. When executed, it may makes one or more copies of itself. Those copies may later be executed to make still more copies, ad itself. Those copies may later be executed to make still more copies, ad infinitum.infinitum.

Not all computer programs are categorized as VIRUS as the incapability Not all computer programs are categorized as VIRUS as the incapability to multiply, however all virus reproduce and not all VIRUS are destructive to multiply, however all virus reproduce and not all VIRUS are destructive because reproduction is not destruction.because reproduction is not destruction.

The scientifically correct term for a virus is The scientifically correct term for a virus is SSelf-elf-RReciprocating eciprocating AAutomation utomation ((SRASRA).).

Page 5: writing a virus program

Types of VIRUSTypes of VIRUS

The *.com file infector. The *.com file infector. Application program infector.Application program infector. Boot sector virus.Boot sector virus.

Page 6: writing a virus program

Functional elements of a virusFunctional elements of a virus

1.1. Search routine.Search routine.

2.2. Copy routine.Copy routine.

3.3. Anti-detection routine.Anti-detection routine.

Anti-detectionroutine

Search Copy

virus

Page 7: writing a virus program

Tools needed for writing virusesTools needed for writing viruses

Assembly language.Assembly language. Knowledge of the technical details of PC’s—like Knowledge of the technical details of PC’s—like

file structures, function calls, segmentation and file structures, function calls, segmentation and hardware design.hardware design.

A compatible assembler likeA compatible assembler like

1.1. Microsoft’s Macro AssemblerMicrosoft’s Macro Assembler, or , or MASMMASM for short. for short.

2.2. The second is The second is Borland’s Turbo AssemblerBorland’s Turbo Assembler, also known , also known as as TASMTASM..

3.3. Thirdly, there is Thirdly, there is A86A86..

Page 8: writing a virus program

Types of VIRUSTypes of VIRUS

The The *.com*.com file infector. (timid virus) file infector. (timid virus) Application program infector.Application program infector. Boot sector virus.Boot sector virus.

Page 9: writing a virus program

Some basics Some basics

The virus must be designed so that it’s code gets executed, rather than just the program it has attached itself to. Only then it can reproduce. Further, it must be able to pass control back to the host program, so that the host can execute in its entirety as well.

Since only the files like *.com and *.exe gets executed so the virus Since only the files like *.com and *.exe gets executed so the virus code must get itself attached to such a file only so that it can get code must get itself attached to such a file only so that it can get itself executed. So a search routine of the virus code searches itself executed. So a search routine of the virus code searches essentially for a file with these extensions.essentially for a file with these extensions.

Page 10: writing a virus program

Dos basicsDos basics When one enters the name of a program at the DOS prompt, DOSbegins looking for files with that name and an extent of “COM”. If it finds one it will load the file into memory and execute it. Otherwise DOS will look for files with the same name and an extent of “EXE” to load andexecute. If no EXE file is found, the operating system will finally look fora file with the extent “BAT” to execute. Failing all three of these possibilities, DOS will display the error message “Bad command or file name.” EXE and COM files are directly executable by the CentralProcessing Unit. Of these two types of program files, COM files are much simpler. They have a predefined segment format which is built intothe structure of DOS, while EXE files are designed to handle a user definedsegment format, typical of very large and complicated programs. The COM file is a direct binary image of what should be put into memory andexecuted by the CPU, but an EXE file is not.

Page 11: writing a virus program

Enter prog. name (dos prompt)

Load&

execute

Look for file with.com extension

yes

no

foundLoad

&execute

Look for file with.exe extension

yesno

found

Look for file withbat extension

foundLoad

&execute

Generate an errorBad Command or File Name

yes

no

Page 12: writing a virus program

Execution of .com fileExecution of .com file To execute a COM file, DOS must do some preparatory work before giving that Program

control. Most importantly, DOS controls and allocates memory usage in the computer. So first

it checks to see if there is enough room in memory to load the program. If it can, DOS then

allocates the memory required for the program. DOS simply records how much space it is making available for such and such a

program, so it won’t try to load another program on top of it later, or give memory space to the

Program that would conflict with another program. Such a step is necessary because more

than one program may reside in memory at any given time. For example, pop-up, memory

resident programs can remain in memory, and parent programs can load child programs into

memory, which execute and then return control to the parent. Next, DOS builds a block of memory 256 bytes long known as the Program Segment

Prefix, or PSP. Once the PSP is built, DOS takes the COM file stored on disk and loads it into memory

just above the PSP, starting at offset 100H. Once this is done, DOS is almost ready to pass

control to the program.

Page 13: writing a virus program

Find size of the program to be loaded

Record all those program sizes whichDoesn't overload other programs.

Create a program control prefix.

Read the com file placed into the disk and Place it just above the PSP.

Com file ready for execution.

Page 14: writing a virus program

Memory map just before executing Memory map just before executing the the .com.com file file

Page 15: writing a virus program

COM files are designed to operate with a very simple, but limited segment structure.namely they have one segment, cs=ds=es=ss. All data is stored in the same segment as the program code itself, and the stack shares this segment. Since any given segment is64 kilobytes long, a COM program can use at most 64 kilobytes for all of its code, data and stack. The EXE file structure is designed to handle space complexity. The drawback with the EXE file is that the program code which is stored on disk must be modified significantly before it can be executed by the CPU. DOS does that at load time, and it iscompletely transparent to the user, but a virus that attaches to EXE files must not upset DOS during this modification process, or it won’t work. A COM program doesn’t require this modification process because it uses only one segment for everything. This makes it possible to store a straight binary image of the code to be executed on disk (the COM file). When it is time to run the program, DOS only needs to set up the segment registers properly and execute it.

Page 16: writing a virus program

Where to effect the host code ?Where to effect the host code ?Once the host program begins to execute, almost anything can

happen,though, and the virus’s job becomes much more difficult. To gaincontrol at startup time, a virus infecting a COM file must replace the

first few bytes in the COM file with a jump to the virus code, which can be appended at the end of the COM file. Then, when the COM file is executed, it jumps to the virus, which goes about looking for more filesto infect, and infecting them. When the virus is ready, it can returncontrol to the host program. The problem in doing this is that the virusalready replaced the first few bytes of the host program with its own code. Thus it must restore those bytes, and then jump back to offset, where the original program begins.

Page 17: writing a virus program
Page 18: writing a virus program

Steps of infection..Steps of infection..1. An infected COM file is loaded into memory and executed. The viral code

gets control first.2. The virus in memory searches the disk to find a suitable COM file to infect.3. If a suitable file is found, the virus appends its own code to the end of the file.4. Next, it reads the first few bytes of the file into memory, and writes them back

out to the file in a special data area within the virus’ code. The new virus will need these bytes when it executes.5. Next the virus in memory writes a jump instruction to the beginning of the file

it is infecting, which will pass control to the new virus when its host program is executed.

6. Then the virus in memory takes the bytes which were originally the first bytes in its host, and puts them back (at offset 100H).

7. Finally, the viral code jumps to offset 100 Hex and allows its host program to execute.

Page 19: writing a virus program

To understand how a virus searches for new files to infect it is Important to

understand how DOS stores files & information about them. All of the information about every file on disk is stored in two areas on disk, known as the directory and the File Allocation Table, or FAT for short. The directory contains a 32 byte file descriptor record for each file. This descriptor record contains the file’s name and extent, its size, date and time of creation, and the file attribute, which contains essential informationfor the operating system about how to handle the file. The FAT is a map

of the entire disk, which simply informs the operating system which areas are occupied by which files. Each disk has two FAT’s, which are identical copies of each other. The second is a backup, in case the first gets corrupted

Page 20: writing a virus program

1.1. The Search routine.The Search routine.A A search routinesearch routine locates new files or new areas on disklocates new files or new areas on diskwhich are worthwhile targets for infection. This routine willwhich are worthwhile targets for infection. This routine willdetermine how well the virus reproduces, e.g.determine how well the virus reproduces, e.g.whether it does so quickly or slowly,whether it does so quickly or slowly,whether it can infect multiple disks or a single disk,whether it can infect multiple disks or a single disk,whether it can infect every portion of a disk or just certainwhether it can infect every portion of a disk or just certainspecific areas.specific areas.As with all programs, there is a size versus functionalityAs with all programs, there is a size versus functionalitytradeoff here. The more sophisticated the search routine is,tradeoff here. The more sophisticated the search routine is,he more space it will take up. So although an efficient searchhe more space it will take up. So although an efficient searchroutine may help a virus to spread faster, it will make the routine may help a virus to spread faster, it will make the virus bigger, and that is not always so good.virus bigger, and that is not always so good.

Page 21: writing a virus program

The dos file structureThe dos file structure

Page 22: writing a virus program
Page 23: writing a virus program

The file search codeThe file search codeFIND_FILE:mov dx,OFFSET COMFILEmov al,00000110Bmov ah,4EH ; perform search firstint 21H

FF_LOOP:or al,al ; any possibilities found?jnz FF_DONE ; no - exit with z resetcall FILE_OK ; yes, go check if we can infect itjz FF_DONE ; yes - exit with z setmov ah,4FH ; no - search for another fileint 21Hjmp FF_LOOP ; go back up and see what happened

FF_DONE:ret ; return to main virus control routine

Page 24: writing a virus program

The additional code for file checkThe additional code for file checkFILE_OK:mov dx,OFFSET FNAME ; first open the filemov ax,3D02H ; r/w access open fileint 21Hjc FOK_NZEND ; error opening file - file can’t be usedmov bx,ax ; put file handle in bxpush bx ; and save it on the stackmov cx,5 ; read 5 bytes at the start of the programmov dx,OFFSET START_IMAGE ;and store them heremov ah,3FH ; DOS read functionint 21Hpop bx ; restore the file handlemov ah,3EHint 21H ; and close the filemov ax,WORD PTR [FSIZE] ; get the file size of the hostadd ax,OFFSET ENDVIRUS - OFFSET VIRUS ;and add size of virus to itjc FOK_NZEND ;c set if ax overflows (size > 64k)cmp BYTE PTR [START_IMAGE],0E9H ;size ok-is first byte a near jmp?jnz FOK_ZEND ; not near jmp, file must be ok, exit with zcmp WORD PTR [START_IMAGE+3],4956H ;ok, is ’VI’ in positions 3 & 4?jnz FOK_ZEND ; no, file can be infected, return with Z setFOK_NZEND:mov al,1 ; we’d better not infect this fileor al,al ; so return with z resetretFOK_ZEND:xor al,al ; ok to infect, return with z setret

Page 25: writing a virus program

2. The Copy routine.2. The Copy routine.Secondly, every computer virus must contain a routine to copy itself into the Secondly, every computer virus must contain a routine to copy itself into the area which the search routine locates. The copy routine will only bearea which the search routine locates. The copy routine will only besophisticated enough to do its job without getting caught. The smaller it is, sophisticated enough to do its job without getting caught. The smaller it is, the better. How small it can be will depend on how complex a virus it must the better. How small it can be will depend on how complex a virus it must copy. For example, a virus which infects only COM files can get by with a copy. For example, a virus which infects only COM files can get by with a much smaller copy routine than a virus which infects EXE files.much smaller copy routine than a virus which infects EXE files.

we want to go to the end of the file and store the virus there. To do so, we first move the file pointer using DOS function 42H. In calling function 42H, the register bx must be set up with the file handle number, and cx:dx must contain a 32 bit long integer telling whereto move the file pointer to. There are three different ways this function can be used, as specified by the contents of the al register. If al=0, the file pointer is set relative to the beginning of the file. If al=1, it is incremented relative to the current location, and if al=2,

cx:dx is used as the offset from the end of the file. Since the first thing the virus must do

is place its code at the end of the COM file it is attacking, it sets the file pointer to the end

of the file. This is easy. Set cx:dx=0, al=2 and call function 42H:

Page 26: writing a virus program

Moving to the end of the file where the virus Moving to the end of the file where the virus appends itself : codesappends itself : codes

xor cx,cx

mov dx,cx

mov bx,WORD PTR [HANDLE]

mov ax,4202H

int 21H

xor cx,cx

mov dx,WORD PTR [FSIZE]

add dx,OFFSET START_CODE - OFFSET VIRUS

mov bx,WORD PTR [HANDLE]

mov ax,4200H

int 21H

Moving the file pointer to the beginning of Moving the file pointer to the beginning of the file : codesthe file : codes

Page 27: writing a virus program

The start image and the start codeThe start image and the start code

Page 28: writing a virus program

Different Types of Computer Different Types of Computer Infection!Infection!

Viruses - A virus is a small piece of software that piggybacks on real Viruses - A virus is a small piece of software that piggybacks on real programs. For example, a virus might attach itself to a program such as a programs. For example, a virus might attach itself to a program such as a spreadsheet program. Each time the spreadsheet program runs, the virus spreadsheet program. Each time the spreadsheet program runs, the virus runs, too, and it has the chance to reproduce (by attaching to other runs, too, and it has the chance to reproduce (by attaching to other programs) or wreak havoc.programs) or wreak havoc.

Worms - A worm is a small piece of software that uses computer networks Worms - A worm is a small piece of software that uses computer networks

and security holes to replicate itself. A copy of the worm scans the network and security holes to replicate itself. A copy of the worm scans the network for another machine that has a specific security hole. It copies itself to the for another machine that has a specific security hole. It copies itself to the new machine using the security hole, and then starts replicating from there, new machine using the security hole, and then starts replicating from there, as well.as well.

Trojan horses - A Trojan horse is simply a computer program. The program Trojan horses - A Trojan horse is simply a computer program. The program claims to do one thing (it may claim to be a game) but instead does damage claims to do one thing (it may claim to be a game) but instead does damage when you run it (it may erase your hard disk). Trojan horses have no way to when you run it (it may erase your hard disk). Trojan horses have no way to replicate automatically.replicate automatically.

Page 29: writing a virus program

3. The Anti-detection routine.3. The Anti-detection routine.It is usually helpful for the virus to avoid detection, either by It is usually helpful for the virus to avoid detection, either by the computer user, or by commercial virus detection software.the computer user, or by commercial virus detection software.Anti-detection routinesAnti-detection routines can either be a part of the search or can either be a part of the search or Copy routines, or functionally separate from them. ForCopy routines, or functionally separate from them. Forexample, the search routine may be severely limited in scopeexample, the search routine may be severely limited in scopeto avoid detection. A routine which checked every file on to avoid detection. A routine which checked every file on every disk drive, without limit, would take a long time andevery disk drive, without limit, would take a long time andcause enough unusual disk activity that an alert user mightcause enough unusual disk activity that an alert user mightbecome suspicious. Alternatively, an anti-Detection routine become suspicious. Alternatively, an anti-Detection routine might cause the virus to activate under certain special might cause the virus to activate under certain special conditions. For example, it might activate only after a certainconditions. For example, it might activate only after a certaindate has passed (so the virus could lie dormant for a time),date has passed (so the virus could lie dormant for a time),or, it might activate only if a key has not been pressed for fiveor, it might activate only if a key has not been pressed for fiveminutes (suggesting that the user was not there watching hisminutes (suggesting that the user was not there watching hiscomputer).computer).

Page 30: writing a virus program

Anti detectionAnti detectionNext comes an important anti-detection step: The master control routine moves the Disk Transfer Area (DTA) to the data area for the virus using DOS function 1A Hex,

mov dx,OFFSET DTA; by default 80H mov ah,1AH int 21H

This move is necessary because the search routine will modify data in the DTA. When a COM file starts up, the DTA is set to a default value of an offset of 80 H in the program segment. The problem is that if the host program requires command line parameters, they are stored for the program at this same location. If the DTA Were not changed temporarily while the virus was executing, the search routine would overwrite any command line parameters before the host program had a chance to access them. That would cause any infected COM program which required a command line parameter to bomb. The virus would execute just fine, and host programs that required no parameters would run fine, but the user could spot trouble with some programs. Temporarily moving the DTA eliminates this problem.

Last of all the program needs to transfer the control back to the host Last of all the program needs to transfer the control back to the host

mov WORD PTR [VIR_START],100H ret

here 100H is assumed to be the location where the host program begins.here 100H is assumed to be the location where the host program begins.

Page 31: writing a virus program

Conclusion Conclusion

Computer viruses are not evil and that programmers

have a right to create them, posses them and

experiment with them. That kind of a stand is going to

offend a lot of people, no matter how it is presented.

Even a purely technical treatment of viruses which

simply discussed how to write them and provided some

examples would be offend some.

Page 32: writing a virus program

References References

The little black book of computer viruses / by Mark A. Ludwig.

IBM Personal Computer AT Technical Reference (IBM Corporation,Racine, WI)

Andrew Schulman, et al., Undocumented DOS, A Programmer’s Guide to Reserved MS-DOS Functions and Data Structures (Addison Wesley, New York)

Ralf Brown and Jim Kyle, PC Interrupts, A Programmer’s Reference to BIOS, DOS and Third-Party Calls (Addison Wesley, New York)