-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnumerateDirInFolder.c
More file actions
177 lines (155 loc) · 4.19 KB
/
EnumerateDirInFolder.c
File metadata and controls
177 lines (155 loc) · 4.19 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
/*
//=================declare filterData in FltCreate sample====================
typedef struct _FILTER_DATA
{
//
// The object that identifies this driver.
//
PDRIVER_OBJECT DriverObject;
//
// The filter handle that results from a call to
// FltRegisterFilter.
//
PFLT_FILTER Filter;
PFLT_INSTANCE VolumeInstance;
ULONG VolumeInstanceCnt;
}FILTER_DATA, *PFILTER_DATA;
FILTER_DATA filterData;
*/
NTSTATUS EnumerateDirInFolder()
{
PVOID pBuffer = NULL;
UNICODE_STRING DirectoryName;
OBJECT_ATTRIBUTES DirectoryAttributes;
NTSTATUS Status = STATUS_SUCCESS;
HANDLE DirectoryHandle = NULL;
PFILE_OBJECT DirectoryFileObject = NULL;
PFLT_VOLUME DirectoryVolume = NULL;
PFLT_INSTANCE DirectoryInstance = NULL;
IO_STATUS_BLOCK DirectoryIoStatusBlock;
PFILE_BOTH_DIR_INFORMATION DirInformation;
unsigned long dwSizeOfBuffer = 2048;
LARGE_INTEGER CurrentTime;
KeQuerySystemTime(&CurrentTime);
RtlInitUnicodeString(&DirectoryName, L"\\SystemRoot\\Folder");
KdPrint(("FOLDER_DIRECTORY name: %wZ\n", DirectoryName));
InitializeObjectAttributes(&DirectoryAttributes, &DirectoryName,OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
Status = FltCreateFileEx(
filterData.Filter,
NULL,
&DirectoryHandle,
&DirectoryFileObject,
(FILE_READ_DATA | FILE_LIST_DIRECTORY | FILE_READ_ATTRIBUTES | SYNCHRONIZE),
&DirectoryAttributes,
&DirectoryIoStatusBlock,
NULL,
NULL,
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE), // FULL sharing
FILE_OPEN, // MUST already exist
FILE_DIRECTORY_FILE, // MUST be a directory
NULL,
0,
IO_NO_PARAMETER_CHECKING
);
if (!NT_SUCCESS(Status)) {
KdPrint(("Unable to open %.*S, error = 0x%x\n", DirectoryName.Length / sizeof(WCHAR), DirectoryName.Buffer, Status));
return Status;
}
pBuffer = ExAllocatePoolWithTag(NonPagedPool, dwSizeOfBuffer, RS_POOL_TAG);
if (pBuffer == NULL) {
KdPrint(("Buffer cannot get memory\r\n"));
}
//
// We pass NO NAME which is the same as *.*
//
Status = FltGetVolumeFromFileObject(iclGuardData.Filter, DirectoryFileObject, &DirectoryVolume);
if (!NT_SUCCESS(Status))
{
KdPrint(("get voume failed: %x\n", Status));
return Status;
}
Status = FltGetVolumeInstanceFromName(
iclGuardData.Filter,
DirectoryVolume,
NULL,
&DirectoryInstance
);
if (!NT_SUCCESS(Status))
{
KdPrint(("get instance failed: %x\n", Status));
return Status;
}
BOOLEAN isDir = FALSE;
FltIsDirectory(DirectoryFileObject, DirectoryInstance, &isDir);
Status = FltQueryDirectoryFile(
DirectoryInstance,
DirectoryFileObject,
pBuffer,
dwSizeOfBuffer,
FileBothDirectoryInformation,
TRUE,
NULL,
FALSE,
NULL
);
if (!NT_SUCCESS(Status)) {
KdPrint(("Unable to query directory contents, error 0x%x\n", Status));
return Status;
}
KdPrint(("Directory List : \n"));
DirInformation = (PFILE_BOTH_DIR_INFORMATION)pBuffer;
// Loop over all files
for (;;)
{
//
// Dump the full name of the file. We could dump the other information
// here as well, but we'll keep the example shorter instead.
//
KdPrint((" %.*S\n", DirInformation->FileNameLength / sizeof(WCHAR), &DirInformation->FileName[0]));
KdPrint((" %d %d\n", CurrentTime.HighPart, DirInformation->CreationTime.HighPart));
//
// If there is no offset in the entry, the buffer has been exhausted.
//
if (DirInformation->NextEntryOffset == 0)
{
// Re-fill buffer
Status = FltQueryDirectoryFile(
DirectoryInstance,
DirectoryFileObject,
pBuffer,
dwSizeOfBuffer,
FileBothDirectoryInformation,
TRUE,
NULL,
FALSE,
NULL
);
if (!NT_SUCCESS(Status))
{
if (Status == STATUS_NO_MORE_FILES)
break;
KdPrint(("Unable to query directory contents,error 0x%x\n", Status));
return Status;
}
DirInformation = (PFILE_BOTH_DIR_INFORMATION)pBuffer;
continue;
}
//
// Advance to the next entry.
//
DirInformation = (PFILE_BOTH_DIR_INFORMATION)(((PUCHAR)DirInformation) + DirInformation->NextEntryOffset);
}
if (DirectoryHandle)
{
FltClose(DirectoryHandle);
}
if (DirectoryVolume)
{
FltObjectDereference(DirectoryVolume);
}
if (DirectoryInstance)
{
FltObjectDereference(DirectoryInstance);
}
return Status;
}