Skip to content

Commit 826c47c

Browse files
committed
Derive the device name from the service
Discussion: #92
1 parent f7781bd commit 826c47c

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

TitanHide/TitanHide.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include "threadhidefromdbg.h"
88

99
static UNICODE_STRING DeviceName;
10+
static wchar_t DeviceNameBuffer[256];
1011
static UNICODE_STRING Win32Device;
12+
static wchar_t Win32DeviceBuffer[256];
1113

1214
static void DriverUnload(IN PDRIVER_OBJECT DriverObject)
1315
{
@@ -65,9 +67,44 @@ static NTSTATUS DriverWrite(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
6567
return RetStatus;
6668
}
6769

68-
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
70+
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
6971
{
70-
UNREFERENCED_PARAMETER(RegistryPath);
72+
// Initialize name buffers
73+
RtlInitEmptyUnicodeString(&DeviceName, DeviceNameBuffer, sizeof(DeviceNameBuffer));
74+
RtlAppendUnicodeToString(&DeviceName, L"\\Device\\");
75+
RtlInitEmptyUnicodeString(&Win32Device, Win32DeviceBuffer, sizeof(Win32DeviceBuffer));
76+
RtlAppendUnicodeToString(&Win32Device, L"\\DosDevices\\");
77+
78+
// Derive the device name and symbolic link from the registry path
79+
UNICODE_STRING DriverName = {};
80+
if (RegistryPath != NULL && RegistryPath->Buffer != NULL)
81+
{
82+
for (int i = 0; i < RegistryPath->Length / sizeof(WCHAR); i++)
83+
{
84+
auto index = RegistryPath->Length / sizeof(WCHAR) - i - 1;
85+
if (RegistryPath->Buffer[index] == L'\\')
86+
{
87+
index++; // skip the backslash
88+
DriverName.Buffer = RegistryPath->Buffer + index;
89+
DriverName.Length = (USHORT)(RegistryPath->Length - index * sizeof(WCHAR));
90+
DriverName.MaximumLength = DriverName.Length;
91+
break;
92+
}
93+
}
94+
}
95+
96+
// Fall back to default driver name
97+
if (DriverName.Length == 0)
98+
{
99+
RtlInitUnicodeString(&DriverName, L"TitanHide");
100+
}
101+
102+
// Use the driver name
103+
RtlAppendUnicodeStringToString(&DeviceName, &DriverName);
104+
RtlAppendUnicodeStringToString(&Win32Device, &DriverName);
105+
InitLog(&DriverName);
106+
Log("[TITANHIDE] DriverName: %.*ws\r\n", DriverName.Length / sizeof(WCHAR), DriverName.Buffer);
107+
71108
PDEVICE_OBJECT DeviceObject = NULL;
72109
NTSTATUS status;
73110

@@ -103,8 +140,6 @@ extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRI
103140
}
104141

105142
//create io device
106-
RtlInitUnicodeString(&DeviceName, L"\\Device\\TitanHide");
107-
RtlInitUnicodeString(&Win32Device, L"\\DosDevices\\TitanHide");
108143
status = IoCreateDevice(DriverObject,
109144
0,
110145
&DeviceName,

TitanHide/log.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
#include "log.h"
22

3+
static UNICODE_STRING LogFilename;
4+
static wchar_t LogFilenameBuffer[256];
5+
6+
void InitLog(const PUNICODE_STRING DriverName)
7+
{
8+
RtlInitEmptyUnicodeString(&LogFilename, LogFilenameBuffer, sizeof(LogFilenameBuffer));
9+
RtlAppendUnicodeToString(&LogFilename, L"\\DosDevices\\C:\\");
10+
RtlAppendUnicodeStringToString(&LogFilename, DriverName);
11+
RtlAppendUnicodeToString(&LogFilename, L".log");
12+
Log("[TITANHIDE] Log file initialized: %.*ws\r\n",
13+
LogFilename.Length / sizeof(WCHAR), LogFilename.Buffer);
14+
}
15+
316
void Log(const char* format, ...)
417
{
518
char msg[1024] = "";
@@ -12,10 +25,8 @@ void Log(const char* format, ...)
1225
DbgPrintEx(DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, msg);
1326
#endif
1427
va_end(format);
15-
UNICODE_STRING FileName;
1628
OBJECT_ATTRIBUTES objAttr;
17-
RtlInitUnicodeString(&FileName, L"\\DosDevices\\C:\\TitanHide.log");
18-
InitializeObjectAttributes(&objAttr, &FileName,
29+
InitializeObjectAttributes(&objAttr, &LogFilename,
1930
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
2031
NULL, NULL);
2132
if(KeGetCurrentIrql() != PASSIVE_LEVEL)

TitanHide/log.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define PRINTF_ATTR(FormatIndex, FirstToCheck)
1414
#endif
1515

16+
void InitLog(const PUNICODE_STRING DriverName);
1617
PRINTF_ATTR(1, 2) void Log(const char* format, ...);
1718

1819
#endif

0 commit comments

Comments
 (0)