6
0
mirror of https://github.com/JKornev/hidden synced 2024-06-16 03:58:04 +00:00
hidden/Hidden/Driver.c

140 lines
3.5 KiB
C
Raw Normal View History

2016-07-21 23:02:31 +00:00
#include <fltKernel.h>
#include <Ntddk.h>
#include "ExcludeList.h"
#include "RegFilter.h"
#include "FsFilter.h"
#include "PsMonitor.h"
#include "Device.h"
#include "Driver.h"
2016-12-18 18:11:10 +00:00
#include "Configs.h"
2016-12-30 16:57:52 +00:00
#include "Helper.h"
#include "KernelAnalyzer.h"
2016-07-21 23:02:31 +00:00
2017-02-02 22:55:19 +00:00
#define DRIVER_ALLOC_TAG 'nddH'
2016-07-21 23:02:31 +00:00
PDRIVER_OBJECT g_driverObject = NULL;
2016-12-12 20:40:35 +00:00
volatile LONG g_driverActive = FALSE;
2016-07-21 23:02:31 +00:00
// =========================================================================================
2016-12-12 20:40:35 +00:00
VOID EnableDisableDriver(BOOLEAN enabled)
2016-07-21 23:02:31 +00:00
{
2016-12-12 20:40:35 +00:00
InterlockedExchange(&g_driverActive, (LONG)enabled);
2016-07-21 23:02:31 +00:00
}
2016-12-12 20:40:35 +00:00
BOOLEAN IsDriverEnabled()
2016-07-21 23:02:31 +00:00
{
2016-12-12 20:40:35 +00:00
return (g_driverActive ? TRUE : FALSE);
2016-07-21 23:02:31 +00:00
}
// =========================================================================================
2016-12-30 16:57:52 +00:00
ULONGLONG g_hiddenRegConfigId = 0;
ULONGLONG g_hiddenDriverFileId = 0;
NTSTATUS InitializeStealthMode(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
2016-12-21 21:04:55 +00:00
{
2016-12-30 16:57:52 +00:00
PLDR_DATA_TABLE_ENTRY LdrEntry;
UNICODE_STRING normalized;
NTSTATUS status;
2016-12-21 21:04:55 +00:00
if (!CfgGetStealthState())
return STATUS_SUCCESS;
2016-12-30 16:57:52 +00:00
LdrEntry = (PLDR_DATA_TABLE_ENTRY)DriverObject->DriverSection;
normalized.Length = 0;
normalized.MaximumLength = LdrEntry->FullModuleName.Length + NORMALIZE_INCREAMENT;
2017-02-02 22:55:19 +00:00
normalized.Buffer = (PWCH)ExAllocatePoolWithQuotaTag(PagedPool, normalized.MaximumLength, DRIVER_ALLOC_TAG);
2016-12-30 16:57:52 +00:00
if (!normalized.Buffer)
{
2018-12-02 21:56:39 +00:00
LogError("Error, can't allocate buffer");
2016-12-30 16:57:52 +00:00
return STATUS_MEMORY_NOT_ALLOCATED;
}
status = NormalizeDevicePath(&LdrEntry->FullModuleName, &normalized);
if (!NT_SUCCESS(status))
{
2018-12-02 21:56:39 +00:00
LogError("Error, path normalization failed with code:%08x, path:%wZ", status, &LdrEntry->FullModuleName);
2017-02-02 22:55:19 +00:00
ExFreePoolWithTag(normalized.Buffer, DRIVER_ALLOC_TAG);
2016-12-30 16:57:52 +00:00
return status;
}
2016-12-21 21:04:55 +00:00
2016-12-30 16:57:52 +00:00
status = AddHiddenFile(&normalized, &g_hiddenDriverFileId);
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, can't hide self registry key");
2016-12-30 16:57:52 +00:00
2017-02-02 22:55:19 +00:00
ExFreePoolWithTag(normalized.Buffer, DRIVER_ALLOC_TAG);
2016-12-30 16:57:52 +00:00
status = AddHiddenRegKey(RegistryPath, &g_hiddenRegConfigId);
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, can't hide self registry key");
2016-12-21 21:04:55 +00:00
2018-12-02 21:56:39 +00:00
LogTrace("Stealth mode has been activated");
2016-12-21 21:04:55 +00:00
return STATUS_SUCCESS;
}
// =========================================================================================
2017-02-02 22:55:19 +00:00
_Function_class_(DRIVER_UNLOAD)
2016-07-21 23:02:31 +00:00
VOID DriverUnload(PDRIVER_OBJECT DriverObject)
{
UNREFERENCED_PARAMETER(DriverObject);
DestroyDevice();
DestroyRegistryFilter();
DestroyFSMiniFilter();
DestroyPsMonitor();
DestroyKernelAnalyzer();
2016-07-21 23:02:31 +00:00
}
2017-02-02 22:55:19 +00:00
_Function_class_(DRIVER_INITIALIZE)
2016-07-21 23:02:31 +00:00
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
{
NTSTATUS status;
UNREFERENCED_PARAMETER(RegistryPath);
2016-12-12 20:40:35 +00:00
EnableDisableDriver(TRUE);
2016-07-21 23:02:31 +00:00
2016-12-18 18:11:10 +00:00
status = InitializeConfigs(RegistryPath);
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, can't initialize configs");
2016-12-18 18:11:10 +00:00
2016-12-21 21:04:55 +00:00
EnableDisableDriver(CfgGetDriverState());
InitializeKernelAnalyzer();
2016-07-21 23:02:31 +00:00
status = InitializePsMonitor(DriverObject);
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, object monitor haven't started");
2016-07-21 23:02:31 +00:00
status = InitializeFSMiniFilter(DriverObject);
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, file-system mini-filter haven't started");
2016-07-21 23:02:31 +00:00
status = InitializeRegistryFilter(DriverObject);
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, registry filter haven't started");
2016-07-21 23:02:31 +00:00
status = InitializeDevice(DriverObject);
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, can't create device");
2016-07-21 23:02:31 +00:00
2016-12-30 16:57:52 +00:00
status = InitializeStealthMode(DriverObject, RegistryPath);
2016-12-21 21:04:55 +00:00
if (!NT_SUCCESS(status))
2018-12-02 21:56:39 +00:00
LogWarning("Error, can't activate stealth mode");
2016-12-21 21:04:55 +00:00
2016-12-18 18:11:10 +00:00
DestroyConfigs();
2016-07-21 23:02:31 +00:00
DriverObject->DriverUnload = DriverUnload;
g_driverObject = DriverObject;
return STATUS_SUCCESS;
}