Skip to content

Commit

Permalink
Utils: GetFileName, GetVolumeName
Browse files Browse the repository at this point in the history
  • Loading branch information
GIL committed Sep 16, 2018
1 parent 49b9f7e commit 1520467
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 45 deletions.
2 changes: 2 additions & 0 deletions GilRan/GilRan.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="PreCreate.c" />
<ClCompile Include="Utils.c" />
<ResourceCompile Include="GilRan.rc" />
<ClCompile Include="GilRan.c" />
<Inf Include="GilRan.inf" />
Expand Down Expand Up @@ -193,6 +194,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="PreCreate.h" />
<ClInclude Include="Utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
6 changes: 6 additions & 0 deletions GilRan/GilRan.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<ClCompile Include="PreCreate.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Utils.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="GilRan.rc">
Expand All @@ -40,5 +43,8 @@
<ClInclude Include="PreCreate.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Utils.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
55 changes: 15 additions & 40 deletions GilRan/PreCreate.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#include <fltKernel.h>
#include <dontuse.h>
#include <suppress.h>
#include <ntstrsafe.h>

#include "PreCreate.h"
#include "Utils.h"

FLT_PREOP_CALLBACK_STATUS
PreCreate(
Expand All @@ -7,50 +13,19 @@ PreCreate(
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
{
UNREFERENCED_PARAMETER(FltObjects);
UNREFERENCED_PARAMETER(CompletionContext);

NTSTATUS status;

PFLT_FILE_NAME_INFORMATION pFileNameInformation;
status = FltGetFileNameInformation(Data, FLT_FILE_NAME_NORMALIZED | FLT_FILE_NAME_QUERY_DEFAULT, &pFileNameInformation);

UNICODE_STRING FileName, VolumeName;
if (NT_SUCCESS(status)) {
status = FltParseFileNameInformation(pFileNameInformation);

if (NT_SUCCESS(status)) {
FileName.Length = 0;
FileName.MaximumLength = NTSTRSAFE_UNICODE_STRING_MAX_CCH * sizeof(WCHAR);
FileName.Buffer = ExAllocatePoolWithTag(NonPagedPool, FileName.MaximumLength, 'FLIG');

if (FileName.Buffer != NULL) {
RtlUnicodeStringCopy(&FileName, &(pFileNameInformation->Name));

VolumeName.Length = 0;
VolumeName.MaximumLength = FltObjects->FileObject->FileName.MaximumLength + 2;

ULONG BufferSizeNeeded;
status = FltGetVolumeName(FltObjects->Volume, NULL, &BufferSizeNeeded);

if (status == STATUS_BUFFER_TOO_SMALL) {
VolumeName.MaximumLength += (USHORT)BufferSizeNeeded;
}

VolumeName.Buffer = ExAllocatePoolWithTag(NonPagedPool, VolumeName.MaximumLength, 'VLIG');
if (VolumeName.Buffer != NULL) {
status = FltGetVolumeName(FltObjects->Volume, &VolumeName, &BufferSizeNeeded);

if (NT_SUCCESS(status)) {
DbgPrintEx(DPFLTR_DEFAULT_ID, DPFLTR_INFO_LEVEL, "FilePath: %ws%ws\n", VolumeName.Buffer, FileName.Buffer);
}
ExFreePoolWithTag(VolumeName.Buffer, 'VLIG');
}
ExFreePoolWithTag(FileName.Buffer, 'FLIG');
}
}
FltReleaseFileNameInformation(pFileNameInformation);
}
WCHAR FilePath[1024], VolumeName[1024];

status = GetFilePath(Data, FilePath);
if (!NT_SUCCESS(status)) return FLT_PREOP_COMPLETE;

status = GetVolumeName(FltObjects, VolumeName);
if (!NT_SUCCESS(status)) return FLT_PREOP_COMPLETE;

DbgPrintEx(DPFLTR_DEFAULT_ID, DPFLTR_INFO_LEVEL, "FilePath: %ws%ws\n", VolumeName, FilePath);

return FLT_PREOP_SUCCESS_NO_CALLBACK;
}
5 changes: 0 additions & 5 deletions GilRan/PreCreate.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
#pragma once
#include <fltKernel.h>
#include <dontuse.h>
#include <suppress.h>
#include <ntstrsafe.h>

FLT_PREOP_CALLBACK_STATUS
PreCreate(
_Inout_ PFLT_CALLBACK_DATA Data,
Expand Down
54 changes: 54 additions & 0 deletions GilRan/Utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <fltKernel.h>
#include <dontuse.h>
#include <suppress.h>
#include <ntstrsafe.h>
#include "Utils.h"

NTSTATUS GetFilePath(
_In_ PFLT_CALLBACK_DATA Data,
_Out_ PWCHAR pFilePath
)
{
PFLT_FILE_NAME_INFORMATION pFileNameInformation;
NTSTATUS status = FltGetFileNameInformation(Data, FLT_FILE_NAME_NORMALIZED | FLT_FILE_NAME_QUERY_DEFAULT, &pFileNameInformation);
if (!NT_SUCCESS(status)) return status;

status = FltParseFileNameInformation(pFileNameInformation);
if (!NT_SUCCESS(status)) return status;

wcscpy_s(pFilePath, pFileNameInformation->ParentDir.Length, pFileNameInformation->ParentDir.Buffer);

FltReleaseFileNameInformation(pFileNameInformation);

return STATUS_SUCCESS;
}

NTSTATUS GetVolumeName(
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Out_ PWCHAR pVolumeName
)
{
NTSTATUS status;

UNICODE_STRING VolumeName;
VolumeName.Length = 0;
VolumeName.MaximumLength = FltObjects->FileObject->FileName.MaximumLength + 2;

ULONG szBufferNeeded;
status = FltGetVolumeName(FltObjects->Volume, NULL, &szBufferNeeded);

if (status == STATUS_BUFFER_TOO_SMALL) {
VolumeName.MaximumLength += (USHORT)szBufferNeeded;
}

VolumeName.Buffer = ExAllocatePoolWithTag(NonPagedPool, VolumeName.MaximumLength, 'vLIG');
if (VolumeName.Buffer == NULL) return STATUS_UNSUCCESSFUL;

status = FltGetVolumeName(FltObjects->Volume, &VolumeName, &szBufferNeeded);
if (NT_SUCCESS(status)) {
wcscpy_s(pVolumeName, VolumeName.Length, VolumeName.Buffer);
}

ExFreePoolWithTag(VolumeName.Buffer, 'vLIG');
return status;
}
10 changes: 10 additions & 0 deletions GilRan/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
NTSTATUS GetFilePath(
_In_ PFLT_CALLBACK_DATA Data,
_Out_ PWCHAR pFilePath
);

NTSTATUS GetVolumeName(
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Out_ PWCHAR pVolumeName
);

0 comments on commit 1520467

Please sign in to comment.