-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsector_0_removal.cpp
More file actions
112 lines (95 loc) · 2.76 KB
/
sector_0_removal.cpp
File metadata and controls
112 lines (95 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
bool clearSectorZeroWinAPI(int diskNumber)
{
std::string diskPath = "\\\\.\\PhysicalDrive" + std::to_string(diskNumber);
// Open disk with write permissions
HANDLE hDisk = CreateFileA(
diskPath.c_str(),
GENERIC_READ | GENERIC_WRITE, // Both read and write permissions
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDisk == INVALID_HANDLE_VALUE)
{
std::cerr << "Failed to open disk. Error: " << GetLastError() << std::endl;
return false;
}
// Create 512 bytes of zeros
char zeroBuffer[512] = {0};
DWORD bytesWritten;
// Write to sector 0
if (!WriteFile(hDisk, zeroBuffer, 512, &bytesWritten, NULL))
{
std::cerr << "Failed to write to disk. Error: " << GetLastError() << std::endl;
CloseHandle(hDisk);
return false;
}
CloseHandle(hDisk);
if (bytesWritten == 512)
{
std::cout << "Successfully cleared sector 0!" << std::endl;
return true;
}
else
{
std::cout << "Warning: Only " << bytesWritten << " bytes written." << std::endl;
return false;
}
}
int main()
{
std::cout << "Disk Sector 0 Cleaner" << std::endl;
std::cout << "======================" << std::endl;
// Test which drives we can access
std::vector<int> availableDrives;
for (int i = 0; i < 10; i++)
{
std::string path = "\\\\.\\PhysicalDrive" + std::to_string(i);
HANDLE h = CreateFileA(path.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (h != INVALID_HANDLE_VALUE)
{
std::cout << "Drive " << i << ": Available" << std::endl;
availableDrives.push_back(i);
CloseHandle(h);
}
}
if (availableDrives.empty())
{
std::cout << "No drives found. Run as Administrator!" << std::endl;
std::cin.get();
return 1;
}
std::cout << "\nEnter drive number to clear: ";
int driveNum;
std::cin >> driveNum;
std::cout << "WARNING: This will destroy sector 0 of drive " << driveNum << std::endl;
std::cout << "Type 'ERASE' to confirm: ";
std::string confirm;
std::cin >> confirm;
if (confirm == "ERASE")
{
if (clearSectorZeroWinAPI(driveNum))
{
std::cout << "Done!" << std::endl;
}
else
{
std::cout << "Failed!" << std::endl;
}
}
else
{
std::cout << "Cancelled." << std::endl;
}
std::cout << "\nPress Enter to exit...";
std::cin.ignore();
std::cin.get();
return 0;
}