Little SYS Infection Tutorial
by Int13h


Well, here we will speak a bit about the infection of devices drivers, they are really very easy to infect. When finishing this reading you will be able to add infection of device drivers to your virus, then it will be more c00l :) A .SYS phile, that is, a device driver, is a bridge of communication between the software and hardware devices. The SYS philes are read from the CONFIG.SYS, they are loaded in an own segment without PSP and are originated at offset 0. They can be block or character devices. The structure of a device driver follows:

               HEADER
               STRATEGY ROUTINE
               INTERRUPT ROUTINE

The first part of a .SYS program is the header, which structure is:

Offset  Length    Description
------  ------    -----------
 0       dd       32 bit pointer to next device driver header
 4       dw       Attribute
 6       dw       Pointer to strategy routine (offset)
 8       dw       Pointer to interrupt routine (offset)
 A       8 bytes  Name of the device

The first field is a 32 bit pointer to the device header of the next device, this is because in a .SYS file can be concatenated various devices, and the last in the chain has this relative offset of his header filled with the value FFFF:FFFF. The attribute word is used to identify if the driver is a character device or a block one. The next field, has the offset in the file where the strategy routine is, a strategy routine must save the location of the request header that DOS gives to it in ES:BX. The next field must hold the offset of the interrupt routine handler, which is the really working horse of the device driver, it must interpret the request header and perform the commands that it read from there. The last one, is a 8 bytes field that have the name of a character device or the number of units of a block device. DOS loads device drivers dynamically at boot time, reading them from the CONFIG.SYS, then a virus can take control before some antiviruses. You could read more about SYS structure in the chapter 9 of the Programmer's Technical Reference for MSDOS and the IBM PC, a very nice shareware document in TXT format.

Just as an example, we will code a simple SYS file which displays a message. Here it is:

Well. As you can see it is simple enough. Now, we will speak about the infection matter. A virus can add itself to the end of file and point the Next_Device field (offset 0) of the header to itself, then it will be acting like a normal program, this is the way that Dark Angel of Phalcom/Skim has choosed, and you can read about it in the number 9 of 40Hex magazine. For our infection purposes, we will hook the strategy routine. This is the routine that grabs the pointer to the request header (ES:BX) then you must be careful not changing these registers, specially with the garbage if you are making a polymorphic virus.

Let's see:

In others words, we will follow the next algorithm:

  1. Open the SYS file.
  2. Read the header (10 bytes).
  3. Save in a buffer the original pointer to strategy routine.
  4. Move pointer to end of file (save AX value).
  5. Write virus there.
  6. Modify header, changing the pointer to the strategy routine to the value in AX (file size), then it will point to virus.
  7. Move the pointer to the beginning.
  8. Write the modified header.
  9. Close the sucker.

And that is all. For some comments I can be reached at Int13h@antisocial.com. Here, as example, follows a runtime appending SYS infector which works by the method of reapoiting the strategy routine. Have fun!

INT13H Paraguay, February 13, 1998