decoder.as2 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. .286
  2. decoder segment
  3. assume cs:decoder, ds:decoder, es:decoder
  4. jumps
  5. cMAX_FILE equ 10
  6. cNAME_LEN equ 12
  7. cPASS_LEN equ 20
  8. cBUF_SIZE equ 300
  9. start:
  10. push ds ;save PSP address
  11. ; Check command line
  12. cmp byte ptr ds:[80h], 0
  13. je install
  14. cmp word ptr ds:[82h], 'u/'
  15. je remove
  16. install:
  17. push cs cs
  18. pop ds es
  19. mov ax,0DEADh ;our decoder residency check
  20. int 21h ;
  21. cmp bx,0CAFEh ;>CAFE :o)
  22. je already_resident ;its already resident
  23. ;----------------
  24. ; Read files&passwords
  25. mov ax, 3d00h
  26. mov dx, offset fname
  27. int 21h
  28. jc exit_prog
  29. xchg bx, ax
  30. mov ah, 3fh
  31. mov cx, cMAX_FILE*cNAME_LEN
  32. mov dx, offset files
  33. int 21h
  34. mov ah, 3eh
  35. int 21h
  36. mov ax, 3d00h
  37. mov dx, offset pname
  38. int 21h
  39. jc exit_prog
  40. xchg bx, ax
  41. mov ah, 3fh
  42. mov cx, cMAX_FILE*cPASS_LEN
  43. mov dx, offset passwords
  44. int 21h
  45. mov ah, 3eh
  46. int 21h
  47. pop ds
  48. push ds ;PSP address back into DS
  49. ;--------------------------------------------------
  50. mov ax,ds ;MCB residency
  51. dec ax
  52. mov ds,ax
  53. sub word ptr ds:[3],80h ; Decrease the size of Memory Block by 2Kb
  54. sub word ptr ds:[12h],80h ; Same, but in PSP
  55. xor ax,ax
  56. mov ds,ax
  57. mov ax, 2
  58. sub word ptr ds:[413h], ax ; Decrease BIOS data's free space by 2Kb
  59. mov ax,word ptr ds:[413h] ; Calculate page number of free 2Kb of space
  60. shl ax,6
  61. mov es,ax
  62. push cs
  63. pop ds
  64. lea si, [RES_BEG] ; Copy our prog there
  65. xor di,di
  66. mov cx, the_end - RES_BEG ; Size of interrupt handlers
  67. rep movsb
  68. ;--------------------------------------------------
  69. xor ax,ax ;Setting of interrupts
  70. mov ds,ax
  71. mov ax,es
  72. mov bx,0 ; Offset to interrupt in new address
  73. cli
  74. xchg bx,word ptr ds:[21h*4]
  75. xchg ax,word ptr ds:[21h*4+2]
  76. mov word ptr es:[old_int21h-RES_BEG],bx
  77. mov word ptr es:[old_int21h+2-RES_BEG],ax
  78. sti
  79. ;--------------------------------------------------
  80. push cs cs
  81. pop ds es
  82. mov ah,9 ; Installed OK message
  83. lea dx,[message]
  84. int 21h
  85. exit_prog:
  86. pop ds ;Restore PSP in DS
  87. mov ax,4c00h ;Request terminate program
  88. int 21h
  89. ;-----------------------------------------------------------------
  90. ; Resident begins here
  91. RES_BEG:
  92. new_int21h: ;our int 21h handler
  93. pushf ;push the flags
  94. cmp ax,0DEADh ;residency check
  95. jne no_install_check
  96. mov bx,0CAFEh ;already resident
  97. popf ;restore all flags
  98. iret ;return
  99. no_install_check:
  100. cmp ax, 0DEDDh
  101. je uninstall
  102. cmp ah, 4bh ;check if execute
  103. je infect
  104. cmp ah, 4ch ;check if terminate
  105. je terminate
  106. return:
  107. popf ;restore all flags
  108. db 0eah ;jmp to orig int 21h
  109. old_int21h dd ?
  110. ;--------------------------------
  111. uninstall:
  112. pusha ;only 286, saves all gen reg
  113. push ds
  114. push es
  115. push cs
  116. pop es
  117. xor ax,ax
  118. mov ds,ax
  119. inc word ptr ds:[413h] ;Increase BIOS data's free space by 1Kb
  120. cli
  121. xchg bx,word ptr es:[old_int21h-RES_BEG]
  122. xchg ax,word ptr es:[old_int21h+2-RES_BEG]
  123. mov word ptr ds:[21h*4],bx
  124. mov word ptr ds:[21h*4+2],ax
  125. sti
  126. pop es
  127. pop ds
  128. popa
  129. popf ;restore all flags
  130. iret ;return
  131. ;-------------------------------
  132. infect:
  133. pusha ;only 286, saves all gen reg
  134. push ds
  135. push es
  136. call tsr_delta
  137. tsr_delta:
  138. pop bp ;a tsr delta offset %-)
  139. sub bp, offset tsr_delta
  140. push ds
  141. push dx
  142. ; Test, if currently opening file is in database
  143. mov di, dx
  144. call SeekZ
  145. push di
  146. std
  147. mov al, '\'
  148. repnz scasb
  149. cld
  150. jnz $+3
  151. inc di
  152. inc di
  153. pop cx
  154. sub cx, di
  155. push cx
  156. push di
  157. mov dx, 12
  158. push cs
  159. pop es
  160. lea di, es:[bp+files]
  161. searchloop:
  162. pop si
  163. pop cx
  164. push cx
  165. push si
  166. push di
  167. mov al, es:[di]
  168. jz not_found
  169. repz cmpsb
  170. jz found
  171. pop di
  172. add di, dx
  173. jmp searchloop
  174. found:
  175. pop di
  176. pop si
  177. pop cx
  178. ; Calculate offset to password
  179. ; Get number of file in table
  180. lea si, es:[bp+files]
  181. sub di, si
  182. mov ax, di
  183. mov bx, cNAME_LEN
  184. xor dx, dx
  185. div bx
  186. mov bx, cPASS_LEN
  187. mul bx
  188. lea di, es:[bp+passwords]
  189. add di, bx
  190. mov es:[bp+pass], di
  191. push cs
  192. pop ds
  193. mov cx, cPASS_LEN
  194. mov al, ' '
  195. repnz scasb
  196. jnz $+3
  197. inc cx
  198. sub cx, cPASS_LEN
  199. neg cx
  200. mov es:[bp+pass_len], cx
  201. pop dx
  202. pop ds
  203. jmp decode_file
  204. not_found:
  205. pop di
  206. pop si
  207. pop cx
  208. pop dx
  209. pop ds
  210. jmp exit
  211. ;------------------------------------
  212. decode_file:
  213. mov ax,3d02h ;open file in DS:DX
  214. int 21h
  215. jc exit
  216. xchg ax,bx ;file handle to bx
  217. mov cs:[bp+handle], ax
  218. push cs cs
  219. pop ds es
  220. mov ah,3fh ;Read the target header
  221. lea dx,[bp+header] ;into our buffer
  222. mov cx,1ch
  223. int 21h
  224. mov ax, word ptr [bp+header+12h] ; Test if program is our coded prog
  225. cmp ax, 'CD'
  226. jnz close
  227. ;-------------------------
  228. ; Decode all the file
  229. ;-------------------------
  230. ; Calculate count of encode bytes
  231. mov ax, 4202h
  232. xor cx, cx
  233. xor dx, dx
  234. int 21h
  235. mov cx, dx ; Hi part
  236. mov dx, ax ; Lo part
  237. mov bx, word ptr [bp+header+8]
  238. mov ax, bx
  239. shr ax, 12
  240. push ax
  241. shl bx, 4
  242. sub cx, ax
  243. sub dx, bx
  244. push dx
  245. mov cx, ax
  246. mov bx, [bp+handle]
  247. mov ax, 4200h
  248. int 21h
  249. pop dx
  250. pop cx
  251. buf_loop:
  252. push cx
  253. push dx
  254. sub dx, cBUF_SIZE
  255. sbb cx, 0
  256. jle last_buf
  257. begins:
  258. mov ah, 3Fh ; Read full buffer of
  259. mov cx, cBUF_SIZE ; EXECing file
  260. lea dx, [bp+buffer]
  261. int 21h
  262. mov cx, ax
  263. dec cx
  264. lea di, [bp+buffer]
  265. call decode
  266. lea di, [bp+buffer]
  267. call submagic
  268. mov ax, 4201h
  269. mov dx, cBUF_SIZE
  270. neg dx
  271. xor cx, cx
  272. int 21h
  273. mov ah, 40h ; Write segment to
  274. mov cx, cBUF_SIZE ; the input file
  275. lea dx, [bp+buffer]
  276. int 21h ;
  277. pop dx
  278. pop cx
  279. sub dx, cBUF_SIZE
  280. sbb cx, 0
  281. jmp buf_loop
  282. last_buf:
  283. pop cx
  284. pop dx
  285. mov ah, 3Fh ; Read data of
  286. push cx
  287. lea dx, [bp+buffer]
  288. int 21h ;
  289. lea di, [bp+buffer]
  290. pop cx
  291. push cx
  292. dec cx
  293. lea di, [bp+buffer]
  294. call decode
  295. lea di, [bp+buffer]
  296. call submagic
  297. mov ax, 4201h
  298. pop dx
  299. push dx
  300. neg dx
  301. xor cx, cx
  302. int 21h
  303. mov ah, 40h ; Write segment to
  304. pop cx
  305. lea dx, [bp+buffer]
  306. int 21h ;
  307. ;- Done
  308. close:
  309. mov ah,3eh ;close file
  310. int 21h
  311. exit:
  312. pop es
  313. pop ds
  314. popa
  315. jmp return
  316. terminate:
  317. jmp return
  318. pass_len dw 0
  319. pass dw 0
  320. header db 1ch dup(?) ;Buffer for header
  321. handle dw 0
  322. passwords db (cPASS_LEN*cMAX_FILE) dup (0)
  323. db 0
  324. files db (cNAME_LEN*cMAX_FILE) dup (0)
  325. db ' '
  326. buffer db cBUF_SIZE dup (?)
  327. test_pass db 10, 12, 15, 17, -20, -13, 05, -30, 55, -51
  328. ;-----------------------
  329. SeekZ proc near
  330. mov al, 0
  331. mov cx, 0ffffh
  332. repnz scasb
  333. neg cx
  334. dec cx
  335. dec di
  336. ret
  337. endp
  338. ;------------------
  339. ; es:di - what to decode
  340. ; cx - length
  341. decode proc near
  342. push si
  343. push ax
  344. push cx
  345. push dx
  346. mov dx, ds:[bp+pass_len]
  347. loops2: mov si, ds:[bp+pass]
  348. cmp cx, dx
  349. jb lasts2
  350. push cx
  351. mov cx, dx
  352. addmag2: mov al, ds:[si]
  353. xor es:[di], al
  354. inc si
  355. inc di
  356. loop addmag2
  357. pop cx
  358. sub cx, dx
  359. jmp loops2
  360. lasts2: inc cx
  361. lasts3: mov al, ds:[si]
  362. xor es:[di], al
  363. inc si
  364. inc di
  365. loop lasts3
  366. pop dx
  367. pop cx
  368. pop ax
  369. pop si
  370. ret
  371. endp
  372. submagic proc near
  373. push si
  374. push ax
  375. push cx
  376. loops: lea si, ds:[bp+test_pass]
  377. cmp cx, 10
  378. jb lasts
  379. push cx
  380. mov cx, 10
  381. addmag: mov al, ds:[si]
  382. sub es:[di], al
  383. inc si
  384. inc di
  385. loop addmag
  386. pop cx
  387. sub cx, 10
  388. jmp loops
  389. lasts: inc cx
  390. lasts0: mov al, ds:[si]
  391. sub es:[di], al
  392. inc si
  393. inc di
  394. loop lasts0
  395. pop cx
  396. pop ax
  397. pop si
  398. ret
  399. endp
  400. ;------------------
  401. the_end:
  402. message db 10,13,10,13
  403. db '- Decoder of encrypted files successfully installed -',10,13
  404. db ' - Copyright Enikeew I.R. 2084/1 -',10,13,'$'
  405. msg_already db 10, 13
  406. db '- Decoder already installed - ', 10, 13
  407. db ' - Copyright Enikeew I.R. 2084/1 -',10,13,'$'
  408. msg_not db 10, 13
  409. db '- Decoder isn''t installed !- ', 10, 13
  410. db ' - Copyright Enikeew I.R. 2084/1 -',10,13,'$'
  411. msg_un db 10,13,10,13
  412. db '- Decoder were successfully removed -',10,13
  413. db ' - Copyright Enikeew I.R. 2084/1 -',10,13,'$'
  414. fname db 'files.txt',0
  415. pname db 'pass.txt', 0
  416. already_resident:
  417. mov ah,9 ; Installed OK message
  418. lea dx,[msg_already]
  419. int 21h
  420. jmp exit_prog
  421. not_installed:
  422. mov ah,9 ; Installed OK message
  423. lea dx,[msg_not]
  424. int 21h
  425. jmp exit_prog
  426. remove:
  427. push cs cs
  428. pop ds es
  429. mov ax,0DEADh ;our decoder residency check
  430. int 21h ;
  431. cmp bx,0CAFEh ;>CAFE :o)
  432. jne not_installed ;it isn't resident
  433. mov ax, 0DEDDh
  434. int 21h
  435. mov ah,9 ; Installed OK message
  436. lea dx,[msg_un]
  437. int 21h
  438. jmp exit_prog
  439. decoder ends
  440. end start