infect.as2 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. jumps
  2. .286
  3. cseg segment byte public 'CODE'
  4. assume cs:cseg, ds:cseg, es:cseg
  5. start:
  6. mov ax, seg cseg
  7. mov es, ax
  8. push ax
  9. mov si, 80h
  10. cmp byte ptr ds:[si], 0
  11. je errusg
  12. xor ax, ax
  13. mov al, ds:[si]
  14. dec ax
  15. mov cx, ax
  16. inc si
  17. inc si
  18. mov di, offset _hname
  19. lp: cmp byte ptr ds:[si], 0dh
  20. je wrk
  21. movsb
  22. loop lp
  23. wrk: pop ds
  24. ;------------- Interceptor -------------
  25. mov ax, 3d00h
  26. mov dx, offset _iname
  27. int 21h
  28. jc IOErr
  29. mov bx, ax
  30. mov ah, 3fh
  31. mov dx, offset buffer
  32. mov cx, 07B00h
  33. int 21h
  34. mov _isize, ax
  35. mov ah, 3eh
  36. int 21h
  37. ;------------- Interceptor -------------
  38. ;------------- Host --------------------
  39. mov ax, 3d02h
  40. mov dx, offset _hname
  41. int 21h
  42. jc IOErr
  43. mov bx, ax
  44. mov handle, ax
  45. mov ah, 3fh
  46. mov dx, offset header
  47. mov cx, 28
  48. int 21h
  49. ;------------- Host --------------------
  50. ;------- Password ----------------------
  51. mov ah, 09h
  52. mov dx, offset enter_pass
  53. int 21h
  54. ; Read password from stdin
  55. ;-------------------------------
  56. lea di, [pass]
  57. mov cx, 20
  58. chrloop:mov ah, 08h
  59. int 21h
  60. cmp al, 0Dh
  61. jz pass_end
  62. stosb
  63. mov ah, 02h
  64. mov dl, '*'
  65. int 21h
  66. dec cx
  67. jnz chrloop
  68. pass_end:
  69. mov ah, 09h
  70. mov dx, offset newline
  71. int 21h
  72. sub cx, 20
  73. neg cx
  74. mov [pass_len], cx
  75. test cx, cx
  76. jz pass_error
  77. ;-----------------------------------
  78. ; Done
  79. lea di, [buffer+21Eh]
  80. mov cx, 9
  81. call encode
  82. ;------------- Header ------------------
  83. mov ax, word ptr header + 22 ; Old rCS1
  84. mov word ptr buffer + 11, ax ; Save
  85. mov ax, word ptr header + 20 ; Old rIP1
  86. mov word ptr buffer + 9, ax ; Save
  87. ;My new entry-point calculation method
  88. ; mov bx, [header+8]
  89. ; mov cx, bx
  90. ; shr bx, 5
  91. ; mov ax, [header+4]
  92. ; dec ax
  93. ; sub ax, bx ; AX - 512b page count
  94. ; and cx, 11111b
  95. ; shl cx, 4
  96. ; mov bx, [header+2]
  97. ; sub bx, cx ; BX - partial page size
  98. ; sbb ax, 0
  99. ; mov [cntH], ax
  100. ; mov [cntL], bx
  101. ; add bx, 10 ; Ten bytes for test_pass
  102. ; adc ax, 0
  103. ; and bx, 111111111b
  104. ; and ax, 11111111111b
  105. ; mov cx, ax
  106. ; shl cx, 5
  107. ; mov dx, bx
  108. ; shr bx, 4
  109. ; add cx, bx ; CX - new rCS2
  110. ; and dx, 0Fh ; DX - new rIP2
  111. ; My second method
  112. ; mov bx, handle
  113. mov ax, 4202h
  114. xor cx, cx
  115. xor dx, dx
  116. int 21h ; In dx-ax we got size
  117. xor bx, bx
  118. mov cx, [header+8]
  119. clc
  120. rcl cx, 1
  121. rcl bx, 1
  122. rcl cx, 1
  123. rcl bx, 1
  124. rcl cx, 1
  125. rcl bx, 1
  126. rcl cx, 1
  127. rcl bx, 1
  128. sub ax, cx
  129. sbb dx, bx
  130. mov [cntH], dx
  131. mov [cntL], ax
  132. and dx, 1111b
  133. shl dx, 12
  134. mov cx, ax
  135. shr cx, 4
  136. or dx, cx
  137. and ax, 1111b
  138. mov [buffer+15], dx ; Code segment
  139. mov [header+22], dx
  140. mov [header+14], dx
  141. mov [buffer+13], ax ; Code offset
  142. mov [header+20], ax
  143. xor ax, ax
  144. not ax
  145. mov [header+16], ax
  146. mov dx, [header+6] ; Relocation table item count
  147. mov [buffer+17], dx
  148. mov cx, dx
  149. shr cx, 7
  150. and dx, 1111111b
  151. shl dx, 2
  152. mov ax, [header+2]
  153. add ax, _isize
  154. add ax, dx
  155. mov bx, ax
  156. shr bx, 9
  157. add bx, cx
  158. add [header+4], bx
  159. and ax, 1FFh
  160. mov [header+2], ax
  161. ;------------- Header ------------------
  162. ;------------- Host --------------------
  163. mov bx, handle
  164. mov ax, 4202h
  165. xor cx, cx
  166. xor dx, dx
  167. int 21h
  168. mov ah, 40h
  169. mov cx, _isize
  170. mov dx, offset buffer
  171. int 21h
  172. mov ah, 3eh
  173. int 21h
  174. mov ax, 3d02h
  175. mov dx, offset _hname
  176. int 21h
  177. mov bx, ax
  178. mov handle, ax
  179. ;------------- Host --------------------
  180. ;--- Add to host additional relocation table ---
  181. mov word ptr [lastOffsH], 0
  182. mov ax, [header+18h]
  183. mov [lastOffsL], ax
  184. big_loop:
  185. cmp [header+6], 100
  186. jl small_loop
  187. sub [header+6], 100
  188. mov ax, 4200h
  189. mov cx, [lastOffsH]
  190. mov dx, [lastOffsL]
  191. int 21h
  192. mov ah, 3fh
  193. mov dx, offset reloBuf
  194. mov cx, 400
  195. int 21h
  196. mov cx, 399
  197. mov di, offset reloBuf
  198. call addmagic
  199. mov di, offset reloBuf
  200. call encode
  201. mov ax, 4202h
  202. xor cx, cx
  203. xor dx, dx
  204. int 21h
  205. mov ah, 40h
  206. mov cx, 400
  207. mov dx, offset reloBuf
  208. int 21h
  209. mov ax, 4200h
  210. mov cx, [lastOffsH]
  211. mov dx, [lastOffsL]
  212. int 21h
  213. mov ah, 40h
  214. mov cx, 400
  215. mov dx, offset zeroBuf
  216. int 21h
  217. add [lastOffsL], 400
  218. adc [lastOffsH], 0
  219. jmp big_loop
  220. small_loop:
  221. mov ax, 4200h
  222. mov cx, [lastOffsH]
  223. mov dx, [lastOffsL]
  224. int 21h
  225. mov ah, 3fh
  226. mov dx, offset reloBuf
  227. mov cx, [header+6]
  228. sub [header+6], cx
  229. shl cx, 2
  230. push cx
  231. int 21h
  232. dec cx
  233. mov di, offset reloBuf
  234. call addmagic
  235. mov di, offset reloBuf
  236. call encode
  237. mov ax, 4202h
  238. xor cx, cx
  239. xor dx, dx
  240. int 21h
  241. mov ah, 40h
  242. pop cx
  243. push cx
  244. mov dx, offset reloBuf
  245. int 21h
  246. mov ax, 4200h
  247. mov cx, [lastOffsH]
  248. mov dx, [lastOffsL]
  249. int 21h
  250. mov ah, 40h
  251. pop cx
  252. mov dx, offset zeroBuf
  253. int 21h
  254. ; Write EXE header
  255. mov ax, 4200h
  256. mov bx, handle
  257. xor cx, cx
  258. xor dx, dx
  259. int 21h
  260. mov ah, 40h
  261. mov cx, 28
  262. mov dx, offset header
  263. int 21h
  264. ; Calculate count of encode bytes
  265. mov dx, [cntL]
  266. ; mov cx, ax
  267. ; shr cx, 7
  268. ; and ax, 1111111b
  269. ; shl ax, 9
  270. mov cx, [cntH]
  271. ; add dx, ax
  272. ; adc cx, 0
  273. assume cs:cseg, ds:dseg, es:dseg
  274. mov ax, seg dseg
  275. mov es, ax
  276. mov ds, ax
  277. mov bx, cs:[header+8]
  278. mov ax, bx
  279. shr ax, 12
  280. mov cs:[lastOffsH], ax
  281. shl bx, 4
  282. mov cs:[lastOffsL], bx
  283. mov bx, [handle]
  284. push dx
  285. seg_loop:
  286. test cx, cx
  287. jz no_seg
  288. ; jnz begins
  289. ; cmp dx, 20000
  290. ; jb no_seg
  291. begins: push cx
  292. ; push dx
  293. mov ax, 4200h
  294. mov cx, cs:[lastOffsH]
  295. mov dx, cs:[lastOffsL]
  296. int 21h
  297. mov ah, 3Fh ; Read segment of
  298. mov cx, 0ffffh ; input file
  299. mov dx, 0 ;
  300. int 21h ;
  301. mov ah, 3Fh ; Read segment of
  302. mov cx, 1h ; input file
  303. mov dx, 0ffffh ;
  304. int 21h ;
  305. xor di, di
  306. xor cx, cx
  307. not cx
  308. call addmagic
  309. call encode
  310. mov ax, 4200h
  311. mov cx, cs:[lastOffsH]
  312. mov dx, cs:[lastOffsL]
  313. int 21h
  314. mov ah, 40h ; Write segment to
  315. mov cx, 0FFFFh ; the input file
  316. mov dx, 0 ;
  317. int 21h ;
  318. mov ah, 40h ; Write segment to
  319. mov dx, 0FFFFh ; the input file
  320. mov cx, 1 ;
  321. int 21h ;
  322. ; add cs:[lastOffsL], 20000
  323. ; adc cs:[lastOffsH], 0
  324. inc cs:[lastOffsH]
  325. ; pop dx
  326. pop cx
  327. ; sub dx, 20000
  328. ; sbb cx, 0
  329. dec cx
  330. jmp seg_loop
  331. no_seg:
  332. mov ax, 4200h
  333. mov cx, cs:[lastOffsH]
  334. mov dx, cs:[lastOffsL]
  335. int 21h
  336. mov ah, 3Fh ; Read data of
  337. pop cx ; input file
  338. push cx
  339. mov dx, 0 ;
  340. int 21h ;
  341. xor di, di
  342. pop cx
  343. push cx
  344. dec cx
  345. call addmagic
  346. xor di, di
  347. call encode
  348. mov ax, 4200h
  349. mov cx, cs:[lastOffsH]
  350. mov dx, cs:[lastOffsL]
  351. int 21h
  352. mov ah, 40h ; Write segment to
  353. pop cx
  354. mov dx, 0 ;
  355. int 21h ;
  356. pop dx
  357. ; Close host file
  358. mov ah, 3eh
  359. int 21h
  360. ;- Done
  361. ;--------------------------------------------------
  362. mov ah, 4ch
  363. int 21h
  364. ;--------------------------------------------------
  365. ;==================================================
  366. ;--------------------------------------------------
  367. pass_error:
  368. mov dx, offset bad_pass
  369. mov ah, 09h
  370. int 21h
  371. mov ax, 4cFFh
  372. int 21h
  373. errusg: pop ds
  374. mov ah, 09h
  375. mov dx, offset errstr
  376. int 21h
  377. mov ax, 4cFEh
  378. int 21h
  379. IOErr: mov ah, 09h
  380. mov dx, offset errstr
  381. int 21h
  382. mov ax, 4cFDh
  383. int 21h
  384. ;-----------------------------------------------
  385. encode proc near
  386. push si
  387. push ax
  388. push cx
  389. loops2: mov si, offset pass
  390. cmp cx, cs:[pass_len]
  391. jb lasts2
  392. push cx
  393. mov cx, cs:[pass_len]
  394. addmag2: mov al, cs:[si]
  395. xor es:[di], al
  396. inc si
  397. inc di
  398. loop addmag2
  399. pop cx
  400. sub cx, cs:[pass_len]
  401. jmp loops2
  402. lasts2: inc cx
  403. lasts3: mov al, cs:[si]
  404. xor es:[di], al
  405. inc si
  406. inc di
  407. loop lasts3
  408. pop cx
  409. pop ax
  410. pop si
  411. ret
  412. endp
  413. addmagic proc near
  414. push si
  415. push ax
  416. push cx
  417. loops: mov si, offset test_pass
  418. cmp cx, 10
  419. jb lasts
  420. push cx
  421. mov cx, 10
  422. addmag: mov al, cs:[si]
  423. add es:[di], al
  424. inc si
  425. inc di
  426. loop addmag
  427. pop cx
  428. sub cx, 10
  429. jmp loops
  430. lasts: inc cx
  431. lasts0: mov al, cs:[si]
  432. add es:[di], al
  433. inc si
  434. inc di
  435. loop lasts0
  436. pop cx
  437. pop ax
  438. pop si
  439. ret
  440. endp
  441. test_pass db 10, 12, 15, 17, -20, -13, 05, -30, 55, -51
  442. cntH dw 0
  443. cntL dw 0
  444. io_err_str db 'Error in file io', 0dh, 0ah, '$'
  445. enter_pass db 'Please enter pass: $'
  446. errstr db 'usage: "crypt.exe source"', 0dh, 0ah,'$'
  447. _hname db 13 dup (0)
  448. _iname db 'interc.com',0
  449. bad_pass db 'Bad password',0dh,0ah,'$'
  450. newline db 0dh,0ah,'$'
  451. zeroBuf db 400 dup (0)
  452. pass db 20 dup (0)
  453. pass_len dw ?
  454. handle dw ?
  455. header dw 14 dup (?)
  456. _isize dw ?
  457. lastOffsH dw ?
  458. lastOffsL dw ?
  459. rCS2 dw ?
  460. rIP2 dw ?
  461. reloBuf dw 200 dup (?)
  462. buffer dw 07B00h dup (?)
  463. ends
  464. dseg segment para public 'DATA'
  465. file_buf db 0FFFFh dup (?)
  466. ends
  467. sseg segment stack
  468. db 200 dup (?)
  469. ends
  470. end start