8000 v2.0.1 by Idov31 · Pull Request #82 · Idov31/Nidhogg · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/wdk-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: WDK Build

on:
pull_request:
branches:
- dev

permissions:
contents: read

jobs:
build:
name: Build Release x64
runs-on: windows-2022

steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up MSBuild
uses: microsoft/setup-msbuild@v2

- name: Install Windows Driver Kit
shell: pwsh
run: choco install windowsdriverkit10 --yes --no-progress

- name: Build solution
shell: pwsh
run: msbuild Nidhogg.sln /t:Build /m /p:Configuration=Release /p:Platform=x64 /p:SignMode=Off /p:DeployOnBuild=false
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# Internal stuff
.gitignore
idatocppsig.py
images/logo_original.png
\.*/
!.github/
.github/*
!.github/ISSUE_TEMPLATE/
!.github/ISSUE_TEMPLATE/**
!.github/workflows/
!.github/workflows/*.yml
*.md
!README.md
!CONTRIBUTING.md

# User-specific files
*.rsuser
*.suo
Expand Down
25 changes: 13 additions & 12 deletions Nidhogg/AntiAnalysisHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,31 +827,28 @@ NTSTATUS AntiAnalysisHandler::ListObCallbacks(_Inout_ IoctlCallbackList<ObCallba
_IRQL_requires_max_(APC_LEVEL)
char* AntiAnalysisHandler::MatchCallback(_In_ PVOID callack) {
NTSTATUS status = STATUS_SUCCESS;
PRTL_PROCESS_MODULES info = NULL;
ULONG infoSize;
char* driverName = nullptr;
errno_t err = 0;
MemoryAllocator<PRTL_PROCESS_MODULES> infoAllocator = MemoryAllocator<PRTL_PROCESS_MODULES>();

status = ZwQuerySystemInformation(SystemModuleInformation, NULL, 0, &infoSize);

while (status == STATUS_INFO_LENGTH_MISMATCH) {
FreeVirtualMemory(info);
info = AllocateMemory<PRTL_PROCESS_MODULES>(infoSize);

if (!info) {
if (!infoAllocator.Realloc(infoSize)) {
status = STATUS_INSUFFICIENT_RESOURCES;
break;
}
status = ZwQuerySystemInformation(SystemModuleInformation, info, infoSize, &infoSize);
status = ZwQuerySystemInformation(SystemModuleInformation, infoAllocator.Get(), infoSize, &infoSize);
}

if (!NT_SUCCESS(status) || !info) {
FreeVirtualMemory(info);
if (!NT_SUCCESS(status) || !infoAllocator.IsValid()) {
infoAllocator.Free();
ExRaiseStatus(status);
}
PRTL_PROCESS_MODULE_INFORMATION modules = info->Modules;
< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker="+"> PRTL_PROCESS_MODULE_INFORMATION modules = infoAllocator.Get()->Modules;

for (ULONG i = 0; i < info->NumberOfModules; i++) {
for (ULONG i = 0; i < infoAllocator.Get()->NumberOfModules; i++) {
if (callack >= modules[i].ImageBase &&
callack < static_cast<PVOID>(static_cast<PUCHAR>(modules[i].ImageBase) + modules[i].ImageSize)) {
if (modules[i].FullPathName) {
Expand All @@ -877,10 +874,14 @@ char* AntiAnalysisHandler::MatchCallback(_In_ PVOID callack) {
break;
}
}
FreeVirtualMemory(info);

if (!NT_SUCCESS(status) || !driverName)
if (!driverName && NT_SUCCESS(status))
status = STATUS_NOT_FOUND;

if (!NT_SUCCESS(status) || !driverName) {
infoAllocator.Free();
ExRaiseStatus(status);
}
return driverName;
}

Expand Down
36 changes: 32 additions & 4 deletions Nidhogg/FileHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FileHandler::FileHandler() {
if (!InitializeList(&protectedFiles))
ExRaiseStatus(STATUS_INSUFFICIENT_RESOURCES);
memset(callbacks, 0, sizeof(callbacks));
rundown = RundownProtection();
}

_IRQL_requires_same_
Expand All @@ -17,7 +18,7 @@ FileHandler::~FileHandler() {
UninstallNtfsHook(i);
}
}

rundown.Complete();
ClearFilesList(FileType::All);
FreeVirtualMemory(this->protectedFiles.Items);
}
Expand All @@ -29,6 +30,16 @@ PVOID FileHandler::GetNtfsCallback(_In_ ULONG index) const {
return callbacks[index].Address;
}

_IRQL_requires_max_(DISPATCH_LEVEL)
bool FileHandler::AcquireRundown() {
return rundown.Acquire();
}

_IRQL_requires_max_(DISPATCH_LEVEL)
void FileHandler::ReleaseRundown() {
rundown.Release();
}

/*
* Description:
* HookedNtfsIrpCreate is responsible for handling the NTFS IRP_MJ_CREATE.
Expand Down Expand Up @@ -56,12 +67,22 @@ NTSTATUS HookedNtfsIrpCreate(_Inout_ PDEVICE_OBJECT DeviceObject, _Inout_ PIRP I

// If we reach here, it means something went horribly wrong.
__try {
if (!NidhoggFileHandler)
return STATUS_DELETE_PENDING;
originalFunction = static_cast<tNtfsIrpFunction>(NidhoggFileHandler->GetNtfsCallback(IRP_MJ_CREATE));

if (!NidhoggFileHandler->AcquireRundown())
return originalFunction ? originalFunction(DeviceObject, Irp) : STATUS_DELETE_PENDING;
}
__except (EXCEPTION_EXECUTE_HANDLER) {
return STATUS_SUCCESS;
}

if (!originalFunction) {
NidhoggFileHandler->ReleaseRundown();
return STATUS_DELETE_PENDING;
}

do {
// Validating the file object.
if (!stack || !stack->FileObject)
Expand Down Expand Up @@ -95,13 +116,18 @@ NTSTATUS HookedNtfsIrpCreate(_Inout_ PDEVICE_OBJECT DeviceObject, _Inout_ PIRP I
if (NidhoggFileHandler->FindFile(fullPath.Buffer, FileType::Protected, false)) {
FreeVirtualMemory(fullPath.Buffer);
Irp->IoStatus.Status = STATUS_ACCESS_DENIED;
return STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
NidhoggFileHandler->ReleaseRundown();
return STATUS_ACCESS_DENIED;
}
} while (false);
FreeVirtualMemory(fullPath.Buffer);

irqlGuard.UnsetIrql();
return originalFunction(DeviceObject, Irp);
NTSTATUS status = originalFunction(DeviceObject, Irp);
NidhoggFileHandler->ReleaseRundown();
return status;
}

/*
Expand Down Expand Up @@ -144,6 +170,7 @@ NTSTATUS FileHandler::InstallNtfsHook(_In_ ULONG irpMjFunction) {
return status;
}

rundown.Reinit();
callbacks[irpMjFunction].Address = reinterpret_cast<PVOID>(InterlockedExchange64(
reinterpret_cast<LONG64*>(&ntfsDriverObject->MajorFunction[irpMjFunction]),
functionAddress));
Expand Down Expand Up @@ -197,6 +224,7 @@ NTSTATUS FileHandler::UninstallNtfsHook(_In_ ULONG irpMjFunction) {
InterlockedExchange64(reinterpret_cast<LONG64*>(&ntfsDriverObject->MajorFunction[irpMjFunction]), functionAddress);
this->callbacks[irpMjFunction].Address = nullptr;
this->callbacks[irpMjFunction].Activated = false;
rundown.Complete();

ObDereferenceObject(ntfsDriverObject);
return status;
Expand Down Expand Up @@ -380,7 +408,7 @@ bool FileHandler::ListProtectedFiles(_Inout_ IoctlFileList* filesList) {
return true;
}
currentEntry = protectedFiles.Items;
MemoryGuard listGuard(filesList->Files, static_cast<ULONG>(protectedFiles.Count * sizeof(WCHAR)), UserMode);
MemoryGuard listGuard(filesList->Files, static_cast<ULONG>(protectedFiles.Count * sizeof(WCHAR*)), UserMode);

if (!listGuard.IsValid())
return false;
Expand Down
8 changes: 8 additions & 0 deletions Nidhogg/FileHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {
#include "NidhoggCommon.h"
}
#include "FileHelper.h"
#include "RundownProtection.h"
#include "ListHelper.hpp"

// Definitions.
Expand Down Expand Up @@ -42,6 +43,7 @@ class FileHandler {
private:
FilesList protectedFiles;
NtfsCallback callbacks[SUPPORTED_HOOKED_NTFS_CALLBACKS];
RundownProtection rundown;

public:
void* operator new(size_t size) noexcept {
Expand Down Expand Up @@ -82,6 +84,12 @@ class FileHandler {

_IRQL_requires_max_(APC_LEVEL)
PVOID GetNtfsCallback(_In_ ULONG index) const;

_IRQL_requires_max_(DISPATCH_LEVEL)
bool AcquireRundown();

_IRQL_requires_max_(DISPATCH_LEVEL)
void ReleaseRundown();
};

inline FileHandler* NidhoggFileHandler;
Expand Down
Loading
0