新手寫的,大牛勿噴。。。
以後會一直更新
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "HOSTSCODE.h"
#define DiskSize 512 //一個扇區512字節
#define SetTitle() SetConsoleTitle("Master Boot Record By:紫玫冰心")
void ReadMBR();//讀取MBR
void WriteMBR(unsigned char *Byte);//改寫MBR
void pause();
int WriteHOSTS(char *data);//設置域名重定向
BOOL EmptyHOSTS();//清空HOSTS
void ReadHOSTS();//讀取HOSTS文件內容
void ReductionHOSTS();
void about();//關於
int info();//系統信息
void help();
int bl;
char command[1000];
int main(int argc,char* argv[])
{
if(argc==2 && !strcmp(argv[1],"ReadMBR"))
{
SetTitle();//設置控制檯程序標題
ReadMBR();//讀MBR
}
else if(argc==3 && !strcmp(argv[1],"WriteMBR"))
{
SetTitle();//設置控制檯程序標題
//寫MBR
FILE *MBRFile;
unsigned char MBR[DiskSize]={0};
MBRFile=fopen(argv[2],"rb+");
if(!MBRFile)
{
printf("Can't open file");
return 0;
}else if(!feof(MBRFile))
{
fread(MBR,1,DiskSize,MBRFile);
WriteMBR(MBR);
fclose(MBRFile);
}
}else if(argc==2 && !strcmp(argv[1],"information"))
{
SetTitle();//設置控制檯程序標題
info();//系統信息
}
else if(argc==2 && !strcmp(argv[1],"command"))//CMD命令
{
while(1)
{
SetTitle();//設置控制檯程序標題
printf("command->");
fgets(command,sizeof(command)+1,stdin);
if(!strcmp("exit\n",command))
break;
system(command);
}
return 0;
}else if(argc==2 && !strcmp(argv[1],"EmptyHOSTS"))
{
SetTitle();
if(EmptyHOSTS()==TRUE)
printf("EmptyHOSTS is successful");
else
printf("Can't EmptyHOSTS");
}else if(argc==4 && !strcmp(argv[1],"WriteHOSTS"))
{
FILE *File;
SetTitle();
char web[300]={0};
strcat(web,argv[3]);
strcat(web," ");
strcat(web,argv[2]);
if(WriteHOSTS(web)==-1)
printf("Can't write hosts");
else
printf("Complete!");
}else if(argc==2 && !strcmp(argv[1],"about"))
{
SetTitle();
about();
}else if(argc==2 && !strcmp(argv[1],"ReadHOSTS"))
{
SetTitle();
ReadHOSTS();
}else if(argc==2 && !strcmp(argv[1],"ReductionHOSTS"))
{
SetTitle();
ReductionHOSTS();
}else if(argc==2 && !strcmp(argv[1],"help"))
{
SetTitle();
help();
}else{
SetTitle();
help();
}
return 0;
}
void ReadMBR()
{
//硬盤文件
FILE *Disk;
//保存文件
FILE *File;
int j;
//引導記錄
unsigned char MBRCode[DiskSize]={0};
Disk=fopen("\\\\.\\PhysicalDrive0","rb+");//打開硬盤
if(!Disk)
printf("Hard Disk not found!");//沒有打開硬盤
else if(!feof(Disk))
{
fseek(Disk,0,SEEK_SET);//移動到引導記錄的位置
fread(MBRCode,1,DiskSize,Disk);//讀取引導記錄
fclose(Disk);//關閉硬盤
File=fopen("MBR.ima","wb+");//硬盤文件
if(!File)
puts("Can't create 'MBR.ima'!");//沒有創建文件
else if(!feof(File))
{
//將引導記錄保存
fwrite(MBRCode,DiskSize,1,File);
fclose(File);//關閉文件
printf("Master Boot Record(Machine Code-char)\n");
for(j=0;j<DiskSize;j++)
printf("%c",MBRCode[j]);
printf("\nMaster Boot Record(Machine Code-int)\n");
for(j=0;j<DiskSize;j++)
{
if(j<511)
printf("%d\t",MBRCode[j]);
else
printf("%d\n",MBRCode[j]);
}
if(MBRCode[510]==85 && MBRCode[511]==170)//引導記錄結束標記
printf("The Master Boot Record is ok!");
else
printf("Operating system not found!");
}
}
}
void WriteMBR(unsigned char *Byte)
{
//硬盤文件
FILE *ADisk;
//保存文件
FILE *AFile;
//引導記錄
unsigned char AMBRCode[DiskSize]={0};
ADisk=fopen("\\\\.\\PhysicalDrive0","rb+");//打開硬盤
if(!ADisk)
puts("Hard Disk not found!");//沒有打開硬盤
else if(!feof(ADisk))
{
fseek(ADisk,0,SEEK_SET);//移動到引導記錄的位置
fread(AMBRCode,1,DiskSize,ADisk);//讀取引導記錄
AFile=fopen("MBR.ima","wb+");//硬盤文件
if(!AFile)
puts("Can't create 'MBR.ima'!");//沒有創建文件
else if(!feof(AFile))
{
printf("MBRCode:\n");
for(bl=0;bl<DiskSize;bl++)
printf("%c",Byte[bl]);
//將引導記錄保存
fwrite(AMBRCode,DiskSize,1,AFile);
fseek(ADisk,0,SEEK_SET);//移動到引導記錄的位置
fwrite(Byte,DiskSize,1,ADisk);
fclose(AFile);//關閉文件
}
fclose(ADisk);//關閉硬盤
}
}
int info()
{
/* 查看系統信息 */
SYSTEM_INFO s;
OSVERSIONINFOEX o;
GetSystemInfo(&s);
printf("OemID:%u\n",s.dwOemId);
printf("ProcessorArchitecture:%u\n",s.wProcessorArchitecture);
printf("PageSize:%u\n",s.dwPageSize);
printf("MinimumApplicationAddress:%u\n",
s.lpMinimumApplicationAddress);
printf("MaximumApplicationAddres:%u\n",
s.lpMaximumApplicationAddress);
printf("ActiveProcessorMask:%u\n",s.dwActiveProcessorMask);
printf("NumberOfProcessors:%u\n",s.dwNumberOfProcessors);
printf("ProcessorType:%u\n",s.dwProcessorType);
printf("AllocationGranularity:%u\n",s.dwAllocationGranularity);
printf("ProcessorLevel:%u\n",s.wProcessorLevel);
printf("ProcessorRevision:%u\n",s.wProcessorRevision);
o.dwOSVersionInfoSize=sizeof(o);
if(GetVersionEx((LPOSVERSIONINFOA)&o))
{
printf("Version:%u.%u\n",o.dwMajorVersion,o.dwMinorVersion);
printf("Build:%u\n",o.dwBuildNumber);
printf("ServicePack:%u.%u\n",
o.wServicePackMajor,
o.wServicePackMinor);
}else
printf("error:GetVersionEx");
return 0;
}
BOOL EmptyHOSTS()//清空hosts文件 不可以初始化
{
FILE *Empty;
Empty=fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","w");
if(!Empty)
return FALSE;//失敗
else if(!feof(Empty))
{
fclose(Empty);
return TRUE;
}
return FALSE;
}
int WriteHOSTS(char *data)
{
FILE *HOSTSFile;
HOSTSFile=fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","a+");
if(!HOSTSFile)
return -1;
else if(!feof(HOSTSFile))
{
fprintf(HOSTSFile,data);
fprintf(HOSTSFile,"\r\n");//只有換行纔會有效
}
fclose(HOSTSFile);
return 0;
}
void ReadHOSTS()
{
FILE *read;
char *CODE;
long int HOSTSSIZE;
read=
fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","rb");//hosts文件位置
if(!read)
printf("Can't open HOSTS");
else if(!feof(read))
{
fseek(read,0L,SEEK_END);//移動到文件尾
HOSTSSIZE=ftell(read);//獲取文件長度
CODE=(char*)malloc(HOSTSSIZE*sizeof(char));//動態申請內存
if(!CODE)
{
printf("error:malloc");
fclose(read);
return;
}else{
fseek(read,1,SEEK_SET);//移動到文件首
fread(CODE,1,HOSTSSIZE,read);//讀取文件內容
for(int j=0;j<HOSTSSIZE;j++)//回顯
printf("%c",CODE[j]);
}
}
printf("\n");
fclose(read);
free(CODE);
return;
}
void ReductionHOSTS()
{
FILE *HOSTSWRITE;
HOSTSWRITE=fopen("C:\\WINDOWS\\system32\\drivers\\etc\\hosts","w");
if(!HOSTSWRITE)
printf("Can't open HOSTS");
else if(!feof(HOSTSWRITE))
{
fprintf(HOSTSWRITE,HOSTSCODE);
printf("ReductionHOSTS is successful");
}
return;
}
void about()
{
printf("簡易控制檯程序工具箱 By:紫玫冰心\n原名:MasterBootRecord");
printf("\nQQ2308902347\n");
return;
}
void help()
{
puts(programHelp);
pause();
return;
}
void pause()
{
printf("Press any key to continue");
getchar();
return;
}
HOSTSCODE.h
#define HOSTSCODE "# Copyright (c) 1993-1999 Microsoft Corp.\r\n\r\n#\r\n\r\n# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.\r\n\r\n#\r\n\r\n# This file contains the mappings of IP addresses to host names. Each\r\n\r\n# entry should be kept on an individual line. The IP address should\r\n\r\n# be placed in the first column followed by the corresponding host name.\r\n\r\n# The IP address and the host name should be separated by at least one\r\n\r\n# space.\r\n\r\n#\r\n\r\n# Additionally, comments (such as these) may be inserted on individual\r\n\r\n# lines or following the machine name denoted by a '#' symbol.\r\n\r\n#\r\n\r\n# For example:\r\n\r\n#\r\n\r\n# 102.54.94.97 rhino.acme.com # source server\r\n\r\n# 38.25.63.10 x.acme.com # x client host\r\n\r\n127.0.0.1 localhost\r\n"
#define programHelp "程序命令:\n文件名 ReadMBR//讀MBR\n文件名 WriteMBR//寫MBR\n文件名 command//運行CMD命令 exit退出\n文件名 information//查看系統信息\n文件名 EmptyHOSTS//清空HOSTS文件\n文件名 WriteHOSTS 要跳轉的網址 跳轉到//設置域名重定向(不能有 http://)\n文件名 ReadHOSTS//讀HOSTS文件\n文件名ReductionHOSTS//還原HOSTS文件\n文件名 about//關於程序\n\n程序提示信息:\nHard Disk not found!//不能打開硬盤\nCan't create 'MBR.ima'!//不能創建備份MBR的文件\nThe Master Boot Record is ok!//系統可以正常引導\nOperating system not found!//雖然能讀取引導記錄,但是系統再次啓動就無法\n找到操作系統\nerror:GetVersionEx//GetVersionEx失敗\nCan't open HOSTS//無法打開HOSTS文件\nReductionHOSTS is successful//還原HOSTS文件完畢\nEmptyHOSTS is successful//清空HOSTS文件成功\nCan't EmptyHOSTS//不能清空HOSTS文件\nCan't write hosts//設置域名重定向失敗\nComplete!//設置域名重定向完畢\nPress any key to continue//按任意鍵繼續\nCan't open file//不能打開文件\n------------------------------------------\n放在windows目錄下就可以使用cmd.exe調用了"