Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search system DLLs for hooking via VADs (not via PEB) #1698

Merged
merged 3 commits into from
Sep 1, 2023

Conversation

1ndahous3
Copy link
Contributor

At the moment, an unreliable algorithm for searching for system DLLs through PEB has been implemented, for at least three reasons:

  1. Sometimes PEB's pages are paged out, so reading fails.
  2. PEB is user mode memory that is sometimes modified by malware (https://www.cynet.com/attack-techniques-hands-on/defense-evasion-techniques-peb-edition/).
  3. Some DLLs (even ntdll) may not be in the PEB list.

The patch implements traverse via VADs.

Also fixed a bug where, after a failed hook attempt, the process is still added to the cache (this is now controlled by the is_hooked flag).

@drakvuf-jenkins
Copy link
Collaborator

Can one of the admins verify this patch?

@tklengyel
Copy link
Owner

@drakvuf-jenkins Test this please

@tklengyel
Copy link
Owner

@drakvuf-jenkins Test this please

@tklengyel tklengyel merged commit 3a0905b into tklengyel:main Sep 1, 2023
41 checks passed
@1ndahous3 1ndahous3 deleted the win_search_dlls_vads branch September 4, 2023 12:38
hook_dll(drakvuf, info, &ctx->mmvad.value(), &ctx->is_hooked);
}

bool get_module_mmvad(drakvuf_t drakvuf, addr_t eprocess, addr_t base_address, mmvad_info_t* mmvad)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More clean would be use std::optional:

std::optional<mmvad_info_t> get_module_mmvad(drakvuf_t drakvuf, addr_t eprocess, addr_t base_address);

if (!mmvad->file_name_ptr)
return false;

if (drakvuf_mmvad_type(drakvuf, mmvad) != 2) // VAD_TYPE_DLL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the VAD_TYPE_DLL constant will eliminate the need to add comments


auto* vctx = (visitor_context_t*)callback_data;

if (name.find(vctx->name) != std::string::npos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comparison is not perfect:

name = "msi.dllhack.dll"
vctx->name = "msi.dll"

and

name = "not_a_ntdll.dll"
vctx->name = "ntdll.dll"

would be true

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open an issue for this to be tracked as the PR has been merged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new code is logically identical to the old one:

if (name.find("ntdll.dll") != std::string::npos)

so the note is about the refactoring and not this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants