home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / diskutil / wprot2.zoo / WPROT2.ASM next >
Encoding:
Assembly Source File  |  1990-08-09  |  11.7 KB  |  274 lines

  1. title WRITE PROTECT V2
  2. ; wprot2 - Protect Hard disk against write attempts
  3. ;
  4. ; Assembly: TASM wprot2;
  5. ;           TLINK /t wprot2;
  6. ;
  7. ; Use:      Just type 'wprot2'.
  8. ;
  9. ; Remarks:  Wprot2 first started as a program to protect hard disks software
  10. ;           installation against some very abusive users at the FCT/UNL
  11. ;       ( Faculdade de Ciências e Tecnologia da Universidade Nova de Lisboa )
  12. ;
  13. ; Author:   José Pedro T. Pina Coelho
  14. ;
  15. ; Disclaimer: I do not warrant that wprot2 operates as designed and I
  16. ;           will not be liable for any damages caused by the use of
  17. ;           this program, by using  this  program  you accept these
  18. ;           terms.
  19. ;
  20. ; Comments & Sugestions:  They are welcome, please send them to:
  21. ;               jpc@fctunl.rccn.pt
  22. ;       or
  23. ;               José Pedro T. Pina Coelho
  24. ;               Rua Jau Nº1 2º Dto.
  25. ;               1300 Lisboa
  26. ;               Portugal
  27. ;
  28. ; Money: Any amount, any colour, any flavour.  Send/Teleport it to the
  29. ;       comments address :  (to me is a comment on this software quality)
  30. ;
  31.  
  32. CR              EQU     13
  33. LF              EQU     10
  34.  
  35. .model  tiny
  36. .code
  37. org     0100h
  38.  
  39. start:          jmp     init
  40.  
  41. old13           dd      (?)             ; Keep this one to chain requests
  42.  
  43. new13:
  44.                 cmp     ah, 03h         ; Write Disk
  45.                 je      write_disk
  46.                 cmp     ah, 05h         ; Format Cylinder
  47.                 je      format_disk
  48.                 cmp     ah, 06h         ; Format Cylinder Set Bad Sector Flags
  49.                 je      format_disk
  50.                 cmp     ah, 07h         ; Format Drive Starting at Cylinder
  51.                 je      format_disk
  52.                 cmp     ah, 17h         ; Set DASD Type for Format
  53.                 je      format_disk
  54.                 cmp     ah, 18h         ; Set Media Type for Format
  55.                 je      format_disk
  56.                 cmp     ah, 1Ah         ; Format Unit
  57.                 je      format_disk
  58.         ; I just consider all other calls to be harmless
  59.  
  60. harmless:       jmp     dword ptr cs:[ old13 ]
  61. wayout:         iret
  62.  
  63. ;
  64. ; Here you can do several things:
  65. ;       REBOOT
  66. ;               - Don't give them a chance
  67. ;       Return with status = OK ( 0 )
  68. ;               - Make them think the operation succeeded
  69. ;       Return with status = Attempted write on protected diskette ( 3 )
  70. ;               - State it's forbidden
  71. ;       Return with any other status (1,2,4..17,32,64,128,170,187,204,224,255)
  72. ;               - If you can't convince them, confuse them
  73. ;
  74. write_disk:     test    dl, 80h         ; Bit 7 is 1 for hard drives
  75.                 jz      harmless        ; Was to diskette
  76.                 call    test_indos      ; Test if both indos and allowed part.
  77.                 jnc     harmless        ; It's allowed
  78.                 jmp     wprotected      ; Slam the door
  79. format_disk:    test    dl, 80h         ; Bit 7 is 1 for hard drives
  80.                 jz      harmless        ; Was to diskette
  81. wprotected:     mov     ah, 3           ; STATUS = Write Protected
  82.                 push    bp              ;
  83.                 mov     bp, sp          ;
  84.                 or      word ptr [bp+6], 0001h  ; Set carry flag in call stack
  85.                 pop     bp              ;
  86.                 jmp     wayout          ; this one shoud bother you no more
  87.  
  88.  
  89. partition:      db      66 dup (?)      ; Partition table
  90.  
  91. ; RETURN: CARRY = FORBIDDEN ( on this partition )
  92. test_partAX     proc    near            ; Test partition AX (0..3)
  93.                 push    cx
  94.                 mov     cl, 4           ; The size of a partition descriptor is
  95.                 shl     ax, cl          ; 16 (2^4) bytes
  96.                 pop     cx
  97.                 add     ax, offset partition
  98.                 mov     si, ax
  99.  
  100.                 mov     al, byte ptr [si+2]     ; AL = C9 C8 S5 S4 S3 S2 S1 S0
  101.                 and     al, 0C0h                ; AL = C9 C8 0 0 0 0 0 0
  102.                 mov     ah, cl                  ; AH = c9 c8 s5 s4 s3 s2 s1 s0
  103.                 and     ah, 0C0h                ; AH = c9 c8 0 0 0 0 0 0
  104.                 cmp     ah, al
  105.                 jb      forbidd                 ; Was a previous cyl
  106.                 ja      test_end                ; In this case we don't need
  107.                                                 ; to check others
  108.                 cmp     ch, byte ptr [si+3]     ; CH = c7 c6 c5 c4 c3 c2 c1 c0
  109.                 jb      forbidd                 ; Was a previous cyl
  110.                 ja      test_end                ; In this case we don't need
  111.                                                 ; to check others
  112.                 mov     al, byte ptr [si+2]     ; AL = C9 C8 S5 S4 S3 S2 S1 S0
  113.                 and     al, 03Fh                ; AL = 0 0 S5 S4 S3 S2 S1 S0
  114.                 mov     ah, cl                  ; AH = c9 c8 s5 s4 s3 s2 s1 s0
  115.                 and     ah, 03Fh                ; AH = 0 0 s5 s4 s3 s2 s1 s0
  116.                 cmp     ah, al
  117.                 jb      forbidd                 ; Was a previous sect
  118.                 ja      test_end                ; In this case we don't need
  119.                                                 ; to check others
  120.                 cmp     dh, byte ptr [si+1]     ; Compare head number
  121.                 jb      forbidd
  122.         ; This part (test_end) is a quick & dirty aproach, it only checks to see
  123.         ; if the write is starting off-limits, not if it writes off-limits
  124.         ; This will be corrected in version 3.0
  125. test_end:
  126.                 mov     al, byte ptr [si+6]     ; AL = C9 C8 S5 S4 S3 S2 S1 S0
  127.                 and     al, 0C0h                ; AL = C9 C8 0 0 0 0 0 0
  128.                 mov     ah, cl                  ; AH = c9 c8 s5 s4 s3 s2 s1 s0
  129.                 and     ah, 0C0h                ; AH = c9 c8 0 0 0 0 0 0
  130.                 cmp     ah, al
  131.                 ja      forbidd                 ; Was a later cyl
  132.                 jb      test_type               ; In this case we don't need
  133.                                                 ; to check others
  134.                 cmp     ch, byte ptr [si+7]     ; CH = c7 c6 c5 c4 c3 c2 c1 c0
  135.                 ja      forbidd                 ; Was a later cyl
  136.                 jb      test_type               ; In this case we don't need
  137.                                                 ; to check others
  138.                 mov     al, byte ptr [si+6]     ; AL = C9 C8 S5 S4 S3 S2 S1 S0
  139.                 and     al, 03Fh                ; AL = 0 0 S5 S4 S3 S2 S1 S0
  140.                 mov     ah, cl                  ; AH = c9 c8 s5 s4 s3 s2 s1 s0
  141.                 and     ah, 03Fh                ; AH = 0 0 s5 s4 s3 s2 s1 s0
  142.                 cmp     ah, al
  143.                 ja      forbidd                 ; Was a later sect
  144.                 jb      test_type               ; In this case we don't need
  145.                                                 ; to check others
  146.                 cmp     dh, byte ptr [si+5]     ; Compare head number
  147.                 ja      forbidd                 ; Is over the top
  148.  
  149. test_type:      mov     al, byte ptr [si+4]
  150.                 cmp     al, 00h         ; Unused
  151.                 je      forbidd
  152.                 cmp     al, 01h         ; DOS Primary 12-Bit Fat
  153.                 je      forbidd         ; I consider it to be off-limits
  154.                 cmp     al, 04h         ; DOS Primary 16-Bit Fat
  155.                 je      forbidd         ; I consider it to be off-limits
  156.                 cmp     al, 05h         ; DOS Extended
  157.                 je      test_ok         ; The only thing they can write
  158.                 cmp     al, 0DBh        ; Concurrent DOS
  159.                 je      forbidd         ; I don't know if data is compatible
  160.                                         ; If anybody knows please email.
  161.         ; Other types can be added, some weird DOS versions use weird ID's on
  162.         ; their partition tables, if this program locks your Extended partition
  163.         ; then you probably have one of those versions, use BIOS read to find
  164.         ; out the types your computer uses.
  165.                 ; ALL Other types are forbidden by default
  166.                 jmp     forbidd
  167.  
  168. test_ok:        xor ax, ax              ; Clear carry to indicate allowed
  169.                 ret
  170. forbidd:        stc                     ; Set carry to indicate forbidden
  171.                 ret
  172. test_partAX     endp
  173.  
  174. ; RETURN: CARRY = FORBIDDEN ( on all partitions )
  175. test_indos      proc    near
  176.                 push    ax
  177.                 push    ds
  178.                 mov     ax, cs
  179.                 mov     ds, ax
  180.  
  181.                 mov     al, 0
  182.                 call    test_partAX
  183.                 jnc     testall_ok      ; Is allowed
  184.                 mov     al, 1
  185.                 call    test_partAX
  186.                 jnc     testall_ok      ; Is allowed
  187.                 mov     al, 2
  188.                 call    test_partAX
  189.                 jnc     testall_ok      ; Is allowed
  190.                 mov     al, 3
  191.                 call    test_partAX
  192.  
  193. testall_ok:     pop     ds
  194.                 pop     ax
  195.                 ret
  196. test_indos      endp
  197.  
  198. init:           mov     ax,cs
  199.                 mov     es,ax           ; Initialize ES
  200.                 mov     ds,ax           ; Initialize DS
  201.                 mov     ss,ax           ; Initialize SS
  202.                 mov     sp,offset stke
  203.  
  204.                 mov     ah, 02h         ; Read Disk
  205.                 mov     al, 01h         ; # Sectors to read
  206.                 mov     bx, offset bootsect ;  offset of pointer to read buffer
  207.                 mov     cx, 0001h       ; Cyl 0, Sect 1
  208.                 mov     dh, 00h         ; Head 0
  209.                 mov     dl, 80h         ; Drive 80h ( First hard disk )
  210.                 int     13h             ;
  211.  
  212.                 jnc     readok          ;
  213.  
  214.                 mov     ah,09h          ; Print string$
  215.                 mov     dx, offset readerrmess
  216.                 int     21h
  217.  
  218.                 mov     ax, 4c01h       ; exit(1)
  219.                 int     21
  220.  
  221. readok:         mov     si, offset bootsect + 01BEh
  222.                 mov     di, offset partition
  223.                 mov     cx, 66          ; 4 partitons (16b) + 1 signature (2b)
  224.                 rep     movsb           ; copy to a safe place
  225.  
  226.                 push    es
  227.                 mov     ah,35h          ; Get interrupt vector
  228.                 mov     al,13h          ; Number 13h
  229.                 int     21h
  230.  
  231.                 lea     di, old13
  232.                 mov     word ptr [di],bx
  233.                 mov     word ptr [di+2],es
  234.                 pop     es
  235.  
  236.                 cmp     bx,offset new13
  237.                 jnz     skip
  238.  
  239.                 mov     ah,09h
  240.                 mov     dx,offset errmes
  241.                 int     21h
  242.  
  243.                 mov     ax,4c01h
  244.                 int     21h
  245.  
  246. skip:           mov     ah,09h
  247.                 mov     dx,offset initmes
  248.                 int     21h
  249.  
  250.                 mov     ah,25h          ; Set interrupt vector
  251.                 mov     al,13h          ; Number 13h
  252.                 lea     dx,new13
  253.                 int     21h
  254.  
  255.                 lea     dx,init         ; Cutting point for release mem.
  256.                 inc     dx
  257.                 int     27h
  258.  
  259. initmes db "   WPROT2.COM   By José Pedro T. Pina Coelho   18-Jul-89",LF,CR
  260.         db LF,CR
  261.         db "   Write requests to drive C: will be ignored.",LF,CR
  262.         db "   Format requests to drives C: & D: will be ignored.",LF,CR
  263.         db "   Reboot for normal operation.",LF,CR,"$"
  264. errmes  db "   ERROR: wprot2 is already installed",LF,CR
  265.         db "   wprot2 Chaining not allowed",LF,CR,"$"
  266. readerrmess     db "ERROR: wprot2 can't read partition table",LF,CR,"$"
  267. ; Copy of boot sector
  268. bootsect db     512 dup (?)
  269.  
  270.         dw      512 dup (?)
  271. stke    equ     this word
  272.  
  273. end     start
  274.