Repository navigation
Expand file tree
/
Copy pathaccessMFT.cpp
More file actions
230 lines (195 loc) · 7.69 KB
/
Copy pathaccessMFT.cpp
File metadata and controls
230 lines (195 loc) · 7.69 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <windows.h>
#include <winioctl.h>
#include "Resources.hpp"
#include "BitMap.hpp"
#include "MFTRuns.hpp"
#include "GetAttributes.hpp"
#include "AttributeList.hpp"
int n = 0 ;
bool all = false ;
std :: vector<std::string>File2Recover ;
bool accessMFT(BYTE* mftEntry,bool isAttributeList) {
WORD fileStatusFlag = *(WORD*)&mftEntry[0x16] ;
if(fileStatusFlag != 0x0000) return false;
DWORD attrOffset = *(WORD*)&mftEntry[20];
DWORD SIoffset = 0, FNoffset = 0, DAoffset = 0 ,AttrListoffset = 0;
DWORD SIsize = 0, FNsize = 0, DAsize = 0, AttrListSize = 0 ;
std::string fname;
bool FileFound = false ; // all will false if all are not to be recovered
bool getOut = false;
int findex = -1;
while (attrOffset + 8 < MFT_ENTRY_SIZE) {
DWORD attrType = *(DWORD*)&mftEntry[attrOffset];
if (attrType == 0xFFFFFFFF) break;
DWORD attrLen = *(DWORD*)&mftEntry[attrOffset + 4] ;
if (attrLen == 0) break;
if (attrOffset + attrLen > MFT_ENTRY_SIZE) {
std::cout << " [!] Bad attribute length " << attrLen << " at offset " << attrOffset << std::endl;
getOut = true;
break;
}
switch (attrType) {
/*
The Attribute List Part is Yet to be Completed
----------------------------------------------
case 0x00000020 :
AttrListoffset = attrOffset;
AttrListSize = attrLen;
if (!getAttrListData(AttrListoffset, AttrListSize, mftEntry, MFT_ENTRY_SIZE)) getOut = true;
break;
*/
case 0x00000030: // File Name attribute
FNoffset = attrOffset ;
FNsize = attrLen ;
fname = getfilename(FNoffset, FNsize, mftEntry, MFT_ENTRY_SIZE);
if(fname.empty()){
FileFound = false ;
getOut = true ;
break ;
}
if(all){
if(frecover.is_open()) frecover.close();
frecover.clear();
frecover.open("Recovered/" + fname, std::ios::binary) ;
FileFound = true ;
}
else{
for (int i = 0; i < n; i++) {
if (File2Recover[i] == fname) {
if(frecover.is_open()) frecover.close();
frecover.clear();
findex = i;
frecover.open("Recovered/" + fname, std::ios::binary) ;
FileFound = true ;
break ;
}
}
}
break;
case 0x00000080: // Data attribute
DAoffset = attrOffset;
DAsize = attrLen;
if (debugMode || FileFound) {
if (!getdata(DAoffset, DAsize, mftEntry, MFT_ENTRY_SIZE)) getOut = true ;
}
break;
}
if(getOut) break;
attrOffset += attrLen;
}
if(debugMode && !getOut){
if(fname.empty()) return false ;
fbug << fname << std::endl ;
return true ;
}
if(FileFound && !getOut) {
if(!all) File2Recover[findex] = "" ;
std :: cout << fname << " : File Recovered Successfully" << std::endl;
return true ;
}
return false ;
}
void RecoverFile(std :: string &Drive) {
std :: string driveLetter = Drive.substr(0,Drive.length() - 1) ;
if(debugMode){
fbug.open("debug" + driveLetter + ".txt") ;
fbug << "Recoverable Files : " << std::endl << std::endl ;
}
Drive = "\\\\.\\" + Drive ;
std::wstring FullPath(Drive.begin(), Drive.end());
hVolume = CreateFileW(FullPath.c_str(), GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL) ;
if (hVolume == INVALID_HANDLE_VALUE) {
std :: cerr << "Failed to open volume. Error: " << GetLastError() << std :: endl;
std :: cin.get();
return ;
}
BYTE bootSector[512];
DWORD bytesRead;
if (!ReadFile(hVolume, bootSector, 512, &bytesRead, NULL)) {
std :: cerr << "Failed to read boot sector. Error: " << GetLastError() << std :: endl;
CloseHandle(hVolume);
std :: cout << "Press Enter to exit...";
std :: cin.get();
return ;
}
bytesPerSector = *(WORD*)&bootSector[11];
sectorsPerCluster = bootSector[13];
mftCluster = *(ULONGLONG*)&bootSector[48];
ClusterSize = bytesPerSector * sectorsPerCluster ;
ULONGLONG mftOffset = mftCluster * ClusterSize ;
CHAR clustersPerMFTRecord = (CHAR)bootSector[64]; // CHAR is SIGNED , BYTE is UNSIGNED
if (clustersPerMFTRecord >= 0) MFT_ENTRY_SIZE = clustersPerMFTRecord * ClusterSize ;
else MFT_ENTRY_SIZE = 1 << (-clustersPerMFTRecord);
DWORD TotalEntries ;
NTFS_VOLUME_DATA_BUFFER volData;
DWORD ret;
if (DeviceIoControl(hVolume,FSCTL_GET_NTFS_VOLUME_DATA,NULL, 0,&volData, sizeof(volData),&ret, NULL))
TotalEntries = (DWORD)volData.MftValidDataLength.QuadPart / MFT_ENTRY_SIZE;
else TotalEntries = 0 ;
GetMftRunList(mftOffset) ;
GetVolumeBitmap() ;
DWORD EntriesToRead = 200 ;
BYTE Entries[MFT_ENTRY_SIZE * EntriesToRead];
ULONGLONG current_entry = 0;
for(auto mftCluster : MftRunList){
LARGE_INTEGER mftPtr ;
mftPtr.QuadPart = mftCluster.StartingLCN * ClusterSize;
SetFilePointerEx(hVolume, mftPtr, NULL, FILE_BEGIN);
DWORD Entry = (mftCluster.ClusterCount * ClusterSize + MFT_ENTRY_SIZE - 1) / MFT_ENTRY_SIZE;
while (Entry > 0) {
EntriesToRead = std :: min(EntriesToRead, Entry);
ULONGLONG BytesToRead = EntriesToRead * MFT_ENTRY_SIZE;
if (!ReadFile(hVolume, Entries, BytesToRead, &bytesRead, NULL)) {
std :: cerr << "Failed to read MFT entries. Error: " << GetLastError() << std :: endl;
break;
}
WORD EntriesRead = bytesRead / MFT_ENTRY_SIZE ;
for (WORD i = 0; i < EntriesRead; i++) {
BYTE* mftEntry = &Entries[i * MFT_ENTRY_SIZE] ;
current_entry++;
accessMFT(mftEntry,false) ;
}
Entry -= EntriesRead;
}
}
fbug.close();
CloseHandle(hVolume);
return ;
}
int main(int argc,char* argv[]) {
if(argc >= 2){
std :: string Drive ;
std :: string status = argv[1] ;
if(status == "--debug") {
debugMode = true ;
Drive = argv[2] ;
RecoverFile(Drive) ; // Debugging mode
std :: cout << "Debug Successful" << std :: endl ;
}
else if(status == "--recoverall") {
CreateDirectoryA("Recovered", NULL);
Drive = argv[2] ;
all = true ;
RecoverFile(Drive) ;
}
else{
CreateDirectoryA("Recovered", NULL);
std :: cout << "Enter the number of files to recover: " ;
std :: cin >> n ;
getchar() ;
File2Recover.resize(n) ;
for(int i = 0 ; i < n ; i++){
std :: cout << "File " << i+1 << " : ";
getline(std :: cin,File2Recover[i]) ;
}
Drive = argv[2] ;
RecoverFile(Drive) ;
for(auto fname : File2Recover){
std :: cout << std :: endl ;
if(!fname.empty()){
std :: cout << fname << " : File Cannot be Recovered" << std :: endl ;
}
}
}
}
}