49908 (588681), страница 3
Текст из файла (страница 3)
#include
#include
#include
#include
#include
#include
unsigned char buffer[3];
FILE *fd;
int handle;
fail(char *);
void pass(char *);
main(int argc,char *argv[])
{
int n;
if(argc!=3)
{
printf("\nFailpass Utility v1.0 Email: chingpongin@yahoo.co.in");
printf("\nEnable/Disable .EXE or .COM execution");
printf("\nsyntex failpass fail ");
printf("\n or");
printf("\n failpass pass ");
exit(2);
}
strupr(argv[1]);
n=strcmp(argv[1],"FAIL");
if(n==0)
{
fail(argv[2]);
exit(2);
}
n=strcmp(argv[1],"PASS");
if(n==0)
{
pass(argv[2]);
exit(2);
}
return 0;
}
fail(char *fname)
{
_chmod(fname,1,FA_ARCH);
fd=fopen(fname,"rb+");
if(fd==NULL)
{
printf("\nCann't open %s\n\n",fname);
exit(2);
}
fseek(fd,0,SEEK_SET);
fread(buffer,2,1,fd);
if(buffer[0]==0xcd)
if(buffer[1]==0x20)
{
fclose(fd);
printf("\n%s already protected...\n\n",fname);
exit(2);
}
fseek(fd,0,SEEK_END);
fwrite(buffer,2,1,fd);
fseek(fd,0,SEEK_SET);
buffer[0]=0xcd; /* don't take any PANGA with this line */
buffer[1]=0x20; /* don't take any PANGA with this line */
fwrite(buffer,2,1,fd);
fclose(fd);
return 0;
}
void pass(char *fname)
{
int xyz;
fd=fopen(fname,"rb+");
if(fd==NULL)
{
fclose(fd);
printf("\nCannot open file %s\n\n",fname);
exit(2);
}
fseek(fd,0,SEEK_SET);
fread(buffer,2,1,fd);
if(buffer[0]!=0xcd)
if(buffer[1]!=0x20)
{
fclose(fd);
printf("\n%s is NOT protected....\n\n",fname);
exit(2);
}
fseek(fd,-2,SEEK_END);
fread(buffer,2,1,fd);
fseek(fd,0,SEEK_SET);
fwrite(buffer,2,1,fd);
fclose(fd);
handle=open(fname,O_RDWR);
chsize(handle,(filelength(handle))-2);
close(handle);
}
Модуль, який пише та читає інформацію напряму в ділянку диску без звертання до FAT системи
include common.mac
text segment use16
assume CS:text, DS:text
org 256
myproc proc
mov AH,09h ;printing function
printf g ;print g
printf g1 ;print g1
;parse the command line...
getf guff ;guff will have the command line
;is the command line empty?
cmp guff[0], 0
je expl
;is the command line starting with ?
cmp guff[0], '?'
je expl
;we are reading the disk
cmp guff[0], '^'
je readdisk
;nothing else but to write...
jmp writedisk
writedisk:
printf g2
waitenter
mov AH, 09h ;writing out
printf g3 ;what are we doing
printf guff ;file name
call disk_write ;write to disk
jmp rc
readdisk:
printf g2
waitenter
call disk_read ;write from disk
jmp rc
expl:
printf gline
jmp rc
rc: ;return
mov AX, 4C00h
int 21h
myproc endp
;====================================================================
;====================================================================
;==================== Procedure: disk_read =======================
;====================================================================
;====================================================================
disk_read proc
mov AH, 02h ;read raw input
mov AL, 1 ;number of sectors
mov CH, 0 ;cylinder
mov CL, 1 ;sector
mov DH, 1 ;head
mov DL, 0 ;disk drive - floppy
lea BX, guff
int 13h ;call BIOS
jnae ocr
;now guff stores file name
mov AH, 02h
mov AL, 1
mov CH, 1
mov CL, 1
mov DH, 0
mov DL, 0
lea BX, file
int 13h ;call BIOS
jnae ocr
;decrypt data
call chiper
;write output to the file saved
mov AH, 3Ch ;create file
mov CX, 0 ;no attributes
lea DX, guff ;name
int 21h ;call DOS
mov handle, AX ;get file handle
mov AH, 40h ;write to file
mov BX, handle ;handle
;get len
mov CX, 0 ;dump CX and SI
mov SI, 0
clen:
cmp file[SI], 0 ;is it zero yet?
je clenf ;this is the len
inc SI ;increase counting
inc CX ;increase counting
jmp clen ;jump back
clenf:
lea DX, file ;file data
int 21h ;call DOS
mov AH, 3Eh ;close file
mov BX, handle ;handle
int 21h ;call DOS
;open file for writing....
mov AH, 09h
printf success
jmp finr
ocr:
mov AH, 09h
printf error
finr: ret
disk_read endp
;====================================================================
;====================================================================
;====================================================================
;====================================================================
;==================== Procedure: disk_write ======================
;====================================================================
;====================================================================
disk_write proc
;write file name
mov AH, 03h ;write raw output
mov AL, 1 ;write 1 sector
mov CH, 0 ;cylinder zero
mov CL, 1 ;sector no 1
mov DH, 1 ;head 1
mov DL, 0 ;disk drive A:
lea BX, guff ;set buffer
int 13h ;call BIOS
;read file...
mov AH, 3Dh ;open file
mov AL, 2 ;read-write
lea DX, guff ;file name
int 21h ;call DOS
mov handle, AX ;get handle
mov AH, 3Fh ;reading file
mov BX, handle ;set handle
mov CX, 1024 ;reading
lea DX, file ;buffer
int 21h ;call DOS
;encrypt data
call chiper
;looping through each
mov AH, 03h
mov AL, 1
mov CH, 1
mov CL, 1
mov DH, 0
mov DL, 0
lea BX, file
int 13h
;show message
jnb okw
occ:
mov AH, 09h
printf error
jmp fin
okw:
mov AH, 09h
printf success
fin: ret ;return backwards
disk_write endp
;====================================================================
;====================================================================
;====================================================================
;====================================================================
;==================== Procedure: chiper ==========================
;====================================================================
;====================================================================
chiper proc
mov SI, 0
;xor values
chipe:
cmp file[SI], 0
je chipf ;jump if zero...
xor file[SI], 3
inc SI ;increase counter
jmp chipe
chipf:
ret ;return
chiper endp
;====================================================================
;====================================================================
g db 'Welcome to Disk Encryptor 1.1...', 10, 13, 'This program allows you to save encrypted data onto the disk....', 10, 13, 'Features: Data Chipering, Maximum of 1024 bytes of raw data', 10, 13,'Software Written in Assembler', 10, 13, '$'
gline db 10, 13, '? - help', 10, 13, '^ - reads a file from the floppy', 10, 13, '[Filename] - Saves the file onto a floppy', 10, 13, '$'
g1 db 'Copyright (C) Daniel Moisyeyev 2002. All Rights Reserved.', 10, 13, 10, 13, '$'
g2 db 10, 13, 'Please insert a floppy and press any key...', 10, 13, '$'
g3 db 10, 13, 'About to write $'
error db 10, 13, 'Error occured$'
success db 10, 13, 'Operation Completed$'
num db 4 dup(0), '$'
guff db 15 dup(0)
;file storage
handle dw 0
file db 1024 dup(0)
text ends
end myproc
Робота з програмою
Розроблена програма являє собою дві утіліти командного рядку. Перша дозволяє блокувати запуск виконавчіх файлів COM та EXE:
failpass fail
failpass pass
друга – тестує роботу з ділянкою диску в обхід FAT системи
Please insert a floppy and press any key...
About to write
Operation Completed
Висновки
Наприкінці роботи відмітимо лише, що доступ до блокових пристроїв здійснюється по секторах. Ядро MS-DOS за допомогою драйвера пристрою розглядає блоковий пристрій як логічний кінцевий масив секторів і припускає, що цей масив містить припустиму в MS-DOS файлову організацію.
Драйвер пристрою, у свою чергу, перетворить запит MS-DOS на логічний сектор у фізичну адресу блокового пристрою. Первісна файлова система MS DOS записана за допомогою програми MS-DOS FORMAT (див. теоретичну частину роботи).
Література
1. Нейбауэр А. Моя первая программа на С/С++. П., 1995. 368 С.
2. Бруно Бабэ. Просто и ясно о Borland C++. М., 1996. 400 С.
3. ObjectWindows для C++. К., 1993., 208 С.
4. Н. Барканати. Программирование игр для Windows на Borland C++. М., 1994.